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 KewAttributeDefinition. 
035 * immutable. 
036 * Instances of KewAttributeDefinition can be (un)marshalled to and from XML.
037 *
038 * @see KewAttributeDefinitionContract
039 */
040@XmlRootElement(name = KewAttributeDefinition.Constants.ROOT_ELEMENT_NAME)
041@XmlAccessorType(XmlAccessType.NONE)
042@XmlType(name = KewAttributeDefinition.Constants.TYPE_NAME, propOrder = {
043                KewAttributeDefinition.Elements.ID,
044                KewAttributeDefinition.Elements.NAME,
045                KewAttributeDefinition.Elements.NAMESPACE,
046        KewAttributeDefinition.Elements.LABEL,
047        KewAttributeDefinition.Elements.DESCRIPTION,
048                KewAttributeDefinition.Elements.ACTIVE,
049                KewAttributeDefinition.Elements.COMPONENT_NAME,
050        CoreConstants.CommonElements.VERSION_NUMBER,
051                CoreConstants.CommonElements.FUTURE_ELEMENTS
052})
053public final class KewAttributeDefinition extends AbstractDataTransferObject implements KewAttributeDefinitionContract {
054        private static final long serialVersionUID = -6356968810972165031L;
055        
056        @XmlElement(name = Elements.ID, required=true)
057        private final String id;
058        @XmlElement(name = Elements.NAME, required=true)
059        private final String name;
060        @XmlElement(name = Elements.NAMESPACE, required=true)
061        private final String namespace;
062    @XmlElement(name = Elements.LABEL, required=false)
063    private final String label;
064    @XmlElement(name = Elements.DESCRIPTION, required=false)
065    private final String description;
066        @XmlElement(name = Elements.ACTIVE, required=false)
067        private final boolean active;
068        @XmlElement(name = Elements.COMPONENT_NAME, required=false)
069        private final String componentName;
070    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
071    private final Long versionNumber;
072        
073        @SuppressWarnings("unused")
074    @XmlAnyElement
075    private final Collection<org.w3c.dom.Element> _futureElements = null;
076        
077         /** 
078     * This constructor should never be called.  It is only present for use during JAXB unmarshalling. 
079     */
080    private KewAttributeDefinition() {
081        this.id = null;
082        this.name = null;
083        this.namespace = null;
084        this.label = null;
085        this.description = null;
086        this.active = false;
087        this.componentName = null;
088        this.versionNumber = null;
089    }
090    
091    /**
092         * Constructs a KewAttributeDefinition from the given builder.  This constructor is private and should only
093         * ever be invoked from the builder.
094         * 
095         * @param builder the Builder from which to construct the KewAttributeDefinition
096         */
097    private KewAttributeDefinition(Builder builder) {
098        this.id = builder.getId();
099        this.name = builder.getName();
100        this.namespace = builder.getNamespace();
101        this.label = builder.getLabel();
102        this.description = builder.getDescription();
103        this.active = builder.isActive();
104        this.componentName = builder.getComponentName();
105        this.versionNumber = builder.getVersionNumber();
106    }
107    
108        public String getId() {
109                return this.id;
110        }
111        
112        public String getName() {
113                return this.name;
114        }
115
116        public String getNamespace() {
117                return this.namespace;
118        }
119
120        public String getLabel() {
121                return this.label;
122        }
123
124    public String getDescription() {
125        return description;
126    }
127
128    public boolean isActive() {
129                return this.active; 
130        }
131
132        public String getComponentName() {
133                return this.componentName;
134        }
135        
136    @Override
137    public Long getVersionNumber() {
138        return versionNumber;
139    }
140        
141        /**
142     * This builder is used to construct instances of KewAttributeDefinition.  It enforces the constraints of the {@link KewAttributeDefinitionContract}.
143     */
144    public static class Builder implements KewAttributeDefinitionContract, ModelBuilder, Serializable {         
145                private static final long serialVersionUID = -2110564370088779631L;
146                
147                private String id;
148        private String name;
149        private String namespace;
150        private String label;
151        private String description;
152        private boolean active;
153        private String componentName;
154        private Long versionNumber;
155
156                /**
157                 * Private constructor for creating a builder with all of it's required attributes.
158                 */
159        private Builder(String id, String name, String namespace) {
160            setId(id);
161            setName(name);
162            setNamespace(namespace);
163                        setActive(true);
164        }
165
166        public Builder label(String label){
167                setLabel(label);
168                return this;
169        }
170        public Builder componentName(String componentName){
171                setComponentName(componentName);
172                return this;
173        }
174        /**
175         * Creates a builder from the given parameters.
176         * 
177         * @param id the KewAttributeDefinition id
178         * @param name the KewAttributeDefinition name
179         * @param namespace the KewAttributeDefinition namespace
180         * @return an instance of the builder with the fields already populated
181         * @throws IllegalArgumentException if the either the id, name or namespace is null or blank
182         */
183        public static Builder create(String id, String name, String namespace) {
184            return new Builder(id, name, namespace);
185        }
186
187        /**
188         * Creates a builder by populating it with data from the given {@link KewAttributeDefinitionContract}.
189         * 
190         * @param contract the contract from which to populate this builder
191         * @return an instance of the builder populated with data from the contract
192         */
193        public static Builder create(KewAttributeDefinitionContract contract) {
194                if (contract == null) {
195                throw new IllegalArgumentException("contract is null");
196            }
197            Builder builder =  new Builder(contract.getId(), contract.getName(), contract.getNamespace());
198            builder.setActive(contract.isActive());
199            builder.setLabel(contract.getLabel());
200            builder.setDescription(contract.getDescription());
201            builder.setComponentName(contract.getComponentName());
202            builder.setVersionNumber(contract.getVersionNumber());
203            return builder;
204        }
205
206                /**
207                 * Sets the value of the id on this builder to the given value.
208                 * 
209                 * @param id the id value to set, must be null or non-blank
210                 * @throws IllegalArgumentException if the id is non-null and blank
211                 */
212        public void setId(String id) {
213            if (null != id && StringUtils.isBlank(id)) {
214                throw new IllegalArgumentException("id must be null or non-blank");
215            }
216            this.id = id;
217        }
218
219                public void setName(String name) {
220            if (StringUtils.isBlank(name)) {
221                throw new IllegalArgumentException("name is blank");
222            }
223                        this.name = name;
224                }
225
226                public void setNamespace(String namespace) {
227            if (StringUtils.isBlank(namespace)) {
228                throw new IllegalArgumentException("namespace is blank");
229            }
230                        this.namespace = namespace;
231                }
232                
233                public void setLabel(String label) {
234                        this.label = label;
235                }
236
237        public void setDescription(String description) {
238            this.description = description;
239        }
240
241
242                public void setComponentName(String componentName) {
243                        this.componentName = componentName;
244                }
245                
246                public void setActive(boolean active) {
247                        this.active = active;
248                }
249
250        public void setVersionNumber(Long versionNumber){
251            this.versionNumber = versionNumber;
252        }
253        
254                @Override
255                public String getId() {
256                        return id;
257                }
258
259                @Override
260                public String getName() {
261                        return name;
262                }
263
264                @Override
265                public String getNamespace() {
266                        return namespace;
267                }
268
269                @Override
270                public String getComponentName() {
271                        return componentName;
272                }
273
274                @Override
275                public String getLabel() {
276                        return label;
277                }
278
279        @Override
280        public String getDescription() {
281            return description;
282        }
283
284        @Override
285                public boolean isActive() {
286                        return active;
287                }
288
289        @Override
290        public Long getVersionNumber() {
291            return versionNumber;
292        }
293
294                /**
295                 * Builds an instance of a CampusType based on the current state of the builder.
296                 * 
297                 * @return the fully-constructed CampusType
298                 */
299        @Override
300        public KewAttributeDefinition build() {
301            return new KewAttributeDefinition(this);
302        }
303                
304    }
305
306        /**
307         * Defines some internal constants used on this class.
308         */
309        static class Constants {
310                final static String ROOT_ELEMENT_NAME = "KewAttributeDefinition";
311                final static String TYPE_NAME = "KewAttributionDefinitionType";
312        }
313        
314        /**
315         * A private class which exposes constants which define the XML element names to use
316         * when this object is marshalled to XML.
317         */
318        public static class Elements {
319                final static String ID = "id";
320                final static String NAME = "name";
321                final static String NAMESPACE = "namespace";
322                final static String LABEL = "label";
323        final static String DESCRIPTION = "description";
324                final static String COMPONENT_NAME = "componentName";
325                final static String ACTIVE = "active";
326    }
327}