All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pronoia.hapi.CanonicalMessageUtil Maven / Gradle / Ivy

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pronoia.hapi;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.GenericMessage;
import ca.uhn.hl7v2.model.Message;


public class CanonicalMessageUtil extends MessageUtil {
    public CanonicalMessageUtil(Class messageClass) {
        super(HapiTestUtil.createCanonicalHapiContext(messageClass));
    }

    @Override
    public T parse(final String er7Data) {
        try {
            return (T) hapiContext.getPipeParser().parse(er7Data);
        } catch (Exception hl7Ex) {
            final String exceptionMessage = String.format("Exception encountered while attempting to parse message from <%s>", er7Data);
            throw new HapiTestException(exceptionMessage, hl7Ex);
        }
    }

    public String[] convertToArrayOfSegmentStrings(final T message) {
        return convertToArrayOfSegmentStrings(encode(message));
    }

    public String encode(T message) {
        try {
            return message.encode();
        } catch (HL7Exception encodeEx) {
            String exceptionMessage = String.format("Exception encountered encoding %s message", message.getClass());
            throw new HapiTestException(exceptionMessage, encodeEx);
        }
    }

    public String encode(T message, String segmentName) {
        try {
            return findNonEmptySegment(segmentName, message).encode();
        } catch (HL7Exception encodeSegmentEx) {
            String exceptionMessage = String.format("Exception encountered encoding %s segment for %s message", segmentName, message.getClass());
            throw new HapiTestException(exceptionMessage, encodeSegmentEx);
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy