1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.rule.bo;
18
19 import org.apache.commons.lang.ArrayUtils;
20 import org.apache.commons.lang.builder.ToStringBuilder;
21 import org.hibernate.annotations.Fetch;
22 import org.hibernate.annotations.FetchMode;
23 import org.kuali.rice.core.jpa.annotations.Sequence;
24 import org.kuali.rice.kew.bo.KewPersistableBusinessObjectBase;
25 import org.kuali.rice.kew.bo.WorkflowPersistable;
26 import org.kuali.rice.kew.rule.Role;
27 import org.kuali.rice.kew.rule.RoleAttribute;
28 import org.kuali.rice.kew.rule.RuleTemplateOption;
29 import org.kuali.rice.kew.rule.WorkflowAttribute;
30 import org.kuali.rice.kew.util.KEWConstants;
31
32 import javax.persistence.*;
33 import java.net.URLEncoder;
34 import java.util.*;
35
36
37
38
39
40
41
42
43
44 @Entity
45 @Table(name="KREW_RULE_TMPL_T")
46 @Sequence(name="KREW_RTE_TMPL_S", property="ruleTemplateId")
47 @NamedQueries({@NamedQuery(name="findAllOrderedByName", query="SELECT rt FROM RuleTemplate rt ORDER BY rt.name ASC")})
48 public class RuleTemplate extends KewPersistableBusinessObjectBase implements WorkflowPersistable {
49
50 private static final long serialVersionUID = -3387940485523951302L;
51
52
53
54
55 public static final String[] DEFAULT_OPTION_KEYS = {
56
57 KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ,
58 KEWConstants.ACTION_REQUEST_APPROVE_REQ,
59 KEWConstants.ACTION_REQUEST_COMPLETE_REQ,
60 KEWConstants.ACTION_REQUEST_FYI_REQ,
61 KEWConstants.ACTION_REQUEST_DEFAULT_CD
62 };
63
64 @Id
65 @Column(name="RULE_TMPL_ID")
66 private Long ruleTemplateId;
67 @Column(name="NM")
68 private String name;
69 @Column(name="RULE_TMPL_DESC")
70 private String description;
71
72 @Column(name="DLGN_RULE_TMPL_ID", insertable=false, updatable=false)
73 private Long delegationTemplateId;
74 @OneToOne(fetch=FetchType.EAGER)
75 @JoinColumn(name="DLGN_RULE_TMPL_ID")
76 private RuleTemplate delegationTemplate;
77 @Fetch(value = FetchMode.SUBSELECT)
78 @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
79 mappedBy="ruleTemplate")
80 private List<RuleTemplateAttribute> ruleTemplateAttributes;
81 @OneToMany(cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
82 mappedBy="ruleTemplate")
83 private List<RuleTemplateOption> ruleTemplateOptions;
84
85
86 @Transient
87 private String returnUrl;
88
89 public RuleTemplate() {
90 ruleTemplateAttributes = new ArrayList<RuleTemplateAttribute>();
91 ruleTemplateOptions = new ArrayList<RuleTemplateOption>();
92 }
93
94
95
96
97
98 public void removeNonDefaultOptions() {
99 Iterator<RuleTemplateOption> it = ruleTemplateOptions.iterator();
100 while (it.hasNext()) {
101 RuleTemplateOption option = it.next();
102
103 if (!ArrayUtils.contains(DEFAULT_OPTION_KEYS, option.getKey())) {
104 it.remove();
105 }
106 }
107 }
108
109 public String getDelegateTemplateName() {
110 if (delegationTemplate != null) {
111 return delegationTemplate.getName();
112 }
113 return "";
114 }
115
116 public String getRuleTemplateActionsUrl() {
117 return "<a href=\"RuleTemplate.do?methodToCall=report¤tRuleTemplateId=" + ruleTemplateId + "\" >report</a>"
118
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 private RuleTemplateAttribute getRuleTemplateAttribute(RuleTemplateAttribute ruleTemplateAttribute, Boolean active) {
151 for (RuleTemplateAttribute currentRuleTemplateAttribute: getRuleTemplateAttributes()) {
152 if (currentRuleTemplateAttribute.getRuleAttribute().getName().equals(ruleTemplateAttribute.getRuleAttribute().getName())) {
153 if (active == null) {
154 return currentRuleTemplateAttribute;
155 }
156 else if (active.compareTo(currentRuleTemplateAttribute.getActive()) == 0) {
157 return currentRuleTemplateAttribute;
158 }
159 }
160 }
161 return null;
162 }
163
164 public RuleTemplateAttribute getRuleTemplateAttribute(RuleTemplateAttribute ruleTemplateAttribute) {
165 return getRuleTemplateAttribute(ruleTemplateAttribute, null);
166 }
167
168 public boolean containsActiveRuleTemplateAttribute(RuleTemplateAttribute templateAttribute) {
169 return (getRuleTemplateAttribute(templateAttribute, Boolean.TRUE) != null);
170 }
171
172 public boolean containsRuleTemplateAttribute(RuleTemplateAttribute templateAttribute) {
173 return (getRuleTemplateAttribute(templateAttribute, null) != null);
174 }
175
176 public RuleTemplateAttribute getRuleTemplateAttribute(int index) {
177 while (getRuleTemplateAttributes().size() <= index) {
178 getRuleTemplateAttributes().add(new RuleTemplateAttribute());
179 }
180 return (RuleTemplateAttribute) getRuleTemplateAttributes().get(index);
181 }
182
183 public List<RuleTemplateAttribute> getRuleTemplateAttributes() {
184 Collections.sort(ruleTemplateAttributes);
185 return ruleTemplateAttributes;
186 }
187
188
189
190
191
192
193 public List<RuleTemplateAttribute> getActiveRuleTemplateAttributes() {
194 List<RuleTemplateAttribute> activeAttributes = new ArrayList<RuleTemplateAttribute>();
195 for (RuleTemplateAttribute templateAttribute : getRuleTemplateAttributes())
196 {
197 if (templateAttribute.isActive())
198 {
199 activeAttributes.add(templateAttribute);
200 }
201 }
202 Collections.sort(activeAttributes);
203 return activeAttributes;
204 }
205
206
207
208
209
210 public void setActiveRuleTemplateAttributes(List<RuleTemplateAttribute> ruleTemplateAttributes) {
211 throw new UnsupportedOperationException("setActiveRuleTemplateAttributes is not implemented");
212 }
213
214 public void setRuleTemplateAttributes(List<RuleTemplateAttribute> ruleTemplateAttributes) {
215 this.ruleTemplateAttributes = ruleTemplateAttributes;
216 }
217
218 public String getDescription() {
219 return description;
220 }
221
222 public void setDescription(String description) {
223 this.description = description;
224 }
225
226 public String getName() {
227 return name;
228 }
229
230 public void setName(String name) {
231 this.name = name;
232 }
233
234 public Long getRuleTemplateId() {
235 return ruleTemplateId;
236 }
237
238 public void setRuleTemplateId(Long ruleTemplateId) {
239 this.ruleTemplateId = ruleTemplateId;
240 }
241
242 public Long getDelegationTemplateId() {
243 return delegationTemplateId;
244 }
245
246 public void setDelegationTemplateId(Long delegationTemplateId) {
247 this.delegationTemplateId = delegationTemplateId;
248 }
249
250 public RuleTemplate getDelegationTemplate() {
251 return delegationTemplate;
252 }
253
254 public void setDelegationTemplate(RuleTemplate delegationTemplate) {
255 this.delegationTemplate = delegationTemplate;
256 }
257
258
259
260
261
262 public Object copy(boolean preserveKeys) {
263 RuleTemplate ruleTemplateClone = new RuleTemplate();
264
265 if (description != null) {
266 ruleTemplateClone.setDescription(new String(description));
267 }
268 if (name != null) {
269 ruleTemplateClone.setName(new String(name));
270 }
271 if (preserveKeys && ruleTemplateId != null) {
272 ruleTemplateClone.setRuleTemplateId(new Long(ruleTemplateId.longValue()));
273 }
274 if ((getRuleTemplateAttributes() != null) && !getRuleTemplateAttributes().isEmpty()) {
275 List<RuleTemplateAttribute> ruleTemplateAttributeList = new ArrayList<RuleTemplateAttribute>();
276
277 for (RuleTemplateAttribute ruleTemplateAttribute: getRuleTemplateAttributes()) {
278 RuleTemplateAttribute ruleTemplateAttributeCopy = (RuleTemplateAttribute) ruleTemplateAttribute.copy(preserveKeys);
279 ruleTemplateAttributeCopy.setRuleTemplate(ruleTemplateClone);
280 ruleTemplateAttributeList.add(ruleTemplateAttributeCopy);
281 }
282 ruleTemplateClone.setRuleTemplateAttributes(ruleTemplateAttributeList);
283 }
284 if ((ruleTemplateOptions != null) && !ruleTemplateOptions.isEmpty()) {
285 List<RuleTemplateOption> ruleTemplateOptionList = new ArrayList<RuleTemplateOption>();
286
287 for (RuleTemplateOption ruleTemplateOption: ruleTemplateOptions) {
288 RuleTemplateOption ruleTemplateOptionCopy = (RuleTemplateOption) ruleTemplateOption.copy(preserveKeys);
289 ruleTemplateOptionCopy.setRuleTemplate(ruleTemplateClone);
290 ruleTemplateOptionList.add(ruleTemplateOptionCopy);
291 }
292 ruleTemplateClone.setRuleTemplateOptions(ruleTemplateOptionList);
293 }
294
295 return ruleTemplateClone;
296 }
297
298 public String getReturnUrl() {
299 return returnUrl;
300 }
301
302 public void setReturnUrl(String returnUrl) {
303 this.returnUrl = returnUrl;
304 }
305
306
307
308
309 public String getEncodedName() {
310 return URLEncoder.encode(getName());
311 }
312
313 public List<RuleTemplateOption> getRuleTemplateOptions() {
314 return ruleTemplateOptions;
315 }
316
317 public void setRuleTemplateOptions(List<RuleTemplateOption> ruleTemplateOptions) {
318 this.ruleTemplateOptions = ruleTemplateOptions;
319 }
320
321 public RuleTemplateOption getRuleTemplateOption(String key) {
322 for (RuleTemplateOption option: ruleTemplateOptions) {
323 if (option.getKey().equals(key)) {
324 return option;
325 }
326 }
327 return null;
328 }
329
330
331
332
333
334
335
336
337 public void setAcknowledge(RuleTemplateOption acknowledge) {
338 RuleTemplateOption option = getRuleTemplateOption(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
339 option.setValue(acknowledge.getValue());
340 option.setRuleTemplateOptionId(acknowledge.getRuleTemplateOptionId());
341 option.setLockVerNbr(acknowledge.getLockVerNbr());
342 }
343
344 public void setComplete(RuleTemplateOption complete) {
345 RuleTemplateOption option = getRuleTemplateOption(KEWConstants.ACTION_REQUEST_COMPLETE_REQ);
346 option.setValue(complete.getValue());
347 option.setRuleTemplateOptionId(complete.getRuleTemplateOptionId());
348 option.setLockVerNbr(complete.getLockVerNbr());
349 }
350
351 public void setApprove(RuleTemplateOption approve) {
352 RuleTemplateOption option = getRuleTemplateOption(KEWConstants.ACTION_REQUEST_APPROVE_REQ);
353 option.setValue(approve.getValue());
354 option.setRuleTemplateOptionId(approve.getRuleTemplateOptionId());
355 option.setLockVerNbr(approve.getLockVerNbr());
356 }
357
358 public void setFyi(RuleTemplateOption fyi) {
359 RuleTemplateOption option = getRuleTemplateOption(KEWConstants.ACTION_REQUEST_FYI_REQ);
360 option.setValue(fyi.getValue());
361 option.setRuleTemplateOptionId(fyi.getRuleTemplateOptionId());
362 option.setLockVerNbr(fyi.getLockVerNbr());
363 }
364
365 public void setDefaultActionRequestValue(RuleTemplateOption defaultActionRequestValue) {
366 RuleTemplateOption option = getRuleTemplateOption(KEWConstants.ACTION_REQUEST_DEFAULT_CD);
367 option.setValue(defaultActionRequestValue.getValue());
368 option.setRuleTemplateOptionId(defaultActionRequestValue.getRuleTemplateOptionId());
369 option.setLockVerNbr(defaultActionRequestValue.getLockVerNbr());
370 }
371
372
373
374
375
376 public RuleTemplateOption getAcknowledge() {
377 return getRuleTemplateOption(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
378 }
379
380 public RuleTemplateOption getComplete() {
381 return getRuleTemplateOption(KEWConstants.ACTION_REQUEST_COMPLETE_REQ);
382 }
383
384 public RuleTemplateOption getApprove() {
385 return getRuleTemplateOption(KEWConstants.ACTION_REQUEST_APPROVE_REQ);
386 }
387
388 public RuleTemplateOption getFyi() {
389 return getRuleTemplateOption(KEWConstants.ACTION_REQUEST_FYI_REQ);
390 }
391
392 public RuleTemplateOption getDefaultActionRequestValue() {
393 return getRuleTemplateOption(KEWConstants.ACTION_REQUEST_DEFAULT_CD);
394 }
395
396
397
398
399
400 public List<Role> getRoles() {
401 List<Role> roles = new ArrayList<Role>();
402 List<RuleTemplateAttribute> ruleTemplateAttributes = getActiveRuleTemplateAttributes();
403 Collections.sort(ruleTemplateAttributes);
404 for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplateAttributes)
405 {
406 if (!ruleTemplateAttribute.isWorkflowAttribute())
407 {
408 continue;
409 }
410 WorkflowAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute();
411 if (workflowAttribute instanceof RoleAttribute)
412 {
413 RoleAttribute roleAttribute = (RoleAttribute) workflowAttribute;
414 roles.addAll(roleAttribute.getRoleNames());
415 }
416 }
417 return roles;
418 }
419
420 public String toString() {
421 return new ToStringBuilder(this).append("ruleTemplateId", ruleTemplateId)
422 .append("name", name)
423 .append("description", description)
424 .append("delegationTemplateId", delegationTemplateId)
425 .append("totalRuleTemplateAttributes", getRuleTemplateAttributes() == null ? "null" : "size: " + getRuleTemplateAttributes().size())
426 .append("activeRuleTemplateAttributes", getActiveRuleTemplateAttributes() == null ? "null" : "size: " + getActiveRuleTemplateAttributes().size())
427 .append("returnUrl", returnUrl)
428 .append("versionNumber", versionNumber)
429 .append("ruleTemplateOptions", ruleTemplateOptions).toString();
430
431 }
432
433
434
435
436
437
438 @Override
439 protected LinkedHashMap<String, Object> toStringMapper() {
440 LinkedHashMap<String, Object> propMap = new LinkedHashMap<String, Object>();
441 propMap.put("ruleTemplateId", getRuleTemplateId());
442 propMap.put("name", getName());
443 propMap.put("description", getDescription());
444 propMap.put("delegationTemplateId", getDelegationTemplateId());
445 propMap.put("totalRuleTemplateAttributes", getRuleTemplateAttributes() == null ? "null" : "size: " + getRuleTemplateAttributes().size());
446 propMap.put("activeRuleTemplateAttributes", getActiveRuleTemplateAttributes() == null ? "null" : "size: " + getActiveRuleTemplateAttributes().size());
447 propMap.put("returnUrl", getReturnUrl());
448 propMap.put("versionNumber", getVersionNumber());
449 propMap.put("ruleTemplateOptions", getRuleTemplateOptions());
450
451 return propMap;
452
453 }
454 }