View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.actiontaken.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
20  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
21  import org.kuali.rice.kew.actiontaken.dao.ActionTakenDAO;
22  import org.kuali.rice.kew.actiontaken.service.ActionTakenService;
23  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
24  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
25  import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
26  import org.kuali.rice.kew.service.KEWServiceLocator;
27  import org.kuali.rice.kew.api.KewApiConstants;
28  import org.kuali.rice.kim.api.group.GroupService;
29  import org.kuali.rice.kim.api.identity.principal.Principal;
30  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
31  
32  import java.sql.Timestamp;
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.List;
36  
37  
38  
39  /**
40   * Default implementation of the {@link ActionTakenService}.
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class ActionTakenServiceImpl implements ActionTakenService {
45      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenServiceImpl.class);
46      private ActionTakenDAO actionTakenDAO;
47  
48      public ActionTakenValue load(String id) {
49          return getActionTakenDAO().load(id);
50      }
51  
52      public ActionTakenValue findByActionTakenId(String actionTakenId) {
53          return getActionTakenDAO().findByActionTakenId(actionTakenId);
54      }
55  
56      public ActionTakenValue getPreviousAction(ActionRequestValue actionRequest) {
57      	return getPreviousAction(actionRequest, null);
58      }
59  
60      public ActionTakenValue getPreviousAction(ActionRequestValue actionRequest, List<ActionTakenValue> simulatedActionsTaken)
61      {
62          GroupService ims = KimApiServiceLocator.getGroupService();
63          ActionTakenValue foundActionTaken = null;
64          List<String> principalIds = new ArrayList<String>();
65          if (actionRequest.isGroupRequest()) {
66              principalIds.addAll( ims.getMemberPrincipalIds(actionRequest.getGroup().getId()));
67          } else if (actionRequest.isUserRequest()) {
68              principalIds.add(actionRequest.getPrincipalId());
69          }
70  
71          for (String id : principalIds)
72          {
73              List<ActionTakenValue> actionsTakenByUser =
74                  getActionTakenDAO().findByDocumentIdWorkflowId(actionRequest.getDocumentId(), id );
75              if (simulatedActionsTaken != null) {
76                  for (ActionTakenValue simulatedAction : simulatedActionsTaken)
77                  {
78                      if (id.equals(simulatedAction.getPrincipalId()))
79                      {
80                          actionsTakenByUser.add(simulatedAction);
81                      }
82                  }
83              }
84  
85              for (ActionTakenValue actionTaken : actionsTakenByUser)
86              {
87                  if (ActionRequestValue.compareActionCode(actionTaken.getActionTaken(),
88                          actionRequest.getActionRequested(), true) >= 0)
89                  {
90                    foundActionTaken = actionTaken;
91                  }
92              }
93          }
94  
95          return foundActionTaken;
96      }
97  
98      public Collection findByDocIdAndAction(String docId, String action) {
99          return getActionTakenDAO().findByDocIdAndAction(docId, action);
100     }
101 
102     public Collection<ActionTakenValue> findByDocumentId(String documentId) {
103         return getActionTakenDAO().findByDocumentId(documentId);
104     }
105 
106     public List<ActionTakenValue> findByDocumentIdWorkflowId(String documentId, String workflowId) {
107         return getActionTakenDAO().findByDocumentIdWorkflowId(documentId, workflowId);
108     }
109 
110     public Collection getActionsTaken(String documentId) {
111         return getActionTakenDAO().findByDocumentId(documentId);
112     }
113 
114     public List findByDocumentIdIgnoreCurrentInd(String documentId) {
115         return getActionTakenDAO().findByDocumentIdIgnoreCurrentInd(documentId);
116     }
117 
118     public void saveActionTaken(ActionTakenValue actionTaken) {
119         this.getActionTakenDAO().saveActionTaken(actionTaken);
120     }
121 
122     public void delete(ActionTakenValue actionTaken) {
123         getActionTakenDAO().deleteActionTaken(actionTaken);
124     }
125 
126     public ActionTakenDAO getActionTakenDAO() {
127         return actionTakenDAO;
128     }
129 
130     public void setActionTakenDAO(ActionTakenDAO actionTakenDAO) {
131         this.actionTakenDAO = actionTakenDAO;
132     }
133 
134     public void deleteByDocumentId(String documentId){
135         actionTakenDAO.deleteByDocumentId(documentId);
136     }
137 
138     public void validateActionTaken(ActionTakenValue actionTaken){
139         LOG.debug("Enter validateActionTaken(..)");
140         List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
141 
142         String documentId = actionTaken.getDocumentId();
143         if(documentId == null){
144             errors.add(new WorkflowServiceErrorImpl("ActionTaken documentid null.", "actiontaken.documentid.empty", actionTaken.getActionTakenId().toString()));
145         } else if(getRouteHeaderService().getRouteHeader(documentId) == null){
146             errors.add(new WorkflowServiceErrorImpl("ActionTaken documentid invalid.", "actiontaken.documentid.invalid", actionTaken.getActionTakenId().toString()));
147         }
148 
149         String principalId = actionTaken.getPrincipalId();
150         if(StringUtils.isBlank(principalId)){
151             errors.add(new WorkflowServiceErrorImpl("ActionTaken personid null.", "actiontaken.personid.empty", actionTaken.getActionTakenId().toString()));
152         } else {
153         	Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
154         	if (principal == null) {
155                 errors.add(new WorkflowServiceErrorImpl("ActionTaken personid invalid.", "actiontaken.personid.invalid", actionTaken.getActionTakenId().toString()));
156             }
157         }
158         String actionTakenCd = actionTaken.getActionTaken();
159         if(actionTakenCd == null || actionTakenCd.trim().equals("")){
160             errors.add(new WorkflowServiceErrorImpl("ActionTaken cd null.", "actiontaken.actiontaken.empty", actionTaken.getActionTakenId().toString()));
161         } else if(!KewApiConstants.ACTION_TAKEN_CD.containsKey(actionTakenCd)){
162             errors.add(new WorkflowServiceErrorImpl("ActionTaken invalid.", "actiontaken.actiontaken.invalid", actionTaken.getActionTakenId().toString()));
163         }
164         if(actionTaken.getActionDate() == null){
165             errors.add(new WorkflowServiceErrorImpl("ActionTaken actiondate null.", "actiontaken.actiondate.empty", actionTaken.getActionTakenId().toString()));
166         }
167 
168         if(actionTaken.getDocVersion() == null){
169             errors.add(new WorkflowServiceErrorImpl("ActionTaken docversion null.", "actiontaken.docverion.empty", actionTaken.getActionTakenId().toString()));
170         }
171         LOG.debug("Exit validateActionRequest(..) ");
172         if (!errors.isEmpty()) {
173             throw new WorkflowServiceErrorException("ActionRequest Validation Error", errors);
174         }
175     }
176 
177     public boolean hasUserTakenAction(String principalId, String documentId) {
178     	return getActionTakenDAO().hasUserTakenAction(principalId, documentId);
179     }
180 
181     private RouteHeaderService getRouteHeaderService() {
182         return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
183     }
184 
185     public Timestamp getLastApprovedDate(String documentId)
186     {
187     	Timestamp dateLastApproved = null;
188     	Collection<ActionTakenValue> actionsTaken= getActionTakenDAO().findByDocIdAndAction(documentId, KewApiConstants.ACTION_TAKEN_APPROVED_CD);
189         for (ActionTakenValue actionTaken : actionsTaken)
190         {
191             // search for the most recent approval action
192             if (dateLastApproved == null || dateLastApproved.compareTo(actionTaken.getActionDate()) <= -1)
193             {
194                 dateLastApproved = actionTaken.getActionDate();
195             }
196         }
197     	LOG.info("Exit getLastApprovedDate("+documentId+") "+dateLastApproved);
198     	return dateLastApproved;
199     }
200 
201 }