Package ca.uhn.hl7v2.util

Examples of ca.uhn.hl7v2.util.Terser


        String outgoingMessageString = null;
        String outgoingMessageCharset = null;
        try {
            incomingMessageObject = myParser.parse(incomingMessageString);
           
            Terser inTerser = new Terser(incomingMessageObject);
            theMetadata.put(MetadataKeys.IN_MESSAGE_CONTROL_ID, inTerser.get("/.MSH-10"));
           
        }
        catch (HL7Exception e) {
      try {
         outgoingMessageString = logAndMakeErrorMessage(e, myParser.getCriticalResponseData(incomingMessageString), myParser, myParser.getEncoding(incomingMessageString));              
      } catch (HL7Exception e2) {
         outgoingMessageString = null;
      }
          if (myExceptionHandler != null) {
            outgoingMessageString = myExceptionHandler.processException(incomingMessageString, theMetadata, outgoingMessageString, e);
            if (outgoingMessageString == null) {
              throw new HL7Exception("Application exception handler may not return null");
            }
          }
        }
       
        if (outgoingMessageString == null) {
            try {
                //optionally check integrity of parse
                String check = System.getProperty("ca.uhn.hl7v2.protocol.impl.check_parse");
                if (check != null && check.equals("TRUE")) {
                    ParseChecker.checkParse(incomingMessageString, incomingMessageObject, myParser);
                }
               
                //message validation (in terms of optionality, cardinality) would go here ***
               
                ReceivingApplication app = findApplication(incomingMessageObject);
                theMetadata.put(RAW_MESSAGE_KEY, incomingMessageString);
               
                log.debug("Sending message to application: {}", app.toString());
                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(METADATA_KEY_MESSAGE_CHARSET);
            } catch (Exception e) {
                outgoingMessageString = handleProcessMessageException(incomingMessageString, theMetadata, incomingMessageObject, e);
            } catch (Error e) {
              log.debug("Caught runtime exception of type {}, going to wrap it as HL7Exception and handle it", e.getClass());
              HL7Exception wrapped = new HL7Exception(e);


   
    /**
     * 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(theMessage, msgData);
       
        //have to send back an application reject if no apps available to process
        if (app == null) {

   */
  public String getName() {
    if (StringUtil.isBlank(myName)) {
      String retVal = null;
      try {
        Terser t = new Terser(this);
        retVal = t.get("/MSH-9-3");

        if (StringUtil.isBlank(retVal)) {
          String msh91 = t.get("/MSH-9-1");
          String msh92 = t.get("/MSH-9-2");
          if (StringUtil.isNotBlank(msh91) && StringUtil.isNotBlank(msh92)) {
            retVal = msh91 + "_" + msh92;
          }
        }
      } catch (HL7Exception e) {

       
        return (ValidationException[]) problems.toArray(new ValidationException[0]);
    }
   
    private String[] getDeclaredProfileIDs(Message theMessage) throws HL7Exception {
        Terser t = new Terser(theMessage);
        boolean noMore = false;
        int c = 0;
        ArrayList declaredProfiles = new ArrayList(8);
        while (!noMore) {
            String path = "MSH-21(" + c++ + ")";
            String idRep = t.get(path);
            //FIXME fails if empty rep precedes full rep ... should add getAll() to Terser and use that
            if (idRep == null || idRep.equals("")) {
                noMore = true;
            } else {
                declaredProfiles.add(idRep);

    /**
     * @see Validator#validate
     */
    public HL7Exception[] validate(Message message, StaticDef profile) throws ProfileException, HL7Exception {
        ArrayList exList = new ArrayList(20);
        Terser t = new Terser(message);
       
        //check msg type, event type, msg struct ID
        String msgType = t.get("/MSH-9-1");
        if (!msgType.equals(profile.getMsgType())) {
            HL7Exception e =
                new ProfileNotFollowedException("Message type " + msgType + " doesn't match profile type of " + profile.getMsgType());
            exList.add(e);
        }
       
        String evType = t.get("/MSH-9-2");
        if (!evType.equals(profile.getEventType()) && !profile.getEventType().equalsIgnoreCase("ALL")) {
            HL7Exception e =
                new ProfileNotFollowedException("Event type " + evType + " doesn't match profile type of " + profile.getEventType());
            exList.add(e);
        }
       
        String msgStruct = t.get("/MSH-9-3");
        if (msgStruct == null || !msgStruct.equals(profile.getMsgStructID())) {
            HL7Exception e =
                new ProfileNotFollowedException("Message structure " + msgStruct + " doesn't match profile type of " + profile.getMsgStructID());
            exList.add(e);
        }

        if (out == null) {
            throw new HL7Exception("Can't encode null message", HL7Exception.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)", HL7Exception.REQUIRED_FIELD_MISSING);
    }

                    public void run() {
                        try {
                                //get message ID
    String ID = MessageIDGenerator.getInstance().getNewID();
                            Message out = parser.parse(outText);
                            Terser tOut = new Terser(out);
                            tOut.set("/MSH-10", ID);

                            //send, get response
                            Message in = initiator.sendAndReceive(out);

                            //get ACK ID
                            Terser tIn = new Terser(in);
                            String ackID = tIn.get("/MSA-2");
                            if (ID.equals(ackID)) {
                                System.out.println("OK - ack ID matches");
                            }
                            else {
                                throw new RuntimeException("Ack ID for message " + ID + " is " + ackID);

            out = (Message) ackClass.newInstance();
        }
        catch (Exception e) {
            throw new HL7Exception("Can't instantiate ACK of class " + ackClassName + ": " + e.getClass().getName());
        }
        Terser terser = new Terser(out);

        //populate outbound MSH using data from inbound message ...            
        Segment outHeader = (Segment) out.get("MSH");
        fillResponseHeader(inboundHeader, outHeader);

        terser.set("/MSH-9", "ACK");
        terser.set("/MSH-12", version);
        terser.set("/MSA-1", "AA");
        terser.set("/MSA-2", terser.get(inboundHeader, 10, 0, 1, 1));

        return out;
    }

     * Returns the Applications that has been registered to handle
     * messages of the type and trigger event of the given message, or null if
     * there are none.
     */
    private Application getMatchingApplication(Message message) throws HL7Exception {
        Terser t = new Terser(message);
        String messageType = t.get("/MSH-9-1");
        String triggerEvent = t.get("/MSH-9-2");
        return this.getMatchingApplication(messageType, triggerEvent);       
    }

     *      form required by <code>Terser</code>. 
     * @return a Map from Terser paths to field values
     */
    public static Map getFields(Message theMessage, List theTerserPaths) throws HL7Exception {
        Map fields = new HashMap();
        Terser terser = new Terser(theMessage);
        for (int i = 0; i < theTerserPaths.size(); i++) {
            String path = (String) theTerserPaths.get(i);
            String fieldValue = terser.get(path);
            fields.put(path, fieldValue);
        }
        return fields;
    }

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.