Package ca.uhn.hl7v2.util

Examples of ca.uhn.hl7v2.util.Terser


          public void run() {
            try {
              // get message ID
              String ID = generator.getID();
              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 "


    return handler.result();
  }

  private void testMessageRules(Message message, ValidationExceptionHandler<R> handler)
      throws HL7Exception {
    Terser t = new Terser(message);
    String messageType = t.get("MSH-9-1");
    String triggerEvent = t.get("MSH-9-2");
    List<MessageRule> rules = new ArrayList<MessageRule>();
    if (getValidationContext() != null) {
      rules.addAll(getValidationContext().getMessageRules(message.getVersion(), messageType,
          triggerEvent));
    }

   * @see Validator#validate
   */
  public HL7Exception[] validate(Message message, StaticDef profile) throws ProfileException,
      HL7Exception {
    List<HL7Exception> exList = new ArrayList<HL7Exception>();
    Terser t = new Terser(message);

        checkMessageType(t.get("/MSH-9-1"), profile, exList);
        checkEventType(t.get("/MSH-9-2"), profile, exList);
        checkMessageStructure(t.get("/MSH-9-3"), profile, exList);

        exList.addAll(doTestGroup(message, profile, profile.getIdentifier(),
        validateChildren));
    return exList.toArray(new HL7Exception[exList.size()]);
  }

       
        return problems.toArray(new ValidationException[problems.size()]);
    }
   
    private String[] getDeclaredProfileIDs(Message theMessage) throws HL7Exception {
        Terser t = new Terser(theMessage);
        boolean noMore = false;
        int c = 0;
        List<String> declaredProfiles = new ArrayList<String>(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);

      super();
      this.expression = expression;
    }

    public Object evaluate(Message msg) throws HL7Exception {
      return new Terser(msg).get(expression);
    }

      // Terser Expression is equivalent with Location
      return "";
    }

    public Location getLocation(Message msg) throws HL7Exception {
      Terser t = new Terser(msg);
      StringTokenizer tok = new StringTokenizer(expression, "-", false);
      Segment segment = t.getSegment(tok.nextToken());
      Location location = new Location()
          .withSegmentName(segment.getName())
          .withFieldIndizes(Terser.getIndices(expression));
      return location;
    }

        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 (myContext.getServerConfiguration().getApplicationExceptionPolicy() == ApplicationExceptionPolicy.DO_NOT_RESPOND) {
              outgoingMessageString = null;
            } else 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);
              if (myContext.getServerConfiguration().getApplicationExceptionPolicy() == ApplicationExceptionPolicy.DO_NOT_RESPOND) {
              outgoingMessageString = null;
            }

   
    /**
     * 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 ResponseCode getResponseCode() {
    if (myResponseCode == null) {
      try {
        myResponseCode = ResponseCode.detect(new Terser(myMessage).get("/MSA-2"));
        if (myResponseCode == null) {
          throw new HL7Exception("No response code in message, this is probably an error");
        }
      } catch (HL7Exception e) {
        ourLog.info("Could not detect response code in message", e);

    public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
        String body = ExchangeHelper.convertToMandatoryType(exchange, String.class, inputStream);
        Message message = HL7Converter.parse(body, parser);

        // add MSH fields as message out headers
        Terser terser = new Terser(message);
        for (Map.Entry<String, String> entry : HEADER_MAP.entrySet()) {
            exchange.getOut().setHeader(entry.getKey(), terser.get(entry.getValue()));
        }
        return message;
    }

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.