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.apache.log4j.Logger; |
18 | |
import org.apache.ojb.broker.query.Criteria; |
19 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
20 | |
import org.apache.ojb.broker.query.QueryFactory; |
21 | |
import org.apache.ojb.broker.query.ReportQueryByCriteria; |
22 | |
import org.kuali.rice.kew.actionitem.ActionItem; |
23 | |
import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension; |
24 | |
import org.kuali.rice.kew.actionitem.dao.ActionItemDAO; |
25 | |
import org.kuali.rice.kew.actionrequest.KimGroupRecipient; |
26 | |
import org.kuali.rice.kew.actionrequest.Recipient; |
27 | |
import org.kuali.rice.kew.api.action.DelegationType; |
28 | |
import org.kuali.rice.kew.util.WebFriendlyRecipient; |
29 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
30 | |
import org.kuali.rice.kim.bo.Person; |
31 | |
import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport; |
32 | |
|
33 | |
import java.sql.Timestamp; |
34 | |
import java.util.Collection; |
35 | |
import java.util.Date; |
36 | |
import java.util.HashMap; |
37 | |
import java.util.Iterator; |
38 | |
import java.util.List; |
39 | |
import java.util.Map; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class ActionItemDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionItemDAO { |
46 | |
|
47 | |
|
48 | 0 | private static final Logger LOG = Logger.getLogger(ActionItemDAOOjbImpl.class); |
49 | |
|
50 | |
|
51 | |
public ActionItem findByActionItemId(String actionItemId) { |
52 | 0 | Criteria crit = new Criteria(); |
53 | 0 | crit.addEqualTo("actionItemId", actionItemId); |
54 | 0 | return (ActionItem) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionItem.class, crit)); |
55 | |
} |
56 | |
|
57 | |
public void deleteActionItems(Long actionRequestId) { |
58 | 0 | Criteria crit = new Criteria(); |
59 | 0 | crit.addEqualTo("actionRequestId", actionRequestId); |
60 | 0 | this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit)); |
61 | 0 | } |
62 | |
|
63 | |
public void deleteActionItem(ActionItem actionItem) { |
64 | 0 | this.getPersistenceBrokerTemplate().delete(actionItem); |
65 | 0 | } |
66 | |
|
67 | |
public void deleteByDocumentIdWorkflowUserId(String documentId, String workflowUserId) { |
68 | 0 | Criteria crit = new Criteria(); |
69 | 0 | crit.addEqualTo("documentId", documentId); |
70 | 0 | crit.addEqualTo("principalId", workflowUserId); |
71 | 0 | this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit)); |
72 | 0 | } |
73 | |
|
74 | |
public void deleteByDocumentId(String documentId) { |
75 | 0 | Criteria crit = new Criteria(); |
76 | 0 | crit.addEqualTo("documentId", documentId); |
77 | 0 | this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit)); |
78 | 0 | } |
79 | |
|
80 | |
public Collection<ActionItem> findByWorkflowUserDocumentId(String workflowId, String documentId) { |
81 | 0 | Criteria crit = new Criteria(); |
82 | 0 | crit.addEqualTo("principalId", workflowId); |
83 | 0 | crit.addEqualTo("documentId", documentId); |
84 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit)); |
85 | |
} |
86 | |
|
87 | |
public Collection<ActionItem> findByDocumentId(String documentId) { |
88 | 0 | Criteria crit = new Criteria(); |
89 | 0 | crit.addEqualTo("documentId", documentId); |
90 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit)); |
91 | |
} |
92 | |
|
93 | |
public Collection<ActionItem> findByActionRequestId(String actionRequestId) { |
94 | 0 | Criteria crit = new Criteria(); |
95 | 0 | crit.addEqualTo("actionRequestId", actionRequestId); |
96 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit)); |
97 | |
} |
98 | |
|
99 | |
public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) { |
100 | 0 | return getItemsByDocumentType(ActionItem.class, documentTypeName); |
101 | |
} |
102 | |
|
103 | |
public Collection<ActionItem> getOutboxItemsByDocumentType(String documentTypeName) { |
104 | 0 | return getItemsByDocumentType(OutboxItemActionListExtension.class, documentTypeName); |
105 | |
} |
106 | |
|
107 | |
private Collection<ActionItem> getItemsByDocumentType(Class<? extends ActionItem> objectClass, String documentTypeName) { |
108 | 0 | Criteria crit = new Criteria(); |
109 | 0 | crit.addEqualTo("docName", documentTypeName); |
110 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(objectClass, crit)); |
111 | |
} |
112 | |
|
113 | |
public void saveActionItem(ActionItem actionItem) { |
114 | 0 | if (actionItem.getDateAssigned() == null) { |
115 | 0 | actionItem.setDateAssigned(new Timestamp(new Date().getTime())); |
116 | |
} |
117 | 0 | this.getPersistenceBrokerTemplate().store(actionItem); |
118 | 0 | } |
119 | |
|
120 | |
public Collection<Recipient> findSecondaryDelegators(String principalId) { |
121 | 0 | Criteria notNullWorkflowCriteria = new Criteria(); |
122 | 0 | notNullWorkflowCriteria.addNotNull("delegatorWorkflowId"); |
123 | 0 | Criteria notNullWorkgroupCriteria = new Criteria(); |
124 | 0 | notNullWorkgroupCriteria.addNotNull("delegatorGroupId"); |
125 | 0 | Criteria orCriteria = new Criteria(); |
126 | 0 | orCriteria.addOrCriteria(notNullWorkflowCriteria); |
127 | 0 | orCriteria.addOrCriteria(notNullWorkgroupCriteria); |
128 | 0 | Criteria criteria = new Criteria(); |
129 | 0 | criteria.addEqualTo("principalId", principalId); |
130 | 0 | criteria.addEqualTo("delegationType", DelegationType.SECONDARY.getCode()); |
131 | 0 | criteria.addAndCriteria(orCriteria); |
132 | 0 | ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionItem.class, criteria); |
133 | |
|
134 | 0 | query.setAttributes(new String[]{"delegatorWorkflowId", "delegatorGroupId"}); |
135 | 0 | Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); |
136 | 0 | Iterator iterator = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query); |
137 | 0 | while (iterator.hasNext()) { |
138 | 0 | Object[] ids = (Object[]) iterator.next(); |
139 | 0 | if (ids[0] != null && !delegators.containsKey((String) ids[0])) { |
140 | 0 | WebFriendlyRecipient rec = new WebFriendlyRecipient(KimApiServiceLocator.getPersonService().getPerson((String) ids[0])); |
141 | 0 | delegators.put((String) ids[0], rec); |
142 | 0 | } else if (ids[1] != null) { |
143 | 0 | String workgroupId = ids[1].toString(); |
144 | 0 | if (!delegators.containsKey(workgroupId)) { |
145 | 0 | delegators.put(workgroupId, new KimGroupRecipient(KimApiServiceLocator.getGroupService().getGroup(workgroupId))); |
146 | |
} |
147 | |
} |
148 | 0 | } |
149 | 0 | return delegators.values(); |
150 | |
} |
151 | |
|
152 | |
public Collection<Recipient> findPrimaryDelegationRecipients(String principalId) { |
153 | 0 | List<String> workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsForPrincipal(principalId); |
154 | 0 | Criteria orCriteria = new Criteria(); |
155 | 0 | Criteria delegatorWorkflowIdCriteria = new Criteria(); |
156 | 0 | delegatorWorkflowIdCriteria.addEqualTo("delegatorWorkflowId", principalId); |
157 | 0 | if (CollectionUtils.isNotEmpty(workgroupIds)) { |
158 | 0 | Criteria delegatorWorkgroupCriteria = new Criteria(); |
159 | 0 | delegatorWorkgroupCriteria.addIn("delegatorGroupId", workgroupIds); |
160 | 0 | orCriteria.addOrCriteria(delegatorWorkgroupCriteria); |
161 | 0 | orCriteria.addOrCriteria(delegatorWorkflowIdCriteria); |
162 | 0 | } |
163 | |
else { |
164 | 0 | orCriteria.addAndCriteria(delegatorWorkflowIdCriteria); |
165 | |
} |
166 | 0 | Criteria criteria = new Criteria(); |
167 | 0 | criteria.addEqualTo("delegationType", DelegationType.PRIMARY.getCode()); |
168 | 0 | criteria.addAndCriteria(orCriteria); |
169 | 0 | ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionItem.class, criteria, true); |
170 | |
|
171 | 0 | query.setAttributes(new String[]{"principalId"}); |
172 | 0 | Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); |
173 | 0 | Iterator iterator = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query); |
174 | 0 | while (iterator.hasNext()) { |
175 | 0 | Object[] ids = (Object[]) iterator.next(); |
176 | 0 | if (ids[0] != null && !delegators.containsKey((String) ids[0])) { |
177 | |
|
178 | 0 | Person person = KimApiServiceLocator.getPersonService().getPerson((String) ids[0]); |
179 | 0 | if (person != null) { |
180 | 0 | WebFriendlyRecipient rec = new WebFriendlyRecipient(person); |
181 | 0 | delegators.put((String) ids[0],rec); |
182 | 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."); |
183 | |
} |
184 | |
|
185 | |
} |
186 | 0 | } |
187 | 0 | return delegators.values(); |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public Collection<ActionItem> findByPrincipalId(String principalId) { |
196 | 0 | Criteria crit = new Criteria(); |
197 | 0 | crit.addEqualTo("principalId", principalId); |
198 | 0 | QueryByCriteria query = new QueryByCriteria(ActionItem.class, crit); |
199 | 0 | query.addOrderByAscending("documentId"); |
200 | 0 | return this.getPersistenceBrokerTemplate().getCollectionByQuery(query); |
201 | |
} |
202 | |
|
203 | |
} |