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 QueryByCriteria query = new QueryByCriteria(RuleDelegationBo.class, crit, true);
107 query.setEndAtIndex(KewApiConstants.DELEGATE_RULE_LOOKUP_MAX_ROWS_RETURNED);
108
109 return (List<RuleDelegationBo>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
110 }
111
112
113
114
115
116
117 public List<RuleDelegationBo> search(String parentRuleBaseVaueId, String parentResponsibilityId, String docTypeName, String ruleTemplateId,
118 String ruleDescription, Collection<String> workgroupIds,
119 String workflowId, String delegationType, Boolean activeInd,
120 Map extensionValues, Collection actionRequestCodes) {
121 Criteria crit = new Criteria();
122
123 if (StringUtils.isNotBlank(delegationType) && !delegationType.equals(KewApiConstants.DELEGATION_BOTH)) {
124 crit.addEqualTo("delegationType", delegationType);
125 }
126
127 if (StringUtils.isNotBlank(parentRuleBaseVaueId) && StringUtils.isNumeric(parentRuleBaseVaueId)) {
128 crit.addIn("responsibilityId", this.getRuleResponsibilitySubQuery(new Long(parentRuleBaseVaueId)));
129 }
130
131 if (StringUtils.isNotBlank(parentResponsibilityId) && StringUtils.isNumeric(parentResponsibilityId)) {
132 crit.addEqualTo("responsibilityId", parentResponsibilityId);
133 }
134
135 crit.addIn("delegateRuleId", getRuleBaseValuesSubQuery(docTypeName, ruleTemplateId,
136 ruleDescription, workgroupIds,
137 workflowId, activeInd,
138 extensionValues, actionRequestCodes));
139
140 QueryByCriteria query = new QueryByCriteria(RuleDelegationBo.class, crit, true);
141 query.setEndAtIndex(KewApiConstants.DELEGATE_RULE_LOOKUP_MAX_ROWS_RETURNED);
142
143 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
144 }
145
146 private ReportQueryByCriteria getResponsibilitySubQuery(String ruleResponsibilityName) {
147 Criteria responsibilityCrit = new Criteria();
148 responsibilityCrit.addLike("ruleResponsibilityName", ruleResponsibilityName);
149 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
150 query.setAttributes(new String[] { "ruleBaseValuesId" });
151 return query;
152 }
153
154 private ReportQueryByCriteria getWorkgroupResponsibilitySubQuery(Set<Long> workgroupIds) {
155 Set<String> workgroupIdStrings = new HashSet<String>();
156 for (Long workgroupId : workgroupIds) {
157 workgroupIdStrings.add(workgroupId.toString());
158 }
159 Criteria responsibilityCrit = new Criteria();
160 responsibilityCrit.addIn("ruleResponsibilityName", workgroupIds);
161 responsibilityCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
162 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
163 query.setAttributes(new String[] { "ruleBaseValuesId" });
164 return query;
165 }
166
167 private ReportQueryByCriteria getRuleBaseValuesSubQuery(String docTypeName, String ruleTemplateId,
168 String ruleDescription, Collection<String> workgroupIds,
169 String workflowId, Boolean activeInd,
170 Map<String, String> extensionValues, Collection actionRequestCodes) {
171 Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, activeInd, extensionValues);
172 crit.addIn("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupIds, workflowId, actionRequestCodes, (workflowId != null), ((workgroupIds != null) && !workgroupIds.isEmpty())));
173 crit.addEqualTo("delegateRule", 1);
174 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleBaseValues.class, crit);
175 query.setAttributes(new String[] { "id" });
176 return query;
177 }
178
179 private ReportQueryByCriteria getRuleBaseValuesSubQuery(String docTypeName, String ruleId,
180 String ruleTemplateId, String ruleDescription, String workgroupId,
181 String principalId, Boolean activeInd,
182 Map<String, String> extensionValues, String workflowIdDirective) {
183 Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, activeInd, extensionValues);
184 if (ruleId != null) {
185 crit.addEqualTo("id", ruleId);
186 }
187 if (workgroupId != null) {
188 crit.addIn("ruleResponsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupId));
189 }
190 List<String> workgroupIds = new ArrayList<String>();
191 Boolean searchUser = Boolean.FALSE;
192 Boolean searchUserInWorkgroups = Boolean.FALSE;
193 if (workflowIdDirective != null) {
194 if ("group".equals(workflowIdDirective)) {
195 searchUserInWorkgroups = Boolean.TRUE;
196 } else if ("".equals(workflowIdDirective)) {
197 searchUser = Boolean.TRUE;
198 searchUserInWorkgroups = Boolean.TRUE;
199 } else {
200 searchUser = Boolean.TRUE;
201 }
202 }
203 if (!org.apache.commons.lang.StringUtils.isEmpty(principalId) && searchUserInWorkgroups)
204 {
205 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
206
207 if (principal == null)
208 {
209 throw new RiceRuntimeException("Failed to locate user for the given workflow id: " + principalId);
210 }
211 workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
212 }
213 if (CollectionUtils.isNotEmpty(workgroupIds) || StringUtils.isNotBlank(principalId)) {
214 crit.addIn("ruleResponsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupIds, principalId, searchUser, searchUserInWorkgroups));
215 }
216 crit.addEqualTo("delegateRule", 1);
217 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleBaseValues.class, crit);
218 query.setAttributes(new String[] { "id" });
219 return query;
220
221 }
222
223 private ReportQueryByCriteria getRuleResponsibilitySubQuery(Long ruleBaseValuesId) {
224 Criteria crit = new Criteria();
225 crit.addEqualTo("ruleBaseValuesId", ruleBaseValuesId);
226 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, crit);
227 query.setAttributes(new String[] { "responsibilityId" });
228 return query;
229
230 }
231
232 private ReportQueryByCriteria getResponsibilitySubQuery(List<String> workgroupIds, String workflowId, Boolean searchUser, Boolean searchUserInWorkgroups) {
233 Collection<String> workgroupIdStrings = new ArrayList<String>();
234 for (String workgroupId : workgroupIds) {
235 workgroupIdStrings.add(workgroupId);
236 }
237 return getResponsibilitySubQuery(workgroupIdStrings,workflowId,new ArrayList<String>(), searchUser, searchUserInWorkgroups);
238 }
239
240 private ReportQueryByCriteria getResponsibilitySubQuery(Collection<String> workgroupIds, String workflowId, Collection<String> actionRequestCodes, Boolean searchUser, Boolean searchUserInWorkgroups) {
241 Criteria responsibilityCrit = new Criteria();
242 if ( (actionRequestCodes != null) && (!actionRequestCodes.isEmpty()) ) {
243 responsibilityCrit.addIn("actionRequestedCd", actionRequestCodes);
244 }
245
246 Criteria ruleResponsibilityNameCrit = null;
247 if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
248
249 if (searchUser != null && searchUser) {
250
251 ruleResponsibilityNameCrit = new Criteria();
252 ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workflowId);
253 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
254 }
255 if ( (searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null) && (!workgroupIds.isEmpty()) ) {
256
257 if (ruleResponsibilityNameCrit == null) {
258 ruleResponsibilityNameCrit = new Criteria();
259 }
260 Criteria workgroupCrit = new Criteria();
261 workgroupCrit.addIn("ruleResponsibilityName", workgroupIds);
262 workgroupCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
263 ruleResponsibilityNameCrit.addOrCriteria(workgroupCrit);
264 }
265 } else if ( (workgroupIds != null) && (workgroupIds.size() == 1) ) {
266
267 ruleResponsibilityNameCrit = new Criteria();
268 ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workgroupIds.iterator().next());
269 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
270 } else if ( (workgroupIds != null) && (workgroupIds.size() > 1) ) {
271
272 ruleResponsibilityNameCrit = new Criteria();
273 ruleResponsibilityNameCrit.addIn("ruleResponsibilityName", workgroupIds);
274 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
275 }
276 if (ruleResponsibilityNameCrit != null) {
277 responsibilityCrit.addAndCriteria(ruleResponsibilityNameCrit);
278 }
279
280 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
281 query.setAttributes(new String[] { "ruleBaseValuesId" });
282 return query;
283 }
284
285
286 private Criteria getSearchCriteria(String docTypeName, String ruleTemplateId, String ruleDescription, Boolean activeInd, Map<String, String> extensionValues) {
287 Criteria crit = new Criteria();
288 crit.addEqualTo("currentInd", Boolean.TRUE);
289 crit.addEqualTo("templateRuleInd", Boolean.FALSE);
290 if (activeInd != null) {
291 crit.addEqualTo("active", activeInd);
292 }
293 if (docTypeName != null) {
294 crit.addLike("UPPER(docTypeName)", docTypeName.toUpperCase());
295 }
296 if (ruleDescription != null && !ruleDescription.trim().equals("")) {
297 crit.addLike("UPPER(description)", ruleDescription.toUpperCase());
298 }
299 if (ruleTemplateId != null) {
300 crit.addEqualTo("ruleTemplateId", ruleTemplateId);
301 }
302 if (extensionValues != null && !extensionValues.isEmpty()) {
303 for (Map.Entry<String,String> entry : extensionValues.entrySet()) {
304 if (!org.apache.commons.lang.StringUtils.isEmpty(entry.getValue())) {
305
306
307
308
309
310
311 Criteria extensionCrit2 = new Criteria();
312 extensionCrit2.addEqualTo("extensionValues.key", entry.getKey());
313 extensionCrit2.addLike("UPPER(extensionValues.value)", ("%" + (String) entry.getValue() + "%").toUpperCase());
314
315
316
317
318
319
320
321
322
323 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleExtensionBo.class, extensionCrit2);
324 query.setAttributes(new String[] { "ruleBaseValuesId" });
325 crit.addIn("ruleExtensions.ruleBaseValuesId", query);
326 }
327 }
328 }
329 return crit;
330 }
331 }