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