View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.api.repository;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  
21  import javax.xml.bind.annotation.XmlAnyElement;
22  import javax.xml.bind.annotation.XmlElement;
23  import javax.xml.bind.annotation.XmlTransient;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
27  import org.kuali.rice.core.api.mo.ModelBuilder;
28  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
29  
30  /**
31   * abstract base model object for KRMS Attribute immutables. 
32   *
33   * @see BaseAttributeContract
34   */
35  @XmlTransient
36  public abstract class BaseAttribute extends AbstractDataTransferObject implements BaseAttributeContract {
37  	private static final long serialVersionUID = -6126133049308968098L;
38  	
39  	@XmlElement(name = Elements.ID, required=true)
40  	private final String id;
41  
42  	@XmlElement(name = Elements.ATTR_DEFN_ID, required=false)
43  	private final String attributeDefinitionId;
44  
45  	@XmlElement(name = Elements.VALUE, required=false)
46  	private final String value;
47  	
48  	@XmlElement(name = Elements.ATTR_DEFN, required=false)
49  	private final KrmsAttributeDefinition attributeDefinition;
50  	
51      @SuppressWarnings("unused")
52  	@XmlAnyElement
53  	private final Collection<org.w3c.dom.Element> _futureElements = null;
54  	
55  	 /** 
56       * This constructor should only be called by the private default constructor of subclasses,
57       * which should only be used by JAXB and never invoked directly.
58       */
59      protected BaseAttribute() {
60      	this.id = null;
61      	this.attributeDefinitionId = null;
62      	this.value = null;
63      	this.attributeDefinition = null;
64      }
65      
66      /**
67  	 * Constructs a BaseAttribute from the given builder.  
68  	 * This constructor is protected and should only ever be invoked from the builder.
69  	 * 
70  	 * @param builder the Builder from which to construct the BaseAttribute
71  	 */
72      protected BaseAttribute(Builder builder) {
73          this.id = builder.getId();
74          this.attributeDefinitionId = builder.getAttributeDefinitionId();
75          this.value = builder.getValue();
76          if (builder.getAttributeDefinition() != null) {
77          	this.attributeDefinition = builder.getAttributeDefinition().build();
78          } else {
79          	this.attributeDefinition = null;
80          }
81      }
82      
83  	@Override
84  	public String getId() {
85  		return this.id;
86  	}
87  	
88  	@Override
89  	public String getAttributeDefinitionId() {
90  		return this.attributeDefinitionId;
91  	}
92  
93  	@Override
94  	public String getValue() {
95  		return this.value;
96  	}
97  	
98  	@Override
99  	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 }