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.repository.type;
017
018import java.io.Serializable;
019import java.util.Collection;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAnyElement;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlType;
027
028import org.apache.commons.lang.StringUtils;
029import org.kuali.rice.core.api.CoreConstants;
030import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
031import org.kuali.rice.core.api.mo.ModelBuilder;
032
033/**
034 * Concrete model object implementation of KEW KewTypeAttribute. 
035 * immutable. 
036 * Instances of KewTypeAttribute can be (un)marshalled to and from XML.
037 *
038 */
039@XmlRootElement(name = KewTypeAttribute.Constants.ROOT_ELEMENT_NAME)
040@XmlAccessorType(XmlAccessType.NONE)
041@XmlType(name = KewTypeAttribute.Constants.TYPE_NAME, propOrder = {
042                KewTypeAttribute.Elements.ID,
043                KewTypeAttribute.Elements.TYPE_ID,
044                KewTypeAttribute.Elements.ATTR_DEFN_ID,
045                KewTypeAttribute.Elements.SEQ_NO,
046                KewTypeAttribute.Elements.ACTIVE,
047                KewTypeAttribute.Elements.ATTR_DEFN,
048                CoreConstants.CommonElements.FUTURE_ELEMENTS
049})
050public final class KewTypeAttribute extends AbstractDataTransferObject implements KewTypeAttributeContract {
051        private static final long serialVersionUID = -304265575559412478L;
052        
053        @XmlElement(name = Elements.ID, required=true)
054        private String id;
055        @XmlElement(name = Elements.TYPE_ID, required=true)
056        private String typeId;
057        @XmlElement(name = Elements.ATTR_DEFN_ID, required=true)
058        private String attributeDefinitionId;
059        @XmlElement(name = Elements.SEQ_NO, required=true)
060        private Integer sequenceNumber;
061        @XmlElement(name = Elements.ACTIVE, required=false)
062        private boolean active;
063        @XmlElement(name = Elements.ATTR_DEFN, required=false)
064        private KewAttributeDefinition attributeDefinition;
065        
066        @SuppressWarnings("unused")
067    @XmlAnyElement
068    private final Collection<org.w3c.dom.Element> _futureElements = null;
069        
070         /** 
071     * This constructor should never be called.  It is only present for use during JAXB unmarshalling. 
072     */
073    private KewTypeAttribute() {
074        this.id = null;
075        this.typeId = null;
076        this.attributeDefinitionId = null;
077        this.sequenceNumber = null;
078        this.active = false;
079        this.attributeDefinition = null;
080    }
081    
082    /**
083         * Constructs a KEW KewTypeAttribute from the given builder.  
084         * This constructor is private and should only ever be invoked from the builder.
085         * 
086         * @param builder the Builder from which to construct the KewTypeAttribute
087         */
088    private KewTypeAttribute(Builder builder) {
089        this.id = builder.getId();
090        this.typeId = builder.getTypeId();
091        this.attributeDefinitionId = builder.getAttributeDefinitionId();
092        this.sequenceNumber = builder.getSequenceNumber();
093        this.active = builder.isActive();
094        if (builder.getAttributeDefinition() != null) {
095                this.attributeDefinition = builder.getAttributeDefinition().build();
096        }
097    }
098    
099        public String getId() {
100                return this.id;
101        }
102        
103        public String getTypeId() {
104                return this.typeId;
105        }
106
107        public String getAttributeDefinitionId() {
108                return this.attributeDefinitionId;
109        }
110
111        public Integer getSequenceNumber() {
112                return this.sequenceNumber;
113        }
114        
115        public boolean isActive() {
116                return this.active; 
117        }
118
119        public KewAttributeDefinition getAttributeDefinition() {
120                return this.attributeDefinition;
121        }
122        
123        /**
124     * This builder is used to construct instances of KewTypeAttribute.  
125     */
126    public static class Builder implements KewTypeAttributeContract, ModelBuilder, Serializable {
127                private static final long serialVersionUID = 2729964674427296346L;
128
129                private String id;
130        private String typeId;
131        private String attributeDefinitionId;
132        private Integer sequenceNumber;
133        private boolean active;
134        private KewAttributeDefinition.Builder attributeDefinition;
135
136                /**
137                 * Private constructor for creating a builder with all of it's required attributes.
138                 */
139        private Builder(String id, String typeId, String attributeDefinitionId, Integer sequenceNumber) {
140            setId(id);
141            setTypeId(typeId);
142            setAttributeDefinitionId(attributeDefinitionId);
143            setSequenceNumber(sequenceNumber);
144                        setActive(true);
145        }
146
147        public Builder attributeDefinition(KewAttributeDefinition.Builder attributeDefinition){
148                setAttributeDefinition(attributeDefinition);
149                return this;
150        }
151        
152        /**
153         * Creates a builder from the given parameters.
154         * 
155         * @param id the KewTypeAtribute id
156         * @param typeId the KewType Id 
157         * @param attributeDefinitionId The attributeDefinitionId
158         * @param sequenceNumber 
159         * @return an instance of the builder with the fields already populated
160         * @throws IllegalArgumentException if the either the id, name or namespace is null or blank
161         */
162        public static Builder create(String id, String typeId, String attributeDefinitionId, Integer sequenceNumber) {
163            return new Builder(id, typeId, attributeDefinitionId, sequenceNumber);
164        }
165        
166        public static Builder create(KewTypeAttributeContract contract){
167                if (contract == null) {
168                throw new IllegalArgumentException("contract is null");
169            }
170                Builder builder = new Builder(contract.getId(), 
171                                contract.getTypeId(),
172                                contract.getAttributeDefinitionId(),
173                                contract.getSequenceNumber());
174                if (contract.getAttributeDefinition() != null){
175                        KewAttributeDefinition.Builder attrBuilder = 
176                                KewAttributeDefinition.Builder.create(contract.getAttributeDefinition());
177                        builder.setAttributeDefinition(attrBuilder);
178                }
179                return builder;
180        }
181
182                /**
183                 * Sets the value of the id on this builder to the given value.
184                 * 
185                 * @param id the id value to set, must  be null or non-blank
186                 * @throws IllegalArgumentException if the id is not null and blank
187                 */
188        public void setId(String id) {
189            if (null != id && StringUtils.isBlank(id)) {
190                throw new IllegalArgumentException("id must be null or non-blank");
191            }
192            this.id = id;
193        }
194
195                public void setTypeId(String typeId) {
196            if (null != typeId && StringUtils.isBlank(typeId)) {
197                throw new IllegalArgumentException("typeId must be null or non-blank");
198            }
199                        this.typeId = typeId;
200                }
201
202                public void setAttributeDefinitionId(String attributeDefinitionId) {
203            if (StringUtils.isBlank(attributeDefinitionId)) {
204                throw new IllegalArgumentException("the attribute definition id is blank");
205            }
206                        this.attributeDefinitionId = attributeDefinitionId;
207                }
208                
209                public void setSequenceNumber(Integer sequenceNumber) {
210                        if (sequenceNumber == null){
211                                 throw new IllegalArgumentException("the sequence number is null");
212                        }
213                        this.sequenceNumber = sequenceNumber;
214                }
215                
216                public void setAttributeDefinition(KewAttributeDefinition.Builder attributeDefinition) {
217                        this.attributeDefinition = attributeDefinition;
218                        //TODO: verify that the attributeDefinitionID field matches the id field in the builder
219                }
220                
221                public void setActive(boolean active) {
222                        this.active = active;
223                }
224
225                public String getId() {
226                        return id;
227                }
228
229                public String getTypeId() {
230                        return typeId;
231                }
232
233                public String getAttributeDefinitionId() {
234                        return attributeDefinitionId;
235                }
236                
237                public Integer getSequenceNumber() {
238                        return sequenceNumber;
239                }
240
241                public KewAttributeDefinition.Builder getAttributeDefinition() {
242                        return attributeDefinition;
243                }
244
245
246                public boolean isActive() {
247                        return active;
248                }
249
250                /**
251                 * Builds an instance of a KewTypeAttribute based on the current state of the builder.
252                 * 
253                 * @return the fully-constructed KewTypeAttribute
254                 */
255        @Override
256        public KewTypeAttribute build() {
257            return new KewTypeAttribute(this);
258        }
259                
260    }
261
262        /**
263         * Defines some internal constants used on this class.
264         */
265        static class Constants {
266                final static String ROOT_ELEMENT_NAME = "KewTypeAttribute";
267                final static String TYPE_NAME = "KewTypeAttributeType";
268        }
269        
270        /**
271         * A private class which exposes constants which define the XML element names to use
272         * when this object is marshalled to XML.
273         */
274        public static class Elements {
275                final static String ID = "id";
276                final static String TYPE_ID = "typeId";
277                final static String ATTR_DEFN_ID = "attributeDefinitionId";
278                final static String SEQ_NO = "sequenceNumber";
279                final static String ACTIVE = "active";
280                final static String ATTR_DEFN = "attributeDefinition";
281        }
282}