1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package org.kuali.rice.kew.actionitem.dao.impl; |
15 | |
|
16 | |
import java.util.ArrayList; |
17 | |
import java.util.Collection; |
18 | |
import java.util.HashMap; |
19 | |
import java.util.Iterator; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import javax.persistence.EntityManager; |
24 | |
import javax.persistence.PersistenceContext; |
25 | |
|
26 | |
import org.apache.commons.collections.CollectionUtils; |
27 | |
import org.kuali.rice.core.jpa.criteria.Criteria; |
28 | |
import org.kuali.rice.core.jpa.criteria.QueryByCriteria; |
29 | |
import org.kuali.rice.core.util.OrmUtils; |
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.service.IdentityManagementService; |
38 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class ActionItemDAOJpaImpl implements ActionItemDAO { |
45 | |
@PersistenceContext(unitName="kew-unit") |
46 | |
private EntityManager entityManager; |
47 | |
|
48 | |
public ActionItem findByActionItemId(Long actionItemId) { |
49 | 0 | return entityManager.find(ActionItem.class, actionItemId); |
50 | |
} |
51 | |
|
52 | |
public void deleteActionItems(Long actionRequestId) { |
53 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
54 | 0 | crit.eq("actionRequestId", actionRequestId); |
55 | 0 | for(Object actionItem:new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ |
56 | 0 | if( ! (actionItem instanceof OutboxItemActionListExtension)) { |
57 | 0 | entityManager.remove(actionItem); |
58 | |
} |
59 | |
} |
60 | |
|
61 | 0 | } |
62 | |
|
63 | |
public void deleteActionItem(ActionItem actionItem) { |
64 | 0 | entityManager.remove(findByActionItemId(actionItem.getActionItemId())); |
65 | 0 | } |
66 | |
|
67 | |
public void deleteByRouteHeaderIdWorkflowUserId(Long routeHeaderId, String workflowUserId) { |
68 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
69 | 0 | crit.eq("routeHeader.routeHeaderId", routeHeaderId); |
70 | 0 | crit.eq("principalId", workflowUserId); |
71 | 0 | for(Object actionItem: new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ |
72 | 0 | if( ! (actionItem instanceof OutboxItemActionListExtension)) { |
73 | 0 | entityManager.remove(actionItem); |
74 | |
} |
75 | |
} |
76 | 0 | } |
77 | |
|
78 | |
public void deleteByRouteHeaderId(Long routeHeaderId) { |
79 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
80 | 0 | crit.eq("routeHeader.routeHeaderId", routeHeaderId); |
81 | 0 | for(Object actionItem: new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ |
82 | 0 | if( ! (actionItem instanceof OutboxItemActionListExtension)) { |
83 | 0 | entityManager.remove(actionItem); |
84 | |
} |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public Collection<ActionItem> findByPrincipalId(String principalId) { |
94 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
95 | 0 | crit.eq("principalId", principalId); |
96 | 0 | crit.orderBy("routeHeader.routeHeaderId", true); |
97 | 0 | List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
98 | |
|
99 | 0 | return removeOutBoxItems(results); |
100 | |
} |
101 | |
|
102 | |
public Collection<ActionItem> findByWorkflowUserRouteHeaderId(String workflowId, Long routeHeaderId) { |
103 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
104 | 0 | crit.eq("principalId", workflowId); |
105 | 0 | crit.eq("routeHeader.routeHeaderId", routeHeaderId); |
106 | 0 | List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
107 | |
|
108 | 0 | return removeOutBoxItems(results); |
109 | |
} |
110 | |
|
111 | |
public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) { |
112 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
113 | 0 | crit.eq("docName", documentTypeName); |
114 | 0 | List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
115 | |
|
116 | 0 | return removeOutBoxItems(results); |
117 | |
} |
118 | |
|
119 | |
public Collection<ActionItem> getOutboxItemsByDocumentType(String documentTypeName) { |
120 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
121 | 0 | crit.eq("docName", documentTypeName); |
122 | 0 | List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
123 | 0 | Iterator<ActionItem> iterator = results.iterator(); |
124 | 0 | while (iterator.hasNext()) { |
125 | 0 | if (!(iterator.next() instanceof OutboxItemActionListExtension)) { |
126 | 0 | iterator.remove(); |
127 | |
} |
128 | |
} |
129 | 0 | return results; |
130 | |
} |
131 | |
|
132 | |
public Collection<ActionItem> findByRouteHeaderId(Long routeHeaderId) { |
133 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
134 | 0 | crit.eq("routeHeader.routeHeaderId", routeHeaderId); |
135 | 0 | List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
136 | |
|
137 | 0 | return removeOutBoxItems(results); |
138 | |
} |
139 | |
|
140 | |
public Collection<ActionItem> findByActionRequestId(Long actionRequestId) { |
141 | 0 | Criteria crit = new Criteria(ActionItem.class.getName()); |
142 | 0 | crit.eq("actionRequestId", actionRequestId); |
143 | 0 | List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
144 | |
|
145 | 0 | return removeOutBoxItems(results); |
146 | |
} |
147 | |
|
148 | |
public void saveActionItem(ActionItem actionItem) { |
149 | 0 | if(actionItem.getActionItemId()==null){ |
150 | 0 | entityManager.persist(actionItem); |
151 | |
}else{ |
152 | 0 | OrmUtils.merge(entityManager, actionItem); |
153 | |
} |
154 | 0 | } |
155 | |
|
156 | |
public Collection<Recipient> findSecondaryDelegators(String principalId) { |
157 | 0 | Criteria notNullWorkflowCriteria = new Criteria(ActionItem.class.getName()); |
158 | 0 | notNullWorkflowCriteria.notNull("delegatorWorkflowId"); |
159 | 0 | Criteria notNullWorkgroupCriteria = new Criteria(ActionItem.class.getName()); |
160 | 0 | notNullWorkgroupCriteria.notNull("delegatorGroupId"); |
161 | 0 | Criteria orCriteria = new Criteria(ActionItem.class.getName()); |
162 | 0 | orCriteria.or(notNullWorkflowCriteria); |
163 | 0 | orCriteria.or(notNullWorkgroupCriteria); |
164 | 0 | Criteria criteria = new Criteria(ActionItem.class.getName()); |
165 | 0 | criteria.eq("principalId", principalId); |
166 | 0 | criteria.eq("delegationType", KEWConstants.DELEGATION_SECONDARY); |
167 | 0 | criteria.and(orCriteria); |
168 | |
|
169 | 0 | Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); |
170 | |
|
171 | 0 | for(Object actionItem:new QueryByCriteria(entityManager, criteria).toQuery().getResultList()){ |
172 | 0 | final String delegatorWorkflowId = ((ActionItem)actionItem).getDelegatorWorkflowId(); |
173 | 0 | String delegatorGroupId = ((ActionItem)actionItem).getDelegatorGroupId(); |
174 | |
|
175 | 0 | if (delegatorWorkflowId != null && !delegators.containsKey(delegatorWorkflowId)) { |
176 | 0 | delegators.put(delegatorWorkflowId,new WebFriendlyRecipient(KIMServiceLocator.getPersonService().getPerson(delegatorWorkflowId))); |
177 | 0 | }else if (delegatorGroupId != null) { |
178 | 0 | if (!delegators.containsKey(delegatorGroupId)) { |
179 | 0 | delegators.put(delegatorGroupId, new KimGroupRecipient(getIdentityManagementService().getGroup(delegatorGroupId))); |
180 | |
} |
181 | |
} |
182 | 0 | } |
183 | 0 | return delegators.values(); |
184 | |
} |
185 | |
|
186 | |
public Collection<Recipient> findPrimaryDelegationRecipients(String principalId) { |
187 | 0 | List<String> workgroupIds = KIMServiceLocator.getIdentityManagementService().getGroupIdsForPrincipal(principalId); |
188 | 0 | Criteria orCriteria = new Criteria(ActionItem.class.getName()); |
189 | 0 | Criteria delegatorWorkflowIdCriteria = new Criteria(ActionItem.class.getName()); |
190 | 0 | delegatorWorkflowIdCriteria.eq("delegatorWorkflowId", principalId); |
191 | 0 | if (CollectionUtils.isNotEmpty(workgroupIds)) { |
192 | 0 | Criteria delegatorWorkgroupCriteria = new Criteria(ActionItem.class.getName()); |
193 | 0 | delegatorWorkgroupCriteria.in("delegatorGroupId", new ArrayList<String>(workgroupIds)); |
194 | 0 | orCriteria.or(delegatorWorkgroupCriteria); |
195 | 0 | orCriteria.or(delegatorWorkflowIdCriteria); |
196 | 0 | } |
197 | |
else { |
198 | 0 | orCriteria.and(delegatorWorkflowIdCriteria); |
199 | |
} |
200 | 0 | Criteria criteria = new Criteria(ActionItem.class.getName()); |
201 | 0 | criteria.eq("delegationType", KEWConstants.DELEGATION_PRIMARY); |
202 | 0 | criteria.and(orCriteria); |
203 | |
|
204 | 0 | Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); |
205 | 0 | for(Object actionItem:new QueryByCriteria(entityManager, criteria).toQuery().getResultList()){ |
206 | 0 | String princlId = ((ActionItem)actionItem).getPrincipalId(); |
207 | 0 | if (princlId != null && !delegators.containsKey(princlId)) { |
208 | 0 | delegators.put(princlId, new WebFriendlyRecipient(KIMServiceLocator.getPersonService().getPerson(princlId))); |
209 | |
} |
210 | 0 | } |
211 | |
|
212 | 0 | return delegators.values(); |
213 | |
} |
214 | |
|
215 | |
private List<ActionItem> removeOutBoxItems(List<ActionItem> results) { |
216 | 0 | Iterator<ActionItem> iterator = results.iterator(); |
217 | 0 | while(iterator.hasNext()) { |
218 | 0 | if(iterator.next() instanceof OutboxItemActionListExtension) { |
219 | 0 | iterator.remove(); |
220 | |
} |
221 | |
} |
222 | |
|
223 | 0 | return results; |
224 | |
} |
225 | |
|
226 | |
private IdentityManagementService getIdentityManagementService() { |
227 | 0 | return (IdentityManagementService) KIMServiceLocator.getService(KIMServiceLocator.KIM_IDENTITY_MANAGEMENT_SERVICE); |
228 | |
} |
229 | |
|
230 | |
public EntityManager getEntityManager() { |
231 | 0 | return this.entityManager; |
232 | |
} |
233 | |
|
234 | |
public void setEntityManager(EntityManager entityManager) { |
235 | 0 | this.entityManager = entityManager; |
236 | 0 | } |
237 | |
|
238 | |
|
239 | |
} |