| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package ca.uhn.hl7v2.protocol.impl; |
| 5 | |
|
| 6 | |
import java.io.IOException; |
| 7 | |
|
| 8 | |
import org.slf4j.Logger; |
| 9 | |
import org.slf4j.LoggerFactory; |
| 10 | |
|
| 11 | |
import ca.uhn.hl7v2.AcknowledgmentCode; |
| 12 | |
import ca.uhn.hl7v2.ErrorCode; |
| 13 | |
import ca.uhn.hl7v2.HL7Exception; |
| 14 | |
import ca.uhn.hl7v2.model.Message; |
| 15 | |
import ca.uhn.hl7v2.model.Segment; |
| 16 | |
import ca.uhn.hl7v2.parser.GenericParser; |
| 17 | |
import ca.uhn.hl7v2.parser.Parser; |
| 18 | |
import ca.uhn.hl7v2.protocol.AcceptValidator; |
| 19 | |
import ca.uhn.hl7v2.protocol.Processor; |
| 20 | |
import ca.uhn.hl7v2.protocol.ProcessorContext; |
| 21 | |
import ca.uhn.hl7v2.protocol.Transportable; |
| 22 | |
import ca.uhn.hl7v2.util.DeepCopy; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 0 | public class AcceptAcknowledger { |
| 32 | |
|
| 33 | 5 | private static final Logger log = LoggerFactory.getLogger(AcceptAcknowledger.class); |
| 34 | |
|
| 35 | 5 | private static Parser ourParser = new GenericParser(); |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public static AcceptACK validate(ProcessorContext theContext, Transportable theMessage) throws HL7Exception { |
| 44 | 45 | AcceptACK ruling = null; |
| 45 | |
|
| 46 | 45 | AcceptValidator[] validators = theContext.getValidators(); |
| 47 | 55 | for (int i = 0; i < validators.length && ruling == null; i++) { |
| 48 | 10 | AcceptValidator.AcceptRuling vr = validators[i].check(theMessage); |
| 49 | 10 | if (!vr.isAcceptable()) { |
| 50 | 5 | String description = (vr.getReasons().length > 0) ? vr.getReasons()[0] : null; |
| 51 | 5 | Transportable ack = makeAcceptAck(theMessage, vr.getAckCode(), ErrorCode.errorCodeFor(vr.getErrorCode()), description); |
| 52 | 5 | ruling = new AcceptACK(false, ack); |
| 53 | |
} |
| 54 | |
} |
| 55 | |
|
| 56 | 45 | if (ruling == null) { |
| 57 | |
try { |
| 58 | 40 | theContext.getSafeStorage().store(theMessage); |
| 59 | 35 | Transportable ack = makeAcceptAck(theMessage, Processor.CA, ErrorCode.MESSAGE_ACCEPTED, ""); |
| 60 | 30 | ruling = new AcceptACK(true, ack); |
| 61 | 10 | } catch (HL7Exception e) { |
| 62 | 10 | log.error(e.getMessage(), e); |
| 63 | 10 | Transportable ack = makeAcceptAck(theMessage, Processor.CR, ErrorCode.APPLICATION_INTERNAL_ERROR, e.getMessage()); |
| 64 | 5 | ruling = new AcceptACK(false, ack); |
| 65 | 30 | } |
| 66 | |
} |
| 67 | |
|
| 68 | 40 | return ruling; |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
private static Transportable makeAcceptAck(Transportable theMessage, String theAckCode, ErrorCode theErrorCode, String theDescription) throws HL7Exception { |
| 73 | 50 | Segment header = ourParser.getCriticalResponseData(theMessage.getMessage()); |
| 74 | 40 | Message dummy = header.getMessage(); |
| 75 | |
|
| 76 | 40 | DeepCopy.copy(header, (Segment)dummy.get("MSH")); |
| 77 | |
|
| 78 | |
try { |
| 79 | 40 | HL7Exception hl7e = new HL7Exception(theDescription, theErrorCode); |
| 80 | 40 | AcknowledgmentCode code = theAckCode == null ? |
| 81 | |
AcknowledgmentCode.CR : |
| 82 | 35 | AcknowledgmentCode.valueOf(theAckCode); |
| 83 | 40 | Message out = dummy.generateACK(code, hl7e); |
| 84 | 40 | String originalEncoding = ourParser.getEncoding(theMessage.getMessage()); |
| 85 | 40 | String ackText = ourParser.encode(out, originalEncoding); |
| 86 | 40 | return new TransportableImpl(ackText); |
| 87 | 0 | } catch (IOException e) { |
| 88 | 0 | throw new HL7Exception(e); |
| 89 | |
} |
| 90 | |
|
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public static class AcceptACK { |
| 102 | |
private Transportable myAck; |
| 103 | |
private boolean myIsAcceptable; |
| 104 | |
|
| 105 | 40 | public AcceptACK(boolean isAcceptable, Transportable theAck) { |
| 106 | 40 | myIsAcceptable = isAcceptable; |
| 107 | 40 | myAck = theAck; |
| 108 | 40 | } |
| 109 | |
|
| 110 | |
public boolean isAcceptable() { |
| 111 | 40 | return myIsAcceptable; |
| 112 | |
} |
| 113 | |
|
| 114 | |
public Transportable getMessage() { |
| 115 | 0 | return myAck; |
| 116 | |
} |
| 117 | |
} |
| 118 | |
|
| 119 | |
} |