Coverage Report - ca.uhn.hl7v2.app.DefaultApplication
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultApplication
61%
16/26
36%
4/11
3
DefaultApplication$1
100%
1/1
N/A
3
 
 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 Application, ReceivingApplication {
 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  55
                 this(AcknowledgmentCode.AR);
 65  55
         }
 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  56
                 this(theAcknowledgmentCode, defaultMessage(theAcknowledgmentCode));
 92  56
         }
 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  56
         public DefaultApplication(AcknowledgmentCode theAcknowledgmentCode, String theMessage) {
 101  56
                 if (theAcknowledgmentCode == null) {
 102  0
                         throw new NullPointerException("Acknowledgment code must not be null");
 103  
                 }
 104  56
                 myAcknowledgmentCode = theAcknowledgmentCode;
 105  56
                 myMessage = theMessage;
 106  56
         }
 107  
 
 108  
         private static String defaultMessage(AcknowledgmentCode theAr) {
 109  1
                 switch (theAr) {
 110  
                 case AA:
 111  
                 case CA:
 112  1
                         return null;
 113  
                 case AR:
 114  
                 case CR:
 115  55
                         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  1
                 return true;
 128  
         }
 129  
 
 130  
         /**
 131  
          * Creates and returns a negative acknowledgement  
 132  
          */
 133  
         public Message processMessage(Message in) throws ApplicationException {
 134  
                 try {
 135  2
                         return processMessage(in, null);
 136  0
                 } catch (ReceivingApplicationException e) {
 137  0
                         throw new ApplicationException(e.getMessage(), e);
 138  
                 }
 139  
         }
 140  
 
 141  
         /**
 142  
          * Creates an ACK message with the minimum required information from an
 143  
          * inbound message. Optional fields can be filled in afterwards, before the
 144  
          * message is returned. Please note that MSH-10, the outbound message
 145  
          * control ID, is also set using the class
 146  
          * <code>ca.uhn.hl7v2.util.MessageIDGenerator</code>. Also note that the ACK
 147  
          * messages returned is the same version as the version stated in the
 148  
          * inbound MSH if there is a generic ACK for that version, otherwise a
 149  
          * version 2.4 ACK is returned. MSA-1 is set to AA by default.
 150  
          * 
 151  
          * @param message message
 152  
          *            the MSH segment if the inbound message
 153  
          * 
 154  
          * @deprecated use {@link Message#generateACK()}
 155  
          */
 156  
         public static Message makeACK(Message message) throws HL7Exception, IOException {
 157  0
                 return message.generateACK();
 158  
         }
 159  
         
 160  
         /**
 161  
          * @deprecated use {@link Message#generateACK()}
 162  
          */
 163  
         public static Message makeACK(Segment inboundHeader) throws HL7Exception,
 164  
                         IOException {
 165  
                 // Make sure that the referenced message is not just a dummy
 166  0
                 DeepCopy.copy(inboundHeader, (Segment)inboundHeader.getMessage().get("MSH"));
 167  0
                 return inboundHeader.getMessage().generateACK();
 168  
         }
 169  
 
 170  
         public Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException {
 171  
                 try {
 172  54
                         if (myMessage != null) {
 173  54
                                 return theMessage.generateACK(myAcknowledgmentCode, new HL7Exception(myMessage));
 174  
                         } else {
 175  0
                                 return theMessage.generateACK(myAcknowledgmentCode, null);
 176  
                         }
 177  0
                 } catch (Exception e) {
 178  0
                         throw new ReceivingApplicationException("Couldn't create response message: "
 179  
                                         + e.getMessage());
 180  
                 }
 181  
         }
 182  
 
 183  
 }