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 "DefaultApplication.java".  Description: 
010"An Application that does nothing with the message and returns an Application 
011 Reject message in response." 
012
013The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0142002.  All Rights Reserved. 
015
016Contributor(s): ______________________________________. 
017
018Alternatively, the contents of this file may be used under the terms of the 
019GNU General Public License (the  "GPL"), in which case the provisions of the GPL are 
020applicable instead of those above.  If you wish to allow use of your version of this 
021file only under the terms of the GPL and not to allow others to use your version 
022of this file under the MPL, indicate your decision by deleting  the provisions above 
023and replace  them with the notice and other provisions required by the GPL License.  
024If you do not delete the provisions above, a recipient may use your version of 
025this file under either the MPL or the GPL. 
026 */
027
028package ca.uhn.hl7v2.app;
029
030import java.io.IOException;
031import java.util.Map;
032
033import ca.uhn.hl7v2.AcknowledgmentCode;
034import ca.uhn.hl7v2.HL7Exception;
035import ca.uhn.hl7v2.model.Message;
036import ca.uhn.hl7v2.model.Segment;
037import ca.uhn.hl7v2.protocol.ReceivingApplication;
038import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
039import ca.uhn.hl7v2.util.DeepCopy;
040
041/**
042 * An Application that does nothing with the message and returns an Application
043 * Reject message in response. To be used when there are no other Applications
044 * that can process a given message.
045 * 
046 * @author Bryan Tripp
047 * @author Christian Ohr
048 */
049public class DefaultApplication implements ReceivingApplication<Message> {
050
051        public static final String MSG_INTERNAL_ERROR = "Application internal error";
052        public static final String MSG_NO_APPROPRIATE_DEST = "No appropriate destination could be found to which this message could be routed.";
053        
054        private final AcknowledgmentCode myAcknowledgmentCode;
055        private final String myMessage;
056        
057        /**
058         * New instance which always returns an acknowledgment code of {@link AcknowledgmentCode#AR} and
059         * a message indicating that no appropriate handler could be found.
060         * 
061         * @see #MSG_NO_APPROPRIATE_DEST
062         */
063        public DefaultApplication() {
064                this(AcknowledgmentCode.AR);
065        }
066
067        /**
068         * <p>
069         * New instance which always uses the given acknowledgment code, and generates an
070         * appropriate message for the given code, according to the following rules:
071         * </p>
072         * <table>
073         * <tr><th>Code</th><th>Message</th></tr>
074         * <tr>
075         *              <td>{@link AcknowledgmentCode#AA}<br/>{@link AcknowledgmentCode#AA}</td>
076         *              <td><code>null</code> (no message)</td>
077         * </tr>
078         * <tr>
079         *              <td>{@link AcknowledgmentCode#AR}<br/>{@link AcknowledgmentCode#AR}</td>
080         *              <td>{@link #MSG_NO_APPROPRIATE_DEST}</td>
081         * </tr>
082         * <tr>
083         *              <td>{@link AcknowledgmentCode#AE}<br/>{@link AcknowledgmentCode#AE}</td>
084         *              <td>{@link #MSG_INTERNAL_ERROR}</td>
085         * </tr>
086         * </table>
087         * 
088         * @param theAcknowledgmentCode The code to always use (must not be null)
089         */
090        public DefaultApplication(AcknowledgmentCode theAcknowledgmentCode) {
091                this(theAcknowledgmentCode, defaultMessage(theAcknowledgmentCode));
092        }
093
094        /**
095         * New instance which always uses the given acknowledgment code and message
096         *  
097         * @param theAcknowledgmentCode The code to always use (must not be null)
098         * @param theMessage The message (may be null, in which case no message will be given)
099         */
100        public DefaultApplication(AcknowledgmentCode theAcknowledgmentCode, String theMessage) {
101                if (theAcknowledgmentCode == null) {
102                        throw new NullPointerException("Acknowledgment code must not be null");
103                }
104                myAcknowledgmentCode = theAcknowledgmentCode;
105                myMessage = theMessage;
106        }
107
108        private static String defaultMessage(AcknowledgmentCode theAr) {
109                switch (theAr) {
110                case AA:
111                case CA:
112                        return null;
113                case AR:
114                case CR:
115                        return MSG_NO_APPROPRIATE_DEST;
116                case AE:
117                case CE:
118                default:
119                        return MSG_INTERNAL_ERROR;
120                }
121        }
122
123        /**
124         * Returns true.
125         */
126        public boolean canProcess(Message in) {
127                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                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                DeepCopy.copy(inboundHeader, (Segment)inboundHeader.getMessage().get("MSH"));
156                return inboundHeader.getMessage().generateACK();
157        }
158
159        public Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException {
160                try {
161                        if (myMessage != null) {
162                                return theMessage.generateACK(myAcknowledgmentCode, new HL7Exception(myMessage));
163                        } else {
164                                return theMessage.generateACK(myAcknowledgmentCode, null);
165                        }
166                } catch (Exception e) {
167                        throw new ReceivingApplicationException("Couldn't create response message: "
168                                        + e.getMessage());
169                }
170        }
171
172}