Coverage Report - org.kuali.rice.kew.actionrequest.dao.impl.ActionRequestDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionRequestDAOJpaImpl
0%
0/129
0%
0/24
1.52
 
 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.io.Serializable;
 19  
 import java.sql.Timestamp;
 20  
 import java.util.Collection;
 21  
 import java.util.Collections;
 22  
 import java.util.List;
 23  
 
 24  
 import javax.persistence.EntityManager;
 25  
 import javax.persistence.PersistenceContext;
 26  
 import javax.persistence.Query;
 27  
 
 28  
 import org.apache.commons.lang.StringUtils;
 29  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 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.api.KewApiConstants;
 35  
 import org.kuali.rice.kim.api.group.Group;
 36  
 
 37  
 /**
 38  
  * This is a description of what this class does - sgibson don't forget to fill this in.
 39  
  * 
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  */
 42  0
 public class ActionRequestDAOJpaImpl implements ActionRequestDAO {
 43  
     
 44  
     @PersistenceContext(name = "kew-unit")
 45  
     private EntityManager entityManager;
 46  
 
 47  
     /**
 48  
          * @return the entityManager
 49  
          */
 50  
         public EntityManager getEntityManager() {
 51  0
                 return this.entityManager;
 52  
         }
 53  
 
 54  
         /**
 55  
          * @param entityManager the entityManager to set
 56  
          */
 57  
         public void setEntityManager(EntityManager entityManager) {
 58  0
                 this.entityManager = entityManager;
 59  0
         }
 60  
 
 61  
         public void delete(String actionRequestId) {
 62  0
         ActionRequestValue actionRequestValue = (ActionRequestValue) entityManager.find(ActionRequestValue.class, actionRequestId);
 63  0
         entityManager.remove(actionRequestValue);
 64  0
     }
 65  
 
 66  
     public void deleteByDocumentId(String documentId) {
 67  
         // FIXME should be jpa bulk update?
 68  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindByDocumentId");
 69  0
         query.setParameter("documentId", documentId);
 70  0
         List<ActionRequestValue> actionRequestValues = (List<ActionRequestValue>) query.getSingleResult();
 71  0
         for(ActionRequestValue arv : actionRequestValues) {
 72  0
             entityManager.remove(arv);
 73  
         }
 74  0
     }
 75  
 
 76  
     public boolean doesDocumentHaveUserRequest(String principalId, String documentId) {
 77  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.GetUserRequestCount");
 78  0
         query.setParameter("principalId", principalId);
 79  0
         query.setParameter("documentId", documentId);
 80  0
         query.setParameter("recipientTypeCd", RecipientType.PRINCIPAL.getCode());
 81  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 82  
         
 83  0
         return ((Long)query.getSingleResult()) > 0;
 84  
     }
 85  
 
 86  
     public List<?> findActivatedByGroup(Group group) {
 87  
         
 88  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindActivatedByGroup");
 89  0
         query.setParameter("groupId", group.getId());
 90  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 91  0
         query.setParameter("status", ActionRequestStatus.ACTIVATED.getCode());
 92  
         
 93  0
         return query.getResultList();
 94  
     }
 95  
 
 96  
     public List<ActionRequestValue> findAllByDocId(String documentId) {
 97  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindAllByDocId");
 98  0
         query.setParameter("documentId", documentId);
 99  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 100  
         
 101  0
         return query.getResultList();
 102  
     }
 103  
 
 104  
     public List<ActionRequestValue> findAllPendingByDocId(String documentId) {
 105  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindAllPendingByDocId");
 106  0
         query.setParameter("documentId", documentId);
 107  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 108  0
         query.setParameter("actionRequestStatus1", ActionRequestStatus.INITIALIZED.getCode());
 109  0
         query.setParameter("actionRequestStatus2", ActionRequestStatus.ACTIVATED.getCode());
 110  
         
 111  0
         return query.getResultList();
 112  
     }
 113  
 
 114  
     @SuppressWarnings("unchecked")
 115  
     public List<ActionRequestValue> findAllRootByDocId(String documentId) {
 116  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindAllRootByDocId");
 117  0
         query.setParameter("documentId", documentId);
 118  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 119  
         
 120  0
         return (List<ActionRequestValue>) query.getResultList();
 121  
     }
 122  
 
 123  
     public List<ActionRequestValue> findByDocumentIdIgnoreCurrentInd(String documentId) {
 124  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindByDocumentId");
 125  0
         query.setParameter("documentId", documentId);
 126  
         
 127  0
         return query.getResultList();
 128  
     }
 129  
 
 130  
     @SuppressWarnings("unchecked")
 131  
     public List<ActionRequestValue> findByStatusAndDocId(String statusCd, String documentId) {
 132  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindByStatusAndDocId");
 133  0
         query.setParameter("documentId", documentId);
 134  0
         query.setParameter("status", statusCd);
 135  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 136  
         
 137  0
         return (List<ActionRequestValue>)query.getResultList();
 138  
     }
 139  
 
 140  
     public List<ActionRequestValue> findPendingByActionRequestedAndDocId(String actionRequestedCd, String documentId) {
 141  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingByActionRequestedAndDocId");
 142  0
         query.setParameter("documentId", documentId);
 143  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 144  0
         query.setParameter("actionRequested", actionRequestedCd);
 145  0
         query.setParameter("actionRequestStatus1", ActionRequestStatus.INITIALIZED.getCode());
 146  0
         query.setParameter("actionRequestStatus2", ActionRequestStatus.ACTIVATED.getCode());
 147  
         
 148  0
         return query.getResultList();
 149  
     }
 150  
 
 151  
     public List<ActionRequestValue> findPendingByDocIdAtOrBelowRouteLevel(String documentId, Integer routeLevel) {
 152  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingByDocIdAtOrBelowRouteLevel");
 153  0
         query.setParameter("documentId", documentId);
 154  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 155  0
         query.setParameter("routeLevel", routeLevel);
 156  0
         query.setParameter("status", ActionRequestStatus.DONE.getCode());
 157  
         
 158  0
         return query.getResultList();
 159  
     }
 160  
 
 161  
     public List<ActionRequestValue> findPendingByResponsibilityIds(Collection responsibilityIds) {
 162  0
         if (responsibilityIds == null || responsibilityIds.size() == 0)
 163  0
             return Collections.emptyList();
 164  
 
 165  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingByDocIdAtOrBelowRouteLevel");
 166  0
         query.setParameter("responsibilityIds", responsibilityIds);
 167  
         
 168  0
         return query.getResultList();
 169  
     }
 170  
 
 171  
     public List<ActionRequestValue> findPendingRootRequestsByDocIdAtOrBelowRouteLevel(String documentId, Integer routeLevel) {
 172  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingRootRequestsByDocIdAtOrBelowRouteLevel");
 173  0
         query.setParameter("documentId", documentId);
 174  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 175  0
         query.setParameter("status", ActionRequestStatus.DONE.getCode());
 176  0
         query.setParameter("routeLevel", routeLevel);
 177  
         
 178  0
         return query.getResultList();
 179  
     }
 180  
 
 181  
     public List<ActionRequestValue> findPendingRootRequestsByDocIdAtRouteLevel(String documentId, Integer routeLevel) {
 182  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingRootRequestsByDocIdAtRouteLevel");
 183  0
         query.setParameter("documentId", documentId);
 184  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 185  0
         query.setParameter("status", ActionRequestStatus.DONE.getCode());
 186  0
         query.setParameter("routeLevel", routeLevel);
 187  
         
 188  0
         return query.getResultList();
 189  
     }
 190  
 
 191  
     public List<ActionRequestValue> findPendingRootRequestsByDocIdAtRouteNode(String documentId, String nodeInstanceId) {
 192  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingRootRequestsByDocIdAtRouteNode");
 193  0
         query.setParameter("documentId", documentId);
 194  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 195  0
         query.setParameter("routeNodeInstanceId", nodeInstanceId);
 196  0
         query.setParameter("actionRequestStatus1", ActionRequestStatus.INITIALIZED.getCode());
 197  0
         query.setParameter("actionRequestStatus2", ActionRequestStatus.ACTIVATED.getCode());
 198  
         
 199  0
         return query.getResultList();
 200  
     }
 201  
 
 202  
     public List<ActionRequestValue> findPendingRootRequestsByDocumentType(String documentTypeId) {
 203  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingRootRequestsByDocumentType");
 204  0
         query.setParameter("documentTypeId", documentTypeId);
 205  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 206  0
         query.setParameter("actionRequestStatus1", ActionRequestStatus.INITIALIZED.getCode());
 207  0
         query.setParameter("actionRequestStatus2", ActionRequestStatus.ACTIVATED.getCode());
 208  
         
 209  0
         return query.getResultList();
 210  
     }
 211  
 
 212  
     public List<ActionRequestValue> findRootRequestsByDocIdAtRouteNode(String documentId, String nodeInstanceId) {
 213  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.FindRootRequestsByDocIdAtRouteNode");
 214  0
         query.setParameter("documentId", documentId);
 215  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 216  0
         query.setParameter("routeNodeInstanceId", nodeInstanceId);
 217  
         
 218  0
         return query.getResultList();
 219  
     }
 220  
 
 221  
     public ActionRequestValue getActionRequestByActionRequestId(String actionRequestId) {
 222  0
         return entityManager.find(ActionRequestValue.class, actionRequestId);
 223  
     }
 224  
 
 225  
     @SuppressWarnings("unchecked")
 226  
     public List<String> getRequestGroupIds(String documentId) {
 227  0
         Query query = entityManager.createNamedQuery("ActionRequestValue.GetRequestGroupIds");
 228  0
         query.setParameter("documentId", documentId);
 229  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 230  0
         query.setParameter("recipientTypeCd", RecipientType.GROUP.getCode());
 231  
         
 232  0
         return query.getResultList();
 233  
     }
 234  
 
 235  
     public void saveActionRequest(ActionRequestValue actionRequest) {
 236  0
         if ( actionRequest.getAnnotation() != null && actionRequest.getAnnotation().length() > 2000 ) {
 237  0
                 actionRequest.setAnnotation( StringUtils.abbreviate(actionRequest.getAnnotation(), 2000) );
 238  
         }
 239  0
             if(actionRequest.getActionRequestId() == null) {
 240  0
                 loadDefaultValues(actionRequest);
 241  0
                 entityManager.persist(actionRequest);
 242  
         }else{
 243  0
             OrmUtils.merge(entityManager, actionRequest);
 244  
         }
 245  0
     }
 246  
     private void loadDefaultValues(ActionRequestValue actionRequest) {
 247  0
         checkNull(actionRequest.getActionRequested(), "action requested");
 248  0
         checkNull(actionRequest.getResponsibilityId(), "responsibility ID");
 249  0
         checkNull(actionRequest.getRouteLevel(), "route level");
 250  0
         checkNull(actionRequest.getDocVersion(), "doc version");
 251  0
         if (actionRequest.getForceAction() == null) {
 252  0
             actionRequest.setForceAction(Boolean.FALSE);
 253  
         }
 254  0
         if (actionRequest.getStatus() == null) {
 255  0
             actionRequest.setStatus(ActionRequestStatus.INITIALIZED.getCode());
 256  
         }
 257  0
         if (actionRequest.getPriority() == null) {
 258  0
             actionRequest.setPriority(KewApiConstants.ACTION_REQUEST_DEFAULT_PRIORITY);
 259  
         }
 260  0
         if (actionRequest.getCurrentIndicator() == null) {
 261  0
             actionRequest.setCurrentIndicator(true);
 262  
         }
 263  0
         actionRequest.setCreateDate(new Timestamp(System.currentTimeMillis()));
 264  0
     }
 265  
     //TODO Runtime might not be the right thing to do here...
 266  
     private void checkNull(Serializable value, String valueName) throws RuntimeException {
 267  0
         if (value == null) {
 268  0
             throw new RuntimeException("Null value for " + valueName);
 269  
         }
 270  0
     }
 271  
     
 272  
         public List<ActionRequestValue> findActivatedByGroup(String groupId) {
 273  0
                 Query query = entityManager.createNamedQuery("ActionRequestValue.FindByStatusAndGroupId");
 274  0
         query.setParameter("status", ActionRequestStatus.ACTIVATED.getCode());
 275  0
         query.setParameter("currentIndicator", Boolean.TRUE);
 276  0
         query.setParameter("groupId", groupId);
 277  
         
 278  0
         return query.getResultList();
 279  
         }
 280  
     
 281  
 }