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.attribute;
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.type.KimType;
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    @XmlRootElement(name = KimAttributeData.Constants.ROOT_ELEMENT_NAME)
036    @XmlAccessorType(XmlAccessType.NONE)
037    @XmlType(name = KimAttributeData.Constants.TYPE_NAME, propOrder = {
038            KimAttributeData.Elements.ID,
039            KimAttributeData.Elements.ASSIGNED_TO_ID,
040            KimAttributeData.Elements.KIM_TYPE_ID,
041            KimAttributeData.Elements.KIM_TYPE,
042            KimAttributeData.Elements.KIM_ATTRIBUTE,
043            KimAttributeData.Elements.ATTRIBUTE_VALUE,
044            CoreConstants.CommonElements.VERSION_NUMBER,
045            CoreConstants.CommonElements.OBJECT_ID,
046            CoreConstants.CommonElements.FUTURE_ELEMENTS
047    })
048    public final class KimAttributeData extends AbstractDataTransferObject implements KimAttributeDataContract {
049        @XmlElement(name = Elements.ID, required = false)
050        private final String id;
051    
052        @XmlElement(name = Elements.ASSIGNED_TO_ID, required = false)
053        private final String assignedToId;
054    
055        @XmlElement(name = Elements.KIM_TYPE_ID, required = true)
056        private final String kimTypeId;
057    
058        @XmlElement(name = Elements.KIM_TYPE, required = false)
059        private final KimType kimType;
060    
061        @XmlElement(name = Elements.KIM_ATTRIBUTE, required = false)
062        private final KimAttribute kimAttribute;
063    
064        @XmlElement(name = Elements.ATTRIBUTE_VALUE, required = false)
065        private final String attributeValue;
066    
067        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
068        private final Long versionNumber;
069    
070        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
071        private final String objectId;
072    
073        @SuppressWarnings("unused")
074        @XmlAnyElement
075        private final Collection<Element> _futureElements = null;
076    
077        @SuppressWarnings("unused")
078        private KimAttributeData() {
079            this.id = null;
080            this.assignedToId = null;
081            this.kimTypeId = null;
082            this.kimType = null;
083            this.kimAttribute = null;
084            this.attributeValue = null;
085            this.versionNumber = null;
086            this.objectId = null;
087        }
088    
089        private KimAttributeData(Builder builder) {
090            this.id = builder.getId();
091            this.assignedToId = builder.getAssignedToId();
092            this.kimTypeId = builder.getKimTypeId();
093            this.kimType =
094                    builder.getKimType() != null ? builder.getKimType().build() : null;
095            this.kimAttribute =
096                    builder.getKimAttribute() != null ? builder.getKimAttribute().build() : null;
097            this.attributeValue = builder.getAttributeValue();
098            this.versionNumber = builder.getVersionNumber();
099            this.objectId = builder.getObjectId();
100        }
101    
102        @Override
103        public String getId() {
104            return id;
105        }
106    
107        @Override
108        public String getAssignedToId() {
109            return assignedToId;
110        }
111    
112        @Override
113        public String getKimTypeId() {
114            return kimTypeId;
115        }
116    
117        @Override
118        public KimType getKimType() {
119            return kimType;
120        }
121    
122        @Override
123        public KimAttribute getKimAttribute() {
124            return kimAttribute;
125        }
126    
127        @Override
128        public String getAttributeValue() {
129            return attributeValue;
130        }
131    
132        @Override
133        public Long getVersionNumber() {
134            return versionNumber;
135        }
136    
137        @Override
138        public String getObjectId() {
139            return objectId;
140        }
141    
142        public static final class Builder implements KimAttributeDataContract, ModelBuilder, Serializable {
143            private String id;
144            private String assignedToId;
145            private String kimTypeId;
146            private KimType.Builder kimType;
147            private KimAttribute.Builder kimAttribute;
148            private String attributeValue;
149            private Long versionNumber;
150            private String objectId;
151    
152            private Builder(String kimTypeId) {
153                setKimTypeId(kimTypeId);
154            }
155    
156            /**
157             * creates a Parameter with the required fields.
158             */
159            public static Builder create(String kimTypeId) {
160                return new Builder(kimTypeId);
161            }
162    
163            /**
164             * creates a KimAttributeData from an existing {@link org.kuali.rice.kim.api.common.attribute.KimAttributeContract}
165             */
166            public static Builder create(KimAttributeDataContract contract) {
167                Builder builder = new Builder(contract.getKimTypeId());
168                builder.setAssignedToId(contract.getAssignedToId());
169    
170                builder.setId(contract.getId());
171                if (contract.getKimAttribute() != null) {
172                    builder.setKimAttribute(KimAttribute.Builder.create(contract.getKimAttribute()));
173                }
174                if (contract.getKimType() != null) {
175                    builder.setKimType(KimType.Builder.create(contract.getKimType()));
176                }
177                builder.setValue(contract.getAttributeValue());
178                builder.setVersionNumber(contract.getVersionNumber());
179                builder.setObjectId(contract.getObjectId());
180                return builder;
181            }
182    
183            @Override
184            public String getId() {
185                return id;
186            }
187    
188            public void setId(final String id) {
189                if (StringUtils.isWhitespace(id)) {
190                    throw new IllegalArgumentException("id is blank");
191                }
192                this.id = id;
193            }
194    
195            @Override
196            public String getAssignedToId() {
197                return assignedToId;
198            }
199    
200            public void setAssignedToId(final String assignedToId) {
201                this.assignedToId = assignedToId;
202            }
203    
204            @Override
205            public String getKimTypeId(){
206                return kimTypeId;
207            }
208    
209            public void setKimTypeId(String kimTypeId) {
210                this.kimTypeId = kimTypeId;
211            }
212    
213            @Override
214            public KimType.Builder getKimType() {
215                return kimType;
216            }
217    
218            public void setKimType(final KimType.Builder kimType) {
219                this.kimType = kimType;
220            }
221    
222            @Override
223            public KimAttribute.Builder getKimAttribute() {
224                return kimAttribute;
225            }
226    
227            public void setKimAttribute(final KimAttribute.Builder kimAttribute) {
228                this.kimAttribute = kimAttribute;
229            }
230    
231            @Override
232            public String getAttributeValue() {
233                return attributeValue;
234            }
235    
236            public void setValue(final String attributeValue) {
237                this.attributeValue = attributeValue;
238            }
239    
240            @Override
241            public Long getVersionNumber() {
242                return versionNumber;
243            }
244    
245            public void setVersionNumber(Long versionNumber) {
246                this.versionNumber = versionNumber;
247            }
248    
249            @Override
250            public String getObjectId() {
251                return objectId;
252            }
253    
254            public void setObjectId(final String objectId) {
255                this.objectId = objectId;
256            }
257    
258            @Override
259            public KimAttributeData build() {
260                return new KimAttributeData(this);
261            }
262        }
263    
264        /**
265         * Defines some internal constants used on this class.
266         */
267        static class Constants {
268            final static String ROOT_ELEMENT_NAME = "kimAttributeData";
269            final static String TYPE_NAME = "KimAttributeDataType";
270        }
271    
272        /**
273         * A private class which exposes constants which define the XML element names to use
274         * when this object is marshalled to XML.
275         */
276        static class Elements {
277            final static String ID = "id";
278            final static String ASSIGNED_TO_ID = "assignedToId";
279            final static String KIM_TYPE_ID = "kimTypeId";
280            final static String KIM_TYPE = "kimType";
281            final static String KIM_ATTRIBUTE = "kimAttribute";
282            final static String ATTRIBUTE_VALUE = "attributeValue";
283        }
284    }