View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "RuleTypeBuilder.java".  Description: 
10  "RuleBuilder that determines which kind of rule shall be built" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2004.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25   */
26  package ca.uhn.hl7v2.validation.builder;
27  
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collection;
31  import java.util.Collections;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Set;
35  
36  import ca.uhn.hl7v2.Version;
37  import ca.uhn.hl7v2.validation.MessageRule;
38  import ca.uhn.hl7v2.validation.PrimitiveTypeRule;
39  import ca.uhn.hl7v2.validation.Rule;
40  import ca.uhn.hl7v2.Severity;
41  import ca.uhn.hl7v2.validation.impl.RuleBinding;
42  import ca.uhn.hl7v2.validation.impl.RuleSupport;
43  
44  /**
45   * Defines the type of rule to be built.
46   * <p>
47   * The recursive type parameter allows the builder methods common to all subclasses (e.g.
48   * {@link #refersToSection}, {@link #active}, {@link #test}) to return their specific builder type.
49   * 
50   * @author Christian Ohr
51   */
52  @SuppressWarnings("serial")
53  public class RuleTypeBuilder<S extends RuleTypeBuilder<S, T>, T extends Rule<?>> extends
54  		BuilderSupport {
55  
56  	private List<RuleBinding<? extends Rule<?>>> rules = new ArrayList<RuleBinding<? extends Rule<?>>>();
57  	private Set<Version> versions;
58  	private String description;
59  	private String sectionReference;
60  	private boolean active = true;
61      private Severity severity = Severity.ERROR;
62  
63  	protected RuleTypeBuilder() {
64  		super();
65  	}
66  	
67  	protected RuleTypeBuilder(List<RuleBinding<? extends Rule<?>>> rules, Set<Version> versions) {
68  		super();
69  		if (versions.size() == 0)
70  			throw new IllegalArgumentException("Must specify a version");
71  		this.rules = rules;
72  		this.versions = versions;
73  	}
74  
75  	protected RuleTypeBuilder(List<RuleBinding<? extends Rule<?>>> rules, Version... versions) {
76  		super();
77  		if (versions.length == 0)
78  			throw new IllegalArgumentException("Must specify a version");
79  		this.rules = rules;
80  		this.versions = new HashSet<Version>(Arrays.asList(versions));
81  	}
82  
83  	@SuppressWarnings("unchecked")
84  	protected S instance() {
85  		return (S) this;
86  	}
87  	
88  	protected List<RuleBinding<? extends Rule<?>>> getRules() {
89  		return rules;
90  	}
91  
92      protected T prepareRule(T rule) {
93          if (rule instanceof RuleSupport) {
94              RuleSupport<?> rs = (RuleSupport<?>)rule;
95              if (description != null) rs.setDescription(description);
96              if (sectionReference != null) rs.setSectionReference(sectionReference);
97              rs.setSeverity(severity);
98          }
99          return rule;
100     }
101 
102 	/**
103 	 * Adds a description to the rule
104 	 * 
105 	 * @param description description
106 	 * @return this instance to build more rules
107 	 */
108 	public S description(String description) {
109 		this.description = description;
110 		return instance();
111 	}
112 
113 	/**
114 	 * Adds a HL7 section reference to a rule
115 	 * 
116 	 * @param sectionReference the section in the HL7 specification
117 	 * @return this instance to build more rules
118 	 */
119 	public S refersToSection(String sectionReference) {
120 		this.sectionReference = sectionReference;
121 		return instance();
122 	}
123 
124     /**
125      * Sets the severity of the rule
126      *
127      * @param severity the the severity of the rule
128      * @return this instance to build more rules
129      */
130     public S severity(Severity severity) {
131         this.severity = severity;
132         return instance();
133     }
134 
135 	/**
136 	 * Marks the rule as being active (default) or inactive
137 	 * 
138 	 * @param active true if this rule shall be active
139 	 * @return this instance to build more rules
140 	 */
141 	public S active(boolean active) {
142 		this.active = active;
143 		return instance();
144 	}
145 
146 	/**
147 	 * Adds the specified rule to the set of rules.
148 	 * 
149 	 * @param rule the rule to be tested
150 	 * @return this instance to build more rules
151 	 */
152 	public S test(T rule) {
153 		addRuleBindings(rule);
154 		return instance();
155 	}
156 
157 	/**
158 	 * Builds {@link PrimitiveTypeRule}s for the specified types
159 	 * 
160 	 * @param type an array of types
161 	 * @return this instance to continue building rules
162 	 */
163 	public PrimitiveRuleBuilder primitive(String... type) {
164 		if (type.length == 0) {
165 			throw new IllegalArgumentException("Must specify a type");
166 		}
167 		return new PrimitiveRuleBuilder(rules, versions, new HashSet<String>(Arrays.asList(type)));
168 	}
169 
170 	/**
171 	 * Builds {@link MessageRule}s for the specified event types and triggers
172 	 * 
173 	 * @param eventType Event type, e.g. "ADT", or "*" for all types
174 	 * @param triggerEvent, e.g. "A01" or "*" for all trigger events
175 	 * @return this instance to continue building rules
176 	 */
177 	public MessageRuleBuilder message(String eventType, String triggerEvent) {
178 		return new MessageRuleBuilder(rules, versions, eventType, triggerEvent);
179 	}
180 
181     /**
182      * Builds {@link MessageRule}s for event types and triggers to be specified
183      * using the returned MessageExpressionBuilder.
184      *
185      * @return MessageExpressionBuilder instance to continue building rules
186      */
187 	public MessageExpressionBuilder message() {
188 		return new MessageExpressionBuilder();
189 	}
190 
191 	/**
192 	 * Builds {@link MessageRule}s for the specified encoding
193 	 * 
194 	 * @param encoding "XML" or "VB"
195 	 * @return this instance to continue building rules
196 	 */
197 	public EncodingRuleBuilder encoding(String encoding) {
198 		return new EncodingRuleBuilder(rules, versions, encoding);
199 	}
200 
201 	/**
202 	 * Add {@link RuleBinding}s for the rule that have been built
203 	 * 
204 	 * @param rule the rule for which bindings shall be added
205 	 */
206 	protected void addRuleBindings(T rule) {
207 		if (Version.allVersions(versions)) {
208 			// Save some bindings when all HL7 versions are affected
209 			rules.addAll(getRuleBindings(rule, "*"));
210 		} else {
211 			for (Version version : versions) {
212 				rules.addAll(getRuleBindings(rule, version.getVersion()));
213 			}
214 		}
215 	}
216 
217 	/**
218 	 * Builder implementation must overwrite this method to return all {@link RuleBinding}s for
219 	 * rules that have been built.
220 	 * 
221 	 * @param rule the rule for which bindings shall be retrieved
222 	 * @param version the HL7 version for which bindings shall be retrieved
223 	 * @return a collection of {@link RuleBinding}s
224 	 */
225 	@SuppressWarnings("unchecked")
226 	protected Collection<RuleBinding<T>> getRuleBindings(T rule, String version) {
227 		return (Collection<RuleBinding<T>>) Collections.EMPTY_LIST;
228 	}
229 
230 	protected Collection<RuleBinding<T>> activate(Collection<RuleBinding<T>> bindings) {
231 		for (RuleBinding<T> ruleBinding : bindings) {
232 			ruleBinding.setActive(active);
233 		}
234 		return bindings;
235 	}
236 
237 	// for tests only
238 	Set<Version> getVersions() {
239 		return versions;
240 	}
241 
242 	/**
243 	 * Helper builder when the versions are not given explicitly but in form of an expression.
244 	 */
245 	public class MessageExpressionBuilder {
246 
247         /**
248          * Applies {@link MessageRule}s for all event types and trigger events
249          * @return rule builder
250          */
251 		public MessageRuleBuilder all() {
252 			return new MessageRuleBuilder(rules, versions, "*", "*");
253 		}
254 
255         /**
256          * Applies {@link MessageRule}s for all trigger events of a given event type
257          * @param eventType  event type, e.g. "ADT"
258          * @return rule builder
259          */
260 		public MessageRuleBuilder allOfEventType(String eventType) {
261 			return new MessageRuleBuilder(rules, versions, eventType, "*");
262 		}
263 
264 	}
265 
266 }