Coverage Report - org.kuali.rice.kew.actionlist.dao.impl.ActionListDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionListDAOJpaImpl
0%
0/319
0%
0/194
5.714
 
 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.actionlist.dao.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 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.ActionItemActionListExtension;
 25  
 import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension;
 26  
 import org.kuali.rice.kew.actionlist.ActionListFilter;
 27  
 import org.kuali.rice.kew.actionlist.dao.ActionListDAO;
 28  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 29  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 30  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueActionListExtension;
 31  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 32  
 import org.kuali.rice.kew.api.KewApiConstants;
 33  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 34  
 
 35  
 import javax.persistence.EntityManager;
 36  
 import javax.persistence.PersistenceContext;
 37  
 import java.sql.Timestamp;
 38  
 import java.util.ArrayList;
 39  
 import java.util.Calendar;
 40  
 import java.util.Collection;
 41  
 import java.util.Date;
 42  
 import java.util.HashMap;
 43  
 import java.util.Iterator;
 44  
 import java.util.List;
 45  
 import java.util.Map;
 46  
 
 47  
 /**
 48  
  * OJB implementation of the {@link ActionListDAO}.
 49  
  *
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  */
 52  0
 public class ActionListDAOJpaImpl implements ActionListDAO {
 53  
 
 54  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionListDAOJpaImpl.class);
 55  
         
 56  
     @PersistenceContext(unitName="kew-unit")
 57  
         private EntityManager entityManager;
 58  
         
 59  
     public Collection<ActionItem> getActionList(String principalId, ActionListFilter filter) {
 60  0
         return toActionItemActionListExtensions(getActionItemsInActionList(ActionItem.class, principalId, filter));
 61  
     }
 62  
     
 63  
     /**
 64  
          * This method ...
 65  
          * 
 66  
          * @param actionItems
 67  
          * @return actionItemActionListExtensions
 68  
          */
 69  
         private Collection<ActionItem> toActionItemActionListExtensions(
 70  
                         Collection<ActionItem> actionItems) {
 71  0
                 List<ActionItem> actionItemActionListExtensions = new ArrayList<ActionItem>();
 72  0
                 for(ActionItem actionItem:actionItems){
 73  0
                         actionItemActionListExtensions.add(toActionItemActionListExtension(actionItem));
 74  
                 }
 75  0
                 return actionItemActionListExtensions;
 76  
         }
 77  
 
 78  
         /**
 79  
          * This method ...
 80  
          * 
 81  
          * @param actionItem
 82  
          * @return
 83  
          */
 84  
         private ActionItemActionListExtension toActionItemActionListExtension(
 85  
                         ActionItem actionItem) {
 86  
 
 87  0
                 if(actionItem==null){
 88  0
                         return null;
 89  
                 }
 90  
                 
 91  0
                 ActionItemActionListExtension actionItemExt = new ActionItemActionListExtension();
 92  
                 
 93  0
                 actionItemExt.setId(actionItem.getId());
 94  0
                 actionItemExt.setPrincipalId(actionItem.getPrincipalId());
 95  0
                 actionItemExt.setDateAssigned(actionItem.getDateAssigned());
 96  0
                 actionItemExt.setActionRequestCd(actionItem.getActionRequestCd());
 97  0
                 actionItemExt.setActionRequestId(actionItem.getActionRequestId());
 98  0
                 actionItemExt.setDocumentId(actionItem.getDocumentId());
 99  0
                 actionItemExt.setResponsibilityId(actionItem.getResponsibilityId());
 100  0
                 actionItemExt.setGroupId(actionItem.getGroupId());
 101  0
                 actionItemExt.setRoleName(actionItem.getRoleName());
 102  0
                 actionItemExt.setDocTitle(actionItem.getDocTitle());
 103  0
                 actionItemExt.setDocLabel(actionItem.getDocLabel());
 104  0
                 actionItemExt.setDocHandlerURL(actionItem.getDocHandlerURL());
 105  0
                 actionItemExt.setDocName(actionItem.getDocName());
 106  0
                 actionItemExt.setDelegatorPrincipalId(actionItem.getDelegatorPrincipalId());
 107  0
                 actionItemExt.setDelegatorGroupId(actionItem.getDelegatorGroupId());
 108  0
                 actionItemExt.setDelegationType(actionItem.getDelegationType());
 109  0
                 actionItemExt.setLockVerNbr(actionItem.getLockVerNbr());
 110  0
                 actionItemExt.setDocumentId(actionItem.getDocumentId());
 111  0
                 actionItemExt.setRequestLabel(actionItem.getRequestLabel());
 112  
                 //actionItemExt.setRouteHeader(toDocumentRouteHeaderValueActionListExtension(actionItem.getRouteHeader()));
 113  
                 
 114  
                 // These properties are not mapped in OJB-repository-kew.xml
 115  
                 // actionItemExt.setActionItemIndex(actionItem.getActionItemIndex());
 116  
                 // actionItemExt.setActionToTake(actionItem.getActionToTake());
 117  
                 // actionItemExt.setCustomActions(actionItem.getCustomActions());
 118  
                 // actionItemExt.setDateAssignedString(actionItem.getDateAssignedString());
 119  
                 // actionItemExt.setDelegatorGroup();
 120  
                 // actionItemExt.setDisplayParameters();
 121  
                 // FIXME this causes null pointer - actionItemExt.setGroup(actionItem.getGroup());
 122  
                 // actionItemExt.setLastApprovedDate(actionItem.getLastApprovedDate());
 123  
                 // actionItemExt.setRowStyleClass();
 124  
                 
 125  0
                 return actionItemExt;
 126  
         }
 127  
 
 128  
         /**
 129  
          * This method ...
 130  
          * 
 131  
          * @param routeHeader
 132  
          * @return
 133  
          */
 134  
         private DocumentRouteHeaderValueActionListExtension toDocumentRouteHeaderValueActionListExtension(
 135  
                         DocumentRouteHeaderValue routeHeader) {
 136  
 
 137  0
                 if(routeHeader==null){
 138  0
                         return null;
 139  
                 }
 140  
                 
 141  0
                 DocumentRouteHeaderValueActionListExtension extension = new DocumentRouteHeaderValueActionListExtension();
 142  
                 
 143  0
                 extension.setDocumentId(routeHeader.getDocumentId());
 144  0
                 extension.setDocumentTypeId(routeHeader.getDocumentTypeId());
 145  0
                 extension.setDocRouteStatus(routeHeader.getDocRouteStatus());
 146  0
                 extension.setDocRouteLevel(routeHeader.getDocRouteLevel());
 147  0
                 extension.setStatusModDate(routeHeader.getStatusModDate());
 148  0
                 extension.setCreateDate(routeHeader.getCreateDate());
 149  0
                 extension.setApprovedDate(routeHeader.getApprovedDate());
 150  0
                 extension.setFinalizedDate(routeHeader.getFinalizedDate());
 151  0
                 extension.setRouteStatusDate(routeHeader.getRouteStatusDate());
 152  0
                 extension.setRouteLevelDate(routeHeader.getRouteLevelDate());
 153  0
                 extension.setDocTitle(routeHeader.getDocTitle());
 154  0
                 extension.setAppDocId(routeHeader.getAppDocId());
 155  0
                 extension.setDocVersion(routeHeader.getDocVersion());
 156  0
                 extension.setInitiatorWorkflowId(routeHeader.getInitiatorWorkflowId());
 157  0
                 extension.setVersionNumber(routeHeader.getVersionNumber());
 158  0
                 extension.setAppDocStatus(routeHeader.getAppDocStatus());
 159  0
                 extension.setAppDocStatusDate(routeHeader.getAppDocStatusDate());
 160  
 
 161  0
                 return extension;
 162  
         }
 163  
 
 164  
         public Collection<ActionItem> getActionListForSingleDocument(String documentId) {
 165  0
         LOG.debug("getting action list for document id " + documentId);
 166  0
         Criteria crit = new Criteria(ActionItem.class.getName());
 167  0
         crit.eq("documentId", documentId);
 168  0
         crit.eq("TYPE(__JPA_ALIAS[[0]]__)", ActionItem.class);
 169  0
         Collection<ActionItem> collection = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 170  0
         LOG.debug("found " + collection.size() + " action items for document id " + documentId);
 171  0
         return toActionItemActionListExtensions(createActionListForRouteHeader(collection));
 172  
     }
 173  
     
 174  
     private Criteria setUpActionListCriteria(Class objectsToRetrieve, String principalId, ActionListFilter filter) {
 175  0
         LOG.debug("setting up Action List criteria");
 176  0
         Criteria crit = new Criteria(objectsToRetrieve.getName());
 177  0
         boolean filterOn = false;
 178  0
         String filteredByItems = "";
 179  
         
 180  0
         if (filter.getActionRequestCd() != null && !"".equals(filter.getActionRequestCd().trim()) && !filter.getActionRequestCd().equals(KewApiConstants.ALL_CODE)) {
 181  0
             if (filter.isExcludeActionRequestCd()) {
 182  0
                 crit.ne("actionRequestCd", filter.getActionRequestCd());
 183  
             } else {
 184  0
                 crit.eq("actionRequestCd", filter.getActionRequestCd());
 185  
             }
 186  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 187  0
             filteredByItems += "Action Requested";
 188  
         }
 189  
 
 190  0
         if (filter.getCreateDateFrom() != null || filter.getCreateDateTo() != null) {
 191  0
             if (filter.isExcludeCreateDate()) {
 192  0
                 if (filter.getCreateDateFrom() != null && filter.getCreateDateTo() != null) {
 193  0
                     crit.notBetween("routeHeader.createDate", new Timestamp(beginningOfDay(filter.getCreateDateFrom()).getTime()), new Timestamp(endOfDay(filter.getCreateDateTo()).getTime()));
 194  0
                 } else if (filter.getCreateDateFrom() != null && filter.getCreateDateTo() == null) {
 195  0
                     crit.lte("routeHeader.createDate", new Timestamp(beginningOfDay(filter.getCreateDateFrom()).getTime()));
 196  0
                 } else if (filter.getCreateDateFrom() == null && filter.getCreateDateTo() != null) {
 197  0
                     crit.gte("routeHeader.createDate", new Timestamp(endOfDay(filter.getCreateDateTo()).getTime()));
 198  
                 }
 199  
             } else {
 200  0
                 if (filter.getCreateDateFrom() != null && filter.getCreateDateTo() != null) {
 201  0
                     crit.between("routeHeader.createDate", new Timestamp(beginningOfDay(filter.getCreateDateFrom()).getTime()), new Timestamp(endOfDay(filter.getCreateDateTo()).getTime()));
 202  0
                 } else if (filter.getCreateDateFrom() != null && filter.getCreateDateTo() == null) {
 203  0
                     crit.gte("routeHeader.createDate", new Timestamp(beginningOfDay(filter.getCreateDateFrom()).getTime()));
 204  0
                 } else if (filter.getCreateDateFrom() == null && filter.getCreateDateTo() != null) {
 205  0
                     crit.lte("routeHeader.createDate", new Timestamp(endOfDay(filter.getCreateDateTo()).getTime()));
 206  
                 }
 207  
             }
 208  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 209  0
             filteredByItems += "Date Created";
 210  
         }
 211  
 
 212  0
         if (filter.getDocRouteStatus() != null && !"".equals(filter.getDocRouteStatus().trim()) && !filter.getDocRouteStatus().equals(KewApiConstants.ALL_CODE)) {
 213  0
             if (filter.isExcludeRouteStatus()) {
 214  0
                 crit.ne("routeHeader.docRouteStatus", filter.getDocRouteStatus());
 215  
             } else {
 216  0
                 crit.eq("routeHeader.docRouteStatus", filter.getDocRouteStatus());
 217  
             }
 218  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 219  0
             filteredByItems += "Document Route Status";
 220  
         }
 221  
 
 222  0
         if (filter.getDocumentTitle() != null && !"".equals(filter.getDocumentTitle().trim())) {
 223  0
             String docTitle = filter.getDocumentTitle();
 224  0
             if (docTitle.trim().endsWith("*")) {
 225  0
                 docTitle = docTitle.substring(0, docTitle.length() - 1);
 226  
             }
 227  
 
 228  0
             if (filter.isExcludeDocumentTitle()) {
 229  0
                 crit.notLike("docTitle", "%" + docTitle + "%");
 230  
             } else {
 231  0
                 crit.like("docTitle", "%" + docTitle + "%");
 232  
             }
 233  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 234  0
             filteredByItems += "Document Title";
 235  
         }
 236  
 
 237  0
         if (filter.getDocumentType() != null && !"".equals(filter.getDocumentType().trim())) {
 238  0
             if (filter.isExcludeDocumentType()) {
 239  0
                 crit.notLike("docName", "%" + filter.getDocumentType() + "%");
 240  
             } else {
 241  0
                     String documentTypeName = filter.getDocumentType();
 242  0
                     DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName);
 243  0
                     if (documentType == null) {
 244  0
                         crit.like("docName", "%" + filter.getDocumentType() + "%");
 245  
                     } else {
 246  
                         // search this document type plus it's children
 247  0
                         Criteria docTypeCrit = new Criteria(objectsToRetrieve.getName());
 248  0
                         constructDocumentTypeCriteria(objectsToRetrieve.getName(), docTypeCrit, documentType);
 249  0
                         crit.and(docTypeCrit);
 250  
                     }
 251  
             }
 252  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 253  0
             filteredByItems += "Document Type";
 254  
         }
 255  
 
 256  0
         if (filter.getLastAssignedDateFrom() != null || filter.getLastAssignedDateTo() != null) {
 257  0
             if (filter.isExcludeLastAssignedDate()) {
 258  0
                 if (filter.getLastAssignedDateFrom() != null && filter.getLastAssignedDateTo() != null) {
 259  0
                     crit.notBetween("dateAssigned", new Timestamp(beginningOfDay(filter.getLastAssignedDateFrom()).getTime()), new Timestamp(endOfDay(filter.getLastAssignedDateTo()).getTime()));
 260  0
                 } else if (filter.getLastAssignedDateFrom() != null && filter.getLastAssignedDateTo() == null) {
 261  0
                     crit.lte("dateAssigned", new Timestamp(beginningOfDay(filter.getLastAssignedDateFrom()).getTime()));
 262  0
                 } else if (filter.getLastAssignedDateFrom() == null && filter.getLastAssignedDateTo() != null) {
 263  0
                     crit.gte("dateAssigned", new Timestamp(endOfDay(filter.getLastAssignedDateTo()).getTime()));
 264  
                 }
 265  
             } else {
 266  0
                 if (filter.getLastAssignedDateFrom() != null && filter.getLastAssignedDateTo() != null) {
 267  0
                     crit.between("dateAssigned", new Timestamp(beginningOfDay(filter.getLastAssignedDateFrom()).getTime()), new Timestamp(endOfDay(filter.getLastAssignedDateTo()).getTime()));
 268  0
                 } else if (filter.getLastAssignedDateFrom() != null && filter.getLastAssignedDateTo() == null) {
 269  0
                     crit.gte("dateAssigned", new Timestamp(beginningOfDay(filter.getLastAssignedDateFrom()).getTime()));
 270  0
                 } else if (filter.getLastAssignedDateFrom() == null && filter.getLastAssignedDateTo() != null) {
 271  0
                     crit.lte("dateAssigned", new Timestamp(endOfDay(filter.getLastAssignedDateTo()).getTime()));
 272  
                 }
 273  
             }
 274  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 275  0
             filteredByItems += "Date Last Assigned";
 276  
         }
 277  
 
 278  0
         filter.setGroupId(null);
 279  0
         if (filter.getGroupIdString() != null && !"".equals(filter.getGroupIdString().trim()) && !filter.getGroupIdString().trim().equals(KewApiConstants.NO_FILTERING)) {
 280  0
             filter.setGroupId(filter.getGroupId());
 281  0
             if (filter.isExcludeGroupId()) {
 282  0
                 Criteria critNotEqual = new Criteria(objectsToRetrieve.getName());
 283  0
                 critNotEqual.ne("groupId", filter.getGroupId());
 284  0
                 Criteria critNull = new Criteria(objectsToRetrieve.getName());
 285  0
                 critNull.isNull("groupId");
 286  0
                 critNotEqual.or(critNull);
 287  0
                 crit.and(critNotEqual);
 288  0
             } else {
 289  0
                 crit.eq("groupId", filter.getGroupId());
 290  
             }
 291  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 292  0
             filteredByItems += "Action Request Workgroup";
 293  
         }
 294  
 
 295  0
         if (filteredByItems.length() > 0) {
 296  0
             filterOn = true;
 297  
         }
 298  
         
 299  0
         boolean addedDelegationCriteria = false;
 300  0
         if (StringUtils.isBlank(filter.getDelegationType()) && StringUtils.isBlank(filter.getPrimaryDelegateId()) && StringUtils.isBlank(filter.getDelegatorId())) {
 301  0
             crit.eq("principalId", principalId);
 302  0
             addedDelegationCriteria = true;
 303  0
         } else if ((StringUtils.isNotBlank(filter.getDelegationType()) && DelegationType.PRIMARY.getCode().equals(filter.getDelegationType()))
 304  
                 || StringUtils.isNotBlank(filter.getPrimaryDelegateId())) {
 305  
             // using a primary delegation
 306  0
             if ((StringUtils.isBlank(filter.getPrimaryDelegateId())) || (filter.getPrimaryDelegateId().trim().equals(KewApiConstants.ALL_CODE))) {
 307  
                 // user wishes to see all primary delegations
 308  0
                 Criteria userCrit = new Criteria(objectsToRetrieve.getName());
 309  0
                 Criteria groupCrit = new Criteria(objectsToRetrieve.getName());
 310  0
                 Criteria orCrit = new Criteria(objectsToRetrieve.getName());
 311  0
                 userCrit.eq("delegatorPrincipalId", principalId);
 312  
                 
 313  0
                 List<String> userGroupIds = new ArrayList<String>();
 314  0
                 for(String id: KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId)){
 315  0
                         userGroupIds.add(id);
 316  
                 }
 317  0
                 if (!userGroupIds.isEmpty()) {
 318  0
                         groupCrit.in("delegatorGroupId", userGroupIds);
 319  
                 }
 320  0
                 orCrit.or(userCrit);
 321  0
                 orCrit.or(groupCrit);
 322  0
                 crit.and(orCrit);
 323  0
                 crit.eq("delegationType", DelegationType.PRIMARY.getCode());
 324  0
                 filter.setDelegationType(DelegationType.PRIMARY.getCode());
 325  0
                 filter.setExcludeDelegationType(false);
 326  0
                 addToFilterDescription(filteredByItems, "Primary Delegator Id");
 327  0
                 addedDelegationCriteria = true;
 328  0
                 filterOn = true;
 329  0
             } else if (!filter.getPrimaryDelegateId().trim().equals(KewApiConstants.PRIMARY_DELEGATION_DEFAULT)) {
 330  
                 // user wishes to see primary delegation for a single user
 331  0
                 crit.eq("principalId", filter.getPrimaryDelegateId());
 332  0
                 Criteria userCrit = new Criteria(objectsToRetrieve.getName());
 333  0
                 Criteria groupCrit = new Criteria(objectsToRetrieve.getName());
 334  0
                 Criteria orCrit = new Criteria(objectsToRetrieve.getName());
 335  0
                 userCrit.eq("delegatorPrincipalId", principalId);
 336  0
                 List<String> userGroupIds = new ArrayList<String>();
 337  0
                 for(String id: KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId)){
 338  0
                         userGroupIds.add(id);
 339  
                 }
 340  0
                 if (!userGroupIds.isEmpty()) {
 341  0
                         groupCrit.in("delegatorGroupId", userGroupIds);
 342  
                 }
 343  0
                 orCrit.or(userCrit);
 344  0
                 orCrit.or(groupCrit);
 345  0
                 crit.and(orCrit);
 346  0
                 crit.eq("delegationType", DelegationType.PRIMARY.getCode());
 347  0
                 filter.setDelegationType(DelegationType.PRIMARY.getCode());
 348  0
                 filter.setExcludeDelegationType(false);
 349  0
                 addToFilterDescription(filteredByItems, "Primary Delegator Id");
 350  0
                 addedDelegationCriteria = true;
 351  0
                 filterOn = true;
 352  
             }
 353  
         }
 354  0
         if (!addedDelegationCriteria && ( (StringUtils.isNotBlank(filter.getDelegationType()) && DelegationType.SECONDARY.getCode().equals(filter.getDelegationType()))
 355  
                 || StringUtils.isNotBlank(filter.getDelegatorId()) )) {
 356  
             // using a secondary delegation
 357  0
             crit.eq("principalId", principalId);
 358  0
             if (StringUtils.isBlank(filter.getDelegatorId())) {
 359  0
                 filter.setDelegationType(DelegationType.SECONDARY.getCode());
 360  
                 // if isExcludeDelegationType() we want to show the default aciton list which is set up later in this method
 361  0
                 if (!filter.isExcludeDelegationType()) {
 362  0
                     crit.eq("delegationType", DelegationType.SECONDARY.getCode());
 363  0
                     addToFilterDescription(filteredByItems, "Secondary Delegator Id");
 364  0
                     addedDelegationCriteria = true;
 365  0
                     filterOn = true;
 366  
                 }
 367  0
             } else if (filter.getDelegatorId().trim().equals(KewApiConstants.ALL_CODE)) {
 368  
                 // user wishes to see all secondary delegations
 369  0
                 crit.eq("delegationType", DelegationType.SECONDARY.getCode());
 370  0
                 filter.setDelegationType(DelegationType.SECONDARY.getCode());
 371  0
                 filter.setExcludeDelegationType(false);
 372  0
                 addToFilterDescription(filteredByItems, "Secondary Delegator Id");
 373  0
                 addedDelegationCriteria = true;
 374  0
                 filterOn = true;
 375  0
             } else if (!filter.getDelegatorId().trim().equals(
 376  
                     KewApiConstants.DELEGATION_DEFAULT)) {
 377  
                 // user has specified an id to see for secondary delegation
 378  0
                 filter.setDelegationType(DelegationType.SECONDARY.getCode());
 379  0
                 filter.setExcludeDelegationType(false);
 380  0
                 Criteria userCrit = new Criteria(objectsToRetrieve.getName());
 381  0
                 Criteria groupCrit = new Criteria(objectsToRetrieve.getName());
 382  0
                 if (filter.isExcludeDelegatorId()) {
 383  0
                     Criteria userNull = new Criteria(objectsToRetrieve.getName());
 384  0
                     userCrit.ne("delegatorPrincipalId", filter.getDelegatorId());
 385  0
                     userNull.isNull("delegatorPrincipalId");
 386  0
                     userCrit.or(userNull);
 387  0
                     Criteria groupNull = new Criteria(objectsToRetrieve.getName());
 388  0
                     groupCrit.ne("delegatorGroupId", filter.getDelegatorId());
 389  0
                     groupNull.isNull("delegatorGroupId");
 390  0
                     groupCrit.or(groupNull);
 391  0
                     crit.and(userCrit);
 392  0
                     crit.and(groupCrit);
 393  0
                 } else {
 394  0
                     userCrit.eq("delegatorPrincipalId", filter.getDelegatorId());
 395  0
                     groupCrit.eq("delegatorGroupId", filter.getDelegatorId());
 396  0
                     userCrit.or(groupCrit);
 397  0
                     crit.and(userCrit);
 398  
                 }
 399  0
                 addToFilterDescription(filteredByItems, "Secondary Delegator Id");
 400  0
                 addedDelegationCriteria = true;
 401  0
                 filterOn = true;
 402  
             }
 403  
         }
 404  
         
 405  
         // if we haven't added delegation criteria then use the default criteria below
 406  0
         if (!addedDelegationCriteria) {
 407  0
             crit.eq("principalId", principalId);
 408  0
             filter.setDelegationType(DelegationType.SECONDARY.getCode());
 409  0
             filter.setExcludeDelegationType(true);
 410  0
             Criteria critNotEqual = new Criteria(objectsToRetrieve.getName());
 411  0
             Criteria critNull = new Criteria(objectsToRetrieve.getName());
 412  0
             critNotEqual.ne("delegationType", DelegationType.SECONDARY.getCode());
 413  0
             critNull.isNull("delegationType");
 414  0
             critNotEqual.or(critNull);
 415  0
             crit.and(critNotEqual);
 416  
         }
 417  
 
 418  
  
 419  0
         if (! "".equals(filteredByItems)) {
 420  0
             filteredByItems = "Filtered by " + filteredByItems;
 421  
         }
 422  0
         filter.setFilterLegend(filteredByItems);
 423  0
         filter.setFilterOn(filterOn);
 424  
 
 425  0
         LOG.debug("returning from Action List criteria");
 426  0
         return crit;
 427  
     }
 428  
     
 429  
     private void constructDocumentTypeCriteria(String entityName, Criteria criteria, DocumentType documentType) {
 430  
             // search this document type plus it's children
 431  0
             Criteria docTypeBaseCrit = new Criteria(entityName);
 432  0
             docTypeBaseCrit.eq("docName", documentType.getName());
 433  0
             criteria.or(docTypeBaseCrit);
 434  0
             Collection children = documentType.getChildrenDocTypes();
 435  0
             if (children != null) {
 436  0
                 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
 437  0
                         DocumentType childDocumentType = (DocumentType) iterator.next();
 438  0
                         constructDocumentTypeCriteria(entityName, criteria, childDocumentType);
 439  0
                 }
 440  
             }
 441  0
     }
 442  
     
 443  
     private void addToFilterDescription(String filterDescription, String labelToAdd) {
 444  0
         filterDescription += filterDescription.length() > 0 ? ", " : "";
 445  0
         filterDescription += labelToAdd;
 446  0
     }
 447  
 
 448  
     private static final String ACTION_LIST_COUNT_QUERY = "select count(distinct(ai.doc_hdr_id)) from krew_actn_itm_t ai where ai.PRNCPL_ID = ? and (ai.dlgn_typ is null or ai.dlgn_typ = 'P')";
 449  
 
 450  
     public int getCount(final String workflowId) {
 451  
             
 452  0
             javax.persistence.Query q = entityManager.createNativeQuery(ACTION_LIST_COUNT_QUERY);
 453  0
             q.setParameter(1, workflowId);
 454  0
             Number result = (Number)q.getSingleResult();
 455  0
             return result.intValue();
 456  
     }
 457  
 
 458  
     /**
 459  
      * Creates an Action List from the given collection of Action Items.  The Action List should
 460  
      * contain only one action item per document.  The action item chosen should be the most "critical"
 461  
      * or "important" one on the document.
 462  
      *
 463  
      * @return the Action List as a Collection of ActionItems
 464  
      */
 465  
     private <T extends ActionItem> Collection<T> createActionListForUser(Collection<T> actionItems) {
 466  0
         Map<String, T> actionItemMap = new HashMap<String, T>();
 467  0
         ActionListPriorityComparator comparator = new ActionListPriorityComparator();
 468  0
         for (T potentialActionItem: actionItems) {
 469  0
             T existingActionItem = actionItemMap.get(potentialActionItem.getDocumentId());
 470  0
             if (existingActionItem == null || comparator.compare(potentialActionItem, existingActionItem) > 0) {
 471  0
                 actionItemMap.put(potentialActionItem.getDocumentId(), potentialActionItem);
 472  
             }
 473  0
         }
 474  0
         return actionItemMap.values();
 475  
     }
 476  
 
 477  
     /**
 478  
      * Creates an Action List from the given collection of Action Items.  The Action List should
 479  
      * contain only one action item per user.  The action item chosen should be the most "critical"
 480  
      * or "important" one on the document.
 481  
      *
 482  
      * @return the Action List as a Collection of ActionItems
 483  
      */
 484  
     private Collection<ActionItem> createActionListForRouteHeader(Collection<ActionItem> actionItems) {
 485  0
         Map<String, ActionItem> actionItemMap = new HashMap<String, ActionItem>();
 486  0
         ActionListPriorityComparator comparator = new ActionListPriorityComparator();
 487  0
         for (ActionItem potentialActionItem: actionItems) {
 488  0
             ActionItem existingActionItem = actionItemMap.get(potentialActionItem.getPrincipalId());
 489  0
             if (existingActionItem == null || comparator.compare(potentialActionItem, existingActionItem) > 0) {
 490  0
                 actionItemMap.put(potentialActionItem.getPrincipalId(), potentialActionItem);
 491  
             }
 492  0
         }
 493  0
         return actionItemMap.values();
 494  
     }
 495  
     
 496  
     private <T extends ActionItem> Collection<ActionItem> getActionItemsInActionList(Class<T> objectsToRetrieve, String principalId, ActionListFilter filter) {
 497  0
         LOG.debug("getting action list for user " + principalId);
 498  0
         Criteria crit = null;
 499  0
         if (filter == null) {
 500  0
             crit = new Criteria(objectsToRetrieve.getName());
 501  0
             crit.eq("principalId", principalId);
 502  
         } else {
 503  0
             crit = setUpActionListCriteria(objectsToRetrieve, principalId, filter);
 504  
         }
 505  0
         LOG.debug("running query to get action list for criteria " + crit);
 506  0
         Collection<ActionItem> collection = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 507  0
         LOG.debug("found " + collection.size() + " action items for user " + principalId);
 508  0
         return createActionListForUser(collection);
 509  
     }
 510  
 
 511  
     public Collection<ActionItem> getOutbox(String principalId, ActionListFilter filter) {
 512  0
         return getActionItemsInActionList(OutboxItemActionListExtension.class, principalId, filter);
 513  
     }
 514  
 
 515  
     /**
 516  
      * Deletes all outbox items specified by the list of ids
 517  
      * 
 518  
      * @see org.kuali.rice.kew.actionlist.dao.ActionListDAO#removeOutboxItems(String, java.util.List)
 519  
      */
 520  
     public void removeOutboxItems(String principalId, List<String> outboxItems) {
 521  0
         Criteria crit = new Criteria(OutboxItemActionListExtension.class.getName());
 522  0
         crit.in("id", outboxItems);
 523  0
         for(Object entity:new QueryByCriteria(entityManager, crit).toQuery().getResultList()){
 524  0
                 entityManager.remove(entity);
 525  
         }
 526  0
     }
 527  
 
 528  
     /**
 529  
      * Saves an outbox item
 530  
      * 
 531  
      * @see org.kuali.rice.kew.actionlist.dao.ActionListDAO#saveOutboxItem(org.kuali.rice.kew.actionitem.OutboxItemActionListExtension)
 532  
      */
 533  
     public void saveOutboxItem(OutboxItemActionListExtension outboxItem) {
 534  0
             if(outboxItem.getId()==null){
 535  0
                     entityManager.persist(outboxItem);
 536  
             }else{
 537  
               //TODO, merge will not update the outboxitem pointer to the merged entity
 538  0
                     OrmUtils.merge(entityManager, outboxItem);
 539  
             }
 540  0
             entityManager.flush();
 541  0
     }
 542  
 
 543  
     /**
 544  
      * Gets the outbox item associated with the document id
 545  
      * 
 546  
      * @see org.kuali.rice.kew.actionlist.dao.ActionListDAO#getOutboxByDocumentId(java.lang.Long)
 547  
      */
 548  
     public OutboxItemActionListExtension getOutboxByDocumentId(String documentId) {
 549  0
         Criteria crit = new Criteria(OutboxItemActionListExtension.class.getName());
 550  0
         crit.eq("documentId", documentId);
 551  0
         return (OutboxItemActionListExtension) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 552  
     }
 553  
     
 554  
     /**
 555  
      * This overridden method ...
 556  
      * 
 557  
      * @see org.kuali.rice.kew.actionlist.dao.ActionListDAO#getOutboxByDocumentIdUserId(Long, String)
 558  
      */
 559  
     public OutboxItemActionListExtension getOutboxByDocumentIdUserId(String documentId, String userId) {
 560  0
         Criteria crit = new Criteria(OutboxItemActionListExtension.class.getName());
 561  0
         crit.eq("documentId", documentId);
 562  0
         crit.eq("principalId", userId);
 563  0
                return (OutboxItemActionListExtension) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 564  
     }
 565  
     
 566  
     private Date beginningOfDay(Date date) {
 567  0
         Calendar cal = Calendar.getInstance();
 568  0
         cal.setTime(date);
 569  0
         cal.set(Calendar.HOUR_OF_DAY, 0);
 570  0
         cal.set(Calendar.MINUTE, 0);
 571  0
         cal.set(Calendar.SECOND, 0);
 572  0
         return cal.getTime();
 573  
     }
 574  
     
 575  
     private Date endOfDay(Date date) {
 576  0
         Calendar cal = Calendar.getInstance();
 577  0
         cal.setTime(date);
 578  0
         cal.set(Calendar.HOUR_OF_DAY, 23);
 579  0
         cal.set(Calendar.MINUTE, 59);
 580  0
         cal.set(Calendar.SECOND, 59);
 581  0
         return cal.getTime();        
 582  
     }
 583  
 
 584  
     public EntityManager getEntityManager() {
 585  0
         return this.entityManager;
 586  
     }
 587  
 
 588  
     public void setEntityManager(EntityManager entityManager) {
 589  0
         this.entityManager = entityManager;
 590  0
     }
 591  
 
 592  
 
 593  
     
 594  
 }