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