View Javadoc

1   /**
2    * Copyright 2005-2015 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.api.validation;
17  
18  import org.kuali.rice.core.api.CoreConstants;
19  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
20  import org.kuali.rice.core.api.mo.ModelBuilder;
21  import org.kuali.rice.kew.api.rule.Rule;
22  import org.kuali.rice.kew.api.rule.RuleContract;
23  import org.kuali.rice.kew.api.rule.RuleDelegation;
24  import org.kuali.rice.kew.api.rule.RuleDelegationContract;
25  import org.w3c.dom.Element;
26  
27  import javax.xml.bind.annotation.XmlAccessType;
28  import javax.xml.bind.annotation.XmlAccessorType;
29  import javax.xml.bind.annotation.XmlAnyElement;
30  import javax.xml.bind.annotation.XmlElement;
31  import javax.xml.bind.annotation.XmlRootElement;
32  import javax.xml.bind.annotation.XmlType;
33  import java.io.Serializable;
34  import java.util.Collection;
35  
36  /**
37   * The RuleValidationContext represents the context under which to validate a Rule which is being entered
38   * into the system, be it through the web-based Rule GUI or via an XML import.
39   * 
40   * The ruleAuthor is the UserSession of the individual who is entering or editing the rule.  This may
41   * be <code>null</code> if the rule is being run through validation from the context of an XML rule
42   * import.
43   * 
44   * The RuleDelegation represents the pointer to the rule from it's parent rule's RuleResponsibility.
45   * This will be <code>null</code> if the rule being entered is not a delegation rule.
46   * 
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  @XmlRootElement(name = RuleValidationContext.Constants.ROOT_ELEMENT_NAME)
50  @XmlAccessorType(XmlAccessType.NONE)
51  @XmlType(name = RuleValidationContext.Constants.TYPE_NAME, propOrder = {
52      RuleValidationContext.Elements.RULE,
53      RuleValidationContext.Elements.RULE_DELEGATION,
54      RuleValidationContext.Elements.RULE_AUTHOR_PRINCIPAL_ID,
55      CoreConstants.CommonElements.FUTURE_ELEMENTS
56  })
57  public class RuleValidationContext
58      extends AbstractDataTransferObject
59      implements RuleValidationContextContract {
60  
61      @XmlElement(name = Elements.RULE, required = true)
62  	private final Rule rule;
63      @XmlElement(name = Elements.RULE_DELEGATION, required = true)
64  	private final RuleDelegation ruleDelegation;
65      @XmlElement(name = Elements.RULE_AUTHOR_PRINCIPAL_ID, required = false)
66  	private final String ruleAuthorPrincipalId;
67  
68      @SuppressWarnings("unused")
69      @XmlAnyElement
70      private final Collection<Element> _futureElements = null;
71  
72      /**
73       * Private constructor used only by JAXB.
74       */
75      private RuleValidationContext() {
76          this.rule = null;
77          this.ruleDelegation = null;
78          this.ruleAuthorPrincipalId = null;
79      }
80  
81      private RuleValidationContext(Builder builder) {
82          this.rule = builder.getRule().build();
83          if (builder.getRuleDelegation() != null) {
84              this.ruleDelegation = builder.getRuleDelegation().build();
85          } else {
86              this.ruleDelegation = null;
87          }
88          this.ruleAuthorPrincipalId = builder.getRuleAuthorPrincipalId();
89      }
90  
91  	/**
92  	 * Retrieve the rule which is being validated.
93  	 */
94      @Override
95  	public Rule getRule() {
96  		return rule;
97  	}
98  
99  	/**
100 	 * Retrieve the principal id of the individual entering the rule into the system.  May be null in the
101 	 * case of an XML rule import. 
102 	 */
103     @Override
104 	public String getRuleAuthorPrincipalId() {
105 		return ruleAuthorPrincipalId;
106 	}
107 
108 	/**
109 	 * Retrieve the RuleDelegation representing the parent of the rule being validated.  If the rule is
110 	 * not a delegation rule, then this will return null;
111 	 */
112     @Override
113 	public RuleDelegation getRuleDelegation() {
114 		return ruleDelegation;
115 	}
116 
117     /**
118      * A builder which can be used to construct {@link RuleValidationContext} instances.  Enforces the constraints of the {@link org.kuali.rice.kew.api.validation.RuleValidationContextContract}.
119      *
120      */
121     public final static class Builder
122         implements Serializable, ModelBuilder, RuleValidationContextContract
123     {
124 
125         private Rule.Builder rule;
126 	    private RuleDelegation.Builder ruleDelegation;
127         private String ruleAuthorPrincipalId;
128 
129         private Builder() {
130         }
131 
132         public static Builder create(RuleContract rule) {
133             if (rule == null) {
134                 throw new IllegalArgumentException("contract was null");
135             }
136             Builder builder = new Builder();
137             builder.setRule(Rule.Builder.create(rule));
138             return builder;
139         }
140 
141         public static Builder create(RuleValidationContextContract contract) {
142             return Builder.create(contract.getRule(), contract.getRuleDelegation(), contract.getRuleAuthorPrincipalId());
143         }
144 
145         /**
146          * Construct a RuleValidationContext under which to validate a rule.  The rule must be non-null, the delegation
147          * and author can be <code>null</code> given the circumstances defined in the description of this class.
148          */
149         public static Builder create(RuleContract rule, RuleDelegationContract ruleDelegation, String ruleAuthorPrincipalId) {
150             Builder builder = Builder.create(rule);
151             if (ruleDelegation != null) {
152                 builder.setRuleDelegation(RuleDelegation.Builder.create(ruleDelegation));
153             }
154             builder.setRuleAuthorPrincipalId(ruleAuthorPrincipalId);
155             return builder;
156         }
157 
158         public RuleValidationContext build() {
159             return new RuleValidationContext(this);
160         }
161 
162         @Override
163         public Rule.Builder getRule() {
164             return this.rule;
165         }
166 
167         @Override
168         public RuleDelegation.Builder getRuleDelegation() {
169             return this.ruleDelegation;
170         }
171 
172         @Override
173         public String getRuleAuthorPrincipalId() {
174             return this.ruleAuthorPrincipalId;
175         }
176 
177         public void setRule(Rule.Builder rule) {
178             this.rule = rule;
179         }
180 
181         public void setRuleDelegation(RuleDelegation.Builder ruleDelegation) {
182             this.ruleDelegation = ruleDelegation;
183         }
184 
185         public void setRuleAuthorPrincipalId(String ruleAuthorPrincipalId) {
186             this.ruleAuthorPrincipalId = ruleAuthorPrincipalId;
187         }
188 
189     }
190 
191     /**
192      * Defines some internal constants used on this class.
193      */
194     static class Constants {
195         final static String ROOT_ELEMENT_NAME = "ruleValidationContext";
196         final static String TYPE_NAME = "RuleValidationContextType";
197     }
198 
199     /**
200      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
201      */
202     static class Elements {
203         final static String RULE = "rule";
204         final static String RULE_DELEGATION = "ruleDelegation";
205         final static String RULE_AUTHOR_PRINCIPAL_ID = "ruleAuthorPrincipalId";
206     }
207 }