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.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 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class ActionItemDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionItemDAO { |
48 | |
|
49 | |
|
50 | 0 | private static final Logger LOG = Logger.getLogger(ActionItemDAOOjbImpl.class); |
51 | |
|
52 | |
|
53 | |
public ActionItem findByActionItemId(String actionItemId) { |
54 | 0 | Criteria crit = new Criteria(); |
55 | 0 | crit.addEqualTo("id", actionItemId); |
56 | 0 | return (ActionItem) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionItem.class, crit)); |
57 | |
} |
58 | |
|
59 | |
public void deleteActionItems(Long actionRequestId) { |
60 | 0 | Criteria crit = new Criteria(); |
61 | 0 | crit.addEqualTo("actionRequestId", actionRequestId); |
62 | 0 | this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit)); |
63 | 0 | } |
64 | |
|
65 | |
public void deleteActionItem(ActionItem actionItem) { |
66 | 0 | this.getPersistenceBrokerTemplate().delete(actionItem); |
67 | 0 | } |
68 | |
|
69 | |
public void deleteByDocumentIdWorkflowUserId(String documentId, String workflowUserId) { |
70 | 0 | Criteria crit = new Criteria(); |
71 | 0 | crit.addEqualTo("documentId", documentId); |
72 | 0 | crit.addEqualTo("principalId", workflowUserId); |
73 | 0 | this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit)); |
74 | 0 | } |
75 | |
|
76 | |
public void deleteByDocumentId(String documentId) { |
77 | 0 | Criteria crit = new Criteria(); |
78 | 0 | crit.addEqualTo("documentId", documentId); |
79 | 0 | this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit)); |
80 | 0 | } |
81 | |
|
82 | |
public Collection<ActionItem> findByWorkflowUserDocumentId(String workflowId, String documentId) { |
83 | 0 | Criteria crit = new Criteria(); |
84 | 0 | crit.addEqualTo("principalId", workflowId); |
85 | 0 | crit.addEqualTo("documentId", documentId); |
86 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit)); |
87 | |
} |
88 | |
|
89 | |
public Collection<ActionItem> findByDocumentId(String documentId) { |
90 | 0 | Criteria crit = new Criteria(); |
91 | 0 | crit.addEqualTo("documentId", documentId); |
92 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit)); |
93 | |
} |
94 | |
|
95 | |
public Collection<ActionItem> findByActionRequestId(String actionRequestId) { |
96 | 0 | Criteria crit = new Criteria(); |
97 | 0 | crit.addEqualTo("actionRequestId", actionRequestId); |
98 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit)); |
99 | |
} |
100 | |
|
101 | |
public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) { |
102 | 0 | return getItemsByDocumentType(ActionItem.class, documentTypeName); |
103 | |
} |
104 | |
|
105 | |
public Collection<ActionItem> getOutboxItemsByDocumentType(String documentTypeName) { |
106 | 0 | return getItemsByDocumentType(OutboxItemActionListExtension.class, documentTypeName); |
107 | |
} |
108 | |
|
109 | |
private Collection<ActionItem> getItemsByDocumentType(Class<? extends ActionItem> objectClass, String documentTypeName) { |
110 | 0 | Criteria crit = new Criteria(); |
111 | 0 | crit.addEqualTo("docName", documentTypeName); |
112 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(objectClass, crit)); |
113 | |
} |
114 | |
|
115 | |
public void saveActionItem(ActionItem actionItem) { |
116 | 0 | if (actionItem.getDateAssigned() == null) { |
117 | 0 | actionItem.setDateAssigned(new Timestamp(new Date().getTime())); |
118 | |
} |
119 | 0 | this.getPersistenceBrokerTemplate().store(actionItem); |
120 | 0 | } |
121 | |
|
122 | |
public Collection<Recipient> findSecondaryDelegators(String principalId) { |
123 | 0 | Criteria notNullWorkflowCriteria = new Criteria(); |
124 | 0 | notNullWorkflowCriteria.addNotNull("delegatorPrincipalId"); |
125 | 0 | Criteria notNullWorkgroupCriteria = new Criteria(); |
126 | 0 | notNullWorkgroupCriteria.addNotNull("delegatorGroupId"); |
127 | 0 | Criteria orCriteria = new Criteria(); |
128 | 0 | orCriteria.addOrCriteria(notNullWorkflowCriteria); |
129 | 0 | orCriteria.addOrCriteria(notNullWorkgroupCriteria); |
130 | 0 | Criteria criteria = new Criteria(); |
131 | 0 | criteria.addEqualTo("principalId", principalId); |
132 | 0 | criteria.addEqualTo("delegationType", DelegationType.SECONDARY.getCode()); |
133 | 0 | criteria.addAndCriteria(orCriteria); |
134 | 0 | ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionItem.class, criteria); |
135 | |
|
136 | 0 | query.setAttributes(new String[]{"delegatorPrincipalId", "delegatorGroupId"}); |
137 | 0 | Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); |
138 | 0 | Iterator iterator = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query); |
139 | 0 | while (iterator.hasNext()) { |
140 | 0 | Object[] ids = (Object[]) iterator.next(); |
141 | 0 | if (ids[0] != null && !delegators.containsKey((String) ids[0])) { |
142 | 0 | WebFriendlyRecipient rec = new WebFriendlyRecipient(KimApiServiceLocator.getPersonService().getPerson((String) ids[0])); |
143 | 0 | delegators.put((String) ids[0], rec); |
144 | 0 | } else if (ids[1] != null) { |
145 | 0 | String workgroupId = ids[1].toString(); |
146 | 0 | if (!delegators.containsKey(workgroupId)) { |
147 | 0 | delegators.put(workgroupId, new KimGroupRecipient(KimApiServiceLocator.getGroupService().getGroup(workgroupId))); |
148 | |
} |
149 | |
} |
150 | 0 | } |
151 | 0 | return delegators.values(); |
152 | |
} |
153 | |
|
154 | |
public Collection<Recipient> findPrimaryDelegationRecipients(String principalId) { |
155 | 0 | List<String> workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId); |
156 | 0 | Criteria orCriteria = new Criteria(); |
157 | 0 | Criteria delegatorPrincipalIdCriteria = new Criteria(); |
158 | 0 | delegatorPrincipalIdCriteria.addEqualTo("delegatorPrincipalId", principalId); |
159 | 0 | if (CollectionUtils.isNotEmpty(workgroupIds)) { |
160 | 0 | Criteria delegatorWorkgroupCriteria = new Criteria(); |
161 | 0 | delegatorWorkgroupCriteria.addIn("delegatorGroupId", workgroupIds); |
162 | 0 | orCriteria.addOrCriteria(delegatorWorkgroupCriteria); |
163 | 0 | orCriteria.addOrCriteria(delegatorPrincipalIdCriteria); |
164 | 0 | } |
165 | |
else { |
166 | 0 | orCriteria.addAndCriteria(delegatorPrincipalIdCriteria); |
167 | |
} |
168 | 0 | Criteria criteria = new Criteria(); |
169 | 0 | criteria.addEqualTo("delegationType", DelegationType.PRIMARY.getCode()); |
170 | 0 | criteria.addAndCriteria(orCriteria); |
171 | 0 | ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionItem.class, criteria, true); |
172 | |
|
173 | 0 | query.setAttributes(new String[]{"principalId"}); |
174 | 0 | Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); |
175 | 0 | Iterator iterator = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query); |
176 | 0 | while (iterator.hasNext()) { |
177 | 0 | Object[] ids = (Object[]) iterator.next(); |
178 | 0 | if (ids[0] != null && !delegators.containsKey((String) ids[0])) { |
179 | |
|
180 | 0 | Person person = KimApiServiceLocator.getPersonService().getPerson((String) ids[0]); |
181 | 0 | if (person != null) { |
182 | 0 | WebFriendlyRecipient rec = new WebFriendlyRecipient(person); |
183 | 0 | delegators.put((String) ids[0],rec); |
184 | 0 | LOG.warn("The name for " + (String) ids[0] + " was not added to the primary delegate drop down list because the delegate does not exist."); |
185 | |
} |
186 | |
|
187 | |
} |
188 | 0 | } |
189 | 0 | return delegators.values(); |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public Collection<ActionItem> findByPrincipalId(String principalId) { |
198 | 0 | Criteria crit = new Criteria(); |
199 | 0 | crit.addEqualTo("principalId", principalId); |
200 | 0 | QueryByCriteria query = new QueryByCriteria(ActionItem.class, crit); |
201 | 0 | query.addOrderByAscending("documentId"); |
202 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(query); |
203 | |
} |
204 | |
|
205 | |
} |