001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.kew.api.rule;
017    
018    import java.io.Serializable;
019    import java.util.Collection;
020    import javax.xml.bind.annotation.XmlAccessType;
021    import javax.xml.bind.annotation.XmlAccessorType;
022    import javax.xml.bind.annotation.XmlAnyElement;
023    import javax.xml.bind.annotation.XmlElement;
024    import javax.xml.bind.annotation.XmlRootElement;
025    import javax.xml.bind.annotation.XmlType;
026    import org.kuali.rice.core.api.CoreConstants;
027    import org.kuali.rice.core.api.delegation.DelegationType;
028    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
029    import org.kuali.rice.core.api.mo.ModelBuilder;
030    import org.kuali.rice.kew.api.KewApiConstants;
031    import org.w3c.dom.Element;
032    
033    @XmlRootElement(name = RuleDelegation.Constants.ROOT_ELEMENT_NAME)
034    @XmlAccessorType(XmlAccessType.NONE)
035    @XmlType(name = RuleDelegation.Constants.TYPE_NAME, propOrder = {
036        RuleDelegation.Elements.DELEGATION_TYPE,
037        RuleDelegation.Elements.DELEGATION_RULE,
038        CoreConstants.CommonElements.FUTURE_ELEMENTS
039    })
040    public final class RuleDelegation
041        extends AbstractDataTransferObject
042        implements RuleDelegationContract
043    {
044    
045        @XmlElement(name = Elements.DELEGATION_TYPE, required = false)
046        private final String delegationType;
047        @XmlElement(name = Elements.DELEGATION_RULE, required = false)
048        private final Rule delegationRule;
049        @SuppressWarnings("unused")
050        @XmlAnyElement
051        private final Collection<Element> _futureElements = null;
052    
053        /**
054         * Private constructor used only by JAXB.
055         * 
056         */
057        private RuleDelegation() {
058            this.delegationType = null;
059            this.delegationRule = null;
060        }
061    
062        private RuleDelegation(Builder builder) {
063            this.delegationType = builder.getDelegationType() != null ? builder.getDelegationType().getCode() : null;
064            this.delegationRule = builder.getDelegationRule() == null ? null : builder.getDelegationRule().build();
065        }
066    
067        @Override
068        public DelegationType getDelegationType() {
069            return DelegationType.fromCode(this.delegationType);
070        }
071    
072        @Override
073        public Rule getDelegationRule() {
074            return this.delegationRule;
075        }
076    
077    
078        /**
079         * A builder which can be used to construct {@link RuleDelegation} instances.  Enforces the constraints of the {@link RuleDelegationContract}.
080         * 
081         */
082        public final static class Builder
083            implements Serializable, ModelBuilder, RuleDelegationContract
084        {
085    
086            private DelegationType delegationType;
087            private Rule.Builder delegationRule;
088    
089            private Builder() {
090            }
091    
092            public static Builder create() {
093                return new Builder();
094            }
095    
096            public static Builder create(RuleDelegationContract contract) {
097                if (contract == null) {
098                    throw new IllegalArgumentException("contract was null");
099                }
100                Builder builder = create();
101                builder.setDelegationType(contract.getDelegationType());
102                if (contract.getDelegationRule() != null) {
103                    builder.setDelegationRule(Rule.Builder.create(contract.getDelegationRule()));
104                }
105                return builder;
106            }
107    
108            public RuleDelegation build() {
109                return new RuleDelegation(this);
110            }
111    
112            @Override
113            public DelegationType getDelegationType() {
114                return this.delegationType;
115            }
116    
117            @Override
118            public Rule.Builder getDelegationRule() {
119                return this.delegationRule;
120            }
121    
122            public void setDelegationType(DelegationType delegationType) {
123                this.delegationType = delegationType;
124            }
125    
126            public void setDelegationRule(Rule.Builder delegationRule) {
127                this.delegationRule = delegationRule;
128            }
129    
130        }
131    
132    
133        /**
134         * Defines some internal constants used on this class.
135         * 
136         */
137        static class Constants {
138    
139            final static String ROOT_ELEMENT_NAME = "ruleDelegation";
140            final static String TYPE_NAME = "RuleDelegationType";
141    
142        }
143    
144    
145        /**
146         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
147         * 
148         */
149        static class Elements {
150    
151            final static String DELEGATION_TYPE = "delegationType";
152            final static String DELEGATION_RULE = "delegationRule";
153    
154        }
155    
156        public static class Cache {
157            public static final String NAME = KewApiConstants.Namespaces.KEW_NAMESPACE_2_0 + "/" + RuleDelegation.Constants.TYPE_NAME;
158        }
159    }