Coverage Report - org.kuali.rice.kew.actionitem.dao.impl.ActionItemDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionItemDAOJpaImpl
0%
0/110
0%
0/42
2.235
 
 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.actionitem.dao.impl;
 17  
 
 18  
 import org.apache.commons.collections.CollectionUtils;
 19  
 import org.kuali.rice.core.api.delegation.DelegationType;
 20  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 21  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
 22  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
 23  
 import org.kuali.rice.kew.actionitem.ActionItem;
 24  
 import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension;
 25  
 import org.kuali.rice.kew.actionitem.dao.ActionItemDAO;
 26  
 import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
 27  
 import org.kuali.rice.kew.actionrequest.Recipient;
 28  
 import org.kuali.rice.kew.util.WebFriendlyRecipient;
 29  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 30  
 
 31  
 import javax.persistence.EntityManager;
 32  
 import javax.persistence.PersistenceContext;
 33  
 import java.sql.Timestamp;
 34  
 import java.util.ArrayList;
 35  
 import java.util.Collection;
 36  
 import java.util.Date;
 37  
 import java.util.HashMap;
 38  
 import java.util.Iterator;
 39  
 import java.util.List;
 40  
 import java.util.Map;
 41  
 
 42  
 /**
 43  
  * OJB implementation of {@link ActionItemDAO}.
 44  
  *
 45  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 46  
  */
 47  0
 public class ActionItemDAOJpaImpl implements ActionItemDAO {
 48  
         @PersistenceContext(unitName="kew-unit")
 49  
         private EntityManager entityManager;
 50  
 
 51  
     public ActionItem findByActionItemId(String actionItemId) {
 52  0
             return entityManager.find(ActionItem.class, actionItemId);
 53  
     }
 54  
 
 55  
     public void deleteActionItems(Long actionRequestId) {
 56  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 57  0
         crit.eq("actionRequestId", actionRequestId);
 58  0
         for(Object actionItem:new QueryByCriteria(entityManager,crit).toQuery().getResultList()){
 59  0
             if( ! (actionItem instanceof OutboxItemActionListExtension)) {
 60  0
                 entityManager.remove(actionItem);
 61  
             }
 62  
         }
 63  
 
 64  0
     }
 65  
 
 66  
     public void deleteActionItem(ActionItem actionItem) {
 67  0
             entityManager.remove(findByActionItemId(actionItem.getId()));
 68  0
     }
 69  
 
 70  
     public void deleteByDocumentIdWorkflowUserId(String documentId, String workflowUserId) {
 71  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 72  0
         crit.eq("documentId", documentId);
 73  0
         crit.eq("principalId", workflowUserId);
 74  0
         for(Object actionItem: new QueryByCriteria(entityManager,crit).toQuery().getResultList()){
 75  0
             if( ! (actionItem instanceof OutboxItemActionListExtension)) {
 76  0
                 entityManager.remove(actionItem);
 77  
             }
 78  
         }
 79  0
     }
 80  
 
 81  
     public void deleteByDocumentId(String documentId) {
 82  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 83  0
         crit.eq("documentId", documentId);
 84  0
         for(Object actionItem: new QueryByCriteria(entityManager,crit).toQuery().getResultList()){
 85  0
             if( ! (actionItem instanceof OutboxItemActionListExtension)) {
 86  0
                 entityManager.remove(actionItem);
 87  
             }
 88  
         }
 89  0
     }
 90  
 
 91  
     /**
 92  
          * This method replaces findByWorkflowUser
 93  
          *
 94  
          * @see org.kuali.rice.kew.actionitem.dao.ActionItemDAO#findByPrincipalId(java.lang.String)
 95  
          */
 96  
         public Collection<ActionItem> findByPrincipalId(String principalId) {
 97  0
                 Criteria crit = new Criteria(ActionItem.class.getName());
 98  0
         crit.eq("principalId", principalId);
 99  0
         crit.orderBy("documentId", true);
 100  0
         List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 101  
         
 102  0
         return removeOutBoxItems(results);
 103  
         }
 104  
 
 105  
     public Collection<ActionItem> findByWorkflowUserDocumentId(String workflowId, String documentId) {
 106  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 107  0
         crit.eq("principalId", workflowId);
 108  0
         crit.eq("documentId", documentId);
 109  0
         List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 110  
         
 111  0
         return removeOutBoxItems(results);
 112  
     }
 113  
 
 114  
     public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) {
 115  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 116  0
         crit.eq("docName", documentTypeName);
 117  0
         List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 118  
 
 119  0
         return removeOutBoxItems(results);
 120  
     }
 121  
 
 122  
     public Collection<ActionItem> getOutboxItemsByDocumentType(String documentTypeName) {
 123  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 124  0
         crit.eq("docName", documentTypeName);
 125  0
         List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 126  0
         Iterator<ActionItem> iterator = results.iterator();
 127  0
         while (iterator.hasNext()) {
 128  0
             if (!(iterator.next() instanceof OutboxItemActionListExtension)) {
 129  0
                 iterator.remove();
 130  
             }
 131  
         }
 132  0
         return results;
 133  
     }
 134  
     
 135  
     public Collection<ActionItem> findByDocumentId(String documentId) {
 136  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 137  0
         crit.eq("documentId", documentId);
 138  0
         List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 139  
         
 140  0
         return removeOutBoxItems(results);
 141  
     }
 142  
 
 143  
     public Collection<ActionItem> findByActionRequestId(String actionRequestId) {
 144  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 145  0
         crit.eq("actionRequestId", actionRequestId);
 146  0
         List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 147  
         
 148  0
         return removeOutBoxItems(results);
 149  
     }
 150  
 
 151  
     public void saveActionItem(ActionItem actionItem) {
 152  0
             if (actionItem.getDateAssigned() == null) {
 153  0
             actionItem.setDateAssigned(new Timestamp(new Date().getTime()));
 154  
         }
 155  
             
 156  0
             if(actionItem.getId()==null){
 157  0
                 entityManager.persist(actionItem);
 158  
             }else{
 159  0
                     OrmUtils.merge(entityManager, actionItem);
 160  
             }
 161  0
     }
 162  
 
 163  
     public Collection<Recipient> findSecondaryDelegators(String principalId) {
 164  0
         Criteria notNullWorkflowCriteria = new Criteria(ActionItem.class.getName());
 165  0
         notNullWorkflowCriteria.notNull("delegatorPrincipalId");
 166  0
         Criteria notNullWorkgroupCriteria = new Criteria(ActionItem.class.getName());
 167  0
         notNullWorkgroupCriteria.notNull("delegatorGroupId");
 168  0
         Criteria orCriteria = new Criteria(ActionItem.class.getName());
 169  0
         orCriteria.or(notNullWorkflowCriteria);
 170  0
         orCriteria.or(notNullWorkgroupCriteria);
 171  0
         Criteria criteria = new Criteria(ActionItem.class.getName());
 172  0
         criteria.eq("principalId", principalId);
 173  0
         criteria.eq("delegationType", DelegationType.SECONDARY.getCode());
 174  0
         criteria.and(orCriteria);
 175  
 
 176  0
         Map<Object, Recipient> delegators = new HashMap<Object, Recipient>();
 177  
 
 178  0
         for(Object actionItem:new QueryByCriteria(entityManager, criteria).toQuery().getResultList()){
 179  0
                 final String delegatorPrincipalId = ((ActionItem)actionItem).getDelegatorPrincipalId();
 180  0
                 String delegatorGroupId = ((ActionItem)actionItem).getDelegatorGroupId();
 181  
 
 182  0
                 if (delegatorPrincipalId != null && !delegators.containsKey(delegatorPrincipalId)) {
 183  0
                 delegators.put(delegatorPrincipalId,new WebFriendlyRecipient(KimApiServiceLocator.getPersonService().getPerson(delegatorPrincipalId)));
 184  0
             }else if (delegatorGroupId != null) {
 185  0
                 if (!delegators.containsKey(delegatorGroupId)) {
 186  0
                     delegators.put(delegatorGroupId, new KimGroupRecipient(KimApiServiceLocator.getGroupService().getGroup(
 187  
                             delegatorGroupId)));
 188  
                 }
 189  
             }
 190  0
         }
 191  0
          return delegators.values();
 192  
     }
 193  
 
 194  
     public Collection<Recipient> findPrimaryDelegationRecipients(String principalId) {
 195  0
             List<String> workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
 196  0
         Criteria orCriteria = new Criteria(ActionItem.class.getName());
 197  0
         Criteria delegatorPrincipalIdCriteria = new Criteria(ActionItem.class.getName());
 198  0
         delegatorPrincipalIdCriteria.eq("delegatorPrincipalId", principalId);
 199  0
         if (CollectionUtils.isNotEmpty(workgroupIds)) {
 200  0
             Criteria delegatorWorkgroupCriteria = new Criteria(ActionItem.class.getName());
 201  0
             delegatorWorkgroupCriteria.in("delegatorGroupId", new ArrayList<String>(workgroupIds));
 202  0
             orCriteria.or(delegatorWorkgroupCriteria);
 203  0
             orCriteria.or(delegatorPrincipalIdCriteria);
 204  0
         }
 205  
         else {
 206  0
             orCriteria.and(delegatorPrincipalIdCriteria);
 207  
         }
 208  0
         Criteria criteria = new Criteria(ActionItem.class.getName());
 209  0
         criteria.eq("delegationType", DelegationType.PRIMARY.getCode());
 210  0
         criteria.and(orCriteria);
 211  
 
 212  0
         Map<Object, Recipient> delegators = new HashMap<Object, Recipient>();
 213  0
         for(Object actionItem:new QueryByCriteria(entityManager, criteria).toQuery().getResultList()){
 214  0
                 String princlId = ((ActionItem)actionItem).getPrincipalId();
 215  0
             if (princlId != null && !delegators.containsKey(princlId)) {
 216  0
                 delegators.put(princlId, new WebFriendlyRecipient(KimApiServiceLocator.getPersonService().getPerson(princlId)));
 217  
             }
 218  0
         }
 219  
 
 220  0
         return delegators.values();
 221  
     }
 222  
     
 223  
     private List<ActionItem> removeOutBoxItems(List<ActionItem> results) {
 224  0
         Iterator<ActionItem> iterator = results.iterator();
 225  0
         while(iterator.hasNext()) {
 226  0
             if(iterator.next() instanceof OutboxItemActionListExtension) {
 227  0
                 iterator.remove();
 228  
             }
 229  
         }
 230  
         
 231  0
         return results;
 232  
     }
 233  
 
 234  
     public EntityManager getEntityManager() {
 235  0
         return this.entityManager;
 236  
     }
 237  
 
 238  
     public void setEntityManager(EntityManager entityManager) {
 239  0
         this.entityManager = entityManager;
 240  0
     }
 241  
 
 242  
 
 243  
 }