001    /**
002     * Copyright 2005-2011 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.kim.api.common.template;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.CoreConstants;
020    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021    import org.kuali.rice.core.api.mo.ModelBuilder;
022    import org.kuali.rice.kim.api.KimConstants;
023    import org.w3c.dom.Element;
024    
025    import javax.xml.bind.annotation.XmlAccessType;
026    import javax.xml.bind.annotation.XmlAccessorType;
027    import javax.xml.bind.annotation.XmlAnyElement;
028    import javax.xml.bind.annotation.XmlElement;
029    import javax.xml.bind.annotation.XmlRootElement;
030    import javax.xml.bind.annotation.XmlType;
031    import java.io.Serializable;
032    import java.util.Collection;
033    
034    /**
035     * An immutable representation of a {@link TemplateContract}.
036     *
037     * <p>To construct an instance of a Template, use the {@link Template.Builder} class.<p/>
038     */
039    @XmlRootElement(name = Template.Constants.ROOT_ELEMENT_NAME)
040    @XmlAccessorType(XmlAccessType.NONE)
041    @XmlType(name = Template.Constants.TYPE_NAME, propOrder = {
042                    Template.Elements.ID,
043                    Template.Elements.NAMESPACE_CODE,
044                    Template.Elements.NAME,
045                    Template.Elements.DESCRIPTION,
046                    Template.Elements.KIM_TYPE_ID,
047            Template.Elements.ACTIVE,
048            CoreConstants.CommonElements.VERSION_NUMBER,
049            CoreConstants.CommonElements.OBJECT_ID,
050            CoreConstants.CommonElements.FUTURE_ELEMENTS
051    })
052    public final class Template extends AbstractDataTransferObject implements TemplateContract {
053    
054            private static final long serialVersionUID = 1L;
055            
056        @XmlElement(name = Template.Elements.ID, required = false)
057        private final String id;
058    
059        @XmlElement(name = Template.Elements.NAMESPACE_CODE, required = true)
060        private final String namespaceCode;
061    
062        @XmlElement(name = Template.Elements.NAME, required = true)
063        private final String name;
064    
065        @XmlElement(name = Template.Elements.DESCRIPTION, required = false)
066        private final String description;
067    
068        @XmlElement(name = Template.Elements.KIM_TYPE_ID, required = true)
069        private final String kimTypeId;
070        
071        @XmlElement(name = Template.Elements.ACTIVE, required = false)
072        private final boolean active;
073    
074        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
075        private final Long versionNumber;
076    
077        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
078        private final String objectId;
079        
080        @SuppressWarnings("unused")
081        @XmlAnyElement
082        private final Collection<Element> _futureElements = null;
083        
084        /**
085             *  A constructor to be used only by JAXB unmarshalling.
086             * 
087             */
088            private Template() {
089                    this.id = null;
090            this.namespaceCode = null;
091            this.name = null;
092            this.description = null;
093            this.kimTypeId = null;
094            this.active = false;
095            this.versionNumber = Long.valueOf(1L);
096            this.objectId = null;
097            }
098            
099        /**
100             * A constructor using the Builder.
101             * 
102             * @param builder
103             */
104            private Template(Builder builder) {
105                    this.id = builder.getId();
106            this.namespaceCode = builder.getNamespaceCode();
107            this.name = builder.getName();
108            this.description = builder.getDescription();
109            this.kimTypeId = builder.getKimTypeId();
110            
111            this.active = builder.isActive();
112            this.versionNumber = builder.getVersionNumber();
113            this.objectId = builder.getObjectId();
114            }
115    
116            /**
117             * @see TemplateContract#getId()
118             */
119            @Override
120            public String getId() {
121                    return id;
122            }
123    
124            /**
125             * @see TemplateContract#getNamespaceCode()
126             */
127            @Override
128            public String getNamespaceCode() {
129                    return namespaceCode;
130            }
131    
132            /**
133             * @see TemplateContract#getName()
134             */
135            @Override
136            public String getName() {
137                    return name;
138            }
139    
140            /**
141             * @see TemplateContract#getDescription()
142             */
143            @Override
144            public String getDescription() {
145                    return description;
146            }
147    
148            /**
149             * @see TemplateContract#getKimTypeId()
150             */
151            @Override
152            public String getKimTypeId() {
153                    return kimTypeId;
154            }
155            
156            /**
157             * @see TemplateContract#isActive()
158             */
159            @Override
160            public boolean isActive() {
161                    return active;
162            }
163            
164            /**
165             * @see org.kuali.rice.core.api.mo.common.Versioned#getVersionNumber()
166             */
167            @Override
168            public Long getVersionNumber() {
169                    return versionNumber;
170            }
171    
172            /**
173             * @see org.kuali.rice.core.api.mo.common.GloballyUnique#getObjectId()
174             */
175            @Override
176            public String getObjectId() {
177                    return objectId;
178        }
179            
180        /**
181         * This builder constructs a Template enforcing the constraints of the {@link TemplateContract}.
182         */
183        public static final class Builder implements TemplateContract, ModelBuilder, Serializable {
184            private String id;
185            private String namespaceCode;
186            private String name;
187            private String description;
188            private String kimTypeId;
189            private Long versionNumber = 1L;
190            private String objectId;
191            private boolean active;
192            
193            private Builder(String namespaceCode, String name, String kimTypeId) {
194                setNamespaceCode(namespaceCode);
195                setName(name);
196                setKimTypeId(kimTypeId);
197            }
198    
199            /**
200             * creates a KimPermission with the required fields.
201             */
202            public static Builder create(String namespaceCode, String name, String kimTypeId) {
203                return new Builder(namespaceCode, name, kimTypeId);
204            }
205    
206            /**
207             * creates a KimPermission from an existing {@link TemplateContract}.
208             */
209            public static Builder create(TemplateContract contract) {
210                Builder builder = new Builder(contract.getNamespaceCode(), contract.getName(), contract.getKimTypeId());
211                builder.setId(contract.getId());
212                builder.setDescription(contract.getDescription());
213                
214                builder.setActive(contract.isActive());
215                builder.setVersionNumber(contract.getVersionNumber());
216                builder.setObjectId(contract.getObjectId());
217    
218                return builder;
219            }
220    
221            @Override
222            public String getId() {
223                return id;
224            }
225    
226            public void setId(final String id) {
227                    this.id = id;
228            }
229            
230            @Override
231            public String getNamespaceCode() {
232                return namespaceCode;
233            }
234    
235            public void setNamespaceCode(final String namespaceCode) {
236                    if (StringUtils.isEmpty(namespaceCode)) {
237                    throw new IllegalArgumentException("namespaceCode is blank");
238                }
239                    this.namespaceCode = namespaceCode;
240            }
241    
242            @Override
243            public String getName() {
244                return name;
245            }
246    
247            public void setName(final String name) {
248                    if (StringUtils.isEmpty(name)) {
249                    throw new IllegalArgumentException("name is blank");
250                }
251                    this.name = name;
252            }
253    
254                    @Override
255                    public String getDescription() {
256                            return description;
257                    }
258                    
259                    public void setDescription(final String description) {
260                            this.description = description;
261                    }
262    
263                    @Override
264                    public String getKimTypeId() {
265                            if (StringUtils.isEmpty(kimTypeId)) {
266                    throw new IllegalArgumentException("kimTypeId is blank");
267                }
268                            return kimTypeId;
269                    }
270                    
271                    public void setKimTypeId(final String kimTypeId) {
272                            this.kimTypeId = kimTypeId;
273                    }
274                    
275                    @Override
276                    public boolean isActive() {
277                            return active;
278                    }
279                    
280                    public void setActive(final boolean active) {
281                this.active = active;
282            }
283    
284                    @Override
285                    public Long getVersionNumber() {
286                            return versionNumber;
287                    }
288    
289                    public void setVersionNumber(final Long versionNumber) {
290                            if (versionNumber == null || versionNumber <= 0) {
291                        throw new IllegalArgumentException("versionNumber is invalid");
292                    }
293                            this.versionNumber = versionNumber;
294                }
295                     
296                    @Override
297                    public String getObjectId() {
298                            return objectId;
299                    }
300    
301            public void setObjectId(final String objectId) {
302                this.objectId = objectId;
303            }
304            
305            @Override
306            public Template build() {
307                return new Template(this);
308            }
309        }
310        
311        /**
312         * Defines some internal constants used on this class.
313         */
314        static class Constants {
315            static final String ROOT_ELEMENT_NAME = "template";
316            static final String TYPE_NAME = "TemplateType";
317        }
318    
319        /**
320         * A private class which exposes constants which define the XML element names to use
321         * when this object is marshalled to XML.
322         */
323        static class Elements {
324            static final String ID = "id";
325            static final String NAMESPACE_CODE = "namespaceCode";
326            static final String NAME = "name";
327            static final String DESCRIPTION = "description";
328            static final String KIM_TYPE_ID = "kimTypeId";        
329            static final String ACTIVE = "active";
330        }
331    
332        public static class Cache {
333            public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + Template.Constants.TYPE_NAME;
334        }
335    }