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