View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
6    * compliance with the License. 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 distributed under the License is distributed on an "AS
11   * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
12   * language governing permissions and limitations under the License.
13   */
14  package org.kuali.rice.kew.actionitem.dao.impl;
15  
16  import java.sql.Timestamp;
17  import java.util.Collection;
18  import java.util.Date;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.collections.CollectionUtils;
25  import org.apache.log4j.Logger;
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.QueryByCriteria;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.apache.ojb.broker.query.ReportQueryByCriteria;
30  import org.kuali.rice.kew.actionitem.ActionItem;
31  import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension;
32  import org.kuali.rice.kew.actionitem.dao.ActionItemDAO;
33  import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
34  import org.kuali.rice.kew.actionrequest.Recipient;
35  import org.kuali.rice.kew.util.KEWConstants;
36  import org.kuali.rice.kew.util.WebFriendlyRecipient;
37  import org.kuali.rice.kim.bo.Person;
38  import org.kuali.rice.kim.service.IdentityManagementService;
39  import org.kuali.rice.kim.service.KIMServiceLocator;
40  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
41  /**
42   * OJB implementation of {@link ActionItemDAO}.
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  public class ActionItemDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionItemDAO {
47  	
48   
49  	private static final Logger LOG = Logger.getLogger(ActionItemDAOOjbImpl.class);
50  
51  	
52      public ActionItem findByActionItemId(Long actionItemId) {
53          Criteria crit = new Criteria();
54          crit.addEqualTo("actionItemId", actionItemId);
55          return (ActionItem) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionItem.class, crit));
56      }
57  
58      public void deleteActionItems(Long actionRequestId) {
59          Criteria crit = new Criteria();
60          crit.addEqualTo("actionRequestId", actionRequestId);
61          this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit));
62      }
63  
64      public void deleteActionItem(ActionItem actionItem) {
65          this.getPersistenceBrokerTemplate().delete(actionItem);
66      }
67  
68      public void deleteByRouteHeaderIdWorkflowUserId(Long routeHeaderId, String workflowUserId) {
69          Criteria crit = new Criteria();
70          crit.addEqualTo("routeHeaderId", routeHeaderId);
71          crit.addEqualTo("principalId", workflowUserId);
72          this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit));
73      }
74  
75      public void deleteByRouteHeaderId(Long routeHeaderId) {
76          Criteria crit = new Criteria();
77          crit.addEqualTo("routeHeaderId", routeHeaderId);
78          this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit));
79      }
80  
81      public Collection<ActionItem> findByWorkflowUserRouteHeaderId(String workflowId, Long routeHeaderId) {
82          Criteria crit = new Criteria();
83          crit.addEqualTo("principalId", workflowId);
84          crit.addEqualTo("routeHeaderId", routeHeaderId);
85          return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit));
86      }
87  
88      public Collection<ActionItem> findByRouteHeaderId(Long routeHeaderId) {
89          Criteria crit = new Criteria();
90          crit.addEqualTo("routeHeaderId", routeHeaderId);
91          return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit));
92      }
93  
94      public Collection<ActionItem> findByActionRequestId(Long actionRequestId) {
95          Criteria crit = new Criteria();
96          crit.addEqualTo("actionRequestId", actionRequestId);
97          return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit));
98      }
99  
100     public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) {
101         return getItemsByDocumentType(ActionItem.class, documentTypeName);
102     }
103 
104     public Collection<ActionItem> getOutboxItemsByDocumentType(String documentTypeName) {
105         return getItemsByDocumentType(OutboxItemActionListExtension.class, documentTypeName);
106     }
107 
108     private Collection<ActionItem> getItemsByDocumentType(Class<? extends ActionItem> objectClass, String documentTypeName) {
109         Criteria crit = new Criteria();
110         crit.addEqualTo("docName", documentTypeName);
111         return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(objectClass, crit));
112     }
113 
114     public void saveActionItem(ActionItem actionItem) {
115         if (actionItem.getDateAssigned() == null) {
116             actionItem.setDateAssigned(new Timestamp(new Date().getTime()));
117         }
118         this.getPersistenceBrokerTemplate().store(actionItem);
119     }
120 
121     public Collection<Recipient> findSecondaryDelegators(String principalId) {
122         Criteria notNullWorkflowCriteria = new Criteria();
123         notNullWorkflowCriteria.addNotNull("delegatorWorkflowId");
124         Criteria notNullWorkgroupCriteria = new Criteria();
125         notNullWorkgroupCriteria.addNotNull("delegatorGroupId");
126         Criteria orCriteria = new Criteria();
127         orCriteria.addOrCriteria(notNullWorkflowCriteria);
128         orCriteria.addOrCriteria(notNullWorkgroupCriteria);
129         Criteria criteria = new Criteria();
130         criteria.addEqualTo("principalId", principalId);
131         criteria.addEqualTo("delegationType", KEWConstants.DELEGATION_SECONDARY);
132         criteria.addAndCriteria(orCriteria);
133         ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionItem.class, criteria);
134 
135         query.setAttributes(new String[]{"delegatorWorkflowId", "delegatorGroupId"});
136         Map<Object, Recipient> delegators = new HashMap<Object, Recipient>();
137         Iterator iterator = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
138         while (iterator.hasNext()) {
139             Object[] ids = (Object[]) iterator.next();
140             if (ids[0] != null && !delegators.containsKey((String) ids[0])) {
141             	WebFriendlyRecipient rec = new WebFriendlyRecipient(KIMServiceLocator.getPersonService().getPerson((String) ids[0]));
142                 delegators.put((String) ids[0], rec);
143             } else if (ids[1] != null) {
144                 String workgroupId = ids[1].toString();
145                 if (!delegators.containsKey(workgroupId)) {
146                     delegators.put(workgroupId, new KimGroupRecipient(getIdentityManagementService().getGroup(workgroupId)));
147                 }
148             }
149         }
150         return delegators.values();
151     }
152 
153     public Collection<Recipient> findPrimaryDelegationRecipients(String principalId) {
154     	List<String> workgroupIds = KIMServiceLocator.getIdentityManagementService().getGroupIdsForPrincipal(principalId);
155         Criteria orCriteria = new Criteria();
156         Criteria delegatorWorkflowIdCriteria = new Criteria();
157         delegatorWorkflowIdCriteria.addEqualTo("delegatorWorkflowId", principalId);
158         if (CollectionUtils.isNotEmpty(workgroupIds)) {
159             Criteria delegatorWorkgroupCriteria = new Criteria();
160             delegatorWorkgroupCriteria.addIn("delegatorGroupId", workgroupIds);
161             orCriteria.addOrCriteria(delegatorWorkgroupCriteria);
162             orCriteria.addOrCriteria(delegatorWorkflowIdCriteria);
163         }
164         else {
165             orCriteria.addAndCriteria(delegatorWorkflowIdCriteria);
166         }
167         Criteria criteria = new Criteria();
168         criteria.addEqualTo("delegationType", KEWConstants.DELEGATION_PRIMARY);
169         criteria.addAndCriteria(orCriteria);
170         ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionItem.class, criteria, true);
171 
172         query.setAttributes(new String[]{"principalId"});
173         Map<Object, Recipient> delegators = new HashMap<Object, Recipient>();
174         Iterator iterator = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
175         while (iterator.hasNext()) {
176             Object[] ids = (Object[]) iterator.next();
177             if (ids[0] != null && !delegators.containsKey((String) ids[0])) {
178                
179             	Person person = KIMServiceLocator.getPersonService().getPerson((String) ids[0]);
180             	if (person != null) {
181             		WebFriendlyRecipient rec = new WebFriendlyRecipient(person);
182             	    delegators.put((String) ids[0],rec);
183             	    LOG.warn("The name for " + (String) ids[0] + " was not added to the primary delegate drop down list because the delegate does not exist.");
184             	}
185               	
186             }
187         }
188         return delegators.values();
189     }
190 
191     private IdentityManagementService getIdentityManagementService() {
192         return (IdentityManagementService) KIMServiceLocator.getService(KIMServiceLocator.KIM_IDENTITY_MANAGEMENT_SERVICE);
193     }
194 
195 	/**
196 	 * This overridden method replaced findByWorkfowUser
197 	 *
198 	 * @see org.kuali.rice.kew.actionitem.dao.ActionItemDAO#findByPrincipalId(java.lang.String)
199 	 */
200 	public Collection<ActionItem> findByPrincipalId(String principalId) {
201 		 Criteria crit = new Criteria();
202 	     crit.addEqualTo("principalId", principalId);
203 	     QueryByCriteria query = new QueryByCriteria(ActionItem.class, crit);
204 	     query.addOrderByAscending("routeHeader.routeHeaderId");
205 	     return this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
206 	}
207 
208 }