View Javadoc

1   /**
2    * Copyright 2005-2011 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.actionrequest.dao.impl;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.QueryByCriteria;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.apache.ojb.broker.query.ReportQueryByCriteria;
30  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
31  import org.kuali.rice.kew.actionrequest.dao.ActionRequestDAO;
32  import org.kuali.rice.kew.api.action.ActionRequestStatus;
33  import org.kuali.rice.kew.api.action.RecipientType;
34  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
35  import org.kuali.rice.kew.api.KewApiConstants;
36  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
37  
38  
39  /**
40   * OJB implementation of the {@link ActionRequestDAO}.
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class ActionRequestDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionRequestDAO {
45  
46      public ActionRequestValue getActionRequestByActionRequestId(String actionRequestId) {
47          Criteria crit = new Criteria();
48          crit.addEqualTo("actionRequestId", actionRequestId);
49          return (ActionRequestValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionRequestValue.class, crit));
50      }
51  
52      public void saveActionRequest(ActionRequestValue actionRequest) {
53          if (actionRequest.getActionRequestId() == null) {
54              loadDefaultValues(actionRequest);
55          }
56          if ( actionRequest.getAnnotation() != null && actionRequest.getAnnotation().length() > 2000 ) {
57          	actionRequest.setAnnotation( StringUtils.abbreviate(actionRequest.getAnnotation(), 2000) );
58          }
59          this.getPersistenceBrokerTemplate().store(actionRequest);
60      }
61  
62      public List<ActionRequestValue> findPendingByResponsibilityIds(Collection responsibilityIds) {
63          if (responsibilityIds == null || responsibilityIds.size() == 0) return Collections.emptyList();
64          Criteria crit = new Criteria();
65          Criteria statusCriteria = new Criteria();
66          Criteria activatedCriteria = new Criteria();
67          activatedCriteria.addEqualTo("status", ActionRequestStatus.ACTIVATED.getCode());
68  
69          Criteria initializedCriteria = new Criteria();
70          initializedCriteria.addEqualTo("status", ActionRequestStatus.INITIALIZED.getCode());
71  
72          statusCriteria.addOrCriteria(activatedCriteria);
73          statusCriteria.addOrCriteria(initializedCriteria);
74          crit.addAndCriteria(statusCriteria);
75          crit.addIn("responsibilityId", responsibilityIds);
76          return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
77      }
78  
79      public List<ActionRequestValue> findPendingByActionRequestedAndDocId(String actionRequestedCd, String documentId) {
80          Criteria crit = new Criteria();
81          crit.addEqualTo("actionRequested", actionRequestedCd);
82          crit.addEqualTo("documentId", documentId);
83          crit.addEqualTo("currentIndicator", Boolean.TRUE);
84          crit.addAndCriteria(getPendingCriteria());
85          return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
86      }
87  
88      @SuppressWarnings("unchecked")
89      public List<ActionRequestValue> findByStatusAndDocId(String statusCd, String documentId) {
90          Criteria crit = new Criteria();
91          crit.addEqualTo("status", statusCd);
92          crit.addEqualTo("documentId", documentId);
93          crit.addEqualTo("currentIndicator", true);
94  
95          return (List<ActionRequestValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit));
96      }
97  
98      private void loadDefaultValues(ActionRequestValue actionRequest) {
99          checkNull(actionRequest.getActionRequested(), "action requested");
100         checkNull(actionRequest.getResponsibilityId(), "responsibility ID");
101         checkNull(actionRequest.getRouteLevel(), "route level");
102         checkNull(actionRequest.getDocVersion(), "doc version");
103         if (actionRequest.getForceAction() == null) {
104             actionRequest.setForceAction(Boolean.FALSE);
105         }
106         if (actionRequest.getStatus() == null) {
107             actionRequest.setStatus(ActionRequestStatus.INITIALIZED.getCode());
108         }
109         if (actionRequest.getPriority() == null) {
110             actionRequest.setPriority(KewApiConstants.ACTION_REQUEST_DEFAULT_PRIORITY);
111         }
112         if (actionRequest.getCurrentIndicator() == null) {
113             actionRequest.setCurrentIndicator(true);
114         }
115         actionRequest.setCreateDate(new Timestamp(System.currentTimeMillis()));
116     }
117 
118     //TODO Runtime might not be the right thing to do here...
119     private void checkNull(Object value, String valueName) throws RuntimeException {
120         if (value == null) {
121             throw new RuntimeException("Null value for " + valueName);
122         }
123     }
124 
125     public List<ActionRequestValue> findPendingRootRequestsByDocIdAtRouteLevel(String documentId, Integer routeLevel) {
126         Criteria crit = new Criteria();
127         crit.addEqualTo("routeLevel", routeLevel);
128         crit.addNotEqualTo("status", ActionRequestStatus.DONE.getCode());
129         crit.addEqualTo("documentId", documentId);
130         crit.addEqualTo("currentIndicator", Boolean.TRUE);
131         crit.addIsNull("parentActionRequest");
132         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
133     }
134 
135     public List<ActionRequestValue> findPendingByDocIdAtOrBelowRouteLevel(String documentId, Integer routeLevel) {
136         Criteria crit = new Criteria();
137         crit.addLessOrEqualThan("routeLevel", routeLevel);
138         crit.addNotEqualTo("status", ActionRequestStatus.DONE.getCode());
139         crit.addEqualTo("documentId", documentId);
140         crit.addEqualTo("currentIndicator", true);
141         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
142     }
143 
144     public List<ActionRequestValue> findPendingRootRequestsByDocIdAtOrBelowRouteLevel(String documentId, Integer routeLevel) {
145         Criteria crit = new Criteria();
146         crit.addLessOrEqualThan("routeLevel", routeLevel);
147         crit.addNotEqualTo("status", ActionRequestStatus.DONE.getCode());
148         crit.addEqualTo("documentId", documentId);
149         crit.addEqualTo("currentIndicator", true);
150         crit.addIsNull("parentActionRequest");
151         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
152     }
153 
154     public void delete(String actionRequestId) {
155     	Criteria crit = new Criteria();
156     	crit.addEqualTo("actionRequestId", actionRequestId);
157     	this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionRequestValue.class, crit));
158     }
159 
160     public List<ActionRequestValue> findAllPendingByDocId(String documentId) {
161         Criteria initializedStatCriteria = new Criteria();
162         initializedStatCriteria.addEqualTo("status", ActionRequestStatus.INITIALIZED.getCode());
163 
164         Criteria activatedStatCriteria = new Criteria();
165         activatedStatCriteria.addEqualTo("status", ActionRequestStatus.ACTIVATED.getCode());
166 
167         Criteria statusCriteria = new Criteria();
168         statusCriteria.addOrCriteria(initializedStatCriteria);
169         statusCriteria.addOrCriteria(activatedStatCriteria);
170 
171         Criteria crit = new Criteria();
172         crit.addEqualTo("documentId", documentId);
173         crit.addEqualTo("currentIndicator", true);
174         crit.addAndCriteria(statusCriteria);
175 
176         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
177     }
178 
179     public List<ActionRequestValue> findAllByDocId(String documentId) {
180         Criteria crit = new Criteria();
181         crit.addEqualTo("documentId", documentId);
182         crit.addEqualTo("currentIndicator", true);
183         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
184     }
185 
186     public List<ActionRequestValue> findAllRootByDocId(String documentId) {
187         Criteria crit = new Criteria();
188         crit.addEqualTo("documentId", documentId);
189         crit.addEqualTo("currentIndicator", true);
190         crit.addIsNull("parentActionRequest");
191         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
192     }
193 
194     public List<ActionRequestValue> findByDocumentIdIgnoreCurrentInd(String documentId) {
195         Criteria crit = new Criteria();
196         crit.addEqualTo("documentId", documentId);
197         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
198     }
199 
200     
201     private Criteria getPendingCriteria() {
202         Criteria pendingCriteria = new Criteria();
203         Criteria activatedCriteria = new Criteria();
204         activatedCriteria.addEqualTo("status", ActionRequestStatus.ACTIVATED.getCode());
205         Criteria initializedCriteria = new Criteria();
206         initializedCriteria.addEqualTo("status", ActionRequestStatus.INITIALIZED.getCode());
207         pendingCriteria.addOrCriteria(activatedCriteria);
208         pendingCriteria.addOrCriteria(initializedCriteria);
209         return pendingCriteria;
210     }
211 
212     public  void deleteByDocumentId(String documentId){
213         Criteria crit = new Criteria();
214         crit.addEqualTo("documentId", documentId);
215         this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionRequestValue.class, crit));
216     }
217 
218     public List<ActionRequestValue> findPendingRootRequestsByDocumentType(String documentTypeId) {
219     	Criteria routeHeaderCrit = new Criteria();
220     	routeHeaderCrit.addEqualTo("documentTypeId", documentTypeId);
221     	Criteria crit = new Criteria();
222     	crit.addIn("documentId", new ReportQueryByCriteria(DocumentRouteHeaderValue.class, new String[] {"documentId"}, routeHeaderCrit));
223         crit.addAndCriteria(getPendingCriteria());
224         crit.addEqualTo("currentIndicator", Boolean.TRUE);
225         crit.addIsNull("parentActionRequest");
226         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
227     }
228 
229     public List<ActionRequestValue> findPendingRootRequestsByDocIdAtRouteNode(String documentId, String nodeInstanceId) {
230     	Criteria crit = new Criteria();
231         crit.addEqualTo("documentId", documentId);
232         crit.addAndCriteria(getPendingCriteria());
233         crit.addEqualTo("currentIndicator", Boolean.TRUE);
234         crit.addIsNull("parentActionRequest");
235         crit.addEqualTo("nodeInstance.routeNodeInstanceId", nodeInstanceId);
236         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
237     }
238 
239     public List<ActionRequestValue> findRootRequestsByDocIdAtRouteNode(String documentId, String nodeInstanceId) {
240         Criteria crit = new Criteria();
241         crit.addEqualTo("documentId", documentId);
242         crit.addEqualTo("currentIndicator", Boolean.TRUE);
243         crit.addIsNull("parentActionRequest");
244         crit.addEqualTo("nodeInstance.routeNodeInstanceId", nodeInstanceId);
245         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
246     }
247 
248     public boolean doesDocumentHaveUserRequest(String principalId, String documentId) {
249     	Criteria crit = new Criteria();
250     	crit.addEqualTo("documentId", documentId);
251     	crit.addEqualTo("recipientTypeCd", RecipientType.PRINCIPAL.getCode());
252     	crit.addEqualTo("principalId", principalId);
253     	crit.addEqualTo("currentIndicator", Boolean.TRUE);
254     	int count = getPersistenceBrokerTemplate().getCount(new QueryByCriteria(ActionRequestValue.class, crit));
255     	return count > 0;
256     }
257 
258     public List<String> getRequestGroupIds(String documentId) {
259     	Criteria crit = new Criteria();
260     	crit.addEqualTo("documentId", documentId);
261     	crit.addEqualTo("recipientTypeCd", RecipientType.GROUP.getCode());
262     	crit.addEqualTo("currentIndicator", Boolean.TRUE);
263 
264     	ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionRequestValue.class, crit);
265     	query.setAttributes(new String[] { "groupId" });
266 
267     	List<String> groupIds = new ArrayList<String>();
268     	Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
269     	while (iter.hasNext()) {
270 			Object[] row = (Object[]) iter.next();
271 			String id = (String)row[0];
272 			groupIds.add(id);
273 		}
274     	return groupIds;
275     }
276 
277 	/**
278 	 * @see org.kuali.rice.kew.actionrequest.dao.ActionRequestDAO#findActivatedByGroup(String)
279 	 */
280 	public List<ActionRequestValue> findActivatedByGroup(String groupId) {
281         Criteria statusCriteria = new Criteria();
282         statusCriteria.addEqualTo("status", ActionRequestStatus.ACTIVATED.getCode());
283         Criteria crit = new Criteria();
284         crit.addEqualTo("groupId", groupId);
285         crit.addEqualTo("currentIndicator", true);
286         crit.addAndCriteria(statusCriteria);
287 
288         return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)));
289 	}
290 }