View Javadoc

1   /**
2    * Copyright 2005-2013 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.api.action.ActionType;
24  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
25  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
26  import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
27  import org.kuali.rice.kew.service.KEWServiceLocator;
28  import org.kuali.rice.kew.api.KewApiConstants;
29  import org.kuali.rice.kim.api.group.GroupService;
30  import org.kuali.rice.kim.api.identity.principal.Principal;
31  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
32  
33  import java.sql.Timestamp;
34  import java.util.ArrayList;
35  import java.util.Collection;
36  import java.util.List;
37  
38  
39  
40  /**
41   * Default implementation of the {@link ActionTakenService}.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class ActionTakenServiceImpl implements ActionTakenService {
46      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenServiceImpl.class);
47      private ActionTakenDAO actionTakenDAO;
48  
49      public ActionTakenValue load(String id) {
50          return getActionTakenDAO().load(id);
51      }
52  
53      public ActionTakenValue findByActionTakenId(String actionTakenId) {
54          return getActionTakenDAO().findByActionTakenId(actionTakenId);
55      }
56  
57      public ActionTakenValue getPreviousAction(ActionRequestValue actionRequest) {
58      	return getPreviousAction(actionRequest, null);
59      }
60  
61      public ActionTakenValue getPreviousAction(ActionRequestValue actionRequest, List<ActionTakenValue> simulatedActionsTaken)
62      {
63          GroupService ims = KimApiServiceLocator.getGroupService();
64          ActionTakenValue foundActionTaken = null;
65          List<String> principalIds = new ArrayList<String>();
66          if (actionRequest.isGroupRequest()) {
67              principalIds.addAll( ims.getMemberPrincipalIds(actionRequest.getGroup().getId()));
68          } else if (actionRequest.isUserRequest()) {
69              principalIds.add(actionRequest.getPrincipalId());
70          }
71  
72          for (String id : principalIds)
73          {
74              List<ActionTakenValue> actionsTakenByUser =
75                  getActionTakenDAO().findByDocumentIdWorkflowId(actionRequest.getDocumentId(), id );
76              if (simulatedActionsTaken != null) {
77                  for (ActionTakenValue simulatedAction : simulatedActionsTaken)
78                  {
79                      if (id.equals(simulatedAction.getPrincipalId()))
80                      {
81                          actionsTakenByUser.add(simulatedAction);
82                      }
83                  }
84              }
85  
86              for (ActionTakenValue actionTaken : actionsTakenByUser)
87              {
88                  if (ActionRequestValue.compareActionCode(actionTaken.getActionTaken(),
89                          actionRequest.getActionRequested(), true) >= 0)
90                  {
91                    foundActionTaken = actionTaken;
92                  }
93              }
94          }
95  
96          return foundActionTaken;
97      }
98  
99      public Collection findByDocIdAndAction(String docId, String action) {
100         return getActionTakenDAO().findByDocIdAndAction(docId, action);
101     }
102 
103     public Collection<ActionTakenValue> findByDocumentId(String documentId) {
104         return getActionTakenDAO().findByDocumentId(documentId);
105     }
106 
107     public List<ActionTakenValue> findByDocumentIdWorkflowId(String documentId, String workflowId) {
108         return getActionTakenDAO().findByDocumentIdWorkflowId(documentId, workflowId);
109     }
110 
111     public Collection getActionsTaken(String documentId) {
112         return getActionTakenDAO().findByDocumentId(documentId);
113     }
114 
115     public List findByDocumentIdIgnoreCurrentInd(String documentId) {
116         return getActionTakenDAO().findByDocumentIdIgnoreCurrentInd(documentId);
117     }
118 
119     public void saveActionTaken(ActionTakenValue actionTaken) {
120         this.getActionTakenDAO().saveActionTaken(actionTaken);
121     }
122 
123     public void delete(ActionTakenValue actionTaken) {
124         getActionTakenDAO().deleteActionTaken(actionTaken);
125     }
126 
127     public ActionTakenDAO getActionTakenDAO() {
128         return actionTakenDAO;
129     }
130 
131     public void setActionTakenDAO(ActionTakenDAO actionTakenDAO) {
132         this.actionTakenDAO = actionTakenDAO;
133     }
134 
135     public void deleteByDocumentId(String documentId){
136         actionTakenDAO.deleteByDocumentId(documentId);
137     }
138 
139     public void validateActionTaken(ActionTakenValue actionTaken){
140         LOG.debug("Enter validateActionTaken(..)");
141         List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
142 
143         String documentId = actionTaken.getDocumentId();
144         if(documentId == null){
145             errors.add(new WorkflowServiceErrorImpl("ActionTaken documentid null.", "actiontaken.documentid.empty", actionTaken.getActionTakenId().toString()));
146         } else if(getRouteHeaderService().getRouteHeader(documentId) == null){
147             errors.add(new WorkflowServiceErrorImpl("ActionTaken documentid invalid.", "actiontaken.documentid.invalid", actionTaken.getActionTakenId().toString()));
148         }
149 
150         String principalId = actionTaken.getPrincipalId();
151         if(StringUtils.isBlank(principalId)){
152             errors.add(new WorkflowServiceErrorImpl("ActionTaken personid null.", "actiontaken.personid.empty", actionTaken.getActionTakenId().toString()));
153         } else {
154         	Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
155         	if (principal == null) {
156                 errors.add(new WorkflowServiceErrorImpl("ActionTaken personid invalid.", "actiontaken.personid.invalid", actionTaken.getActionTakenId().toString()));
157             }
158         }
159         String actionTakenCd = actionTaken.getActionTaken();
160         if(actionTakenCd == null || actionTakenCd.trim().equals("")){
161             errors.add(new WorkflowServiceErrorImpl("ActionTaken cd null.", "actiontaken.actiontaken.empty", actionTaken.getActionTakenId().toString()));
162         } else if(!KewApiConstants.ACTION_TAKEN_CD.containsKey(actionTakenCd)){
163             errors.add(new WorkflowServiceErrorImpl("ActionTaken invalid.", "actiontaken.actiontaken.invalid", actionTaken.getActionTakenId().toString()));
164         }
165         if(actionTaken.getActionDate() == null){
166             errors.add(new WorkflowServiceErrorImpl("ActionTaken actiondate null.", "actiontaken.actiondate.empty", actionTaken.getActionTakenId().toString()));
167         }
168 
169         if(actionTaken.getDocVersion() == null){
170             errors.add(new WorkflowServiceErrorImpl("ActionTaken docversion null.", "actiontaken.docverion.empty", actionTaken.getActionTakenId().toString()));
171         }
172         LOG.debug("Exit validateActionRequest(..) ");
173         if (!errors.isEmpty()) {
174             throw new WorkflowServiceErrorException("ActionRequest Validation Error", errors);
175         }
176     }
177 
178     public boolean hasUserTakenAction(String principalId, String documentId) {
179     	return getActionTakenDAO().hasUserTakenAction(principalId, documentId);
180     }
181 
182     private RouteHeaderService getRouteHeaderService() {
183         return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
184     }
185 
186     public Timestamp getLastApprovedDate(String documentId)
187     {
188         return getActionTakenDAO().getLastActionTakenDate(documentId, ActionType.APPROVE);
189     }
190 
191 }