View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  /*import org.kuali.rice.kim.api.group.Group;*/
63  
64  
65  /**
66   * A model bean for a Rule within the KEW rules engine.
67   *
68   * @author Kuali Rice Team (rice.collab@kuali.org)
69   */
70  @Entity
71  @Table(name="KREW_RULE_T")
72  //@Sequence(name="KREW_RTE_TMPL_S", property="id")
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       * Unique Rule name
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      * Indicator that signifies that this rule is a defaults/template rule which contains
133      * template-defined rule defaults for other rules which use the associated template
134      */
135     @Column(name="TMPL_RULE_IND")
136     private Boolean templateRuleInd = Boolean.FALSE;
137 
138     // required to be lookupable
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         /*personResponsibilities = new AutoPopulatingList<PersonRuleResponsibility>(PersonRuleResponsibility.class);
166         groupResponsibilities = new AutoPopulatingList<GroupRuleResponsibility>(GroupRuleResponsibility.class);
167         roleResponsibilities = new AutoPopulatingList<RoleRuleResponsibility>(RoleRuleResponsibility.class);*/
168         fieldValues = new HashMap<String, String>();
169     }
170 
171     /**
172      * @return the rule expression definition for this rule, if defined
173      */
174     public RuleExpressionDef getRuleExpressionDef() {
175         return ruleExpressionDef;
176     }
177 
178     /**
179      * @param ruleExpressionDef the rule expression definition to set for this rule
180      */
181     public void setRuleExpressionDef(RuleExpressionDef ruleExpressionDef) {
182         this.ruleExpressionDef = ruleExpressionDef;
183     }
184 
185     public String getRuleTemplateName() {
186         if (ruleTemplate != null) {
187             return ruleTemplate.getName();
188         }
189         return null;
190     }
191 
192     public RuleBaseValues getPreviousVersion() {
193         if (previousVersion == null && previousRuleId != null) {
194             RuleServiceInternal ruleService = (RuleServiceInternal) KEWServiceLocator.getService(KEWServiceLocator.RULE_SERVICE);
195             return ruleService.findRuleBaseValuesById(previousRuleId);
196         }
197         return previousVersion;
198     }
199 
200     public void setPreviousVersion(RuleBaseValues previousVersion) {
201         this.previousVersion = previousVersion;
202     }
203 
204     public RuleResponsibilityBo getResponsibility(int index) {
205         while (getRuleResponsibilities().size() <= index) {
206             RuleResponsibilityBo ruleResponsibility = new RuleResponsibilityBo();
207             ruleResponsibility.setRuleBaseValues(this);
208             getRuleResponsibilities().add(ruleResponsibility);
209         }
210         return (RuleResponsibilityBo) getRuleResponsibilities().get(index);
211     }
212 
213     public RuleExtensionBo getRuleExtension(int index) {
214         while (getRuleExtensions().size() <= index) {
215             getRuleExtensions().add(new RuleExtensionBo());
216         }
217         return (RuleExtensionBo) getRuleExtensions().get(index);
218     }
219 
220     public RuleExtensionValue getRuleExtensionValue(String key) {
221         for (Iterator iter = getRuleExtensions().iterator(); iter.hasNext();) {
222             RuleExtensionBo ruleExtension = (RuleExtensionBo) iter.next();
223             for (Iterator iterator = ruleExtension.getExtensionValues().iterator(); iterator.hasNext();) {
224                 RuleExtensionValue ruleExtensionValue = (RuleExtensionValue) iterator.next();
225                 if (ruleExtensionValue.getKey().equals(key)) {
226                     return ruleExtensionValue;
227                 }
228             }
229         }
230         return null;
231     }
232 
233     public RuleExtensionValue getRuleExtensionValue(String ruleTemplateAttributeId, String key) {
234         for (Iterator iter = getRuleExtensions().iterator(); iter.hasNext();) {
235             RuleExtensionBo ruleExtension = (RuleExtensionBo) iter.next();
236             if (ruleExtension.getRuleTemplateAttributeId().equals(ruleTemplateAttributeId)) {
237                 for (Iterator iterator = ruleExtension.getExtensionValues().iterator(); iterator.hasNext();) {
238                     RuleExtensionValue ruleExtensionValue = (RuleExtensionValue) iterator.next();
239                     if (ruleExtensionValue.getKey().equals(key)) {
240                         return ruleExtensionValue;
241                     }
242                 }
243             }
244         }
245         return null;
246     }
247 
248     public String getPreviousRuleId() {
249         return previousRuleId;
250     }
251 
252     public void setPreviousRuleId(String previousVersion) {
253         this.previousRuleId = previousVersion;
254     }
255 
256     public void addRuleResponsibility(RuleResponsibilityBo ruleResponsibility) {
257         addRuleResponsibility(ruleResponsibility, new Integer(getRuleResponsibilities().size()));
258     }
259 
260     public void addRuleResponsibility(RuleResponsibilityBo ruleResponsibility, Integer counter) {
261         boolean alreadyAdded = false;
262         int location = 0;
263         if (counter != null) {
264             for (RuleResponsibilityBo ruleResponsibilityRow : getRuleResponsibilities()) {
265                 if (counter.intValue() == location) {
266                     ruleResponsibilityRow.setPriority(ruleResponsibility.getPriority());
267                     ruleResponsibilityRow.setActionRequestedCd(ruleResponsibility.getActionRequestedCd());
268                     ruleResponsibilityRow.setVersionNumber(ruleResponsibility.getVersionNumber());
269                     ruleResponsibilityRow.setRuleBaseValuesId(ruleResponsibility.getRuleBaseValuesId());
270                     ruleResponsibilityRow.setRuleResponsibilityName(ruleResponsibility.getRuleResponsibilityName());
271                     ruleResponsibilityRow.setRuleResponsibilityType(ruleResponsibility.getRuleResponsibilityType());
272                     //ruleResponsibilityRow.setDelegationRules(ruleResponsibility.getDelegationRules());
273                     ruleResponsibilityRow.setApprovePolicy(ruleResponsibility.getApprovePolicy());
274                     alreadyAdded = true;
275                 }
276                 location++;
277             }
278         }
279         if (!alreadyAdded) {
280             getRuleResponsibilities().add(ruleResponsibility);
281         }
282     }
283 
284     public RuleTemplateBo getRuleTemplate() {
285         return ruleTemplate;
286     }
287 
288     public void setRuleTemplate(RuleTemplateBo ruleTemplate) {
289         this.ruleTemplate = ruleTemplate;
290     }
291 
292     public String getRuleTemplateId() {
293         return ruleTemplateId;
294     }
295 
296     public void setRuleTemplateId(String ruleTemplateId) {
297         this.ruleTemplateId = ruleTemplateId;
298     }
299 
300     public DocumentType getDocumentType() {
301     	return KEWServiceLocator.getDocumentTypeService().findByName(getDocTypeName());
302     }
303 
304     public String getDocTypeName() {
305         return docTypeName;
306     }
307 
308     public void setDocTypeName(String docTypeName) {
309         this.docTypeName = docTypeName;
310     }
311 
312     public List<RuleExtensionBo> getRuleExtensions() {
313         return ruleExtensions;
314     }
315 
316     public Map<String, String> getRuleExtensionMap() {
317         Map<String, String> extensions = new HashMap<String, String>();
318         for (RuleExtensionBo ext : this.getRuleExtensions()) {
319             for (RuleExtensionValue value : ext.getExtensionValues()) {
320                 extensions.put(value.getKey(), value.getValue());
321             }
322         }
323         return extensions;
324     }
325 
326     public void setRuleExtensions(List<RuleExtensionBo> ruleExtensions) {
327         this.ruleExtensions = ruleExtensions;
328     }
329 
330     public List<RuleResponsibilityBo> getRuleResponsibilities() {
331         return this.ruleResponsibilities;
332     }
333 
334     public void setRuleResponsibilities(List<RuleResponsibilityBo> ruleResponsibilities) {
335         this.ruleResponsibilities = ruleResponsibilities;
336     }
337 
338     public RuleResponsibilityBo getResponsibility(Long ruleResponsibilityKey) {
339         for (Iterator iterator = getRuleResponsibilities().iterator(); iterator.hasNext();) {
340             RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iterator.next();
341             if (responsibility.getId() != null
342                     && responsibility.getId().equals(ruleResponsibilityKey)) {
343                 return responsibility;
344             }
345         }
346         return null;
347     }
348 
349     public void removeResponsibility(int index) {
350         getRuleResponsibilities().remove(index);
351     }
352 
353     @Override
354     public boolean isActive() {
355         return active;
356     }
357 
358     public void setActive(boolean active) {
359         this.active = active;
360     }
361 
362     public String getActiveIndDisplay() {
363         return CodeTranslator.getActiveIndicatorLabel(isActive());
364     }
365 
366     public Boolean getCurrentInd() {
367         return currentInd;
368     }
369 
370     public void setCurrentInd(Boolean currentInd) {
371         this.currentInd = currentInd;
372     }
373 
374     public Timestamp getFromDateValue() {
375         return fromDateValue;
376     }
377     
378     @Override
379     public DateTime getFromDate() {
380         if (this.fromDateValue == null) {
381             return null;
382         }
383         return new DateTime(this.fromDateValue.getTime());
384     }
385 
386     public void setFromDateValue(Timestamp fromDateValue) {
387         this.fromDateValue = fromDateValue;
388     }
389 
390     public String getDescription() {
391         return description;
392     }
393 
394     public void setDescription(String description) {
395         this.description = description;
396     }
397 
398     public String getId() {
399         return id;
400     }
401 
402     public void setId(String id) {
403         this.id = id;
404     }
405 
406     public Timestamp getToDateValue() {
407         return toDateValue;
408     }
409     
410     @Override
411     public DateTime getToDate() {
412         if (this.toDateValue == null) {
413             return null;
414         }
415         return new DateTime(this.toDateValue.getTime());
416     }
417 
418     public void setToDateValue(Timestamp toDateValue) {
419         this.toDateValue = toDateValue;
420     }
421 
422     public Integer getVersionNbr() {
423         return versionNbr;
424     }
425 
426     public void setVersionNbr(Integer versionNbr) {
427         this.versionNbr = versionNbr;
428     }
429 
430     public String getReturnUrl() {
431         return returnUrl;
432     }
433 
434     public void setReturnUrl(String returnUrl) {
435         this.returnUrl = returnUrl;
436     }
437 
438     public String getFromDateString() {
439         if (this.fromDateValue != null) {
440             return RiceConstants.getDefaultDateFormat().format(this.fromDateValue);
441         }
442         return null;
443     }
444 
445     public String getToDateString() {
446         if (this.toDateValue != null) {
447             return RiceConstants.getDefaultDateFormat().format(this.toDateValue);
448         }
449         return null;
450     }
451 
452     @Override
453     public boolean isForceAction() {
454         return forceAction;
455     }
456 
457     public void setForceAction(boolean forceAction) {
458         this.forceAction = forceAction;
459     }
460 
461     public boolean isActive(Date date) {
462     	boolean isAfterFromDate = getFromDateValue() == null || date.after(getFromDateValue());
463     	boolean isBeforeToDate = getToDateValue() == null || date.before(getToDateValue());
464     	return isActive() && isAfterFromDate && isBeforeToDate;
465     }
466 
467     public boolean isMatch(DocumentContent docContent) {
468         for (RuleTemplateAttributeBo ruleTemplateAttribute : getRuleTemplate().getActiveRuleTemplateAttributes()) {
469             if (!ruleTemplateAttribute.isWorkflowAttribute()) {
470                 continue;
471             }
472             WorkflowRuleAttribute routingAttribute = (WorkflowRuleAttribute) ruleTemplateAttribute.getWorkflowAttribute();
473 
474             RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
475             if (ruleAttribute.getType().equals(KewApiConstants.RULE_XML_ATTRIBUTE_TYPE)) {
476                 ((GenericXMLRuleAttribute) routingAttribute).setExtensionDefinition(RuleAttribute.to(ruleAttribute));
477             }
478             String className = ruleAttribute.getResourceDescriptor();
479             List<RuleExtension> editedRuleExtensions = new ArrayList<RuleExtension>();
480             for (RuleExtensionBo extension : getRuleExtensions()) {
481                 if (extension.getRuleTemplateAttribute().getRuleAttribute().getResourceDescriptor().equals(className)) {
482                     editedRuleExtensions.add(RuleExtensionBo.to(extension));
483                 }
484             }
485             if (!routingAttribute.isMatch(docContent, editedRuleExtensions)) {
486                 return false;
487             }
488         }
489         return true;
490     }
491 
492     public RuleResponsibilityBo findResponsibility(String roleName) {
493         for (Iterator iter = getRuleResponsibilities().iterator(); iter.hasNext();) {
494             RuleResponsibilityBo resp = (RuleResponsibilityBo) iter.next();
495             if (KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID.equals(resp.getRuleResponsibilityType())
496                     && roleName.equals(resp.getRuleResponsibilityName())) {
497                 return resp;
498             }
499         }
500         return null;
501     }
502 
503     public String getDocumentId() {
504         return documentId;
505     }
506 
507     public void setDocumentId(String documentId) {
508         this.documentId = documentId;
509     }
510 
511     public Boolean getDelegateRule() {
512         return delegateRule;
513     }
514 
515     public void setDelegateRule(Boolean isDelegateRule) {
516         this.delegateRule = isDelegateRule;
517     }
518 
519     public Timestamp getActivationDate() {
520         return activationDate;
521     }
522 
523     public void setActivationDate(Timestamp activationDate) {
524         this.activationDate = activationDate;
525     }
526 
527     public MyColumns getMyColumns() {
528         return myColumns;
529     }
530 
531     public void setMyColumns(MyColumns additionalColumns) {
532         this.myColumns = additionalColumns;
533     }
534 
535     public String getDestinationUrl() {
536         return destinationUrl;
537     }
538 
539     public void setDestinationUrl(String destinationUrl) {
540         this.destinationUrl = destinationUrl;
541     }
542 
543     public Timestamp getDeactivationDate() {
544         return deactivationDate;
545     }
546 
547     public void setDeactivationDate(Timestamp deactivationDate) {
548         this.deactivationDate = deactivationDate;
549     }
550 
551     /**
552      * @return whether this is a defaults/template rule
553      */
554     public Boolean getTemplateRuleInd() {
555         return templateRuleInd;
556     }
557 
558     /**
559      * @param templateRuleInd whether this is a defaults/template rule
560      */
561     public void setTemplateRuleInd(Boolean templateRuleInd) {
562         this.templateRuleInd = templateRuleInd;
563     }
564 
565     /**
566      * Get the rule name
567      * @return the rule name
568      */
569     public String getName() {
570         return name;
571     }
572 
573     /**
574      * Set the rule name
575      * @param name the rule name
576      */
577     public void setName(String name) {
578         this.name = name;
579     }
580 
581 	public List<PersonRuleResponsibility> getPersonResponsibilities() {
582 		return this.personResponsibilities;
583 	}
584 
585 	public void setPersonResponsibilities(List<PersonRuleResponsibility> personResponsibilities) {
586 		this.personResponsibilities = personResponsibilities;
587 	}
588 
589 	public List<GroupRuleResponsibility> getGroupResponsibilities() {
590 		return this.groupResponsibilities;
591 	}
592 
593 	public void setGroupResponsibilities(List<GroupRuleResponsibility> groupResponsibilities) {
594 		this.groupResponsibilities = groupResponsibilities;
595 	}
596 
597 	public List<RoleRuleResponsibility> getRoleResponsibilities() {
598 		return this.roleResponsibilities;
599 	}
600 
601 	public void setRoleResponsibilities(List<RoleRuleResponsibility> roleResponsibilities) {
602 		this.roleResponsibilities = roleResponsibilities;
603 	}
604 
605 	/**
606 	 * @return the fieldValues
607 	 */
608 	public Map<String, String> getFieldValues() {
609 		return this.fieldValues;
610 	}
611 
612 	/**
613 	 * @param fieldValues the fieldValues to set
614 	 */
615 	public void setFieldValues(Map<String, String> fieldValues) {
616 		this.fieldValues = fieldValues;
617 	}
618 
619     public String getGroupReviewerName() {
620         return this.groupReviewerName;
621     }
622 
623     public String getGroupReviewerNamespace() {
624         return this.groupReviewerNamespace;
625     }
626 
627     public String getPersonReviewer() {
628         return this.personReviewer;
629     }
630 
631     public void setGroupReviewerName(String groupReviewerName) {
632         this.groupReviewerName = groupReviewerName;
633     }
634 
635     public void setGroupReviewerNamespace(String groupReviewerNamespace) {
636         this.groupReviewerNamespace = groupReviewerNamespace;
637     }
638 
639     public void setPersonReviewer(String personReviewer) {
640         this.personReviewer = personReviewer;
641     }
642 
643     /*public Group getKimGroupImpl() {
644         return new GroupImpl;
645     }*/
646 
647     public PersonImpl getPersonImpl() {
648         return new PersonImpl();
649     }
650 
651     public String getPersonReviewerType() {
652         return this.personReviewerType;
653     }
654 
655     public void setPersonReviewerType(String personReviewerType) {
656         this.personReviewerType = personReviewerType;
657     }
658 
659         /**
660      * Converts a mutable bo to its immutable counterpart
661      * @param bo the mutable business object
662      * @return the immutable object
663      */
664     public static org.kuali.rice.kew.api.rule.Rule to(RuleBaseValues bo) {
665         if (bo == null) {
666             return null;
667         }
668         return org.kuali.rice.kew.api.rule.Rule.Builder.create(bo).build();
669     }
670 }