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 "RespondingValidationExceptionHandler.java". Description: 010"ValidationExceptionHandler that generates response messages" 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.io.IOException; 029import java.util.List; 030 031import ca.uhn.hl7v2.AcknowledgmentCode; 032import ca.uhn.hl7v2.HL7Exception; 033import ca.uhn.hl7v2.HapiContext; 034import ca.uhn.hl7v2.Severity; 035import ca.uhn.hl7v2.model.Message; 036import ca.uhn.hl7v2.model.Segment; 037import ca.uhn.hl7v2.util.DeepCopy; 038 039/** 040 * ValidationExceptionHandler that generates response messages as statically 041 * configured and based on the outcome of a validation process. Aspects of 042 * creating and populating the response message can be overwritten in 043 * subclasses. 044 * <p> 045 * This handler (or a subclass thereof) can be used for validation if a 046 * response message must be generated upon the validation result. 047 * 048 * @author Christian Ohr 049 */ 050public class RespondingValidationExceptionHandler extends 051 CollectingValidationExceptionHandler<Message> implements 052 ValidationExceptionHandlerFactory<Message> { 053 054 private AcknowledgmentCode successAcknowledgementCode = AcknowledgmentCode.AA; 055 private AcknowledgmentCode errorAcknowledgementCode = AcknowledgmentCode.AE; 056 057 /** 058 * @param context Hapi context 059 */ 060 public RespondingValidationExceptionHandler(HapiContext context) { 061 super(context); 062 } 063 064 /** 065 * Returns the generated response message. 066 * 067 * @return the generated response 068 * @throws HL7Exception if no response could be generated 069 * @see {@link #generateResponseMessage(Object)} 070 * @see {@link #populateResponseMessage(ca.uhn.hl7v2.model.Message)} 071 * 072 */ 073 public final Message result() throws HL7Exception { 074 Object validationSubject = getValidationSubject(); 075 if (validationSubject == null) { 076 throw new HL7Exception("Need non-null validation subject"); 077 } 078 Message response = generateResponseMessage(validationSubject); 079 populateResponseMessage(response); 080 return response; 081 } 082 083 /** 084 * Set acknowledgment code (AA,CA) in case validation passes. 085 * 086 * @param successAcknowledgementCode (AA, CA) 087 */ 088 public void setSuccessAcknowledgementCode(AcknowledgmentCode successAcknowledgementCode) { 089 this.successAcknowledgementCode = successAcknowledgementCode; 090 } 091 092 /** 093 * Set acknowledgment code (AR, AE ,CR, CE) in case validation passes. 094 * 095 * @param errorAcknowledgementCode (AR, AE ,CR, CE) 096 */ 097 public void setErrorAcknowledgementCode(AcknowledgmentCode errorAcknowledgementCode) { 098 this.errorAcknowledgementCode = errorAcknowledgementCode; 099 } 100 101 /** 102 * @return the acknowledgement code if validation has succeeded. Default is AA. 103 */ 104 public AcknowledgmentCode getSuccessAcknowledgementCode() { 105 return successAcknowledgementCode; 106 } 107 108 /** 109 * @return the acknowledgement code if validation has failed. Default is AE. 110 */ 111 public AcknowledgmentCode getErrorAcknowledgementCode() { 112 return errorAcknowledgementCode; 113 } 114 115 /** 116 * Generates an empty response message. This class generates an 117 * ACKnowledgement using the code returned by {@link #getSuccessAcknowledgementCode()}. 118 * 119 * @param request request message, either a {@link String} or a 120 * {@link Message} 121 * @return acknowledgment to the request 122 * @throws HL7Exception 123 */ 124 protected Message generateResponseMessage(Object request) throws HL7Exception { 125 try { 126 Message in; 127 if (request instanceof String) { 128 Segment s = getHapiContext().getGenericParser().getCriticalResponseData( 129 (String)request); 130 in = s.getMessage(); 131 DeepCopy.copy(s, (Segment) in.get("MSH")); 132 } else if (request instanceof Message) { 133 in = (Message) request; 134 } else { 135 throw new HL7Exception("Validated message must be either Message or String"); 136 } 137 return in.generateACK(getSuccessAcknowledgementCode(), null); 138 139 } catch (IOException e) { 140 throw new HL7Exception(e); 141 } 142 } 143 144 /** 145 * Populates the generated response based on the collected 146 * {@link ValidationException}s. In case of exceptions, each exception will 147 * cause an entry in one or more ERR segments. 148 * 149 * @param response response message to be populated 150 * @throws HL7Exception 151 */ 152 protected void populateResponseMessage(Message response) throws HL7Exception { 153 if (response == null) 154 return; 155 List<ValidationException> exceptions = getExceptions(); 156 for (int i = 0; i < exceptions.size(); i++) { 157 ValidationException ve = exceptions.get(i); 158 // TODO respect minimumSeverity here? 159 if (ve.getSeverity() == Severity.ERROR) { 160 ve.populateResponse(response, getErrorAcknowledgementCode(), i); 161 } 162 } 163 } 164 165 public ValidationExceptionHandler<Message> getNewInstance(HapiContext context) { 166 RespondingValidationExceptionHandler handler = new RespondingValidationExceptionHandler(context); 167 handler.setErrorAcknowledgementCode(getErrorAcknowledgementCode()); 168 handler.setSuccessAcknowledgementCode(getSuccessAcknowledgementCode()); 169 handler.setMinimumSeverityToCollect(getMinimumSeverityToCollect()); 170 return handler; 171 } 172 173}