View Javadoc
1   /**
2    * Copyright 2005-2014 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.kuali.rice.core.api.criteria.CountFlag;
19  import org.kuali.rice.core.api.criteria.OrderByField;
20  import org.kuali.rice.core.api.criteria.OrderDirection;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
23  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
24  import org.kuali.rice.kew.actiontaken.dao.ActionTakenDao;
25  import org.kuali.rice.kew.actiontaken.service.ActionTakenService;
26  import org.kuali.rice.kew.api.action.ActionType;
27  import org.kuali.rice.kim.api.group.GroupService;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  import org.kuali.rice.krad.data.DataObjectService;
30  
31  import java.sql.Timestamp;
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.List;
35  
36  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
37  
38  /**
39   * Default implementation of the {@link ActionTakenService}.
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class ActionTakenServiceImpl implements ActionTakenService {
44  
45      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenServiceImpl.class);
46  
47      private static final String DOCUMENT_ID = "documentId";
48      private static final String PRINCIPAL_ID = "principalId";
49      private static final String CURRENT_INDICATOR = "currentIndicator";
50      private static final String ACTION_DATE = "actionDate";
51  
52      private DataObjectService dataObjectService;
53      private ActionTakenDao actionTakenDao;
54  
55      @Override
56      public ActionTakenValue findByActionTakenId(String actionTakenId) {
57          return getDataObjectService().find(ActionTakenValue.class, actionTakenId);
58      }
59  
60      @Override
61      public ActionTakenValue getPreviousAction(ActionRequestValue actionRequest) {
62      	return getPreviousAction(actionRequest, null);
63      }
64  
65      @Override
66      public ActionTakenValue getPreviousAction(ActionRequestValue actionRequest, List<ActionTakenValue> simulatedActionsTaken) {
67          GroupService ims = KimApiServiceLocator.getGroupService();
68          ActionTakenValue foundActionTaken = null;
69          List<String> principalIds = new ArrayList<String>();
70          if (actionRequest.isGroupRequest()) {
71              principalIds.addAll( ims.getMemberPrincipalIds(actionRequest.getGroup().getId()));
72          } else if (actionRequest.isUserRequest()) {
73              principalIds.add(actionRequest.getPrincipalId());
74          }
75  
76          for (String id : principalIds) {
77              List<ActionTakenValue> actionsTakenByUser =
78                  new ArrayList<ActionTakenValue>(findByDocumentIdPrincipalId(actionRequest.getDocumentId(), id));
79              if (simulatedActionsTaken != null) {
80                  for (ActionTakenValue simulatedAction : simulatedActionsTaken) {
81                      if (id.equals(simulatedAction.getPrincipalId())) {
82                          actionsTakenByUser.add(simulatedAction);
83                      }
84                  }
85              }
86  
87              for (ActionTakenValue actionTaken : actionsTakenByUser) {
88                  if (ActionRequestValue.compareActionCode(actionTaken.getActionTaken(),
89                          actionRequest.getActionRequested(), true) >= 0) {
90                    foundActionTaken = actionTaken;
91                  }
92              }
93          }
94  
95          return foundActionTaken;
96      }
97  
98      @Override
99      public Collection<ActionTakenValue> findByDocumentId(String documentId) {
100         LOG.debug("finding Action Takens by documentId " + documentId);
101         QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create().setPredicates(equal(DOCUMENT_ID,
102                 documentId), equal(CURRENT_INDICATOR, Boolean.TRUE));
103         criteria.setOrderByFields(OrderByField.Builder.create(ACTION_DATE, OrderDirection.ASCENDING).build());
104         return getDataObjectService().findMatching(ActionTakenValue.class, criteria.build()).getResults();
105     }
106 
107     @Override
108     public List<ActionTakenValue> findByDocumentIdPrincipalId(String documentId, String principalId) {
109         LOG.debug("finding Action Takens by documentId " + documentId + " and principalId" + principalId);
110         QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create().setPredicates(equal(DOCUMENT_ID,
111                 documentId), equal(PRINCIPAL_ID, principalId), equal(CURRENT_INDICATOR, Boolean.TRUE));
112         return getDataObjectService().findMatching(ActionTakenValue.class, criteria.build()).getResults();
113     }
114 
115     @Override
116     public List<ActionTakenValue> findByDocumentIdIgnoreCurrentInd(String documentId) {
117         LOG.debug("finding ActionsTaken ignoring currentInd by documentId:" + documentId);
118         QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create().setPredicates(equal(DOCUMENT_ID,
119                 documentId));
120         criteria.setOrderByFields(OrderByField.Builder.create(ACTION_DATE, OrderDirection.ASCENDING).build());
121         return getDataObjectService().findMatching(ActionTakenValue.class, criteria.build()).getResults();
122     }
123 
124     @Override
125     public ActionTakenValue saveActionTaken(ActionTakenValue actionTaken) {
126         LOG.debug("saving ActionTaken");
127         checkNull(actionTaken.getDocumentId(), "Document ID");
128         checkNull(actionTaken.getActionTaken(), "action taken code");
129         checkNull(actionTaken.getDocVersion(), "doc version");
130         checkNull(actionTaken.getPrincipal(), "user principalId");
131 
132         if (actionTaken.getActionDate() == null) {
133             actionTaken.setActionDate(new Timestamp(System.currentTimeMillis()));
134         }
135         if (actionTaken.getCurrentIndicator() == null) {
136             actionTaken.setCurrentIndicator(Boolean.TRUE);
137         }
138         LOG.debug("saving ActionTaken: routeHeader " + actionTaken.getDocumentId() +
139                 ", actionTaken " + actionTaken.getActionTaken() + ", principalId " + actionTaken.getPrincipalId());
140         return getDataObjectService().save(actionTaken);
141     }
142 
143     @Override
144     public void delete(ActionTakenValue actionTaken) {
145         LOG.debug("deleting ActionTaken " + actionTaken.getActionTakenId());
146         getDataObjectService().delete(actionTaken);
147     }
148 
149     @Override
150     public boolean hasUserTakenAction(String principalId, String documentId) {
151         QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create().setPredicates(
152                 equal(DOCUMENT_ID, documentId),
153                 equal(PRINCIPAL_ID, principalId),
154                 equal(CURRENT_INDICATOR, Boolean.TRUE)
155         );
156         criteria.setCountFlag(CountFlag.ONLY);
157         return getDataObjectService().findMatching(ActionTakenValue.class, criteria.build()).getTotalRowCount() > 0;
158     }
159 
160 
161     @Override
162     public Timestamp getLastApprovedDate(String documentId)
163     {
164         return getActionTakenDao().getLastActionTakenDate(documentId, ActionType.APPROVE);
165     }
166 
167     private void checkNull(Object value, String valueName) throws RuntimeException {
168         if (value == null) {
169             throw new IllegalArgumentException("Null value for " + valueName);
170         }
171     }
172 
173 
174     public ActionTakenDao getActionTakenDao() {
175         return actionTakenDao;
176     }
177 
178     public void setActionTakenDao(ActionTakenDao actionTakenDao) {
179         this.actionTakenDao = actionTakenDao;
180     }
181 
182 
183     public DataObjectService getDataObjectService() {
184         return dataObjectService;
185     }
186 
187     public void setDataObjectService(DataObjectService dataObjectService) {
188         this.dataObjectService = dataObjectService;
189     }
190 }