001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.api.validation;
017
018import org.kuali.rice.core.api.CoreConstants;
019import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
020import org.kuali.rice.core.api.mo.ModelBuilder;
021import org.kuali.rice.kew.api.rule.Rule;
022import org.kuali.rice.kew.api.rule.RuleContract;
023import org.kuali.rice.kew.api.rule.RuleDelegation;
024import org.kuali.rice.kew.api.rule.RuleDelegationContract;
025import org.w3c.dom.Element;
026
027import javax.xml.bind.annotation.XmlAccessType;
028import javax.xml.bind.annotation.XmlAccessorType;
029import javax.xml.bind.annotation.XmlAnyElement;
030import javax.xml.bind.annotation.XmlElement;
031import javax.xml.bind.annotation.XmlRootElement;
032import javax.xml.bind.annotation.XmlType;
033import java.io.Serializable;
034import java.util.Collection;
035
036/**
037 * The RuleValidationContext represents the context under which to validate a Rule which is being entered
038 * into the system, be it through the web-based Rule GUI or via an XML import.
039 * 
040 * The ruleAuthor is the UserSession of the individual who is entering or editing the rule.  This may
041 * be <code>null</code> if the rule is being run through validation from the context of an XML rule
042 * import.
043 * 
044 * The RuleDelegation represents the pointer to the rule from it's parent rule's RuleResponsibility.
045 * This will be <code>null</code> if the rule being entered is not a delegation rule.
046 * 
047 * @author Kuali Rice Team (rice.collab@kuali.org)
048 */
049@XmlRootElement(name = RuleValidationContext.Constants.ROOT_ELEMENT_NAME)
050@XmlAccessorType(XmlAccessType.NONE)
051@XmlType(name = RuleValidationContext.Constants.TYPE_NAME, propOrder = {
052    RuleValidationContext.Elements.RULE,
053    RuleValidationContext.Elements.RULE_DELEGATION,
054    RuleValidationContext.Elements.RULE_AUTHOR_PRINCIPAL_ID,
055    CoreConstants.CommonElements.FUTURE_ELEMENTS
056})
057public class RuleValidationContext
058    extends AbstractDataTransferObject
059    implements RuleValidationContextContract {
060
061    @XmlElement(name = Elements.RULE, required = true)
062        private final Rule rule;
063    @XmlElement(name = Elements.RULE_DELEGATION, required = true)
064        private final RuleDelegation ruleDelegation;
065    @XmlElement(name = Elements.RULE_AUTHOR_PRINCIPAL_ID, required = false)
066        private final String ruleAuthorPrincipalId;
067
068    @SuppressWarnings("unused")
069    @XmlAnyElement
070    private final Collection<Element> _futureElements = null;
071
072    /**
073     * Private constructor used only by JAXB.
074     */
075    private RuleValidationContext() {
076        this.rule = null;
077        this.ruleDelegation = null;
078        this.ruleAuthorPrincipalId = null;
079    }
080
081    private RuleValidationContext(Builder builder) {
082        this.rule = builder.getRule().build();
083        if (builder.getRuleDelegation() != null) {
084            this.ruleDelegation = builder.getRuleDelegation().build();
085        } else {
086            this.ruleDelegation = null;
087        }
088        this.ruleAuthorPrincipalId = builder.getRuleAuthorPrincipalId();
089    }
090
091        /**
092         * Retrieve the rule which is being validated.
093         */
094    @Override
095        public Rule getRule() {
096                return rule;
097        }
098
099        /**
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}