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;
017
018import java.io.Serializable;
019import java.util.Collection;
020
021import javax.xml.bind.annotation.XmlAnyElement;
022import javax.xml.bind.annotation.XmlElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.commons.lang.StringUtils;
026import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
027import org.kuali.rice.core.api.mo.ModelBuilder;
028import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
029
030/**
031 * abstract base model object for KRMS Attribute immutables. 
032 *
033 * @see BaseAttributeContract
034 */
035@XmlTransient
036public abstract class BaseAttribute extends AbstractDataTransferObject implements BaseAttributeContract {
037        private static final long serialVersionUID = -6126133049308968098L;
038        
039        @XmlElement(name = Elements.ID, required=true)
040        private final String id;
041
042        @XmlElement(name = Elements.ATTR_DEFN_ID, required=false)
043        private final String attributeDefinitionId;
044
045        @XmlElement(name = Elements.VALUE, required=false)
046        private final String value;
047        
048        @XmlElement(name = Elements.ATTR_DEFN, required=false)
049        private final KrmsAttributeDefinition attributeDefinition;
050        
051    @SuppressWarnings("unused")
052        @XmlAnyElement
053        private final Collection<org.w3c.dom.Element> _futureElements = null;
054        
055         /** 
056     * This constructor should only be called by the private default constructor of subclasses,
057     * which should only be used by JAXB and never invoked directly.
058     */
059    protected BaseAttribute() {
060        this.id = null;
061        this.attributeDefinitionId = null;
062        this.value = null;
063        this.attributeDefinition = null;
064    }
065    
066    /**
067         * Constructs a BaseAttribute from the given builder.  
068         * This constructor is protected and should only ever be invoked from the builder.
069         * 
070         * @param builder the Builder from which to construct the BaseAttribute
071         */
072    protected BaseAttribute(Builder builder) {
073        this.id = builder.getId();
074        this.attributeDefinitionId = builder.getAttributeDefinitionId();
075        this.value = builder.getValue();
076        if (builder.getAttributeDefinition() != null) {
077                this.attributeDefinition = builder.getAttributeDefinition().build();
078        } else {
079                this.attributeDefinition = null;
080        }
081    }
082    
083        @Override
084        public String getId() {
085                return this.id;
086        }
087        
088        @Override
089        public String getAttributeDefinitionId() {
090                return this.attributeDefinitionId;
091        }
092
093        @Override
094        public String getValue() {
095                return this.value;
096        }
097        
098        @Override
099        public KrmsAttributeDefinition getAttributeDefinition() {
100                return this.attributeDefinition;
101        }
102        
103        /**
104     * This builder is used to construct the fields that {@link BaseAttribute} is responsible for.  It is abstract,
105     * and intended to be subclassed by extenders of {@link BaseAttribute}.
106     */
107    public abstract static class Builder implements BaseAttributeContract, ModelBuilder, Serializable {         
108                private static final long serialVersionUID = 5799994031051731535L;
109
110                private String id;
111        private String attributeDefinitionId;
112        private String value;
113        private KrmsAttributeDefinition.Builder attributeDefinition;
114        
115                /**
116                 * Private constructor for creating a builder with all of it's required attributes.
117                 */
118        protected Builder(String id, String attributeDefinitionId, String value) {
119            setId(id);
120            setAttributeDefinitionId(attributeDefinitionId);
121            setValue(value);
122        }
123
124        protected Builder(BaseAttributeContract attr) {
125                this (attr.getId(), attr.getAttributeDefinitionId(), attr.getValue());
126        }
127
128                /**
129                 * Sets the value of the id on this builder to the given value.
130                 * 
131                 * @param id the id value to set, may be null if attribute has not yet
132         * been stored in the repository
133                 */
134        public void setId(String id) {
135//            if (StringUtils.isBlank(id)) {
136//                throw new IllegalArgumentException("id is blank");
137//            }
138            this.id = id;
139        }
140
141        /**
142         * Sets the attibuteDefinitionId value.
143         * @param attributeDefinitionId; must not be null or blank
144         * @throws IllegalArgumentException if the id is null or blank
145         */
146                public void setAttributeDefinitionId(String attributeDefinitionId) {
147            if (StringUtils.isBlank(attributeDefinitionId)) {
148                throw new IllegalArgumentException("the attribute definition id is blank");
149            }
150                        this.attributeDefinitionId = attributeDefinitionId;
151                }
152
153        /**
154         * Sets the value of the attribute
155         * @param value a String representing the value of the attribute
156         */
157                public void setValue(String value) {
158                        this.value = value;
159                }
160
161        /**
162         * Sets the attributeDefinition object related to the attribute.
163         * @param attributeDefinition the attribute definition
164         */
165                public void setAttributeDefinition(KrmsAttributeDefinition.Builder attributeDefinition) {
166                        this.attributeDefinition = attributeDefinition;
167                }
168                
169                @Override
170                public String getId() {
171                        return id;
172                }
173
174                @Override
175                public String getAttributeDefinitionId() {
176                        return attributeDefinitionId;
177                }
178                
179                @Override
180                public String getValue() {
181                        return value;
182                }
183
184                @Override
185                public KrmsAttributeDefinition.Builder getAttributeDefinition() {
186                        return attributeDefinition;
187                }
188
189    }
190        
191        /**
192         * A protected class which exposes constants which define the XML element names to use
193         * when this object is marshalled to XML.
194         */
195        public static class Elements {
196                public final static String ID = "id";
197                public final static String ATTR_DEFN_ID = "attributeDefinitionId";
198                public final static String VALUE = "value";
199                public final static String ATTR_DEFN = "attributeDefinition";
200        }
201}