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 <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
037 * @version $Revision: 1.2 $ updated on $Date: 2010-04-25 16:22:52 $ by $Author: jamesagnew $
038 */
039public class RuleBinding<T extends Rule> implements Serializable {
040
041    private static final long serialVersionUID = 1L;
042    
043    private boolean myActiveFlag;
044    private String myVersion;
045    private String myScope;
046    private T myRule;
047    
048    /**
049     * Active by default.  
050     * 
051     * @param theVersion see {@link #getVersion()}
052     * @param theScope see {@link #getScope()}
053     * @param theRule see {@link #getRule()}
054     */
055    public RuleBinding(String theVersion, String theScope, T theRule) {
056        myActiveFlag = true;
057        myVersion = theVersion;
058        myScope = theScope;
059        myRule = theRule;
060    }
061    
062    /**
063     * @return true if the binding is currently active
064     */
065    public boolean getActive() {
066        return myActiveFlag;
067    }
068    
069    /**
070     * @param isActive true if the binding is currently active
071     */
072    public void setActive(boolean isActive) {
073        myActiveFlag = isActive;
074    }
075    
076    /**
077     * @return the version to which the binding applies (* means all versions)
078     */
079    public String getVersion() {
080        return myVersion;
081    }
082    
083    /**
084     * @return the scope of item types to which the rule applies.  For <code>MessageRule</code>s
085     *      this is the message type and trigger event, separated by a ^ (either value may be *, meaning 
086     *      any).  For <code>PrimitiveTypeRule</code>s this is the datatype name (* means any).  For 
087     *      <code>EncodingRule</code>s this is the encoding name (again, * means any).   
088     */
089    public String getScope() {
090        return myScope;
091    }
092    
093    /**
094     * @return a <code>Rule</code> that applies to the associated version and scope
095     */
096    public T getRule() {
097        return myRule;
098    }
099
100    /**
101     * @param theVersion an HL7 version
102     * @return true if this binding applies to the given version (ie getVersion() matches or is *)  
103     */
104    public boolean appliesToVersion(String theVersion) {
105        return applies(getVersion(), theVersion);
106    }
107    
108    /**
109     * @param theType an item description to be checked against getScope()  
110     * @return true if the given type is within scope, ie if it matches getScope() or getScope() is * 
111     */
112    public boolean appliesToScope(String theType) {
113        return applies(getScope(), theType);
114    }
115    
116    /**
117     * An abstraction of appliesToVersion() and appliesToScope(). 
118     * 
119     * @param theBindingData
120     * @param theItemData
121     * @return
122     */
123    protected boolean applies(String theBindingData, String theItemData) {
124        boolean applies = false;
125        if (theBindingData.equals(theItemData) || theBindingData.equals("*")) {
126            applies = true;
127        }
128        return applies;        
129    }
130}