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