1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.rule.dao.impl;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
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.exception.RiceRuntimeException;
25 import org.kuali.rice.kew.rule.RuleBaseValues;
26 import org.kuali.rice.kew.rule.RuleDelegationBo;
27 import org.kuali.rice.kew.rule.RuleExtensionBo;
28 import org.kuali.rice.kew.rule.RuleResponsibilityBo;
29 import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
30 import org.kuali.rice.kew.api.KewApiConstants;
31 import org.kuali.rice.kim.api.identity.principal.Principal;
32 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
34
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41
42
43 public class RuleDelegationDAOOjbImpl extends PersistenceBrokerDaoSupport implements RuleDelegationDAO {
44
45 public List<RuleDelegationBo> findByDelegateRuleId(String ruleId) {
46 Criteria crit = new Criteria();
47 crit.addEqualTo("delegateRuleId", ruleId);
48 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegationBo.class, crit));
49 }
50
51 public void save(RuleDelegationBo ruleDelegation) {
52 this.getPersistenceBrokerTemplate().store(ruleDelegation);
53 }
54 public List<RuleDelegationBo> findAllCurrentRuleDelegations(){
55 Criteria crit = new Criteria();
56 crit.addEqualTo("delegationRule.currentInd", true);
57 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegationBo.class, crit));
58 }
59
60 public RuleDelegationBo findByRuleDelegationId(String ruleDelegationId){
61 Criteria crit = new Criteria();
62 crit.addEqualTo("ruleDelegationId", ruleDelegationId);
63 return (RuleDelegationBo) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleDelegationBo.class, crit));
64
65 }
66 public void delete(String ruleDelegationId){
67 this.getPersistenceBrokerTemplate().delete(findByRuleDelegationId(ruleDelegationId));
68 }
69
70 public List<RuleDelegationBo> findByResponsibilityIdWithCurrentRule(String responsibilityId) {
71 Criteria crit = new Criteria();
72 crit.addEqualTo("responsibilityId", responsibilityId);
73 crit.addEqualTo("delegationRule.currentInd", true);
74 Collection delegations = getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegationBo.class, crit));
75 return new ArrayList<RuleDelegationBo>(delegations);
76 }
77
78
79
80
81
82
83 public List<RuleDelegationBo> search(String parentRuleBaseVaueId, String parentResponsibilityId, String docTypeName, String ruleId,
84 String ruleTemplateId, String ruleDescription, String workgroupId,
85 String principalId, String delegationType, Boolean activeInd,
86 Map extensionValues, String workflowIdDirective) {
87 Criteria crit = new Criteria();
88
89 if (StringUtils.isNotBlank(delegationType) && !delegationType.equals(KewApiConstants.DELEGATION_BOTH)) {
90 crit.addEqualTo("delegationType", delegationType);
91 }
92
93 if (StringUtils.isNotBlank(parentRuleBaseVaueId) && StringUtils.isNumeric(parentRuleBaseVaueId)) {
94 crit.addIn("responsibilityId", this.getRuleResponsibilitySubQuery(new Long(parentRuleBaseVaueId)));
95 }
96
97 if (StringUtils.isNotBlank(parentResponsibilityId) && StringUtils.isNumeric(parentResponsibilityId)) {
98 crit.addEqualTo("responsibilityId", parentResponsibilityId);
99 }
100
101 crit.addIn("delegateRuleId", getRuleBaseValuesSubQuery(docTypeName, ruleId,
102 ruleTemplateId, ruleDescription, workgroupId,
103 principalId, activeInd,
104 extensionValues, workflowIdDirective));
105
106 return (List<RuleDelegationBo>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegationBo.class, crit, true));
107 }
108
109
110
111
112
113
114 public List<RuleDelegationBo> search(String parentRuleBaseVaueId, String parentResponsibilityId, String docTypeName, String ruleTemplateId,
115 String ruleDescription, Collection<String> workgroupIds,
116 String workflowId, String delegationType, Boolean activeInd,
117 Map extensionValues, Collection actionRequestCodes) {
118 Criteria crit = new Criteria();
119
120 if (StringUtils.isNotBlank(delegationType) && !delegationType.equals(KewApiConstants.DELEGATION_BOTH)) {
121 crit.addEqualTo("delegationType", delegationType);
122 }
123
124 if (StringUtils.isNotBlank(parentRuleBaseVaueId) && StringUtils.isNumeric(parentRuleBaseVaueId)) {
125 crit.addIn("responsibilityId", this.getRuleResponsibilitySubQuery(new Long(parentRuleBaseVaueId)));
126 }
127
128 if (StringUtils.isNotBlank(parentResponsibilityId) && StringUtils.isNumeric(parentResponsibilityId)) {
129 crit.addEqualTo("responsibilityId", parentResponsibilityId);
130 }
131
132 crit.addIn("delegateRuleId", getRuleBaseValuesSubQuery(docTypeName, ruleTemplateId,
133 ruleDescription, workgroupIds,
134 workflowId, activeInd,
135 extensionValues, actionRequestCodes));
136 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegationBo.class, crit, true));
137 }
138
139 private ReportQueryByCriteria getResponsibilitySubQuery(String ruleResponsibilityName) {
140 Criteria responsibilityCrit = new Criteria();
141 responsibilityCrit.addLike("ruleResponsibilityName", ruleResponsibilityName);
142 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
143 query.setAttributes(new String[] { "ruleBaseValuesId" });
144 return query;
145 }
146
147 private ReportQueryByCriteria getWorkgroupResponsibilitySubQuery(Set<Long> workgroupIds) {
148 Set<String> workgroupIdStrings = new HashSet<String>();
149 for (Long workgroupId : workgroupIds) {
150 workgroupIdStrings.add(workgroupId.toString());
151 }
152 Criteria responsibilityCrit = new Criteria();
153 responsibilityCrit.addIn("ruleResponsibilityName", workgroupIds);
154 responsibilityCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
155 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
156 query.setAttributes(new String[] { "ruleBaseValuesId" });
157 return query;
158 }
159
160 private ReportQueryByCriteria getRuleBaseValuesSubQuery(String docTypeName, String ruleTemplateId,
161 String ruleDescription, Collection<String> workgroupIds,
162 String workflowId, Boolean activeInd,
163 Map<String, String> extensionValues, Collection actionRequestCodes) {
164 Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, activeInd, extensionValues);
165 crit.addIn("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupIds, workflowId, actionRequestCodes, (workflowId != null), ((workgroupIds != null) && !workgroupIds.isEmpty())));
166 crit.addEqualTo("delegateRule", 1);
167 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleBaseValues.class, crit);
168 query.setAttributes(new String[] { "id" });
169 return query;
170 }
171
172 private ReportQueryByCriteria getRuleBaseValuesSubQuery(String docTypeName, String ruleId,
173 String ruleTemplateId, String ruleDescription, String workgroupId,
174 String principalId, Boolean activeInd,
175 Map<String, String> extensionValues, String workflowIdDirective) {
176 Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, activeInd, extensionValues);
177 if (ruleId != null) {
178 crit.addEqualTo("id", ruleId);
179 }
180 if (workgroupId != null) {
181 crit.addIn("ruleResponsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupId));
182 }
183 List<String> workgroupIds = new ArrayList<String>();
184 Boolean searchUser = Boolean.FALSE;
185 Boolean searchUserInWorkgroups = Boolean.FALSE;
186 if (workflowIdDirective != null) {
187 if ("group".equals(workflowIdDirective)) {
188 searchUserInWorkgroups = Boolean.TRUE;
189 } else if ("".equals(workflowIdDirective)) {
190 searchUser = Boolean.TRUE;
191 searchUserInWorkgroups = Boolean.TRUE;
192 } else {
193 searchUser = Boolean.TRUE;
194 }
195 }
196 if (!org.apache.commons.lang.StringUtils.isEmpty(principalId) && searchUserInWorkgroups)
197 {
198 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
199
200 if (principal == null)
201 {
202 throw new RiceRuntimeException("Failed to locate user for the given workflow id: " + principalId);
203 }
204 workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
205 }
206 if (CollectionUtils.isNotEmpty(workgroupIds) || StringUtils.isNotBlank(principalId)) {
207 crit.addIn("ruleResponsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupIds, principalId, searchUser, searchUserInWorkgroups));
208 }
209 crit.addEqualTo("delegateRule", 1);
210 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleBaseValues.class, crit);
211 query.setAttributes(new String[] { "id" });
212 return query;
213
214 }
215
216 private ReportQueryByCriteria getRuleResponsibilitySubQuery(Long ruleBaseValuesId) {
217 Criteria crit = new Criteria();
218 crit.addEqualTo("ruleBaseValuesId", ruleBaseValuesId);
219 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, crit);
220 query.setAttributes(new String[] { "responsibilityId" });
221 return query;
222
223 }
224
225 private ReportQueryByCriteria getResponsibilitySubQuery(List<String> workgroupIds, String workflowId, Boolean searchUser, Boolean searchUserInWorkgroups) {
226 Collection<String> workgroupIdStrings = new ArrayList<String>();
227 for (String workgroupId : workgroupIds) {
228 workgroupIdStrings.add(workgroupId);
229 }
230 return getResponsibilitySubQuery(workgroupIdStrings,workflowId,new ArrayList<String>(), searchUser, searchUserInWorkgroups);
231 }
232
233 private ReportQueryByCriteria getResponsibilitySubQuery(Collection<String> workgroupIds, String workflowId, Collection<String> actionRequestCodes, Boolean searchUser, Boolean searchUserInWorkgroups) {
234 Criteria responsibilityCrit = new Criteria();
235 if ( (actionRequestCodes != null) && (!actionRequestCodes.isEmpty()) ) {
236 responsibilityCrit.addIn("actionRequestedCd", actionRequestCodes);
237 }
238
239 Criteria ruleResponsibilityNameCrit = null;
240 if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
241
242 if (searchUser != null && searchUser) {
243
244 ruleResponsibilityNameCrit = new Criteria();
245 ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workflowId);
246 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
247 }
248 if ( (searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null) && (!workgroupIds.isEmpty()) ) {
249
250 if (ruleResponsibilityNameCrit == null) {
251 ruleResponsibilityNameCrit = new Criteria();
252 }
253 Criteria workgroupCrit = new Criteria();
254 workgroupCrit.addIn("ruleResponsibilityName", workgroupIds);
255 workgroupCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
256 ruleResponsibilityNameCrit.addOrCriteria(workgroupCrit);
257 }
258 } else if ( (workgroupIds != null) && (workgroupIds.size() == 1) ) {
259
260 ruleResponsibilityNameCrit = new Criteria();
261 ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workgroupIds.iterator().next());
262 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
263 } else if ( (workgroupIds != null) && (workgroupIds.size() > 1) ) {
264
265 ruleResponsibilityNameCrit = new Criteria();
266 ruleResponsibilityNameCrit.addIn("ruleResponsibilityName", workgroupIds);
267 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
268 }
269 if (ruleResponsibilityNameCrit != null) {
270 responsibilityCrit.addAndCriteria(ruleResponsibilityNameCrit);
271 }
272
273 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
274 query.setAttributes(new String[] { "ruleBaseValuesId" });
275 return query;
276 }
277
278
279 private Criteria getSearchCriteria(String docTypeName, String ruleTemplateId, String ruleDescription, Boolean activeInd, Map<String, String> extensionValues) {
280 Criteria crit = new Criteria();
281 crit.addEqualTo("currentInd", Boolean.TRUE);
282 crit.addEqualTo("templateRuleInd", Boolean.FALSE);
283 if (activeInd != null) {
284 crit.addEqualTo("active", activeInd);
285 }
286 if (docTypeName != null) {
287 crit.addLike("UPPER(docTypeName)", docTypeName.toUpperCase());
288 }
289 if (ruleDescription != null && !ruleDescription.trim().equals("")) {
290 crit.addLike("UPPER(description)", ruleDescription.toUpperCase());
291 }
292 if (ruleTemplateId != null) {
293 crit.addEqualTo("ruleTemplateId", ruleTemplateId);
294 }
295 if (extensionValues != null && !extensionValues.isEmpty()) {
296 for (Map.Entry<String,String> entry : extensionValues.entrySet()) {
297 if (!org.apache.commons.lang.StringUtils.isEmpty(entry.getValue())) {
298
299
300
301
302
303
304 Criteria extensionCrit2 = new Criteria();
305 extensionCrit2.addEqualTo("extensionValues.key", entry.getKey());
306 extensionCrit2.addLike("UPPER(extensionValues.value)", ("%" + (String) entry.getValue() + "%").toUpperCase());
307
308
309
310
311
312
313
314
315
316 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleExtensionBo.class, extensionCrit2);
317 query.setAttributes(new String[] { "ruleBaseValuesId" });
318 crit.addIn("ruleExtensions.ruleBaseValuesId", query);
319 }
320 }
321 }
322 return crit;
323 }
324 }