001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kew.actionrequest.dao.impl; 017 018 import java.sql.Timestamp; 019 import java.util.ArrayList; 020 import java.util.Collection; 021 import java.util.Collections; 022 import java.util.Iterator; 023 import java.util.List; 024 025 import org.apache.commons.lang.StringUtils; 026 import org.apache.ojb.broker.query.Criteria; 027 import org.apache.ojb.broker.query.QueryByCriteria; 028 import org.apache.ojb.broker.query.QueryFactory; 029 import org.apache.ojb.broker.query.ReportQueryByCriteria; 030 import org.kuali.rice.kew.actionrequest.ActionRequestValue; 031 import org.kuali.rice.kew.actionrequest.dao.ActionRequestDAO; 032 import org.kuali.rice.kew.api.action.ActionRequestStatus; 033 import org.kuali.rice.kew.api.action.RecipientType; 034 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; 035 import org.kuali.rice.kew.api.KewApiConstants; 036 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport; 037 038 039 /** 040 * OJB implementation of the {@link ActionRequestDAO}. 041 * 042 * @author Kuali Rice Team (rice.collab@kuali.org) 043 */ 044 public class ActionRequestDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionRequestDAO { 045 046 public ActionRequestValue getActionRequestByActionRequestId(String actionRequestId) { 047 Criteria crit = new Criteria(); 048 crit.addEqualTo("actionRequestId", actionRequestId); 049 return (ActionRequestValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionRequestValue.class, crit)); 050 } 051 052 public void saveActionRequest(ActionRequestValue actionRequest) { 053 if (actionRequest.getActionRequestId() == null) { 054 loadDefaultValues(actionRequest); 055 } 056 if ( actionRequest.getAnnotation() != null && actionRequest.getAnnotation().length() > 2000 ) { 057 actionRequest.setAnnotation( StringUtils.abbreviate(actionRequest.getAnnotation(), 2000) ); 058 } 059 this.getPersistenceBrokerTemplate().store(actionRequest); 060 } 061 062 public List<ActionRequestValue> findPendingByResponsibilityIds(Collection responsibilityIds) { 063 if (responsibilityIds == null || responsibilityIds.size() == 0) return Collections.emptyList(); 064 Criteria crit = new Criteria(); 065 Criteria statusCriteria = new Criteria(); 066 Criteria activatedCriteria = new Criteria(); 067 activatedCriteria.addEqualTo("status", ActionRequestStatus.ACTIVATED.getCode()); 068 069 Criteria initializedCriteria = new Criteria(); 070 initializedCriteria.addEqualTo("status", ActionRequestStatus.INITIALIZED.getCode()); 071 072 statusCriteria.addOrCriteria(activatedCriteria); 073 statusCriteria.addOrCriteria(initializedCriteria); 074 crit.addAndCriteria(statusCriteria); 075 crit.addIn("responsibilityId", responsibilityIds); 076 return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit))); 077 } 078 079 public List<ActionRequestValue> findPendingByActionRequestedAndDocId(String actionRequestedCd, String documentId) { 080 Criteria crit = new Criteria(); 081 crit.addEqualTo("actionRequested", actionRequestedCd); 082 crit.addEqualTo("documentId", documentId); 083 crit.addEqualTo("currentIndicator", Boolean.TRUE); 084 crit.addAndCriteria(getPendingCriteria()); 085 return new ArrayList<ActionRequestValue>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit))); 086 } 087 088 @SuppressWarnings("unchecked") 089 public List<ActionRequestValue> findByStatusAndDocId(String statusCd, String documentId) { 090 Criteria crit = new Criteria(); 091 crit.addEqualTo("status", statusCd); 092 crit.addEqualTo("documentId", documentId); 093 crit.addEqualTo("currentIndicator", true); 094 095 return (List<ActionRequestValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionRequestValue.class, crit)); 096 } 097 098 private void loadDefaultValues(ActionRequestValue actionRequest) { 099 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 291 @Override 292 public ActionRequestValue getRoleActionRequestByActionTakenId(String actionTakenId) { 293 Criteria crit = new Criteria(); 294 crit.addEqualTo("actionTakenId", actionTakenId); 295 crit.addEqualTo("currentIndicator", true); 296 crit.addEqualTo("recipientTypeCd", RecipientType.ROLE.getCode()); 297 crit.addIsNull("parentActionRequest"); 298 return (ActionRequestValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionRequestValue.class, crit)); 299 } 300 }