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.action;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
23  
24  import javax.xml.bind.annotation.XmlAccessType;
25  import javax.xml.bind.annotation.XmlAccessorType;
26  import javax.xml.bind.annotation.XmlAnyElement;
27  import javax.xml.bind.annotation.XmlElement;
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.bind.annotation.XmlType;
30  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
31  import java.io.Serializable;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.HashMap;
35  import java.util.Map;
36  
37  /**
38   * Concrete model object implementation of KRMS Repository Action 
39   * immutable. 
40   * Instances of Action can be (un)marshalled to and from XML.
41   *
42   * @see ActionDefinitionContract
43   */
44  @XmlRootElement(name = ActionDefinition.Constants.ROOT_ELEMENT_NAME)
45  @XmlAccessorType(XmlAccessType.NONE)
46  @XmlType(name = ActionDefinition.Constants.TYPE_NAME, propOrder = {
47  		ActionDefinition.Elements.ID,
48  		ActionDefinition.Elements.NAME,
49  		ActionDefinition.Elements.NAMESPACE,
50  		ActionDefinition.Elements.DESC,
51  		ActionDefinition.Elements.TYPE_ID,
52  		ActionDefinition.Elements.RULE_ID,
53  		ActionDefinition.Elements.SEQUENCE_NUMBER,
54  		ActionDefinition.Elements.ATTRIBUTES,
55          CoreConstants.CommonElements.VERSION_NUMBER,
56  		CoreConstants.CommonElements.FUTURE_ELEMENTS
57  })
58  public final class ActionDefinition extends AbstractDataTransferObject implements ActionDefinitionContract {
59  	private static final long serialVersionUID = 2783959459503209577L;
60  
61  	@XmlElement(name = Elements.ID, required=true)
62  	private String id;
63  	@XmlElement(name = Elements.NAME, required=true)
64  	private String name;
65  	@XmlElement(name = Elements.NAMESPACE, required=true)
66  	private String namespace;
67  	@XmlElement(name = Elements.DESC, required=true)
68  	private String description;
69  	@XmlElement(name = Elements.TYPE_ID, required=true)
70  	private String typeId;
71  	@XmlElement(name = Elements.RULE_ID, required=true)
72  	private String ruleId;
73  	@XmlElement(name = Elements.SEQUENCE_NUMBER, required=true)
74  	private Integer sequenceNumber;
75  	
76  	@XmlElement(name = Elements.ATTRIBUTES, required = false)
77  	@XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
78  	private final Map<String, String> attributes;
79  	
80      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
81      private final Long versionNumber;
82      	
83  	@SuppressWarnings("unused")
84      @XmlAnyElement
85      private final Collection<org.w3c.dom.Element> _futureElements = null;
86  	
87  	
88  	 /** 
89       * This constructor should never be called.  
90       * It is only present for use during JAXB unmarshalling. 
91       */
92      private ActionDefinition() {
93      	this.id = null;
94      	this.name = null;
95      	this.namespace = null;
96      	this.description = null;
97      	this.typeId = null;
98      	this.ruleId = null;
99      	this.sequenceNumber = null;
100     	this.attributes = null;
101         this.versionNumber = null;
102     }
103     
104     /**
105 	 * Constructs a KRMS Repository Action object from the given builder.  
106 	 * This constructor is private and should only ever be invoked from the builder.
107 	 * 
108 	 * @param builder the Builder from which to construct the Action
109 	 */
110     private ActionDefinition(Builder builder) {
111         this.id = builder.getId();
112         this.name = builder.getName();
113         this.namespace = builder.getNamespace();
114         this.description = builder.getDescription();
115         this.typeId = builder.getTypeId();
116         this.ruleId = builder.getRuleId();
117         this.sequenceNumber = builder.getSequenceNumber();
118         if (builder.attributes != null){
119         	this.attributes = Collections.unmodifiableMap(builder.getAttributes());
120         } else {
121         	this.attributes = null;
122         }
123         this.versionNumber = builder.getVersionNumber();
124     }
125     
126 	@Override
127 	public String getId() {
128 		return this.id;
129 	}
130 
131 	@Override
132 	public String getName() {
133 		return this.name;
134 	}
135 
136 	@Override
137 	public String getNamespace() {
138 		return this.namespace;
139 	}
140 
141 	@Override
142 	public String getDescription() {
143 		return this.description;
144 	}
145 
146 	@Override
147 	public String getTypeId() {
148 		return this.typeId;
149 	}
150 
151 	@Override
152 	public String getRuleId() {
153 		return this.ruleId;
154 	}
155 
156 	@Override
157 	public Integer getSequenceNumber() {
158 		return this.sequenceNumber;
159 	}
160 
161 	@Override
162 	public Map<String, String> getAttributes() {
163 		return this.attributes; 
164 	}
165 
166     @Override
167     public Long getVersionNumber() {
168         return versionNumber;
169     }
170         
171 	/**
172      * This builder is used to construct instances of KRMS Repository Action.  It enforces the constraints of the {@link ActionDefinitionContract}.
173      */
174     public static class Builder implements ActionDefinitionContract, ModelBuilder, Serializable {
175         private static final long serialVersionUID = -6773634512570180267L;
176 
177         private String id;
178         private String name;
179         private String namespace;
180         private String description;
181         private String typeId;
182         private String ruleId;
183         private Integer sequenceNumber;
184         private Map<String, String> attributes;
185         private Long versionNumber;
186 
187 		/**
188 		 * Private constructor for creating a builder with all of it's required attributes.
189 		 */
190         private Builder(String actionId, String name, String namespace, String typeId, String ruleId, Integer sequenceNumber) {
191             setId(actionId);
192             setName(name);
193             setNamespace(namespace);
194             setTypeId(typeId);
195             setRuleId(ruleId);
196             setSequenceNumber(sequenceNumber);
197             setAttributes(new HashMap<String, String>());
198         }
199         
200         public static Builder create(String actionId, String name, String namespace, String typeId, String ruleId, Integer sequenceNumber){
201         	return new Builder(actionId, name, namespace, typeId, ruleId, sequenceNumber);
202         }
203         /**
204          * Creates a builder by populating it with data from the given {@link ActionDefinitionContract}.
205          * 
206          * @param contract the contract from which to populate this builder
207          * @return an instance of the builder populated with data from the contract
208          */
209         public static Builder create(ActionDefinitionContract contract) {
210         	if (contract == null) {
211                 throw new IllegalArgumentException("contract is null");
212             }
213             Builder builder =  new Builder(contract.getId(), contract.getName(),
214             		contract.getNamespace(), contract.getTypeId(), contract.getRuleId(),
215             		contract.getSequenceNumber());
216             builder.setDescription(contract.getDescription());
217         	if (contract.getAttributes() != null){
218                 builder.setAttributes(new HashMap<String, String>(contract.getAttributes()));
219         	}
220             builder.setVersionNumber(contract.getVersionNumber());
221             return builder;
222         }
223 
224 		/**
225 		 * Sets the value of the id on this builder to the given value.
226 		 * 
227 		 * @param id the id value to set, must be null or non-blank
228 		 * @throws IllegalArgumentException if the id is non-null and blank
229 		 */
230 
231         public void setId(String actionId) {
232             if (actionId != null && StringUtils.isBlank(actionId)) {
233                 throw new IllegalArgumentException("action ID must be null or non-blank");
234             }
235 			this.id = actionId;
236 		}
237 
238      
239         public void setName(String name) {
240             if (StringUtils.isBlank(name)) {
241                 throw new IllegalArgumentException("name is blank");
242             }
243 			this.name = name;
244 		}
245      
246         public void setNamespace(String namespace) {
247             if (StringUtils.isBlank(namespace)) {
248                 throw new IllegalArgumentException("namespace is blank");
249             }
250 			this.namespace = namespace;
251 		}
252      
253 		public void setDescription(String desc) {
254 			this.description = desc;
255 		}
256 		
257 		public void setTypeId(String typeId) {
258 			if (StringUtils.isBlank(typeId)) {
259 	                throw new IllegalArgumentException("KRMS type id is blank");
260 			}
261 			this.typeId = typeId;
262 		}
263 		
264 		public void setRuleId(String ruleId) {
265 			if (StringUtils.isBlank(ruleId)) {
266 	                throw new IllegalArgumentException("rule id is blank");
267 			}
268 			this.ruleId = ruleId;
269 		}
270 		
271 		public void setSequenceNumber(Integer sequenceNumber) {
272 			if (sequenceNumber == null) {
273 	                throw new IllegalArgumentException("sequence number is null");
274 			}
275 			this.sequenceNumber = sequenceNumber;
276 		}
277 		
278 		public void setAttributes(Map<String, String> attributes){
279 			if (attributes == null){
280 				this.attributes = Collections.emptyMap();
281 			}
282 			this.attributes = Collections.unmodifiableMap(attributes);
283 		}
284 		
285         public void setVersionNumber(Long versionNumber){
286             this.versionNumber = versionNumber;
287         }
288         
289 		@Override
290 		public String getId() {
291 			return id;
292 		}
293 
294 		@Override
295 		public String getName() {
296 			return name;
297 		}
298 
299 		@Override
300 		public String getNamespace() {
301 			return namespace;
302 		}
303 
304 		@Override
305 		public String getDescription() {
306 			return description;
307 		}
308 
309 		@Override
310 		public String getTypeId() {
311 			return typeId;
312 		}
313 
314 		@Override
315 		public String getRuleId() {
316 			return ruleId;
317 		}
318 
319 		@Override
320 		public Integer getSequenceNumber() {
321 			return sequenceNumber;
322 		}
323 
324 		@Override
325 		public Map<String, String> getAttributes() {
326 			return attributes;
327 		}
328 
329         @Override
330         public Long getVersionNumber() {
331             return versionNumber;
332         }
333 
334 		/**
335 		 * Builds an instance of a Action based on the current state of the builder.
336 		 * 
337 		 * @return the fully-constructed Action
338 		 */
339         @Override
340         public ActionDefinition build() {
341             return new ActionDefinition(this);
342         }
343 		
344     }
345 	
346 	/**
347 	 * Defines some internal constants used on this class.
348 	 */
349 	static class Constants {
350 		final static String ROOT_ELEMENT_NAME = "action";
351 		final static String TYPE_NAME = "ActionType";
352 	}
353 	
354 	/**
355 	 * A private class which exposes constants which define the XML element names to use
356 	 * when this object is marshalled to XML.
357 	 */
358 	public static class Elements {
359 		final static String ID = "id";
360 		final static String NAME = "name";
361 		final static String NAMESPACE = "namespace";
362 		final static String DESC = "description";
363 		final static String TYPE_ID = "typeId";
364 		final static String RULE_ID = "ruleId";
365 		final static String SEQUENCE_NUMBER = "sequenceNumber";
366 		final static String ATTRIBUTES = "attributes";
367 	}
368 
369 }