1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.rule;
17
18 import org.hibernate.annotations.Fetch;
19 import org.hibernate.annotations.FetchMode;
20 import org.hibernate.annotations.GenericGenerator;
21 import org.hibernate.annotations.Parameter;
22 import org.joda.time.DateTime;
23 import org.kuali.rice.core.api.util.RiceConstants;
24 import org.kuali.rice.kew.api.rule.RuleContract;
25 import org.kuali.rice.kew.api.util.CodeTranslator;
26 import org.kuali.rice.kew.doctype.bo.DocumentType;
27 import org.kuali.rice.kew.lookupable.MyColumns;
28 import org.kuali.rice.kew.routeheader.DocumentContent;
29 import org.kuali.rice.kew.rule.bo.RuleAttribute;
30 import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
31 import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
32 import org.kuali.rice.kew.rule.service.RuleServiceInternal;
33 import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
34 import org.kuali.rice.kew.service.KEWServiceLocator;
35 import org.kuali.rice.kew.api.KewApiConstants;
36 import org.kuali.rice.kim.impl.identity.PersonImpl;
37 import org.kuali.rice.kns.web.ui.Field;
38 import org.kuali.rice.kns.web.ui.Row;
39 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
40
41 import javax.persistence.CascadeType;
42 import javax.persistence.Column;
43 import javax.persistence.Entity;
44 import javax.persistence.FetchType;
45 import javax.persistence.GeneratedValue;
46 import javax.persistence.Id;
47 import javax.persistence.JoinColumn;
48 import javax.persistence.ManyToOne;
49 import javax.persistence.OneToMany;
50 import javax.persistence.OneToOne;
51 import javax.persistence.Table;
52 import javax.persistence.Transient;
53 import java.sql.Timestamp;
54 import java.util.ArrayList;
55 import java.util.Date;
56 import java.util.HashMap;
57 import java.util.Iterator;
58 import java.util.List;
59 import java.util.Map;
60
61
62
63
64
65
66
67
68
69 @Entity
70 @Table(name="KREW_RULE_T")
71
72 public class RuleBaseValues extends PersistableBusinessObjectBase implements RuleContract {
73
74 private static final long serialVersionUID = 6137765574728530156L;
75 @Id
76 @GeneratedValue(generator="KREW_RTE_TMPL_S")
77 @GenericGenerator(name="KREW_RTE_TMPL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
78 @Parameter(name="sequence_name",value="KREW_RTE_TMPL_S"),
79 @Parameter(name="value_column",value="id")
80 })
81 @Column(name="RULE_ID")
82 private String id;
83
84
85
86 @Column(name="NM")
87 private String name;
88 @Column(name="RULE_TMPL_ID", insertable=false, updatable=false)
89 private String ruleTemplateId;
90 @Column(name="PREV_VER_RULE_ID")
91 private String previousRuleId;
92 @Column(name="ACTV_IND")
93 private boolean active = true;
94 @Column(name="RULE_BASE_VAL_DESC")
95 private String description;
96 @Column(name="DOC_TYP_NM")
97 private String docTypeName;
98 @Column(name="DOC_HDR_ID")
99 private String documentId;
100 @Column(name="FRM_DT")
101 private Timestamp fromDateValue;
102 @Column(name="TO_DT")
103 private Timestamp toDateValue;
104 @Column(name="DACTVN_DT")
105 private Timestamp deactivationDate;
106 @Column(name="CUR_IND")
107 private Boolean currentInd = Boolean.TRUE;
108 @Column(name="RULE_VER_NBR")
109 private Integer versionNbr = new Integer(0);
110 @Column(name="FRC_ACTN")
111 private boolean forceAction;
112 @Fetch(value = FetchMode.SELECT)
113 @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},mappedBy="ruleBaseValues")
114 private List<RuleResponsibilityBo> ruleResponsibilities;
115 @Fetch(value = FetchMode.SELECT)
116 @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},mappedBy="ruleBaseValues")
117 private List<RuleExtensionBo> ruleExtensions;
118 @ManyToOne(fetch=FetchType.EAGER)
119 @JoinColumn(name="RULE_TMPL_ID")
120 private RuleTemplateBo ruleTemplate;
121 @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
122 @JoinColumn(name="RULE_EXPR_ID")
123 private RuleExpressionDef ruleExpressionDef;
124 @Transient
125 private RuleBaseValues previousVersion;
126 @Column(name="ACTVN_DT")
127 private Timestamp activationDate;
128 @Column(name="DLGN_IND")
129 private Boolean delegateRule = Boolean.FALSE;
130
131
132
133
134 @Column(name="TMPL_RULE_IND")
135 private Boolean templateRuleInd = Boolean.FALSE;
136
137
138 @Transient
139 private String returnUrl;
140 @Transient
141 private String destinationUrl;
142 @Transient
143 private MyColumns myColumns;
144 @Transient
145 private List<PersonRuleResponsibility> personResponsibilities = new ArrayList<PersonRuleResponsibility>();
146 @Transient
147 private List<GroupRuleResponsibility> groupResponsibilities = new ArrayList<GroupRuleResponsibility>();
148 @Transient
149 private List<RoleRuleResponsibility> roleResponsibilities = new ArrayList<RoleRuleResponsibility>();
150 @Transient
151 private Map<String, String> fieldValues;
152 @Transient
153 private String groupReviewerName;
154 @Transient
155 private String groupReviewerNamespace;
156 @Transient
157 private String personReviewer;
158 @Transient
159 private String personReviewerType;
160
161 public RuleBaseValues() {
162 ruleResponsibilities = new ArrayList<RuleResponsibilityBo>();
163 ruleExtensions = new ArrayList<RuleExtensionBo>();
164
165
166
167 fieldValues = new HashMap<String, String>();
168 }
169
170
171
172
173 public RuleExpressionDef getRuleExpressionDef() {
174 return ruleExpressionDef;
175 }
176
177
178
179
180 public void setRuleExpressionDef(RuleExpressionDef ruleExpressionDef) {
181 this.ruleExpressionDef = ruleExpressionDef;
182 }
183
184 public Map getRuleExtensionValueLabels() {
185 Map extensionLabels = new HashMap();
186 for (RuleExtensionBo ruleExtension : getRuleExtensions()) {
187 if (!ruleExtension.getRuleTemplateAttribute().isWorkflowAttribute()) {
188 continue;
189 }
190 WorkflowRuleAttribute workflowAttribute = ruleExtension.getRuleTemplateAttribute().getWorkflowAttribute();
191
192 RuleAttribute ruleAttribute = ruleExtension.getRuleTemplateAttribute().getRuleAttribute();
193
194
195
196 for (Row row : workflowAttribute.getRuleRows()) {
197 for (Field field : row.getFields()) {
198 extensionLabels.put(field.getPropertyName(), field.getFieldLabel());
199 }
200 }
201 }
202 return extensionLabels;
203 }
204
205 public String getRuleTemplateName() {
206 if (ruleTemplate != null) {
207 return ruleTemplate.getName();
208 }
209 return null;
210 }
211
212 public RuleBaseValues getPreviousVersion() {
213 if (previousVersion == null && previousRuleId != null) {
214 RuleServiceInternal ruleService = (RuleServiceInternal) KEWServiceLocator.getService(KEWServiceLocator.RULE_SERVICE);
215 return ruleService.findRuleBaseValuesById(previousRuleId);
216 }
217 return previousVersion;
218 }
219
220 public void setPreviousVersion(RuleBaseValues previousVersion) {
221 this.previousVersion = previousVersion;
222 }
223
224 public RuleResponsibilityBo getResponsibility(int index) {
225 while (getRuleResponsibilities().size() <= index) {
226 RuleResponsibilityBo ruleResponsibility = new RuleResponsibilityBo();
227 ruleResponsibility.setRuleBaseValues(this);
228 getRuleResponsibilities().add(ruleResponsibility);
229 }
230 return (RuleResponsibilityBo) getRuleResponsibilities().get(index);
231 }
232
233 public RuleExtensionBo getRuleExtension(int index) {
234 while (getRuleExtensions().size() <= index) {
235 getRuleExtensions().add(new RuleExtensionBo());
236 }
237 return (RuleExtensionBo) getRuleExtensions().get(index);
238 }
239
240 public RuleExtensionValue getRuleExtensionValue(String key) {
241 for (Iterator iter = getRuleExtensions().iterator(); iter.hasNext();) {
242 RuleExtensionBo ruleExtension = (RuleExtensionBo) iter.next();
243 for (Iterator iterator = ruleExtension.getExtensionValues().iterator(); iterator.hasNext();) {
244 RuleExtensionValue ruleExtensionValue = (RuleExtensionValue) iterator.next();
245 if (ruleExtensionValue.getKey().equals(key)) {
246 return ruleExtensionValue;
247 }
248 }
249 }
250 return null;
251 }
252
253 public RuleExtensionValue getRuleExtensionValue(String ruleTemplateAttributeId, String key) {
254 for (Iterator iter = getRuleExtensions().iterator(); iter.hasNext();) {
255 RuleExtensionBo ruleExtension = (RuleExtensionBo) iter.next();
256 if (ruleExtension.getRuleTemplateAttributeId().equals(ruleTemplateAttributeId)) {
257 for (Iterator iterator = ruleExtension.getExtensionValues().iterator(); iterator.hasNext();) {
258 RuleExtensionValue ruleExtensionValue = (RuleExtensionValue) iterator.next();
259 if (ruleExtensionValue.getKey().equals(key)) {
260 return ruleExtensionValue;
261 }
262 }
263 }
264 }
265 return null;
266 }
267
268 public String getPreviousRuleId() {
269 return previousRuleId;
270 }
271
272 public void setPreviousRuleId(String previousVersion) {
273 this.previousRuleId = previousVersion;
274 }
275
276 public void addRuleResponsibility(RuleResponsibilityBo ruleResponsibility) {
277 addRuleResponsibility(ruleResponsibility, new Integer(getRuleResponsibilities().size()));
278 }
279
280 public void addRuleResponsibility(RuleResponsibilityBo ruleResponsibility, Integer counter) {
281 boolean alreadyAdded = false;
282 int location = 0;
283 if (counter != null) {
284 for (RuleResponsibilityBo ruleResponsibilityRow : getRuleResponsibilities()) {
285 if (counter.intValue() == location) {
286 ruleResponsibilityRow.setPriority(ruleResponsibility.getPriority());
287 ruleResponsibilityRow.setActionRequestedCd(ruleResponsibility.getActionRequestedCd());
288 ruleResponsibilityRow.setVersionNumber(ruleResponsibility.getVersionNumber());
289 ruleResponsibilityRow.setRuleBaseValuesId(ruleResponsibility.getRuleBaseValuesId());
290 ruleResponsibilityRow.setRuleResponsibilityName(ruleResponsibility.getRuleResponsibilityName());
291 ruleResponsibilityRow.setRuleResponsibilityType(ruleResponsibility.getRuleResponsibilityType());
292
293 ruleResponsibilityRow.setApprovePolicy(ruleResponsibility.getApprovePolicy());
294 alreadyAdded = true;
295 }
296 location++;
297 }
298 }
299 if (!alreadyAdded) {
300 getRuleResponsibilities().add(ruleResponsibility);
301 }
302 }
303
304 public RuleTemplateBo getRuleTemplate() {
305 return ruleTemplate;
306 }
307
308 public void setRuleTemplate(RuleTemplateBo ruleTemplate) {
309 this.ruleTemplate = ruleTemplate;
310 }
311
312 public String getRuleTemplateId() {
313 return ruleTemplateId;
314 }
315
316 public void setRuleTemplateId(String ruleTemplateId) {
317 this.ruleTemplateId = ruleTemplateId;
318 }
319
320 public DocumentType getDocumentType() {
321 return KEWServiceLocator.getDocumentTypeService().findByName(getDocTypeName());
322 }
323
324 public String getDocTypeName() {
325 return docTypeName;
326 }
327
328 public void setDocTypeName(String docTypeName) {
329 this.docTypeName = docTypeName;
330 }
331
332 public List<RuleExtensionBo> getRuleExtensions() {
333 return ruleExtensions;
334 }
335
336 public Map<String, String> getRuleExtensionMap() {
337 Map<String, String> extensions = new HashMap<String, String>();
338 for (RuleExtensionBo ext : this.getRuleExtensions()) {
339 for (RuleExtensionValue value : ext.getExtensionValues()) {
340 extensions.put(value.getKey(), value.getValue());
341 }
342 }
343 return extensions;
344 }
345
346 public void setRuleExtensions(List<RuleExtensionBo> ruleExtensions) {
347 this.ruleExtensions = ruleExtensions;
348 }
349
350 public List<RuleResponsibilityBo> getRuleResponsibilities() {
351 return this.ruleResponsibilities;
352 }
353
354 public void setRuleResponsibilities(List<RuleResponsibilityBo> ruleResponsibilities) {
355 this.ruleResponsibilities = ruleResponsibilities;
356 }
357
358 public RuleResponsibilityBo getResponsibility(Long ruleResponsibilityKey) {
359 for (Iterator iterator = getRuleResponsibilities().iterator(); iterator.hasNext();) {
360 RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iterator.next();
361 if (responsibility.getId() != null
362 && responsibility.getId().equals(ruleResponsibilityKey)) {
363 return responsibility;
364 }
365 }
366 return null;
367 }
368
369 public void removeResponsibility(int index) {
370 getRuleResponsibilities().remove(index);
371 }
372
373 @Override
374 public boolean isActive() {
375 return active;
376 }
377
378 public void setActive(boolean active) {
379 this.active = active;
380 }
381
382 public String getActiveIndDisplay() {
383 return CodeTranslator.getActiveIndicatorLabel(isActive());
384 }
385
386 public Boolean getCurrentInd() {
387 return currentInd;
388 }
389
390 public void setCurrentInd(Boolean currentInd) {
391 this.currentInd = currentInd;
392 }
393
394 public Timestamp getFromDateValue() {
395 return fromDateValue;
396 }
397
398 @Override
399 public DateTime getFromDate() {
400 if (this.fromDateValue == null) {
401 return null;
402 }
403 return new DateTime(this.fromDateValue.getTime());
404 }
405
406 public void setFromDateValue(Timestamp fromDateValue) {
407 this.fromDateValue = fromDateValue;
408 }
409
410 public String getDescription() {
411 return description;
412 }
413
414 public void setDescription(String description) {
415 this.description = description;
416 }
417
418 public String getId() {
419 return id;
420 }
421
422 public void setId(String id) {
423 this.id = id;
424 }
425
426 public Timestamp getToDateValue() {
427 return toDateValue;
428 }
429
430 @Override
431 public DateTime getToDate() {
432 if (this.toDateValue == null) {
433 return null;
434 }
435 return new DateTime(this.toDateValue.getTime());
436 }
437
438 public void setToDateValue(Timestamp toDateValue) {
439 this.toDateValue = toDateValue;
440 }
441
442 public Integer getVersionNbr() {
443 return versionNbr;
444 }
445
446 public void setVersionNbr(Integer versionNbr) {
447 this.versionNbr = versionNbr;
448 }
449
450 public String getReturnUrl() {
451 return returnUrl;
452 }
453
454 public void setReturnUrl(String returnUrl) {
455 this.returnUrl = returnUrl;
456 }
457
458 public String getFromDateString() {
459 if (this.fromDateValue != null) {
460 return RiceConstants.getDefaultDateFormat().format(this.fromDateValue);
461 }
462 return null;
463 }
464
465 public String getToDateString() {
466 if (this.toDateValue != null) {
467 return RiceConstants.getDefaultDateFormat().format(this.toDateValue);
468 }
469 return null;
470 }
471
472 @Override
473 public boolean isForceAction() {
474 return forceAction;
475 }
476
477 public void setForceAction(boolean forceAction) {
478 this.forceAction = forceAction;
479 }
480
481 public boolean isActive(Date date) {
482 boolean isAfterFromDate = getFromDateValue() == null || date.after(getFromDateValue());
483 boolean isBeforeToDate = getToDateValue() == null || date.before(getToDateValue());
484 return isActive() && isAfterFromDate && isBeforeToDate;
485 }
486
487 public boolean isMatch(DocumentContent docContent) {
488 for (RuleTemplateAttributeBo ruleTemplateAttribute : getRuleTemplate().getActiveRuleTemplateAttributes()) {
489 if (!ruleTemplateAttribute.isWorkflowAttribute()) {
490 continue;
491 }
492 WorkflowRuleAttribute routingAttribute = (WorkflowRuleAttribute) ruleTemplateAttribute.getWorkflowAttribute();
493
494 RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
495 if (ruleAttribute.getType().equals(KewApiConstants.RULE_XML_ATTRIBUTE_TYPE)) {
496 ((GenericXMLRuleAttribute) routingAttribute).setExtensionDefinition(RuleAttribute.to(ruleAttribute));
497 }
498 String className = ruleAttribute.getResourceDescriptor();
499 List<RuleExtensionBo> editedRuleExtensions = new ArrayList<RuleExtensionBo>();
500 for (RuleExtensionBo extension : getRuleExtensions()) {
501 if (extension.getRuleTemplateAttribute().getRuleAttribute().getResourceDescriptor().equals(className)) {
502 editedRuleExtensions.add(extension);
503 }
504 }
505 if (!routingAttribute.isMatch(docContent, editedRuleExtensions)) {
506 return false;
507 }
508 }
509 return true;
510 }
511
512 public RuleResponsibilityBo findResponsibility(String roleName) {
513 for (Iterator iter = getRuleResponsibilities().iterator(); iter.hasNext();) {
514 RuleResponsibilityBo resp = (RuleResponsibilityBo) iter.next();
515 if (KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID.equals(resp.getRuleResponsibilityType())
516 && roleName.equals(resp.getRuleResponsibilityName())) {
517 return resp;
518 }
519 }
520 return null;
521 }
522
523 public String getDocumentId() {
524 return documentId;
525 }
526
527 public void setDocumentId(String documentId) {
528 this.documentId = documentId;
529 }
530
531 public Boolean getDelegateRule() {
532 return delegateRule;
533 }
534
535 public void setDelegateRule(Boolean isDelegateRule) {
536 this.delegateRule = isDelegateRule;
537 }
538
539 public Timestamp getActivationDate() {
540 return activationDate;
541 }
542
543 public void setActivationDate(Timestamp activationDate) {
544 this.activationDate = activationDate;
545 }
546
547 public MyColumns getMyColumns() {
548 return myColumns;
549 }
550
551 public void setMyColumns(MyColumns additionalColumns) {
552 this.myColumns = additionalColumns;
553 }
554
555 public String getDestinationUrl() {
556 return destinationUrl;
557 }
558
559 public void setDestinationUrl(String destinationUrl) {
560 this.destinationUrl = destinationUrl;
561 }
562
563 public Timestamp getDeactivationDate() {
564 return deactivationDate;
565 }
566
567 public void setDeactivationDate(Timestamp deactivationDate) {
568 this.deactivationDate = deactivationDate;
569 }
570
571
572
573
574 public Boolean getTemplateRuleInd() {
575 return templateRuleInd;
576 }
577
578
579
580
581 public void setTemplateRuleInd(Boolean templateRuleInd) {
582 this.templateRuleInd = templateRuleInd;
583 }
584
585
586
587
588
589 public String getName() {
590 return name;
591 }
592
593
594
595
596
597 public void setName(String name) {
598 this.name = name;
599 }
600
601 public List<PersonRuleResponsibility> getPersonResponsibilities() {
602 return this.personResponsibilities;
603 }
604
605 public void setPersonResponsibilities(List<PersonRuleResponsibility> personResponsibilities) {
606 this.personResponsibilities = personResponsibilities;
607 }
608
609 public List<GroupRuleResponsibility> getGroupResponsibilities() {
610 return this.groupResponsibilities;
611 }
612
613 public void setGroupResponsibilities(List<GroupRuleResponsibility> groupResponsibilities) {
614 this.groupResponsibilities = groupResponsibilities;
615 }
616
617 public List<RoleRuleResponsibility> getRoleResponsibilities() {
618 return this.roleResponsibilities;
619 }
620
621 public void setRoleResponsibilities(List<RoleRuleResponsibility> roleResponsibilities) {
622 this.roleResponsibilities = roleResponsibilities;
623 }
624
625
626
627
628 public Map<String, String> getFieldValues() {
629 return this.fieldValues;
630 }
631
632
633
634
635 public void setFieldValues(Map<String, String> fieldValues) {
636 this.fieldValues = fieldValues;
637 }
638
639 public String getGroupReviewerName() {
640 return this.groupReviewerName;
641 }
642
643 public String getGroupReviewerNamespace() {
644 return this.groupReviewerNamespace;
645 }
646
647 public String getPersonReviewer() {
648 return this.personReviewer;
649 }
650
651 public void setGroupReviewerName(String groupReviewerName) {
652 this.groupReviewerName = groupReviewerName;
653 }
654
655 public void setGroupReviewerNamespace(String groupReviewerNamespace) {
656 this.groupReviewerNamespace = groupReviewerNamespace;
657 }
658
659 public void setPersonReviewer(String personReviewer) {
660 this.personReviewer = personReviewer;
661 }
662
663
664
665
666
667 public PersonImpl getPersonImpl() {
668 return new PersonImpl();
669 }
670
671 public String getPersonReviewerType() {
672 return this.personReviewerType;
673 }
674
675 public void setPersonReviewerType(String personReviewerType) {
676 this.personReviewerType = personReviewerType;
677 }
678
679
680
681
682
683
684 public static org.kuali.rice.kew.api.rule.Rule to(RuleBaseValues bo) {
685 if (bo == null) {
686 return null;
687 }
688 return org.kuali.rice.kew.api.rule.Rule.Builder.create(bo).build();
689 }
690 }