| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RespondingValidationExceptionHandler |
|
| 2.3333333333333335;2.333 |
| 1 | /** | |
| 2 | The contents of this file are subject to the Mozilla Public License Version 1.1 | |
| 3 | (the "License"); you may not use this file except in compliance with the License. | |
| 4 | You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
| 5 | Software distributed under the License is distributed on an "AS IS" basis, | |
| 6 | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the | |
| 7 | specific language governing rights and limitations under the License. | |
| 8 | ||
| 9 | The Original Code is "RespondingValidationExceptionHandler.java". Description: | |
| 10 | "ValidationExceptionHandler that generates response messages" | |
| 11 | ||
| 12 | The Initial Developer of the Original Code is University Health Network. Copyright (C) | |
| 13 | 2002. All Rights Reserved. | |
| 14 | ||
| 15 | Contributor(s): ______________________________________. | |
| 16 | ||
| 17 | Alternatively, the contents of this file may be used under the terms of the | |
| 18 | GNU General Public License (the "GPL"), in which case the provisions of the GPL are | |
| 19 | applicable instead of those above. If you wish to allow use of your version of this | |
| 20 | file only under the terms of the GPL and not to allow others to use your version | |
| 21 | of this file under the MPL, indicate your decision by deleting the provisions above | |
| 22 | and replace them with the notice and other provisions required by the GPL License. | |
| 23 | If you do not delete the provisions above, a recipient may use your version of | |
| 24 | this file under either the MPL or the GPL. | |
| 25 | */ | |
| 26 | package ca.uhn.hl7v2.validation; | |
| 27 | ||
| 28 | import java.io.IOException; | |
| 29 | import java.util.List; | |
| 30 | ||
| 31 | import ca.uhn.hl7v2.AcknowledgmentCode; | |
| 32 | import ca.uhn.hl7v2.HL7Exception; | |
| 33 | import ca.uhn.hl7v2.HapiContext; | |
| 34 | import ca.uhn.hl7v2.Severity; | |
| 35 | import ca.uhn.hl7v2.model.Message; | |
| 36 | import ca.uhn.hl7v2.model.Segment; | |
| 37 | import ca.uhn.hl7v2.util.DeepCopy; | |
| 38 | ||
| 39 | /** | |
| 40 | * ValidationExceptionHandler that generates response messages as statically | |
| 41 | * configured and based on the outcome of a validation process. Aspects of | |
| 42 | * creating and populating the response message can be overwritten in | |
| 43 | * subclasses. | |
| 44 | * <p> | |
| 45 | * This handler (or a subclass thereof) can be used for validation if a | |
| 46 | * response message must be generated upon the validation result. | |
| 47 | * | |
| 48 | * @author Christian Ohr | |
| 49 | */ | |
| 50 | 55 | public class RespondingValidationExceptionHandler extends |
| 51 | CollectingValidationExceptionHandler<Message> implements | |
| 52 | ValidationExceptionHandlerFactory<Message> { | |
| 53 | ||
| 54 | 75 | private AcknowledgmentCode successAcknowledgementCode = AcknowledgmentCode.AA; |
| 55 | 75 | private AcknowledgmentCode errorAcknowledgementCode = AcknowledgmentCode.AE; |
| 56 | ||
| 57 | /** | |
| 58 | * @param context Hapi context | |
| 59 | */ | |
| 60 | public RespondingValidationExceptionHandler(HapiContext context) { | |
| 61 | 75 | super(context); |
| 62 | 75 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Returns the generated response message. | |
| 66 | * | |
| 67 | * @return the generated response | |
| 68 | * @throws HL7Exception if no response could be generated | |
| 69 | * @see {@link #generateResponseMessage(Object)} | |
| 70 | * @see {@link #populateResponseMessage(ca.uhn.hl7v2.model.Message)} | |
| 71 | * | |
| 72 | */ | |
| 73 | public final Message result() throws HL7Exception { | |
| 74 | 55 | Object validationSubject = getValidationSubject(); |
| 75 | 55 | if (validationSubject == null) { |
| 76 | 0 | throw new HL7Exception("Need non-null validation subject"); |
| 77 | } | |
| 78 | 55 | Message response = generateResponseMessage(validationSubject); |
| 79 | 55 | populateResponseMessage(response); |
| 80 | 55 | return response; |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Set acknowledgment code (AA,CA) in case validation passes. | |
| 85 | * | |
| 86 | * @param successAcknowledgementCode (AA, CA) | |
| 87 | */ | |
| 88 | public void setSuccessAcknowledgementCode(AcknowledgmentCode successAcknowledgementCode) { | |
| 89 | 50 | this.successAcknowledgementCode = successAcknowledgementCode; |
| 90 | 50 | } |
| 91 | ||
| 92 | /** | |
| 93 | * Set acknowledgment code (AR, AE ,CR, CE) in case validation passes. | |
| 94 | * | |
| 95 | * @param errorAcknowledgementCode (AR, AE ,CR, CE) | |
| 96 | */ | |
| 97 | public void setErrorAcknowledgementCode(AcknowledgmentCode errorAcknowledgementCode) { | |
| 98 | 65 | this.errorAcknowledgementCode = errorAcknowledgementCode; |
| 99 | 65 | } |
| 100 | ||
| 101 | /** | |
| 102 | * @return the acknowledgement code if validation has succeeded. Default is AA. | |
| 103 | */ | |
| 104 | public AcknowledgmentCode getSuccessAcknowledgementCode() { | |
| 105 | 105 | return successAcknowledgementCode; |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * @return the acknowledgement code if validation has failed. Default is AE. | |
| 110 | */ | |
| 111 | public AcknowledgmentCode getErrorAcknowledgementCode() { | |
| 112 | 90 | 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 | 55 | if (request instanceof String) { |
| 128 | 20 | Segment s = getHapiContext().getGenericParser().getCriticalResponseData( |
| 129 | (String)request); | |
| 130 | 20 | in = s.getMessage(); |
| 131 | 20 | DeepCopy.copy(s, (Segment) in.get("MSH")); |
| 132 | 20 | } else if (request instanceof Message) { |
| 133 | 35 | in = (Message) request; |
| 134 | } else { | |
| 135 | 0 | throw new HL7Exception("Validated message must be either Message or String"); |
| 136 | } | |
| 137 | 55 | return in.generateACK(getSuccessAcknowledgementCode(), null); |
| 138 | ||
| 139 | 0 | } catch (IOException e) { |
| 140 | 0 | 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 | 55 | if (response == null) |
| 154 | 0 | return; |
| 155 | 55 | List<ValidationException> exceptions = getExceptions(); |
| 156 | 95 | for (int i = 0; i < exceptions.size(); i++) { |
| 157 | 40 | ValidationException ve = exceptions.get(i); |
| 158 | // TODO respect minimumSeverity here? | |
| 159 | 40 | if (ve.getSeverity() == Severity.ERROR) { |
| 160 | 40 | ve.populateResponse(response, getErrorAcknowledgementCode(), i); |
| 161 | } | |
| 162 | } | |
| 163 | 55 | } |
| 164 | ||
| 165 | public ValidationExceptionHandler<Message> getNewInstance(HapiContext context) { | |
| 166 | 50 | RespondingValidationExceptionHandler handler = new RespondingValidationExceptionHandler(context); |
| 167 | 50 | handler.setErrorAcknowledgementCode(getErrorAcknowledgementCode()); |
| 168 | 50 | handler.setSuccessAcknowledgementCode(getSuccessAcknowledgementCode()); |
| 169 | 50 | handler.setMinimumSeverityToCollect(getMinimumSeverityToCollect()); |
| 170 | 50 | return handler; |
| 171 | } | |
| 172 | ||
| 173 | } |