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