001    /**
002    The 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. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "ValidationContextImpl.java".  Description: 
010    "A default implementation of ValidationContext." 
011    
012    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
013    2004.  All Rights Reserved. 
014    
015    Contributor(s): ______________________________________. 
016    
017    Alternatively, the contents of this file may be used under the terms of the 
018    GNU General Public License (the  ???GPL???), in which case the provisions of the GPL are 
019    applicable instead of those above.  If you wish to allow use of your version of this 
020    file only under the terms of the GPL and not to allow others to use your version 
021    of this file under the MPL, indicate your decision by deleting  the provisions above 
022    and replace  them with the notice and other provisions required by the GPL License.  
023    If you do not delete the provisions above, a recipient may use your version of 
024    this file under either the MPL or the GPL. 
025    */
026    package ca.uhn.hl7v2.validation.impl;
027    
028    import java.io.Serializable;
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    import ca.uhn.hl7v2.model.Primitive;
033    import ca.uhn.hl7v2.validation.PrimitiveTypeRule;
034    import ca.uhn.hl7v2.validation.EncodingRule;
035    import ca.uhn.hl7v2.validation.MessageRule;
036    import ca.uhn.hl7v2.validation.ValidationContext;
037    
038    /**
039     * A default implementation of <code>ValidationContext</code>. 
040     * 
041     * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
042     * @version $Revision: 1.2 $ updated on $Date: 2009/08/09 13:31:13 $ by $Author: jamesagnew $
043     */
044    public class ValidationContextImpl implements ValidationContext, Serializable {
045    
046        private List myPrimitiveRuleBindings;
047        private List myMessageRuleBindings;
048        private List myEncodingRuleBindings;
049        private boolean myCheckPrimitivesFlag;
050        
051        public ValidationContextImpl() {
052            myPrimitiveRuleBindings = new ArrayList(30);
053            myMessageRuleBindings = new ArrayList(20);
054            myEncodingRuleBindings = new ArrayList(10);
055            myCheckPrimitivesFlag = true;
056        }
057    
058        /** 
059         * @see ca.uhn.hl7v2.validation.ValidationContext#getDataTypeRules(java.lang.String, java.lang.String)
060         * @param theType ignored 
061         */
062        public PrimitiveTypeRule[] getPrimitiveRules(String theVersion, String theTypeName, Primitive theType) {
063            List active = new ArrayList(myPrimitiveRuleBindings.size());
064            for (int i = 0; i < myPrimitiveRuleBindings.size(); i++) {
065                Object o = myPrimitiveRuleBindings.get(i);
066                if ( !(o instanceof RuleBinding) ) {
067                    throw new ClassCastException("Item in rule binding list is not a RuleBinding");
068                }
069                
070                RuleBinding binding = (RuleBinding) o;
071                if (binding.getActive() 
072                        && binding.appliesToVersion(theVersion) 
073                        && binding.appliesToScope(theTypeName)) {
074                    active.add(binding.getRule());
075                }
076            }
077            return (PrimitiveTypeRule[]) active.toArray(new PrimitiveTypeRule[0]);
078        }
079        
080        /**
081         * @return a List of <code>RuleBinding</code>s for <code>PrimitiveTypeRule</code>s.    
082         */
083        public List getPrimitiveRuleBindings() {
084            return myPrimitiveRuleBindings;
085        }
086    
087        /**
088         * @see ca.uhn.hl7v2.validation.ValidationContext
089         *      #getMessageRules(java.lang.String, java.lang.String, java.lang.String)
090         */
091        public MessageRule[] getMessageRules(String theVersion, String theMessageType, String theTriggerEvent) {
092                
093            List active = new ArrayList(myMessageRuleBindings.size());
094            for (int i = 0; i < myMessageRuleBindings.size(); i++) {
095                Object o = myMessageRuleBindings.get(i);
096                if ( !(o instanceof RuleBinding) ) {
097                    throw new ClassCastException("Item in rule binding list is not a RuleBinding");
098                }
099            
100                RuleBinding binding = (RuleBinding) o;
101                if (binding.getActive() 
102                        && binding.appliesToVersion(theVersion) 
103                        && binding.appliesToScope(theMessageType + "^" + theTriggerEvent)) {
104                    active.add(binding.getRule());
105                }
106            }
107            return (MessageRule[]) active.toArray(new MessageRule[0]);
108        }
109    
110        /**
111         * @return a List of <code>RuleBinding</code>s for <code>MessageRule</code>s.    
112         */
113        public List getMessageRuleBindings() {
114            return myMessageRuleBindings;
115        }
116    
117        /** 
118         * @see ca.uhn.hl7v2.validation.ValidationContext#getEncodingRules(java.lang.String, java.lang.String)
119         */
120        public EncodingRule[] getEncodingRules(String theVersion, String theEncoding) {
121            List active = new ArrayList(myEncodingRuleBindings.size());
122            for (int i = 0; i < myEncodingRuleBindings.size(); i++) {
123                Object o = myEncodingRuleBindings.get(i);
124                if ( !(o instanceof RuleBinding) ) {
125                    throw new ClassCastException("Item in rule binding list is not a RuleBinding");
126                }
127                
128                RuleBinding binding = (RuleBinding) o;
129                if (binding.getActive() 
130                        && binding.appliesToVersion(theVersion) 
131                        && binding.appliesToScope(theEncoding)) {
132                    active.add(binding.getRule());
133                }
134            }
135            return (EncodingRule[]) active.toArray(new EncodingRule[0]);
136        }
137    
138        /**
139         * @return a List of <code>RuleBinding</code>s for <code>EncodingRule</code>s.    
140         */
141        public List getEncodingRuleBindings() {
142            return myEncodingRuleBindings;
143        }
144    
145    //    /**
146    //     * @see ca.uhn.hl7v2.validation.ValidationContext#getCheckPrimitivesOnSet()
147    //     */
148    //    public boolean getCheckPrimitivesOnSet() {
149    //        return myCheckPrimitivesFlag;
150    //    }
151    //
152    //    /**
153    //     * @see ca.uhn.hl7v2.validation.ValidationContext#setCheckPrimitivesOnSet(boolean)
154    //     */
155    //    public void setCheckPrimitivesOnSet(boolean check) {
156    //        myCheckPrimitivesFlag = check;
157    //    }
158        
159    }