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