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.krms.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 KRMS {@link KrmsTypeAttributeContract}
035 * <p>immutable. To construct an instance of a KrmsTypeAttribute, use the {@link KrmsTypeAttribute.Builder} class.
036 * Instances of KrmsTypeAttribute can be (un)marshalled to and from XML.</p>
037 *
038 */
039@XmlRootElement(name = KrmsTypeAttribute.Constants.ROOT_ELEMENT_NAME)
040@XmlAccessorType(XmlAccessType.NONE)
041@XmlType(name = KrmsTypeAttribute.Constants.TYPE_NAME, propOrder = {
042                KrmsTypeAttribute.Elements.ID,
043                KrmsTypeAttribute.Elements.TYPE_ID,
044                KrmsTypeAttribute.Elements.ATTR_DEFN_ID,
045                KrmsTypeAttribute.Elements.SEQ_NO,
046                KrmsTypeAttribute.Elements.ACTIVE,
047        CoreConstants.CommonElements.VERSION_NUMBER,
048                CoreConstants.CommonElements.FUTURE_ELEMENTS
049})
050public final class KrmsTypeAttribute extends AbstractDataTransferObject implements KrmsTypeAttributeContract {
051        private static final long serialVersionUID = -304265575559412478L;
052        
053        @XmlElement(name = Elements.ID, required = false)
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 = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
064    private final Long versionNumber;
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 KrmsTypeAttribute() {
074        this.id = null;
075        this.typeId = null;
076        this.attributeDefinitionId = null;
077        this.sequenceNumber = null;
078        this.active = true;
079        this.versionNumber = null;
080    }
081    
082    /**
083         * Constructs a KRMS KrmsTypeAttribute from the given builder.  
084         * <p>This constructor is private and should only ever be invoked from the builder.</p>
085         * 
086         * @param builder the Builder from which to construct the KrmsTypeAttribute
087         */
088    private KrmsTypeAttribute(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        this.versionNumber = builder.getVersionNumber();
095    }
096
097    @Override
098        public String getId() {
099                return this.id;
100        }
101        
102    @Override
103    public String getTypeId() {
104                return this.typeId;
105        }
106
107    @Override
108        public String getAttributeDefinitionId() {
109                return this.attributeDefinitionId;
110        }
111
112    @Override
113        public Integer getSequenceNumber() {
114                return this.sequenceNumber;
115        }
116        
117    @Override
118        public boolean isActive() {
119                return this.active; 
120        }
121
122    @Override
123    public Long getVersionNumber() {
124        return versionNumber;
125    }
126
127        /**
128     * This builder is used to construct instances of KrmsTypeAttribute.  
129     */
130    public static class Builder implements KrmsTypeAttributeContract, ModelBuilder, Serializable {
131                private static final long serialVersionUID = 2729964674427296346L;
132
133                private String id;
134        private String typeId;
135        private String attributeDefinitionId;
136        private Integer sequenceNumber;
137        private boolean active;
138        private Long versionNumber;
139
140                /**
141                 * Private constructor for creating a builder with all of it's required attributes.
142                 */
143        private Builder(String typeId, String attributeDefinitionId, Integer sequenceNumber) {
144            setTypeId(typeId);
145            setAttributeDefinitionId(attributeDefinitionId);
146            setSequenceNumber(sequenceNumber);
147                        setActive(true);
148        }
149
150        /**
151         * Creates a builder from the given parameters.
152         * 
153         * @param typeId the KrmsType Id
154         * @param attributeDefinitionId The attributeDefinitionId
155         * @param sequenceNumber 
156         * @return an instance of the builder with the fields already populated
157         * @throws IllegalArgumentException if the either the id, name or namespace is null or blank
158         */
159        public static Builder create(String typeId, String attributeDefinitionId, Integer sequenceNumber) {
160            return new Builder(typeId, attributeDefinitionId, sequenceNumber);
161        }
162
163        /**
164         * Creates a builder by populating it with data from the given {@link KrmsTypeAttributeContract}.
165         *
166         * @param contract the contract from which to populate this builder
167         * @return an instance of the builder populated with data from the contract
168         * @throws IllegalArgumentException if the contract is null
169         */
170        public static Builder create(KrmsTypeAttributeContract contract){
171                if (contract == null) {
172                throw new IllegalArgumentException("contract is null");
173            }
174                Builder builder = new Builder(contract.getTypeId(),
175                                contract.getAttributeDefinitionId(),
176                                contract.getSequenceNumber());
177            builder.setId(contract.getId());
178            builder.setVersionNumber(contract.getVersionNumber());
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; can be null; a null id is an indicator
186         * the this has not yet been persisted to the database.
187                 */
188        public void setId(String id) {
189            this.id = id;
190        }
191
192        /**
193         * Sets the id of the KrmsTypeDefinition to which this attribute belongs.
194         * @param typeId the id of the KrmsTypeDefinition; may not be null or blank
195         * @throws IllegalArgumentException if the typeId is null or blank
196         *
197         */
198                public void setTypeId(String typeId) {
199            if (null != typeId && StringUtils.isBlank(typeId)) {
200                throw new IllegalArgumentException("typeId must be null or non-blank");
201            }
202                        this.typeId = typeId;
203                }
204
205        /**
206         * Sets the id of the KrmsAttributeDefinition that describes this attribute.
207         * @param attributeDefinitionId id of the KrmsAttributeDefinition; may not be null or blank
208         * @throws IllegalArgumentException if the attributeDefinitionId is null or blank
209         *
210         */
211                public void setAttributeDefinitionId(String attributeDefinitionId) {
212            if (StringUtils.isBlank(attributeDefinitionId)) {
213                throw new IllegalArgumentException("the attribute definition id is blank");
214            }
215                        this.attributeDefinitionId = attributeDefinitionId;
216                }
217
218        /**
219         * Sets the sequence number. This represents the order of the attributes
220         * within the KrmsTypeDefinition.
221         * @param sequenceNumber the order of the attribute in the attribute list; cannot be null
222         * @throws IllegalArgumentException if the sequenceNumber is null
223         */
224                public void setSequenceNumber(Integer sequenceNumber) {
225                        if (sequenceNumber == null){
226                                 throw new IllegalArgumentException("the sequence number is null");
227                        }
228                        this.sequenceNumber = sequenceNumber;
229                }
230
231        /**
232         * sets the active indicator value
233         * @param active boolean value to set
234         */
235                public void setActive(boolean active) {
236                        this.active = active;
237                }
238
239        /**
240         * Sets the version number for this object.
241         * <p>In general, this value should only
242         * be null if the object has not yet been stored to a persistent data store.
243         * This version number is generally used for the purposes of optimistic locking.</p>
244         * @param versionNumber the version number, or null if one has not been assigned yet
245         */
246        public void setVersionNumber(Long versionNumber){
247            this.versionNumber = versionNumber;
248        }
249
250        @Override
251                public String getId() {
252                        return id;
253                }
254
255        @Override
256                public String getTypeId() {
257                        return typeId;
258                }
259
260        @Override
261                public String getAttributeDefinitionId() {
262                        return attributeDefinitionId;
263                }
264                
265        @Override
266                public Integer getSequenceNumber() {
267                        return sequenceNumber;
268                }
269
270        @Override
271                public boolean isActive() {
272                        return active;
273                }
274
275        @Override
276        public Long getVersionNumber() {
277            return versionNumber;
278        }
279                /**
280                 * Builds an instance of a KrmsTypeAttribute based on the current state of the builder.
281                 * 
282                 * @return the fully-constructed KrmsTypeAttribute
283                 */
284        @Override
285        public KrmsTypeAttribute build() {
286            return new KrmsTypeAttribute(this);
287        }
288                
289    }
290
291        /**
292         * Defines some internal constants used on this class.
293         */
294        static class Constants {
295                final static String ROOT_ELEMENT_NAME = "krmsTypeAttribute";
296                final static String TYPE_NAME = "KrmsTypeAttributeType";
297        }
298        
299        /**
300         * A private class which exposes constants which define the XML element names to use
301         * when this object is marshalled to XML.
302         */
303        public static class Elements {
304                final static String ID = "id";
305                final static String TYPE_ID = "typeId";
306                final static String ATTR_DEFN_ID = "attributeDefinitionId";
307                public final static String SEQ_NO = "sequenceNumber";
308                final static String ACTIVE = "active";
309        }
310}