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