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 "RuleBinding.java".  Description: 
010"An association between a type of item to be validated (eg a datatype or message) and a validation Rule." 
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;
029
030import ca.uhn.hl7v2.validation.Rule;
031
032/**
033 * An association between a type of item to be validated (eg a datatype or
034 * message) and a validation <code>Rule</code>.
035 * 
036 * @author Bryan Tripp
037 * @author James Agnew
038 * @author Christian Ohr
039 * */
040public class RuleBinding<T extends Rule<?>> implements Serializable {
041
042        private static final long serialVersionUID = 1L;
043
044        private boolean myActiveFlag;
045        private String myVersion;
046        private String myScope;
047        private T myRule;
048
049        /**
050         * Active by default.
051         * 
052         * @param theVersion
053         *            see {@link #getVersion()}
054         * @param theScope
055         *            see {@link #getScope()}
056         * @param theRule
057         *            see {@link #getRule()}
058         */
059        public RuleBinding(String theVersion, String theScope, T theRule) {
060                myActiveFlag = true;
061                myVersion = theVersion;
062                myScope = theScope;
063                myRule = theRule;
064        }
065
066        /**
067         * @return true if the binding is currently active
068         */
069        public boolean getActive() {
070                return myActiveFlag;
071        }
072
073        /**
074         * @param isActive
075         *            true if the binding is currently active
076         */
077        public void setActive(boolean isActive) {
078                myActiveFlag = isActive;
079        }
080
081        /**
082         * @return the version to which the binding applies (* means all versions)
083         */
084        public String getVersion() {
085                return myVersion;
086        }
087
088        /**
089         * @return the scope of item types to which the rule applies. For
090         *         <code>MessageRule</code>s this is the message type and trigger
091         *         event, separated by a ^ (either value may be *, meaning any). For
092         *         <code>PrimitiveTypeRule</code>s this is the datatype name (*
093         *         means any). For <code>EncodingRule</code>s this is the encoding
094         *         name (again, * means any).
095         */
096        public String getScope() {
097                return myScope;
098        }
099
100        /**
101         * @return a <code>Rule</code> that applies to the associated version and
102         *         scope
103         */
104        public T getRule() {
105                return myRule;
106        }
107
108        /**
109         * @param theVersion
110         *            an HL7 version
111         * @return true if this binding applies to the given version (ie
112         *         getVersion() matches or is *)
113         */
114        public boolean appliesToVersion(String theVersion) {
115                return applies(getVersion(), theVersion);
116        }
117
118        /**
119         * @param theType
120         *            an item description to be checked against getScope()
121         * @return true if the given type is within scope, ie if it matches
122         *         getScope() or getScope() is *
123         */
124        public boolean appliesToScope(String theType) {
125                return applies(getScope(), theType);
126        }
127
128        /**
129         * An abstraction of appliesToVersion() and appliesToScope(). Compares two values
130     * and returns true if they are equal or if the binding data is "*"
131         */
132        protected static boolean applies(String theBindingData, String theItemData) {
133                return (theBindingData.equals(theItemData) || theBindingData.equals("*"));
134        }
135
136        @Override
137        public String toString() {
138                return myRule.toString() + " for " + myScope + " in version " + myVersion;
139        }
140        
141        
142}