Coverage Report - org.kuali.rice.kew.actionlist.dao.impl.ActionListDAOOjbImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionListDAOOjbImpl
0%
0/258
0%
0/184
7.059
ActionListDAOOjbImpl$1
0%
0/20
0%
0/6
7.059
 
 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.apache.ojb.broker.PersistenceBroker;
 20  
 import org.apache.ojb.broker.accesslayer.LookupException;
 21  
 import org.apache.ojb.broker.query.Criteria;
 22  
 import org.apache.ojb.broker.query.QueryByCriteria;
 23  
 import org.kuali.rice.core.api.delegation.DelegationType;
 24  
 import org.kuali.rice.kew.actionitem.ActionItem;
 25  
 import org.kuali.rice.kew.actionitem.ActionItemActionListExtension;
 26  
 import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension;
 27  
 import org.kuali.rice.kew.actionlist.ActionListFilter;
 28  
 import org.kuali.rice.kew.actionlist.dao.ActionListDAO;
 29  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 30  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 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  
 import org.springmodules.orm.ojb.PersistenceBrokerCallback;
 35  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 36  
 
 37  
 import java.sql.Connection;
 38  
 import java.sql.PreparedStatement;
 39  
 import java.sql.ResultSet;
 40  
 import java.sql.SQLException;
 41  
 import java.sql.Timestamp;
 42  
 import java.util.Calendar;
 43  
 import java.util.Collection;
 44  
 import java.util.Date;
 45  
 import java.util.HashMap;
 46  
 import java.util.Iterator;
 47  
 import java.util.List;
 48  
 import java.util.Map;
 49  
 
 50  
 /**
 51  
  * OJB implementation of the {@link ActionListDAO}.
 52  
  *
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  */
 55  0
 public class ActionListDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionListDAO {
 56  
 
 57  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionListDAOOjbImpl.class);
 58  
 
 59  
     public Collection<ActionItem> getActionList(String principalId, ActionListFilter filter) {
 60  0
         return getActionItemsInActionList(ActionItemActionListExtension.class, principalId, filter);
 61  
 //        LOG.debug("getting action list for user " + workflowUser.getWorkflowUserId().getWorkflowId());
 62  
 //        Criteria crit = null;
 63  
 //        if (filter == null) {
 64  
 //            crit = new Criteria();
 65  
 //            crit.addEqualTo("workflowId", workflowUser.getWorkflowUserId().getWorkflowId());
 66  
 //        } else {
 67  
 //            crit = setUpActionListCriteria(workflowUser, filter);
 68  
 //        }
 69  
 //        LOG.debug("running query to get action list for criteria " + crit);
 70  
 //        Collection<ActionItem> collection = this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItemActionListExtension.class, crit));
 71  
 //        LOG.debug("found " + collection.size() + " action items for user " + workflowUser.getWorkflowUserId().getWorkflowId());
 72  
 //        return createActionListForUser(collection);
 73  
     }
 74  
 
 75  
     public Collection<ActionItem> getActionListForSingleDocument(String documentId) {
 76  0
         LOG.debug("getting action list for document id " + documentId);
 77  0
         Criteria crit = new Criteria();
 78  0
         crit.addEqualTo("documentId", documentId);
 79  0
         Collection<ActionItem> collection = this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItemActionListExtension.class, crit));
 80  0
         LOG.debug("found " + collection.size() + " action items for document id " + documentId);
 81  0
         return createActionListForRouteHeader(collection);
 82  
     }
 83  
 
 84  
     private Criteria setUpActionListCriteria(String principalId, ActionListFilter filter) {
 85  0
         LOG.debug("setting up Action List criteria");
 86  0
         Criteria crit = new Criteria();
 87  0
         boolean filterOn = false;
 88  0
         String filteredByItems = "";
 89  
 
 90  0
         if (filter.getActionRequestCd() != null && !"".equals(filter.getActionRequestCd().trim()) && !filter.getActionRequestCd().equals(KewApiConstants.ALL_CODE)) {
 91  0
             if (filter.isExcludeActionRequestCd()) {
 92  0
                 crit.addNotEqualTo("actionRequestCd", filter.getActionRequestCd());
 93  
             } else {
 94  0
                 crit.addEqualTo("actionRequestCd", filter.getActionRequestCd());
 95  
             }
 96  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 97  0
             filteredByItems += "Action Requested";
 98  
         }
 99  
 
 100  0
         if (filter.getCreateDateFrom() != null || filter.getCreateDateTo() != null) {
 101  0
             if (filter.isExcludeCreateDate()) {
 102  0
                 if (filter.getCreateDateFrom() != null && filter.getCreateDateTo() != null) {
 103  0
                     crit.addNotBetween("routeHeader.createDate", new Timestamp(beginningOfDay(filter.getCreateDateFrom()).getTime()), new Timestamp(endOfDay(filter.getCreateDateTo()).getTime()));
 104  0
                 } else if (filter.getCreateDateFrom() != null && filter.getCreateDateTo() == null) {
 105  0
                     crit.addLessOrEqualThan("routeHeader.createDate", new Timestamp(beginningOfDay(filter.getCreateDateFrom()).getTime()));
 106  0
                 } else if (filter.getCreateDateFrom() == null && filter.getCreateDateTo() != null) {
 107  0
                     crit.addGreaterOrEqualThan("routeHeader.createDate", new Timestamp(endOfDay(filter.getCreateDateTo()).getTime()));
 108  
                 }
 109  
             } else {
 110  0
                 if (filter.getCreateDateFrom() != null && filter.getCreateDateTo() != null) {
 111  0
                     crit.addBetween("routeHeader.createDate", new Timestamp(beginningOfDay(filter.getCreateDateFrom()).getTime()), new Timestamp(endOfDay(filter.getCreateDateTo()).getTime()));
 112  0
                 } else if (filter.getCreateDateFrom() != null && filter.getCreateDateTo() == null) {
 113  0
                     crit.addGreaterOrEqualThan("routeHeader.createDate", new Timestamp(beginningOfDay(filter.getCreateDateFrom()).getTime()));
 114  0
                 } else if (filter.getCreateDateFrom() == null && filter.getCreateDateTo() != null) {
 115  0
                     crit.addLessOrEqualThan("routeHeader.createDate", new Timestamp(endOfDay(filter.getCreateDateTo()).getTime()));
 116  
                 }
 117  
             }
 118  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 119  0
             filteredByItems += "Date Created";
 120  
         }
 121  
 
 122  0
         if (filter.getDocRouteStatus() != null && !"".equals(filter.getDocRouteStatus().trim()) && !filter.getDocRouteStatus().equals(KewApiConstants.ALL_CODE)) {
 123  0
             if (filter.isExcludeRouteStatus()) {
 124  0
                 crit.addNotEqualTo("routeHeader.docRouteStatus", filter.getDocRouteStatus());
 125  
             } else {
 126  0
                 crit.addEqualTo("routeHeader.docRouteStatus", filter.getDocRouteStatus());
 127  
             }
 128  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 129  0
             filteredByItems += "Document Route Status";
 130  
         }
 131  
 
 132  0
         if (filter.getDocumentTitle() != null && !"".equals(filter.getDocumentTitle().trim())) {
 133  0
             String docTitle = filter.getDocumentTitle();
 134  0
             if (docTitle.trim().endsWith("*")) {
 135  0
                 docTitle = docTitle.substring(0, docTitle.length() - 1);
 136  
             }
 137  
 
 138  0
             if (filter.isExcludeDocumentTitle()) {
 139  0
                 crit.addNotLike("docTitle", "%" + docTitle + "%");
 140  
             } else {
 141  0
                 crit.addLike("docTitle", "%" + docTitle + "%");
 142  
             }
 143  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 144  0
             filteredByItems += "Document Title";
 145  
         }
 146  
 
 147  0
         if (filter.getDocumentType() != null && !"".equals(filter.getDocumentType().trim())) {
 148  0
             if (filter.isExcludeDocumentType()) {
 149  0
                 crit.addNotLike("docName", "%" + filter.getDocumentType() + "%");
 150  
             } else {
 151  0
                     String documentTypeName = filter.getDocumentType();
 152  0
                     DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName);
 153  0
                     if (documentType == null) {
 154  0
                         crit.addLike("docName", "%" + filter.getDocumentType() + "%");
 155  
                     } else {
 156  
                         // search this document type plus it's children
 157  0
                         Criteria docTypeCrit = new Criteria();
 158  0
                         constructDocumentTypeCriteria(docTypeCrit, documentType);
 159  0
                         crit.addAndCriteria(docTypeCrit);
 160  
                     }
 161  
             }
 162  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 163  0
             filteredByItems += "Document Type";
 164  
         }
 165  
 
 166  0
         if (filter.getLastAssignedDateFrom() != null || filter.getLastAssignedDateTo() != null) {
 167  0
             if (filter.isExcludeLastAssignedDate()) {
 168  0
                 if (filter.getLastAssignedDateFrom() != null && filter.getLastAssignedDateTo() != null) {
 169  0
                     crit.addNotBetween("dateAssigned", new Timestamp(beginningOfDay(filter.getLastAssignedDateFrom()).getTime()), new Timestamp(endOfDay(filter.getLastAssignedDateTo()).getTime()));
 170  0
                 } else if (filter.getLastAssignedDateFrom() != null && filter.getLastAssignedDateTo() == null) {
 171  0
                     crit.addLessOrEqualThan("dateAssigned", new Timestamp(beginningOfDay(filter.getLastAssignedDateFrom()).getTime()));
 172  0
                 } else if (filter.getLastAssignedDateFrom() == null && filter.getLastAssignedDateTo() != null) {
 173  0
                     crit.addGreaterOrEqualThan("dateAssigned", new Timestamp(endOfDay(filter.getLastAssignedDateTo()).getTime()));
 174  
                 }
 175  
             } else {
 176  0
                 if (filter.getLastAssignedDateFrom() != null && filter.getLastAssignedDateTo() != null) {
 177  0
                     crit.addBetween("dateAssigned", new Timestamp(beginningOfDay(filter.getLastAssignedDateFrom()).getTime()), new Timestamp(endOfDay(filter.getLastAssignedDateTo()).getTime()));
 178  0
                 } else if (filter.getLastAssignedDateFrom() != null && filter.getLastAssignedDateTo() == null) {
 179  0
                     crit.addGreaterOrEqualThan("dateAssigned", new Timestamp(beginningOfDay(filter.getLastAssignedDateFrom()).getTime()));
 180  0
                 } else if (filter.getLastAssignedDateFrom() == null && filter.getLastAssignedDateTo() != null) {
 181  0
                     crit.addLessOrEqualThan("dateAssigned", new Timestamp(endOfDay(filter.getLastAssignedDateTo()).getTime()));
 182  
                 }
 183  
             }
 184  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 185  0
             filteredByItems += "Date Last Assigned";
 186  
         }
 187  
 
 188  0
         filter.setGroupId(null);
 189  0
         if (filter.getGroupIdString() != null && !"".equals(filter.getGroupIdString().trim()) && !filter.getGroupIdString().trim().equals(KewApiConstants.NO_FILTERING)) {
 190  
 
 191  0
             filter.setGroupId(filter.getGroupIdString().trim());
 192  
           
 193  0
             if (filter.isExcludeGroupId()) {
 194  0
                 Criteria critNotEqual = new Criteria();
 195  0
                 critNotEqual.addNotEqualTo("groupId", filter.getGroupId());
 196  0
                 Criteria critNull = new Criteria();
 197  0
                 critNull.addIsNull("groupId");
 198  0
                 critNotEqual.addOrCriteria(critNull);
 199  0
                 crit.addAndCriteria(critNotEqual);
 200  0
             } else {
 201  0
                 crit.addEqualTo("groupId", filter.getGroupId());
 202  
             }
 203  0
             filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 204  0
             filteredByItems += "Action Request Workgroup";
 205  
         }
 206  
 
 207  0
         if (filteredByItems.length() > 0) {
 208  0
             filterOn = true;
 209  
         }
 210  
 
 211  0
         boolean addedDelegationCriteria = false;
 212  0
         if (StringUtils.isBlank(filter.getDelegationType()) && StringUtils.isBlank(filter.getPrimaryDelegateId()) && StringUtils.isBlank(filter.getDelegatorId())) {
 213  0
             crit.addEqualTo("principalId", principalId);
 214  0
             addedDelegationCriteria = true;
 215  0
         } else if ((StringUtils.isNotBlank(filter.getDelegationType()) && DelegationType.PRIMARY.getCode().equals(filter.getDelegationType()))
 216  
                 || StringUtils.isNotBlank(filter.getPrimaryDelegateId())) {
 217  
             // using a primary delegation
 218  0
             if ((StringUtils.isBlank(filter.getPrimaryDelegateId())) || (filter.getPrimaryDelegateId().trim().equals(KewApiConstants.ALL_CODE))) {
 219  
                 // user wishes to see all primary delegations
 220  0
                 Criteria userCrit = new Criteria();
 221  0
                 Criteria groupCrit = new Criteria();
 222  0
                 Criteria orCrit = new Criteria();
 223  0
                 userCrit.addEqualTo("delegatorPrincipalId", principalId);
 224  0
                 List<String> delegatorGroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(
 225  
                         principalId);
 226  0
                 if (delegatorGroupIds != null && !delegatorGroupIds.isEmpty()) {
 227  0
                         groupCrit.addIn("delegatorGroupId", delegatorGroupIds);
 228  
                 }
 229  0
                 orCrit.addOrCriteria(userCrit);
 230  0
                 orCrit.addOrCriteria(groupCrit);
 231  0
                 crit.addAndCriteria(orCrit);
 232  0
                 crit.addEqualTo("delegationType", DelegationType.PRIMARY.getCode());
 233  0
                 filter.setDelegationType(DelegationType.PRIMARY.getCode());
 234  0
                 filter.setExcludeDelegationType(false);
 235  0
                 addToFilterDescription(filteredByItems, "Primary Delegator Id");
 236  0
                 addedDelegationCriteria = true;
 237  0
                 filterOn = true;
 238  0
             } else if (!filter.getPrimaryDelegateId().trim().equals(KewApiConstants.PRIMARY_DELEGATION_DEFAULT)) {
 239  
                 // user wishes to see primary delegation for a single user
 240  0
                 crit.addEqualTo("principalId", filter.getPrimaryDelegateId());
 241  0
                 Criteria userCrit = new Criteria();
 242  0
                 Criteria groupCrit = new Criteria();
 243  0
                 Criteria orCrit = new Criteria();
 244  0
                 userCrit.addEqualTo("delegatorPrincipalId", principalId);
 245  0
                 List<String> delegatorGroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(
 246  
                         principalId);
 247  0
                 if (delegatorGroupIds != null && !delegatorGroupIds.isEmpty()) {
 248  0
                         groupCrit.addIn("delegatorGroupId", delegatorGroupIds);
 249  
                 }
 250  0
                 orCrit.addOrCriteria(userCrit);
 251  0
                 orCrit.addOrCriteria(groupCrit);
 252  0
                 crit.addAndCriteria(orCrit);
 253  0
                 crit.addEqualTo("delegationType", DelegationType.PRIMARY.getCode());
 254  0
                 filter.setDelegationType(DelegationType.PRIMARY.getCode());
 255  0
                 filter.setExcludeDelegationType(false);
 256  0
                 addToFilterDescription(filteredByItems, "Primary Delegator Id");
 257  0
                 addedDelegationCriteria = true;
 258  0
                 filterOn = true;
 259  
             }
 260  
         }
 261  0
         if (!addedDelegationCriteria && ( (StringUtils.isNotBlank(filter.getDelegationType()) && DelegationType.SECONDARY.getCode().equals(filter.getDelegationType()))
 262  
                 || StringUtils.isNotBlank(filter.getDelegatorId()) )) {
 263  
             // using a secondary delegation
 264  0
             crit.addEqualTo("principalId", principalId);
 265  0
             if (StringUtils.isBlank(filter.getDelegatorId())) {
 266  0
                 filter.setDelegationType(DelegationType.SECONDARY.getCode());
 267  
                 // if isExcludeDelegationType() we want to show the default action list which is set up later in this method
 268  0
                 if (!filter.isExcludeDelegationType()) {
 269  0
                     crit.addEqualTo("delegationType", DelegationType.SECONDARY.getCode());
 270  0
                     addToFilterDescription(filteredByItems, "Secondary Delegator Id");
 271  0
                     addedDelegationCriteria = true;
 272  0
                     filterOn = true;
 273  
                 }
 274  0
             } else if (filter.getDelegatorId().trim().equals(KewApiConstants.ALL_CODE)) {
 275  
                 // user wishes to see all secondary delegations
 276  0
                 crit.addEqualTo("delegationType", DelegationType.SECONDARY.getCode());
 277  0
                 filter.setDelegationType(DelegationType.SECONDARY.getCode());
 278  0
                 filter.setExcludeDelegationType(false);
 279  0
                 addToFilterDescription(filteredByItems, "Secondary Delegator Id");
 280  0
                 addedDelegationCriteria = true;
 281  0
                 filterOn = true;
 282  0
             } else if (!filter.getDelegatorId().trim().equals(
 283  
                     KewApiConstants.DELEGATION_DEFAULT)) {
 284  
                 // user has specified an id to see for secondary delegation
 285  0
                 filter.setDelegationType(DelegationType.SECONDARY.getCode());
 286  0
                 filter.setExcludeDelegationType(false);
 287  0
                 Criteria userCrit = new Criteria();
 288  0
                 Criteria groupCrit = new Criteria();
 289  0
                 if (filter.isExcludeDelegatorId()) {
 290  0
                     Criteria userNull = new Criteria();
 291  0
                     userCrit.addNotEqualTo("delegatorPrincipalId", filter.getDelegatorId());
 292  0
                     userNull.addIsNull("delegatorPrincipalId");
 293  0
                     userCrit.addOrCriteria(userNull);
 294  0
                     Criteria groupNull = new Criteria();
 295  0
                     groupCrit.addNotEqualTo("delegatorGroupId", filter.getDelegatorId());
 296  0
                     groupNull.addIsNull("delegatorGroupId");
 297  0
                     groupCrit.addOrCriteria(groupNull);
 298  0
                     crit.addAndCriteria(userCrit);
 299  0
                     crit.addAndCriteria(groupCrit);
 300  0
                 } else {
 301  0
                     Criteria orCrit = new Criteria();
 302  0
                     userCrit.addEqualTo("delegatorPrincipalId", filter.getDelegatorId());
 303  0
                     groupCrit.addEqualTo("delegatorGroupId", filter.getDelegatorId());
 304  0
                     orCrit.addOrCriteria(userCrit);
 305  0
                     orCrit.addOrCriteria(groupCrit);
 306  0
                     crit.addAndCriteria(orCrit);
 307  
                 }
 308  0
                 addToFilterDescription(filteredByItems, "Secondary Delegator Id");
 309  0
                 addedDelegationCriteria = true;
 310  0
                 filterOn = true;
 311  
             }
 312  
 //            } else if ( (StringUtils.isNotBlank(filter.getDelegationType()) && KewApiConstants.DELEGATION_DEFAULT.equals(filter.getDelegationType())) ||
 313  
 //                    StringUtils.isNotBlank(filter.getDelegatorId()) ) {
 314  
 //            // not using a primary delegation so we can assume the action item will be assigned to the given user
 315  
 //            crit.addEqualTo("workflowId", user.getWorkflowUserId().getWorkflowId());
 316  
 //            if (filter.getDelegatorId() != null && !"".equals(filter.getDelegatorId().trim()) && !filter.getDelegatorId().trim().equals(KewApiConstants.DELEGATION_DEFAULT)
 317  
 //                    && !filter.getDelegatorId().trim().equals(KewApiConstants.ALL_CODE)) {
 318  
 //                filter.setDelegationType(DelegationType.SECONDARY.getCode());
 319  
 //                filter.setExcludeDelegationType(false);
 320  
 //                Criteria userCrit = new Criteria();
 321  
 //                Criteria groupCrit = new Criteria();
 322  
 //                if (filter.isExcludeDelegatorId()) {
 323  
 //                    Criteria userNull = new Criteria();
 324  
 //                    userCrit.addNotEqualTo("delegatorPrincipalId", filter.getDelegatorId());
 325  
 //                    userNull.addIsNull("delegatorPrincipalId");
 326  
 //                    userCrit.addOrCriteria(userNull);
 327  
 //                    Criteria groupNull = new Criteria();
 328  
 //                    groupCrit.addNotEqualTo("delegatorGroupId", filter.getDelegatorId());
 329  
 //                    groupNull.addIsNull("delegatorGroupId");
 330  
 //                    groupCrit.addOrCriteria(groupNull);
 331  
 //                    crit.addAndCriteria(userCrit);
 332  
 //                    crit.addAndCriteria(groupCrit);
 333  
 //                } else {
 334  
 //                    Criteria orCrit = new Criteria();
 335  
 //                    userCrit.addEqualTo("delegatorPrincipalId", filter.getDelegatorId());
 336  
 //                    groupCrit.addEqualTo("delegatorGroupId", filter.getDelegatorId());
 337  
 //                    orCrit.addOrCriteria(userCrit);
 338  
 //                    orCrit.addOrCriteria(groupCrit);
 339  
 //                    crit.addAndCriteria(orCrit);
 340  
 //                }
 341  
 //                addToFilterDescription(filteredByItems, "Secondary Delegator Id");
 342  
 //                addedDelegationCriteria = true;
 343  
 //            } else if (filter.getDelegatorId().trim().equals(KewApiConstants.ALL_CODE)) {
 344  
 //                filter.setDelegationType(DelegationType.SECONDARY.getCode());
 345  
 //                filter.setExcludeDelegationType(false);
 346  
 //                addToFilterDescription(filteredByItems, "Secondary Delegator Id");
 347  
 //                addedDelegationCriteria = true;
 348  
 //            }
 349  
         }
 350  
 
 351  
         // if we haven't added delegation criteria then use the default criteria below
 352  0
         if (!addedDelegationCriteria) {
 353  0
             crit.addEqualTo("principalId", principalId);
 354  0
             filter.setDelegationType(DelegationType.SECONDARY.getCode());
 355  0
             filter.setExcludeDelegationType(true);
 356  0
             Criteria critNotEqual = new Criteria();
 357  0
             Criteria critNull = new Criteria();
 358  0
             critNotEqual.addNotEqualTo("delegationType", DelegationType.SECONDARY.getCode());
 359  0
             critNull.addIsNull("delegationType");
 360  0
             critNotEqual.addOrCriteria(critNull);
 361  0
             crit.addAndCriteria(critNotEqual);
 362  
         }
 363  
 
 364  
 
 365  
 //        if (filter.getPrimaryDelegateId().equals(KewApiConstants.PRIMARY_DELEGATION_DEFAULT) && filter.getDelegatorId().equals(KewApiConstants.DELEGATION_DEFAULT)) {
 366  
 //            // no secondary or primary delegation displayed
 367  
 //            crit.addEqualTo("workflowId", user.getWorkflowUserId().getWorkflowId());
 368  
 //            filter.setDelegationType(DelegationType.SECONDARY.getCode());
 369  
 //            Criteria critNotEqual = new Criteria();
 370  
 //            Criteria critNull = new Criteria();
 371  
 //            critNotEqual.addNotEqualTo("delegationType", DelegationType.SECONDARY.getCode());
 372  
 //            critNull.addIsNull("delegationType");
 373  
 //            critNotEqual.addOrCriteria(critNull);
 374  
 //            crit.addAndCriteria(critNotEqual);
 375  
 //            filter.setExcludeDelegationType(true);
 376  
 //        } else if (filter.getPrimaryDelegateId().trim().equals(KewApiConstants.ALL_CODE)) {
 377  
 //            // user wishes to see all primary delegations
 378  
 //            Criteria userCrit = new Criteria();
 379  
 //            Criteria groupCrit = new Criteria();
 380  
 //            Criteria orCrit = new Criteria();
 381  
 //            userCrit.addEqualTo("delegatorPrincipalId", user.getWorkflowUserId().getWorkflowId());
 382  
 //            groupCrit.addEqualTo("delegatorGroupId", filter.getPrimaryDelegateId()); // TODO delyea: add all workgroups here?
 383  
 //            orCrit.addOrCriteria(userCrit);
 384  
 //            orCrit.addOrCriteria(groupCrit);
 385  
 //            crit.addAndCriteria(orCrit);
 386  
 //            crit.addEqualTo("delegationType", DelegationType.PRIMARY.getCode());
 387  
 //            filter.setDelegationType(DelegationType.PRIMARY.getCode());
 388  
 //            filter.setExcludeDelegationType(false);
 389  
 //            filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 390  
 //            filteredByItems += "Primary Delegator Id";
 391  
 //            filterOn = true;
 392  
 //        } else if (filter.getDelegatorId().trim().equals(KewApiConstants.ALL_CODE)) {
 393  
 //            // user wishes to see all secondary delegations
 394  
 //            crit.addEqualTo("workflowId", user.getWorkflowUserId().getWorkflowId());
 395  
 //            crit.addEqualTo("delegationType", DelegationType.SECONDARY.getCode());
 396  
 //            filter.setDelegationType(DelegationType.SECONDARY.getCode());
 397  
 //            filter.setExcludeDelegationType(false);
 398  
 //            filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 399  
 //            filteredByItems += "Secondary Delegator Id";
 400  
 //            filterOn = true;
 401  
 //        } else if (filter.getPrimaryDelegateId() != null && !"".equals(filter.getPrimaryDelegateId().trim())) {
 402  
 //            // user wishes to see primary delegation for a single user
 403  
 //            Criteria userCrit = new Criteria();
 404  
 //            Criteria groupCrit = new Criteria();
 405  
 //            Criteria orCrit = new Criteria();
 406  
 //            userCrit.addEqualTo("delegatorPrincipalId", user.getWorkflowUserId().getWorkflowId());
 407  
 //            groupCrit.addEqualTo("delegatorGroupId", filter.getDelegatorId()); // TODO delyea: add all workgroups here?
 408  
 //            orCrit.addOrCriteria(userCrit);
 409  
 //            orCrit.addOrCriteria(groupCrit);
 410  
 //            crit.addAndCriteria(orCrit);
 411  
 //            crit.addEqualTo("delegationType", DelegationType.PRIMARY.getCode());
 412  
 //            filter.setDelegationType(DelegationType.PRIMARY.getCode());
 413  
 //            filter.setExcludeDelegationType(false);
 414  
 //            filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 415  
 //            filteredByItems += "Primary Delegator Id";
 416  
 //            filterOn = true;
 417  
 //        } else if (filter.getDelegatorId() != null && !"".equals(filter.getDelegatorId().trim())) {
 418  
 //            // user wishes to see secondary delegation for a single user
 419  
 //            crit.addEqualTo("workflowId", user.getWorkflowUserId().getWorkflowId());
 420  
 //            crit.addEqualTo("delegationType", DelegationType.SECONDARY.getCode());
 421  
 //            filter.setDelegationType(DelegationType.SECONDARY.getCode());
 422  
 //            filter.setExcludeDelegationType(false);
 423  
 //            Criteria userCrit = new Criteria();
 424  
 //            Criteria groupCrit = new Criteria();
 425  
 //            if (filter.isExcludeDelegatorId()) {
 426  
 //                Criteria userNull = new Criteria();
 427  
 //                userCrit.addNotEqualTo("delegatorPrincipalId", filter.getDelegatorId());
 428  
 //                userNull.addIsNull("delegatorPrincipalId");
 429  
 //                userCrit.addOrCriteria(userNull);
 430  
 //                Criteria groupNull = new Criteria();
 431  
 //                groupCrit.addNotEqualTo("delegatorGroupId", filter.getDelegatorId());
 432  
 //                groupNull.addIsNull("delegatorGroupId");
 433  
 //                groupCrit.addOrCriteria(groupNull);
 434  
 //                crit.addAndCriteria(userCrit);
 435  
 //                crit.addAndCriteria(groupCrit);
 436  
 //            } else {
 437  
 //                Criteria orCrit = new Criteria();
 438  
 //                userCrit.addEqualTo("delegatorPrincipalId", filter.getDelegatorId());
 439  
 //                groupCrit.addEqualTo("delegatorGroupId", filter.getDelegatorId());
 440  
 //                orCrit.addOrCriteria(userCrit);
 441  
 //                orCrit.addOrCriteria(groupCrit);
 442  
 //                crit.addAndCriteria(orCrit);
 443  
 //            }
 444  
 //            filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 445  
 //            filteredByItems += "SeDelegator Id";
 446  
 //            filterOn = true;
 447  
 //        } else if (StringUtils.isBlank(filter.getPrimaryDelegateId()) && StringUtils.isBlank(filter.getDelegatorId())) {
 448  
 //            crit.addEqualTo("workflowId", user.getWorkflowUserId().getWorkflowId());
 449  
 //            if (filter.getDelegationType() != null && !"".equals(filter.getDelegationType().trim())) {
 450  
 //                if (filter.isExcludeDelegationType()) {
 451  
 //                    Criteria critNotEqual = new Criteria();
 452  
 //                    Criteria critNull = new Criteria();
 453  
 //                    critNotEqual.addNotEqualTo("delegationType", filter.getDelegationType());
 454  
 //                    critNull.addIsNull("delegationType");
 455  
 //                    critNotEqual.addOrCriteria(critNull);
 456  
 //                    crit.addAndCriteria(critNotEqual);
 457  
 //                } else {
 458  
 //                    crit.addEqualTo("delegationType", filter.getDelegationType());
 459  
 //                }
 460  
 //            }
 461  
 //        }
 462  
 
 463  
 
 464  
 //        if (primary delegation) {
 465  
 //            filter.setDelegationType(DelegationType.PRIMARY.getCode());
 466  
 //            crit.addEqualTo("delegatorPrincipalId", user.getWorkflowUserId().getWorkflowId());
 467  
 //
 468  
 //        } else {
 469  
 //            crit.addEqualTo("workflowId", user.getWorkflowUserId().getWorkflowId());
 470  
 //            if (filter.getDelegatorId() != null && !"".equals(filter.getDelegatorId().trim()) && !filter.getDelegatorId().trim().equals(KewApiConstants.DELEGATION_DEFAULT)
 471  
 //                    && !filter.getDelegatorId().trim().equals(KewApiConstants.ALL_CODE)) {
 472  
 //                filter.setDelegationType(DelegationType.SECONDARY.getCode());
 473  
 //                filter.setExcludeDelegationType(false);
 474  
 //                Criteria userCrit = new Criteria();
 475  
 //                Criteria groupCrit = new Criteria();
 476  
 //                if (filter.isExcludeDelegatorId()) {
 477  
 //                    Criteria userNull = new Criteria();
 478  
 //                    userCrit.addNotEqualTo("delegatorPrincipalId", filter.getDelegatorId());
 479  
 //                    userNull.addIsNull("delegatorPrincipalId");
 480  
 //                    userCrit.addOrCriteria(userNull);
 481  
 //                    Criteria groupNull = new Criteria();
 482  
 //                    groupCrit.addNotEqualTo("delegatorGroupId", filter.getDelegatorId());
 483  
 //                    groupNull.addIsNull("delegatorGroupId");
 484  
 //                    groupCrit.addOrCriteria(groupNull);
 485  
 //                    crit.addAndCriteria(userCrit);
 486  
 //                    crit.addAndCriteria(groupCrit);
 487  
 //                } else {
 488  
 //                    Criteria orCrit = new Criteria();
 489  
 //                    userCrit.addEqualTo("delegatorPrincipalId", filter.getDelegatorId());
 490  
 //                    groupCrit.addEqualTo("delegatorGroupId", filter.getDelegatorId());
 491  
 //                    orCrit.addOrCriteria(userCrit);
 492  
 //                    orCrit.addOrCriteria(groupCrit);
 493  
 //                    crit.addAndCriteria(orCrit);
 494  
 //                }
 495  
 //                filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 496  
 //                filteredByItems += "Delegator Id";
 497  
 //                filterOn = true;
 498  
 //            } else if (filter.getDelegatorId().trim().equals(KewApiConstants.DELEGATION_DEFAULT)) {
 499  
 //                filter.setDelegationType(DelegationType.SECONDARY.getCode());
 500  
 //                filter.setExcludeDelegationType(true);
 501  
 //            } else if (filter.getDelegatorId().trim().equals(KewApiConstants.ALL_CODE)) {
 502  
 //                filter.setDelegationType(DelegationType.SECONDARY.getCode());
 503  
 //                filter.setExcludeDelegationType(false);
 504  
 //                filteredByItems += filteredByItems.length() > 0 ? ", " : "";
 505  
 //                filteredByItems += "Delegator Id";
 506  
 //                filterOn = true;
 507  
 //            }
 508  
 //
 509  
 //        }
 510  
 //
 511  
 //
 512  
 //        //must come after delegation id since the delegation choices are all secondary delegations
 513  
 //        if (filter.getDelegationType() != null && !"".equals(filter.getDelegationType().trim())) {
 514  
 //            if (filter.isExcludeDelegationType()) {
 515  
 //                Criteria critNotEqual = new Criteria();
 516  
 //                Criteria critNull = new Criteria();
 517  
 //                critNotEqual.addNotEqualTo("delegationType", filter.getDelegationType());
 518  
 //                critNull.addIsNull("delegationType");
 519  
 //                critNotEqual.addOrCriteria(critNull);
 520  
 //                crit.addAndCriteria(critNotEqual);
 521  
 //            } else {
 522  
 //                crit.addEqualTo("delegationType", filter.getDelegationType());
 523  
 //            }
 524  
 //        }
 525  
 
 526  0
         if (! "".equals(filteredByItems)) {
 527  0
             filteredByItems = "Filtered by " + filteredByItems;
 528  
         }
 529  0
         filter.setFilterLegend(filteredByItems);
 530  0
         filter.setFilterOn(filterOn);
 531  
 
 532  0
         LOG.debug("returning from Action List criteria");
 533  0
         return crit;
 534  
     }
 535  
 
 536  
     private void constructDocumentTypeCriteria(Criteria criteria, DocumentType documentType) {
 537  
             // search this document type plus it's children
 538  0
             Criteria docTypeBaseCrit = new Criteria();
 539  0
             docTypeBaseCrit.addEqualTo("docName", documentType.getName());
 540  0
             criteria.addOrCriteria(docTypeBaseCrit);
 541  0
             Collection children = documentType.getChildrenDocTypes();
 542  0
             if (children != null) {
 543  0
                 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
 544  0
                         DocumentType childDocumentType = (DocumentType) iterator.next();
 545  0
                         constructDocumentTypeCriteria(criteria, childDocumentType);
 546  0
                 }
 547  
             }
 548  0
     }
 549  
     
 550  
     private void addToFilterDescription(String filterDescription, String labelToAdd) {
 551  0
         filterDescription += filterDescription.length() > 0 ? ", " : "";
 552  0
         filterDescription += labelToAdd;
 553  0
     }
 554  
 
 555  
     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')";
 556  
 
 557  
     public int getCount(final String workflowId) {
 558  0
             return (Integer)getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
 559  
             public Object doInPersistenceBroker(PersistenceBroker broker) {
 560  0
                 PreparedStatement statement = null;
 561  0
                 ResultSet resultSet = null;
 562  
                 try {
 563  0
                     Connection connection = broker.serviceConnectionManager().getConnection();
 564  0
                     statement = connection.prepareStatement(ACTION_LIST_COUNT_QUERY);
 565  0
                     statement.setString(1, workflowId);
 566  0
                     resultSet = statement.executeQuery();
 567  0
                     if (!resultSet.next()) {
 568  0
                         throw new WorkflowRuntimeException("Error determining Action List Count.");
 569  
                     }
 570  0
                     return resultSet.getInt(1);
 571  0
                 } catch (SQLException e) {
 572  0
                     throw new WorkflowRuntimeException("Error determining Action List Count.", e);
 573  0
                 } catch (LookupException e) {
 574  0
                     throw new WorkflowRuntimeException("Error determining Action List Count.", e);
 575  
                 } finally {
 576  0
                     if (statement != null) {
 577  
                         try {
 578  0
                             statement.close();
 579  0
                         } catch (SQLException e) {}
 580  
                     }
 581  0
                     if (resultSet != null) {
 582  
                         try {
 583  0
                             resultSet.close();
 584  0
                         } catch (SQLException e) {}
 585  
                     }
 586  
                 }
 587  
             }
 588  
         });
 589  
     }
 590  
 
 591  
     /**
 592  
      * Creates an Action List from the given collection of Action Items.  The Action List should
 593  
      * contain only one action item per document.  The action item chosen should be the most "critical"
 594  
      * or "important" one on the document.
 595  
      *
 596  
      * @return the Action List as a Collection of ActionItems
 597  
      */
 598  
     private Collection<ActionItem> createActionListForUser(Collection<ActionItem> actionItems) {
 599  0
         Map<String, ActionItem> actionItemMap = new HashMap<String, ActionItem>();
 600  0
         ActionListPriorityComparator comparator = new ActionListPriorityComparator();
 601  0
         for (ActionItem potentialActionItem: actionItems) {
 602  0
             ActionItem existingActionItem = actionItemMap.get(potentialActionItem.getDocumentId());
 603  0
             if (existingActionItem == null || comparator.compare(potentialActionItem, existingActionItem) > 0) {
 604  0
                 actionItemMap.put(potentialActionItem.getDocumentId(), potentialActionItem);
 605  
             }
 606  0
         }
 607  0
         return actionItemMap.values();
 608  
     }
 609  
 
 610  
     /**
 611  
      * Creates an Action List from the given collection of Action Items.  The Action List should
 612  
      * contain only one action item per user.  The action item chosen should be the most "critical"
 613  
      * or "important" one on the document.
 614  
      *
 615  
      * @return the Action List as a Collection of ActionItems
 616  
      */
 617  
     private Collection<ActionItem> createActionListForRouteHeader(Collection<ActionItem> actionItems) {
 618  0
         Map<String, ActionItem> actionItemMap = new HashMap<String, ActionItem>();
 619  0
         ActionListPriorityComparator comparator = new ActionListPriorityComparator();
 620  0
         for (ActionItem potentialActionItem: actionItems) {
 621  0
             ActionItem existingActionItem = actionItemMap.get(potentialActionItem.getPrincipalId());
 622  0
             if (existingActionItem == null || comparator.compare(potentialActionItem, existingActionItem) > 0) {
 623  0
                 actionItemMap.put(potentialActionItem.getPrincipalId(), potentialActionItem);
 624  
             }
 625  0
         }
 626  0
         return actionItemMap.values();
 627  
     }
 628  
 
 629  
     private Collection<ActionItem> getActionItemsInActionList(Class objectsToRetrieve, String principalId, ActionListFilter filter) {
 630  0
         LOG.debug("getting action list for user " + principalId);
 631  0
         Criteria crit = null;
 632  0
         if (filter == null) {
 633  0
             crit = new Criteria();
 634  0
             crit.addEqualTo("principalId", principalId);
 635  
         } else {
 636  0
             crit = setUpActionListCriteria(principalId, filter);
 637  
         }
 638  0
         LOG.debug("running query to get action list for criteria " + crit);
 639  0
         Collection<ActionItem> collection = this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(objectsToRetrieve, crit));
 640  0
         LOG.debug("found " + collection.size() + " action items for user " + principalId);
 641  0
         return createActionListForUser(collection);
 642  
     }
 643  
 
 644  
     public Collection<ActionItem> getOutbox(String principalId, ActionListFilter filter) {
 645  0
         return getActionItemsInActionList(OutboxItemActionListExtension.class, principalId, filter);
 646  
 //        LOG.debug("getting action list for user " + workflowUser.getWorkflowUserId().getWorkflowId());
 647  
 //        Criteria crit = new Criteria();
 648  
 //        crit.addEqualTo("workflowId", workflowUser.getWorkflowUserId().getWorkflowId());
 649  
 //        if (filter != null) {
 650  
 //            setUpActionListCriteria(workflowUser, filter);
 651  
 //        }
 652  
 //        LOG.debug("running query to get action list for criteria " + crit);
 653  
 //        Collection<ActionItem> collection = this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(OutboxItemActionListExtension.class, crit));
 654  
 //        LOG.debug("found " + collection.size() + " action items for user " + workflowUser.getWorkflowUserId().getWorkflowId());
 655  
 //        return createActionListForUser(collection);
 656  
     }
 657  
 
 658  
     /**
 659  
      *
 660  
      * Deletes all outbox items specified by the list of ids
 661  
      *
 662  
      * @see org.kuali.rice.kew.actionlist.dao.ActionListDAO#removeOutboxItems(java.lang.String, java.util.List)
 663  
      */
 664  
     public void removeOutboxItems(String principalId, List<String> outboxItems) {
 665  0
         Criteria crit = new Criteria();
 666  0
         crit.addIn("id", outboxItems);
 667  0
         getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(OutboxItemActionListExtension.class, crit));
 668  0
     }
 669  
 
 670  
     /**
 671  
      * Saves an outbox item
 672  
      *
 673  
      * @see org.kuali.rice.kew.actionlist.dao.ActionListDAO#saveOutboxItem(org.kuali.rice.kew.actionitem.OutboxItemActionListExtension)
 674  
      */
 675  
     public void saveOutboxItem(OutboxItemActionListExtension outboxItem) {
 676  0
         this.getPersistenceBrokerTemplate().store(outboxItem);
 677  0
     }
 678  
 
 679  
     /**
 680  
      * Gets the outbox item associated with the document id
 681  
      *
 682  
      * @see org.kuali.rice.kew.actionlist.dao.ActionListDAO#getOutboxByDocumentId(java.lang.String)
 683  
      */
 684  
     public OutboxItemActionListExtension getOutboxByDocumentId(String documentId) {
 685  0
         Criteria crit = new Criteria();
 686  0
         crit.addEqualTo("documentId", documentId);
 687  0
         return (OutboxItemActionListExtension)getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(OutboxItemActionListExtension.class, crit));
 688  
     }
 689  
 
 690  
     /**
 691  
      * This overridden method ...
 692  
      *
 693  
      * @see org.kuali.rice.kew.actionlist.dao.ActionListDAO#getOutboxByDocumentIdUserId(java.lang.String)
 694  
      */
 695  
     public OutboxItemActionListExtension getOutboxByDocumentIdUserId(String documentId, String userId) {
 696  0
         Criteria crit = new Criteria();
 697  0
         crit.addEqualTo("documentId", documentId);
 698  0
         crit.addEqualTo("principalId", userId);
 699  0
         return (OutboxItemActionListExtension)getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(OutboxItemActionListExtension.class, crit));
 700  
     }
 701  
 
 702  
     private Date beginningOfDay(Date date) {
 703  0
         Calendar cal = Calendar.getInstance();
 704  0
         cal.setTime(date);
 705  0
         cal.set(Calendar.HOUR_OF_DAY, 0);
 706  0
         cal.set(Calendar.MINUTE, 0);
 707  0
         cal.set(Calendar.SECOND, 0);
 708  0
         return cal.getTime();
 709  
     }
 710  
 
 711  
     private Date endOfDay(Date date) {
 712  0
         Calendar cal = Calendar.getInstance();
 713  0
         cal.setTime(date);
 714  0
         cal.set(Calendar.HOUR_OF_DAY, 23);
 715  0
         cal.set(Calendar.MINUTE, 59);
 716  0
         cal.set(Calendar.SECOND, 59);
 717  0
         return cal.getTime();
 718  
     }
 719  
 
 720  
 }