Coverage Report - ca.uhn.hl7v2.app.DefaultApplication
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultApplication
66%
16/24
45%
5/11
2.875
DefaultApplication$1
100%
1/1
N/A
2.875
 
 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 "DefaultApplication.java".  Description: 
 10  
 "An Application that does nothing with the message and returns an Application 
 11  
  Reject message in response." 
 12  
 
 13  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 14  
 2002.  All Rights Reserved. 
 15  
 
 16  
 Contributor(s): ______________________________________. 
 17  
 
 18  
 Alternatively, the contents of this file may be used under the terms of the 
 19  
 GNU General Public License (the  "GPL"), in which case the provisions of the GPL are 
 20  
 applicable instead of those above.  If you wish to allow use of your version of this 
 21  
 file only under the terms of the GPL and not to allow others to use your version 
 22  
 of this file under the MPL, indicate your decision by deleting  the provisions above 
 23  
 and replace  them with the notice and other provisions required by the GPL License.  
 24  
 If you do not delete the provisions above, a recipient may use your version of 
 25  
 this file under either the MPL or the GPL. 
 26  
  */
 27  
 
 28  
 package ca.uhn.hl7v2.app;
 29  
 
 30  
 import java.io.IOException;
 31  
 import java.util.Map;
 32  
 
 33  
 import ca.uhn.hl7v2.AcknowledgmentCode;
 34  
 import ca.uhn.hl7v2.HL7Exception;
 35  
 import ca.uhn.hl7v2.model.Message;
 36  
 import ca.uhn.hl7v2.model.Segment;
 37  
 import ca.uhn.hl7v2.protocol.ReceivingApplication;
 38  
 import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
 39  
 import ca.uhn.hl7v2.util.DeepCopy;
 40  
 
 41  
 /**
 42  
  * An Application that does nothing with the message and returns an Application
 43  
  * Reject message in response. To be used when there are no other Applications
 44  
  * that can process a given message.
 45  
  * 
 46  
  * @author Bryan Tripp
 47  
  * @author Christian Ohr
 48  
  */
 49  
 public class DefaultApplication implements ReceivingApplication<Message> {
 50  
 
 51  
         public static final String MSG_INTERNAL_ERROR = "Application internal error";
 52  
         public static final String MSG_NO_APPROPRIATE_DEST = "No appropriate destination could be found to which this message could be routed.";
 53  
         
 54  
         private final AcknowledgmentCode myAcknowledgmentCode;
 55  
         private final String myMessage;
 56  
         
 57  
         /**
 58  
          * New instance which always returns an acknowledgment code of {@link AcknowledgmentCode#AR} and
 59  
          * a message indicating that no appropriate handler could be found.
 60  
          * 
 61  
          * @see #MSG_NO_APPROPRIATE_DEST
 62  
          */
 63  
         public DefaultApplication() {
 64  265
                 this(AcknowledgmentCode.AR);
 65  265
         }
 66  
 
 67  
         /**
 68  
          * <p>
 69  
          * New instance which always uses the given acknowledgment code, and generates an
 70  
          * appropriate message for the given code, according to the following rules:
 71  
          * </p>
 72  
          * <table>
 73  
          * <tr><th>Code</th><th>Message</th></tr>
 74  
          * <tr>
 75  
          *                 <td>{@link AcknowledgmentCode#AA}<br/>{@link AcknowledgmentCode#AA}</td>
 76  
          *                 <td><code>null</code> (no message)</td>
 77  
          * </tr>
 78  
          * <tr>
 79  
          *                 <td>{@link AcknowledgmentCode#AR}<br/>{@link AcknowledgmentCode#AR}</td>
 80  
          *                 <td>{@link #MSG_NO_APPROPRIATE_DEST}</td>
 81  
          * </tr>
 82  
          * <tr>
 83  
          *                 <td>{@link AcknowledgmentCode#AE}<br/>{@link AcknowledgmentCode#AE}</td>
 84  
          *                 <td>{@link #MSG_INTERNAL_ERROR}</td>
 85  
          * </tr>
 86  
          * </table>
 87  
          * 
 88  
          * @param theAcknowledgmentCode The code to always use (must not be null)
 89  
          */
 90  
         public DefaultApplication(AcknowledgmentCode theAcknowledgmentCode) {
 91  275
                 this(theAcknowledgmentCode, defaultMessage(theAcknowledgmentCode));
 92  275
         }
 93  
 
 94  
         /**
 95  
          * New instance which always uses the given acknowledgment code and message
 96  
          *  
 97  
          * @param theAcknowledgmentCode The code to always use (must not be null)
 98  
          * @param theMessage The message (may be null, in which case no message will be given)
 99  
          */
 100  275
         public DefaultApplication(AcknowledgmentCode theAcknowledgmentCode, String theMessage) {
 101  275
                 if (theAcknowledgmentCode == null) {
 102  0
                         throw new NullPointerException("Acknowledgment code must not be null");
 103  
                 }
 104  275
                 myAcknowledgmentCode = theAcknowledgmentCode;
 105  275
                 myMessage = theMessage;
 106  275
         }
 107  
 
 108  
         private static String defaultMessage(AcknowledgmentCode theAr) {
 109  5
                 switch (theAr) {
 110  
                 case AA:
 111  
                 case CA:
 112  10
                         return null;
 113  
                 case AR:
 114  
                 case CR:
 115  265
                         return MSG_NO_APPROPRIATE_DEST;
 116  
                 case AE:
 117  
                 case CE:
 118  
                 default:
 119  0
                         return MSG_INTERNAL_ERROR;
 120  
                 }
 121  
         }
 122  
 
 123  
         /**
 124  
          * Returns true.
 125  
          */
 126  
         public boolean canProcess(Message in) {
 127  5
                 return true;
 128  
         }
 129  
 
 130  
         /**
 131  
          * Creates an ACK message with the minimum required information from an
 132  
          * inbound message. Optional fields can be filled in afterwards, before the
 133  
          * message is returned. Please note that MSH-10, the outbound message
 134  
          * control ID, is also set using the class
 135  
          * <code>ca.uhn.hl7v2.util.MessageIDGenerator</code>. Also note that the ACK
 136  
          * messages returned is the same version as the version stated in the
 137  
          * inbound MSH if there is a generic ACK for that version, otherwise a
 138  
          * version 2.4 ACK is returned. MSA-1 is set to AA by default.
 139  
          * 
 140  
          * @param message message
 141  
          *            the MSH segment if the inbound message
 142  
          * 
 143  
          * @deprecated use {@link Message#generateACK()}
 144  
          */
 145  
         public static Message makeACK(Message message) throws HL7Exception, IOException {
 146  0
                 return message.generateACK();
 147  
         }
 148  
         
 149  
         /**
 150  
          * @deprecated use {@link Message#generateACK()}
 151  
          */
 152  
         public static Message makeACK(Segment inboundHeader) throws HL7Exception,
 153  
                         IOException {
 154  
                 // Make sure that the referenced message is not just a dummy
 155  0
                 DeepCopy.copy(inboundHeader, (Segment)inboundHeader.getMessage().get("MSH"));
 156  0
                 return inboundHeader.getMessage().generateACK();
 157  
         }
 158  
 
 159  
         public Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException {
 160  
                 try {
 161  270
                         if (myMessage != null) {
 162  265
                                 return theMessage.generateACK(myAcknowledgmentCode, new HL7Exception(myMessage));
 163  
                         } else {
 164  5
                                 return theMessage.generateACK(myAcknowledgmentCode, null);
 165  
                         }
 166  0
                 } catch (Exception e) {
 167  0
                         throw new ReceivingApplicationException("Couldn't create response message: "
 168  0
                                         + e.getMessage());
 169  
                 }
 170  
         }
 171  
 
 172  
 }