001/** 002The contents of this file are subject to the Mozilla Public License Version 1.1 003(the "License"); you may not use this file except in compliance with the License. 004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005Software distributed under the License is distributed on an "AS IS" basis, 006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007specific language governing rights and limitations under the License. 008 009The Original Code is "ValidationContextImpl.java". Description: 010"A default implementation of ValidationContext." 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132004. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the ?GPL?), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025 */ 026package ca.uhn.hl7v2.validation.impl; 027 028import java.io.Serializable; 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.List; 032 033import ca.uhn.hl7v2.model.Primitive; 034import ca.uhn.hl7v2.validation.EncodingRule; 035import ca.uhn.hl7v2.validation.MessageRule; 036import ca.uhn.hl7v2.validation.PrimitiveTypeRule; 037import ca.uhn.hl7v2.validation.Rule; 038import ca.uhn.hl7v2.validation.ValidationContext; 039import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder; 040 041/** 042 * A default implementation of <code>ValidationContext</code>. 043 * 044 * @author Bryan Tripp 045 * @author Christian Ohr 046 */ 047@SuppressWarnings("serial") 048public class ValidationContextImpl implements ValidationContext, Serializable { 049 050 private List<RuleBinding<PrimitiveTypeRule>> myPrimitiveRuleBindings; 051 private List<RuleBinding<MessageRule>> myMessageRuleBindings; 052 private List<RuleBinding<EncodingRule>> myEncodingRuleBindings; 053 054 public ValidationContextImpl() { 055 myPrimitiveRuleBindings = new ArrayList<RuleBinding<PrimitiveTypeRule>>(); 056 myMessageRuleBindings = new ArrayList<RuleBinding<MessageRule>>(); 057 myEncodingRuleBindings = new ArrayList<RuleBinding<EncodingRule>>(); 058 } 059 060 ValidationContextImpl(ValidationRuleBuilder builder) { 061 this(); 062 for (RuleBinding<? extends Rule<?>> ruleBinding : builder.initialize()) { 063 if (ruleBinding instanceof MessageRuleBinding) 064 myMessageRuleBindings.add((MessageRuleBinding)ruleBinding); 065 else if (ruleBinding instanceof EncodingRuleBinding) 066 myEncodingRuleBindings.add((EncodingRuleBinding)ruleBinding); 067 else if (ruleBinding instanceof PrimitiveTypeRuleBinding) 068 myPrimitiveRuleBindings.add((PrimitiveTypeRuleBinding)ruleBinding); 069 } 070 } 071 072 073 /** 074 * @see ValidationContext#getPrimitiveRules(String, String, Primitive) 075 * @param theType ignored 076 */ 077 public Collection<PrimitiveTypeRule> getPrimitiveRules(String theVersion, String theTypeName, Primitive theType) { 078 return getRules(myPrimitiveRuleBindings, theVersion, theTypeName); 079 } 080 081 /** 082 * @return a List of <code>RuleBinding</code>s for 083 * <code>PrimitiveTypeRule</code>s. 084 */ 085 public List<RuleBinding<PrimitiveTypeRule>> getPrimitiveRuleBindings() { 086 return myPrimitiveRuleBindings; 087 } 088 089 090 /** 091 * @see ValidationContext#getMessageRules(java.lang.String, java.lang.String, java.lang.String) 092 */ 093 public Collection<MessageRule> getMessageRules(String theVersion, String theMessageType, String theTriggerEvent) { 094 return getRules(myMessageRuleBindings, theVersion, theMessageType + "^" + theTriggerEvent); 095 } 096 097 /** 098 * @return a List of <code>RuleBinding</code>s for <code>MessageRule</code>s. 099 */ 100 public List<RuleBinding<MessageRule>> getMessageRuleBindings() { 101 return myMessageRuleBindings; 102 } 103 104 /** 105 * @see ca.uhn.hl7v2.validation.ValidationContext#getEncodingRules(java.lang.String, 106 * java.lang.String) 107 */ 108 public Collection<EncodingRule> getEncodingRules(String theVersion, String theEncoding) { 109 return getRules(myEncodingRuleBindings, theVersion, theEncoding); 110 } 111 112 /** 113 * @return a List of <code>RuleBinding</code>s for <code>EncodingRule</code>s. 114 */ 115 public List<RuleBinding<EncodingRule>> getEncodingRuleBindings() { 116 return myEncodingRuleBindings; 117 } 118 119 private <T extends Rule<?>> Collection<T> getRules(List<RuleBinding<T>> bindings, String version, String scope) { 120 List<T> active = new ArrayList<T>(bindings.size()); 121 for (RuleBinding<T> binding : bindings) { 122 if (applies(binding, version, scope)) 123 active.add(binding.getRule()); 124 } 125 return active; 126 } 127 128 private boolean applies(RuleBinding<?> binding, String version, String scope) { 129 return (binding.getActive() && binding.appliesToVersion(version) && binding.appliesToScope(scope)); 130 } 131 132 133}