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