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