View Javadoc

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