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 "


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

  }
 
  protected void applySuperStructureName(Message theMessage) throws HL7Exception {
        if (theMessage instanceof AbstractSuperMessage) {
          if (theMessage.getName() == null) {
            Terser t = new Terser(theMessage);
            String name = null;
        try {
          name = t.get("/MSH-9-3");
        } catch (HL7Exception e) {
          // ignore
        }
       
        if (StringUtil.isBlank(name)) {
          name = t.get("/MSH-9-1") + "_" + t.get("/MSH-9-2");
        }
       
        ((AbstractSuperMessage)theMessage).setName(name);
          }
        }

      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();
      location.setSegmentName(segment.getName());
      location.setFieldIndizes(Terser.getIndices(expression));
      return location;
    }

   
    /**
     * @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<String, Object> metadata = getMetadata(theMessage);
        Transportable out = new TransportableImpl(messageText, metadata);
       

       
        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);

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

        //snapshot so concurrent changes won't break our iteration
        String[] fields = getMetadataFields().toArray(new String[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;
    }

   * @see Validator#validate
   */
  public HL7Exception[] validate(Message message, StaticDef profile) throws ProfileException,
      HL7Exception {
    List<HL7Exception> exList = new ArrayList<HL7Exception>();
    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);
    }

   * 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);
  }

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.