View Javadoc

1   /**
2    * Copyright 2005-2012 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.coreservice.api.style;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlAnyElement;
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.XmlType;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.rice.core.api.CoreConstants;
30  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
31  import org.kuali.rice.core.api.mo.ModelBuilder;
32  import org.w3c.dom.Element;
33  
34  /**
35   * An immutable representation of a Style.  A style is essentially a block of
36   * XML containing and XSL stylesheet. These can be used in various places for
37   * the transformation of XML data from one form to another.
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  @XmlRootElement(name = Style.Constants.ROOT_ELEMENT_NAME)
43  @XmlAccessorType(XmlAccessType.NONE)
44  @XmlType(name = Style.Constants.TYPE_NAME, propOrder = {
45  		Style.Elements.ID,
46  		Style.Elements.NAME,
47  		Style.Elements.XML_CONTENT,
48  		Style.Elements.ACTIVE,
49          CoreConstants.CommonElements.VERSION_NUMBER,
50          CoreConstants.CommonElements.OBJECT_ID,
51          CoreConstants.CommonElements.FUTURE_ELEMENTS
52  })
53  public final class Style extends AbstractDataTransferObject implements StyleContract {
54  
55  	private static final long serialVersionUID = -26426318682076660L;
56  	
57  	@XmlElement(name = Elements.ID, required = false)
58  	private final String id;
59  	
60  	@XmlElement(name = Elements.NAME, required = true)
61      private final String name;
62  	
63  	@XmlElement(name = Elements.XML_CONTENT, required = false)
64      private final String xmlContent;
65  	
66  	@XmlElement(name = Elements.ACTIVE, required = true)
67      private final boolean active;
68      
69  	@XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
70      private final Long versionNumber;
71  	
72  	@XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
73  	private final String objectId;
74  
75      @SuppressWarnings("unused")
76      @XmlAnyElement
77      private final Collection<Element> _futureElements = null;
78  	
79      /**
80       * Private constructor used only by JAXB.
81       */
82      private Style() {
83      	this.id = null;
84      	this.name = null;
85      	this.xmlContent = null;
86      	this.active = false;
87      	this.versionNumber = null;
88      	this.objectId = null;
89      }
90      
91      private Style(Builder builder) {
92      	this.id = builder.getId();
93      	this.name = builder.getName();
94      	this.xmlContent = builder.getXmlContent();
95      	this.active = builder.isActive();
96      	this.versionNumber = builder.getVersionNumber();
97      	this.objectId = builder.getObjectId();
98      }
99  
100     @Override
101     public String getId() {
102 		return this.id;
103 	}
104 
105     @Override
106     public String getName() {
107 		return this.name;
108 	}
109 
110     @Override
111     public String getXmlContent() {
112 		return this.xmlContent;
113 	}
114 	
115     @Override
116     public boolean isActive() {
117 		return this.active;
118 	}
119         
120 	@Override
121 	public Long getVersionNumber() {
122 		return this.versionNumber;
123 	}
124 	
125 	@Override
126 	public String getObjectId() {
127 		return this.objectId;
128 	}
129 
130 	/**
131 	 * A builder which can be used to construct {@link Style} instances.
132 	 * Enforces the constraints of the {@link StyleContract}.
133 	 * 
134 	 * @author Kuali Rice Team (rice.collab@kuali.org)
135 	 *
136 	 */
137 	public static final class Builder implements StyleContract, ModelBuilder, Serializable  {
138     	
139     	private static final long serialVersionUID = -219369603932108436L;
140     	
141 		private String id;
142         private String name;
143         private String xmlContent;
144         private boolean active;
145         private Long versionNumber;
146         private String objectId;
147         
148         private Builder(String name) {
149         	setName(name);
150         	setActive(true);
151         }
152         
153         /**
154          * Creates a style builder with the given required values.  The builder
155          * is the only means by which a {@link Style} object should be created.
156          * 
157          * <p>Will default the active flag to true.
158          * 
159          * @param name the name of the style to create, must not be null or blank
160          * 
161          * @return a builder with the required values already initialized
162          * 
163          * @throws IllegalArgumentException if the given name is null or blank
164          */
165         public static Builder create(String name) {
166         	return new Builder(name);
167         }
168         
169         /**
170          * Creates a populates a builder with the data on the given StyleContract.
171          * This is similar in nature to a "copy constructor" for Style.
172          * 
173          * @param contract an object implementing the StyleContract from which
174          * to copy property values
175          *  
176          * @return a builder with the values from the contract already initialized
177          * 
178          * @throws IllegalArgumentException if the given contract is null
179          */
180         public static Builder create(StyleContract contract) {
181         	if (contract == null) {
182         		throw new IllegalArgumentException("contract was null");
183         	}
184         	Builder builder = create(contract.getName());
185         	builder.setId(contract.getId());
186         	builder.setXmlContent(contract.getXmlContent());
187         	builder.setActive(contract.isActive());
188         	builder.setVersionNumber(contract.getVersionNumber());
189         	builder.setObjectId(contract.getObjectId());
190         	return builder;
191         }
192         
193         @Override
194         public Style build() {
195         	return new Style(this);
196         }
197 
198         @Override
199 		public String getId() {
200 			return this.id;
201 		}
202 
203         /**
204          * Sets the id for the style that will be returned by this builder.
205          * 
206          * @param id the id to set
207          */
208 		public void setId(String id) {
209 			this.id = id;
210 		}
211 
212 		@Override
213 		public String getName() {
214 			return this.name;
215 		}
216 
217 		/**
218          * Sets the name for the style that will be returned by this builder.
219          * The name must not be blank or null.
220          * 
221          * @param name the name to set on this builder, must not be null or
222          * blank
223          * 
224          * @throws IllegalArgumentException if the given name is null or blank
225          */
226 		public void setName(String name) {
227 			if (StringUtils.isBlank(name)) {
228 				throw new IllegalArgumentException("name is blank");
229 			}
230 			this.name = name;
231 		}
232 
233 		@Override
234 		public String getXmlContent() {
235 			return this.xmlContent;
236 		}
237 
238 		/**
239 		 * Sets the XML content for the style that will be returned by this
240 		 * builder.
241 		 * 
242 		 * @param xmlContent the xmlContent to set on this builder
243 		 */
244 		public void setXmlContent(String xmlContent) {
245 			this.xmlContent = xmlContent;
246 		}
247 
248 		@Override
249 		public boolean isActive() {
250 			return this.active;
251 		}
252 
253 		/**
254          * Sets the active flag for the style that will be returned by this
255          * builder.
256          * 
257          * @param active the active flag to set
258          */
259 		public void setActive(boolean active) {
260 			this.active = active;
261 		}
262 
263 		@Override
264 		public Long getVersionNumber() {
265 			return this.versionNumber;
266 		}
267 
268 		/**
269          * Sets the version number for the style that will be returned by this
270          * builder.
271          * 
272          * <p>In general, this value should not be manually set on the builder,
273          * but rather copied from an existing {@link StyleContract} when
274          * invoking {@link Builder#create(StyleContract)}.
275          * 
276          * @param versionNumber the version number to set
277          */
278 		public void setVersionNumber(Long versionNumber) {
279 			this.versionNumber = versionNumber;
280 		}
281 
282 		@Override
283 		public String getObjectId() {
284 			return objectId;
285 		}
286 		
287 		/**
288          * Sets the globally unique object ID for the style that will be
289          * returned by this builder.
290          * 
291          * <p>In general, this value should not be manually set on the builder,
292          * but rather copied from an existing {@link StyleContract} when
293          * invoking {@link Builder#create(StyleContract)}.
294          * 
295          * @param objectId the object ID to set
296          */
297 		public void setObjectId(String objectId) {
298 			this.objectId = objectId;
299 		}
300     	
301     }
302 
303     /**
304      * Defines some internal constants used on this class.
305      */
306     static class Constants {
307         final static String ROOT_ELEMENT_NAME = "style";
308         final static String TYPE_NAME = "StyleType";
309     }
310 
311     /**
312      * A private class which exposes constants which define the XML element names to use
313      * when this object is marshalled to XML.
314      */
315     static class Elements {
316         final static String ID = "id";
317         final static String NAME = "name";
318         final static String XML_CONTENT = "xmlContent";
319         final static String ACTIVE = "active";
320     }
321 
322 }