Package ca.uhn.hl7v2.util

Examples of ca.uhn.hl7v2.util.Terser


            out = DefaultApplication.makeACK(header);
        } catch (IOException e) {
            throw new HL7Exception(e);
        }
       
        Terser t = new Terser(out);
        t.set("/MSA-1", theAckCode);

        //TODO: when 2.5 is available, use 2.5 fields for remaining problems
        if (theErrorCode != HL7Exception.MESSAGE_ACCEPTED) {
            t.set("/MSA-3", theDescription.substring(0, Math.min(80, theDescription.length())));           
            t.set("/ERR-1-4-1", String.valueOf(theErrorCode));
            t.set("/ERR-1-4-3", "HL70357");
        }
       
        String originalEncoding = ourParser.getEncoding(theMessage.getMessage());
        String ackText = ourParser.encode(out, originalEncoding);
        return new TransportableImpl(ackText);


    }
   
    private void parse(StringTokenizer tok, Message message, StructRef root, EncodingCharacters ec)
            throws HL7Exception {
       
        Terser t = new Terser(message);
       
        synchronized (root) {
            StructRef ref = root.getSuccessor("MSH");           
           
            int field = 0;
            Segment segment = null;
            int[] fields = new int[0];
           
            while (tok.hasMoreTokens()) {
                String token = tok.nextToken();
                if (token.charAt(0) == ec.getFieldSeparator()) {
                    field++;
                } else if (token.charAt(0) == ourSegmentSeparator) {
                    field = 0;
                } else if (field == 0) {
                    StructRef newref = drill(ref, token);
                    if (newref == null) {
                        segment = null;
                        fields = new int[0];
                    } else {
                        ref = newref;
                        if (ourLog.isDebugEnabled()) {
                            ourLog.debug("Parsing into segment " + ref.getFullPath());
                        }
                        segment = t.getSegment(ref.getFullPath());
                        fields = ref.getFields();
                    }
                } else if (segment != null && Arrays.binarySearch(fields, field) >= 0) {
                    parse(token, segment, field, ec);
                }

                Message response = app.processMessage(incomingMessageObject, theMetadata);
               
                //Here we explicitly use the same encoding as that of the inbound message - this is important with GenericParser, which might use a different encoding by default
                outgoingMessageString = myParser.encode(response, myParser.getEncoding(incomingMessageString));
               
                Terser t = new Terser(response);
                outgoingMessageCharset = t.get("MSH-18");
            }
            catch (Exception e) {
                outgoingMessageString = Responder.logAndMakeErrorMessage(e, (Segment) incomingMessageObject.get("MSH"), myParser, myParser.getEncoding(incomingMessageString));
            }
        }

   
    /**
     * Returns the first Application that has been bound to messages of this type. 
     */
    private ReceivingApplication findApplication(Message theMessage) throws HL7Exception {
        Terser t = new Terser(theMessage);
        AppRoutingData msgData =
            new AppRoutingDataImpl(t.get("/MSH-9-1"), t.get("/MSH-9-2"), t.get("/MSH-11-1"), t.get("/MSH-12"));
           
        ReceivingApplication app = findDestination(msgData);
       
        //have to send back an application reject if no apps available to process
        if (app == null)

   
    /**
     * @see ca.uhn.hl7v2.protocol.Initiator#sendAndReceive(ca.uhn.hl7v2.model.Message)
     */
    public Message sendAndReceive(Message theMessage) throws HL7Exception {
        Terser t = new Terser(theMessage);
        String appAckNeeded = t.get("/MSH-16");
        String msgId = t.get("/MSH-10");
       
        String messageText = getParser().encode(theMessage);
        Map metadata = getMetadata(theMessage);
        Transportable out = new TransportableImpl(messageText, metadata);
       

        return need;
    }
   
    private Map getMetadata(Message theMessage) throws HL7Exception {
        Map md = new HashMap();
        Terser t = new Terser(theMessage);

        //snapshot so concurrent changes won't break our iteration
        Object[] fields = getMetadataFields().toArray(new Object[0]);
       
        for (int i = 0; i < fields.length; i++) {
            String field = fields[i].toString();
            String val = t.get(field);
            md.put(field, val);
        }
       
        return md;
    }

     * @param message a parsed message to validate (note that MSH-9-1 and MSH-9-2 must be valued)
     * @return true if the message is OK
     * @throws HL7Exception if there is at least one error and this validator is set to fail on errors
     */
    public boolean validate(Message message) throws HL7Exception {
        Terser t = new Terser(message);
       
        MessageRule[] rules = EMPTY_MESSAGE_RULES_ARRAY;
        if (myContext != null) {
          rules = myContext.getMessageRules(message.getVersion(), t.get("MSH-9-1"), t.get("MSH-9-2"));
        }
       
        ValidationException toThrow = null;
        boolean result = true;
        for (int i = 0; i < rules.length; i++) {

            LogFactory.getLog(logName).error("message validation failure", problems[i]);
        }
    }
   
    private void addProblemsToACK(Message ack, HL7Exception[] problems) throws HL7Exception {
        Terser t = new Terser(ack);
       
        if (problems.length > 0) {
            t.set("MSA-1", "AE");       
            t.set("MSA-3", "Errors were encountered while testing the message");
        }
       
        Segment err = (Segment) ack.get("ERR");
        for (int i = 0; i < problems.length; i++) {
            // problems[i].populate(err); FIXME: broken! needs database

       
        // create error message ...
        String errorMessage = null;
        try {
            Message out = DefaultApplication.makeACK(inHeader);
            Terser t = new Terser(out);
           
            //copy required data from incoming message ...
            try {
                t.set("/MSH-10", MessageIDGenerator.getInstance().getNewID());
            }
            catch (IOException ioe) {
                throw new HL7Exception("Problem creating error message ID: " + ioe.getMessage());
            }
           
            //populate MSA ...
            t.set("/MSA-1", "AE"); //should this come from HL7Exception constructor?
            t.set("/MSA-2", Terser.get(inHeader, 10, 0, 1, 1));
            String excepMessage = e.getMessage();
            if (excepMessage != null)
                t.set("/MSA-3", excepMessage.substring(0, Math.min(80, excepMessage.length())));
           
            /* Some earlier ACKs don't have ERRs, but I think we'll change this within HAPI
               so that there is a single ACK for each version (with an ERR). */
            //see if it's an HL7Exception (so we can get specific information) ...
            if (e.getClass().equals(HL7Exception.class)) {
                Segment err = (Segment) out.get("ERR");
                // ((HL7Exception) e).populate(err); // FIXME: this is broken, it relies on the database in a place where it's not available
            }
            else {
                t.set("/ERR-1-4-1", "207");
                t.set("/ERR-1-4-2", "Application Internal Error");
                t.set("/ERR-1-4-3", "HL70357");
            }
           
            if (encoding != null) {
                errorMessage = p.encode(out, encoding);
            } else {

      throw new HL7Exception("Can't encode null message",
          ErrorCode.REQUIRED_FIELD_MISSING);
    }

    // register message with response Receiver(s) (by message ID)
    Terser t = new Terser(out);
    String messID = t.get("/MSH-10");

    if (messID == null || messID.length() == 0) {
      throw new HL7Exception(
          "MSH segment missing required field Control ID (MSH-10)",
          ErrorCode.REQUIRED_FIELD_MISSING);

TOP

Related Classes of ca.uhn.hl7v2.util.Terser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.