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