View Javadoc

1   /**
2    * Copyright 2005-2011 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   */
34  @XmlTransient
35  public abstract class BaseAttribute extends AbstractDataTransferObject implements BaseAttributeContract {
36  	private static final long serialVersionUID = -6126133049308968098L;
37  	
38  	@XmlElement(name = Elements.ID, required=true)
39  	private final String id;
40  
41  	@XmlElement(name = Elements.ATTR_DEFN_ID, required=false)
42  	private final String attributeDefinitionId;
43  
44  	@XmlElement(name = Elements.VALUE, required=false)
45  	private final String value;
46  	
47  	@XmlElement(name = Elements.ATTR_DEFN, required=false)
48  	private final KrmsAttributeDefinition attributeDefinition;
49  	
50      @SuppressWarnings("unused")
51  	@XmlAnyElement
52  	private final Collection<org.w3c.dom.Element> _futureElements = null;
53  	
54  	 /** 
55       * This constructor should only be called by the private default constructor of subclasses,
56       * which should only be used by JAXB and never invoked directly.
57       */
58      protected BaseAttribute() {
59      	this.id = null;
60      	this.attributeDefinitionId = null;
61      	this.value = null;
62      	this.attributeDefinition = null;
63      }
64      
65      /**
66  	 * Constructs a BaseAttribute from the given builder.  
67  	 * This constructor is protected and should only ever be invoked from the builder.
68  	 * 
69  	 * @param builder the Builder from which to construct the BaseAttribute
70  	 */
71      protected BaseAttribute(Builder builder) {
72          this.id = builder.getId();
73          this.attributeDefinitionId = builder.getAttributeDefinitionId();
74          this.value = builder.getValue();
75          if (builder.getAttributeDefinition() != null) {
76          	this.attributeDefinition = builder.getAttributeDefinition().build();
77          } else {
78          	this.attributeDefinition = null;
79          }
80      }
81      
82  	@Override
83  	public String getId() {
84  		return this.id;
85  	}
86  	
87  	@Override
88  	public String getAttributeDefinitionId() {
89  		return this.attributeDefinitionId;
90  	}
91  
92  	@Override
93  	public String getValue() {
94  		return this.value;
95  	}
96  	
97  	@Override
98  	public KrmsAttributeDefinition getAttributeDefinition() {
99  		return this.attributeDefinition;
100 	}
101 	
102 	/**
103      * This builder is used to construct the fields that {@link BaseAttribute} is responsible for.  It is abstract,
104      * and intended to be subclassed by extenders of {@link BaseAttribute}.
105      */
106     public abstract static class Builder implements BaseAttributeContract, ModelBuilder, Serializable {		
107 		private static final long serialVersionUID = 5799994031051731535L;
108 
109 		private String id;
110         private String attributeDefinitionId;
111         private String value;
112         private KrmsAttributeDefinition.Builder attributeDefinition;
113         
114 		/**
115 		 * Private constructor for creating a builder with all of it's required attributes.
116 		 */
117         protected Builder(String id, String attributeDefinitionId, String value) {
118             setId(id);
119             setAttributeDefinitionId(attributeDefinitionId);
120             setValue(value);
121         }
122 
123         protected Builder(BaseAttributeContract attr) {
124         	this (attr.getId(), attr.getAttributeDefinitionId(), attr.getValue());
125         }
126 
127 		/**
128 		 * Sets the value of the id on this builder to the given value.
129 		 * 
130 		 * @param id the id value to set, must not be null or blank
131 		 * @throws IllegalArgumentException if the id is null or blank
132 		 */
133         public void setId(String id) {
134 //            if (StringUtils.isBlank(id)) {
135 //                throw new IllegalArgumentException("id is blank");
136 //            }
137             this.id = id;
138         }
139 
140 		public void setAttributeDefinitionId(String attributeDefinitionId) {
141             if (StringUtils.isBlank(attributeDefinitionId)) {
142                 throw new IllegalArgumentException("the attribute definition id is blank");
143             }
144 			this.attributeDefinitionId = attributeDefinitionId;
145 		}
146 		
147 		public void setValue(String value) {
148 			this.value = value;
149 		}
150 		
151 		public void setAttributeDefinition(KrmsAttributeDefinition.Builder attributeDefinition) {
152 			this.attributeDefinition = attributeDefinition;
153 		}
154 		
155 		@Override
156 		public String getId() {
157 			return id;
158 		}
159 
160 		@Override
161 		public String getAttributeDefinitionId() {
162 			return attributeDefinitionId;
163 		}
164 		
165 		@Override
166 		public String getValue() {
167 			return value;
168 		}
169 
170 		@Override
171 		public KrmsAttributeDefinition.Builder getAttributeDefinition() {
172 			return attributeDefinition;
173 		}
174 
175     }
176 	
177 	/**
178 	 * A protected class which exposes constants which define the XML element names to use
179 	 * when this object is marshalled to XML.
180 	 */
181 	public static class Elements {
182 		public final static String ID = "id";
183 		public final static String ATTR_DEFN_ID = "attributeDefinitionId";
184 		public final static String VALUE = "value";
185 		public final static String ATTR_DEFN = "attributeDefinition";
186 	}
187 }