View Javadoc

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