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 "ReportingValidationExceptionHandler.java". Description: 010"ValidationExceptionHandler that logs 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 org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import ca.uhn.hl7v2.HL7Exception; 032import ca.uhn.hl7v2.HapiContext; 033 034/** 035 * ValidationExceptionHandler that logs all {@link ValidationException}s and 036 * optionally throws the first one wrapped in a {@link HL7Exception}. 037 * <p> 038 * The mimics the behavior of the {@link MessageValidator} of previous releases. 039 * 040 * @author Christian Ohr 041 * 042 */ 043public class ReportingValidationExceptionHandler implements ValidationExceptionHandler<Boolean>, 044 ValidationExceptionHandlerFactory<Boolean> { 045 046 private static final Logger LOG = LoggerFactory 047 .getLogger(ReportingValidationExceptionHandler.class); 048 private boolean throwFirstException; 049 private ValidationException firstException; 050 051 /** 052 * @param throwFirstException true if first exception shall be thrown 053 */ 054 public ReportingValidationExceptionHandler(boolean throwFirstException) { 055 this.throwFirstException = throwFirstException; 056 } 057 058 /** 059 * Logs all exceptions 060 * 061 * @see ca.uhn.hl7v2.validation.CollectingValidationExceptionHandler#onExceptions(ca.uhn.hl7v2.validation.ValidationException[]) 062 */ 063 public void onExceptions(ValidationException... exceptions) { 064 if (firstException == null) 065 firstException = exceptions[0]; 066 for (ValidationException ve : exceptions) { 067 switch (ve.getSeverity()) { 068 case ERROR : LOG.error("Invalid message", ve); break; 069 case WARNING: LOG.warn("Message with warnings", ve); break; 070 case INFO: LOG.info("Message with comments", ve); break; 071 } 072 } 073 } 074 075 /** 076 * If the validation failed, throws Exception or returns <code>false</code>, 077 * depending on {link {@link #throwFirstException}. 078 * 079 * @throws HL7Exception if validation has failed and 080 * {@link #throwFirstException} is true 081 * @see ca.uhn.hl7v2.validation.DefaultValidationExceptionHandler#result() 082 */ 083 public Boolean result() throws HL7Exception { 084 if (hasFailed() && throwFirstException) 085 throw new HL7Exception(firstException.getMessage(), firstException); 086 return !hasFailed(); 087 } 088 089 public boolean hasFailed() { 090 return firstException != null; 091 } 092 093 public ValidationExceptionHandler<Boolean> getNewInstance(HapiContext context) { 094 return new ReportingValidationExceptionHandler(throwFirstException); 095 } 096 097 /** 098 * Does nothing 099 */ 100 public void setValidationSubject(Object subject) { 101 } 102 103 104 105}