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 "CollectingValidationExceptionHandler.java".  Description: 
010"ValidationExceptionHandler that collects exceptions" 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132002.  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;
027
028import java.util.ArrayList;
029import java.util.Arrays;
030import java.util.Collections;
031import java.util.List;
032
033import ca.uhn.hl7v2.HapiContext;
034import ca.uhn.hl7v2.Severity;
035
036/**
037 * ValidationExceptionHandler that collects all {@link ValidationException}s in a list.
038 * Subclasses can then derive a overall {@link #result() result} from this list.
039 *
040 * @author Christian Ohr
041 * 
042 */
043public abstract class CollectingValidationExceptionHandler<R> extends AbstractValidationExceptionHandler<R> {
044
045        private List<ValidationException> exceptions = new ArrayList<ValidationException>();
046    private Severity minimumSeverityToCollect = Severity.ERROR;
047
048    /**
049     * @param context Hapi context
050     */
051    public CollectingValidationExceptionHandler(HapiContext context) {
052        super(context);
053    }
054
055    @Override
056    public void error(ValidationException exception) {
057        if (isSeverityAtLeast(Severity.ERROR)) exceptions.add(exception);
058    }
059
060    @Override
061    public void info(ValidationException exception) {
062        if (isSeverityAtLeast(Severity.INFO)) exceptions.add(exception);
063    }
064
065    @Override
066    public void warning(ValidationException exception) {
067        if (isSeverityAtLeast(Severity.WARNING)) exceptions.add(exception);
068    }
069
070    private boolean isSeverityAtLeast(Severity s) {
071        return s.compareTo(getMinimumSeverityToCollect()) >= 0;
072    }
073
074    /**
075         * @return unmodifiable list of collected exceptions. If none have occurred, the list is empty.
076         */
077        public List<ValidationException> getExceptions() {
078                return Collections.unmodifiableList(exceptions);
079        }
080
081    /**
082     * Set the minimum severity to be added to the list of exceptions. Default is ERROR.
083     * @param minimumSeverityToCollect the minimum severity to be added to the list of exceptions
084     */
085    public void setMinimumSeverityToCollect(Severity minimumSeverityToCollect) {
086        this.minimumSeverityToCollect = minimumSeverityToCollect;
087    }
088
089    /**
090     * @return the minimum severity to be added to the list of exceptions. Default is ERROR.
091     */
092    public Severity getMinimumSeverityToCollect() {
093        return minimumSeverityToCollect;
094    }
095
096    /**
097     * @see ca.uhn.hl7v2.validation.ValidationExceptionHandler#hasFailed()
098     */
099    public boolean hasFailed() {
100        for (ValidationException ve : exceptions) {
101            if (ve.getSeverity() == Severity.ERROR) return true;
102        }
103        return false;
104    }
105        
106        
107
108}