<?xml version="1.0" encoding="UTF-8"?>
<!--
  Raw HL7 v2 over HTTPS sender (api-platform §12.3, §12.3.1, §17).
  Store and forward: the MLLP source ACKs the EHR as soon as the message is persisted
  ("Auto-generate (After source transformer)" with message storage on), and the API is a queued
  destination ("queue always") whose JavaScript Writer fetches the token and sends at dispatch time.
  503/429 pause the queue for Retry-After; AE/AR go to "Integration API errors" and never hold it.
  Requires the "Integration API" code template library and its configuration map keys.
-->
<channel version="4.5.0">
  <id>0b1d2c3e-4f50-4a61-8b72-93a4b5c6d7e1</id>
  <nextMetaDataId>2</nextMetaDataId>
  <name>Integration API - Raw HL7 sender</name>
  <description>Forwards ADT, ORM, ORU and MDM messages as raw ER7 to POST /v1/hl7v2. Filter unused triggers at the engine; the API answers them with AA and ERR-4 I.</description>
  <revision>1</revision>
  <sourceConnector version="4.5.0">
    <metaDataId>0</metaDataId>
    <name>sourceConnector</name>
    <properties class="com.mirth.connect.connectors.tcp.TcpReceiverProperties" version="4.5.0">
      <pluginProperties/>
      <listenerConnectorProperties version="4.5.0">
        <host>0.0.0.0</host>
        <port>6661</port>
      </listenerConnectorProperties>
      <sourceConnectorProperties version="4.5.0">
        <responseVariable>Auto-generate (After source transformer)</responseVariable>
        <respondAfterProcessing>false</respondAfterProcessing>
        <processBatch>false</processBatch>
        <firstResponse>false</firstResponse>
        <processingThreads>1</processingThreads>
        <queueBufferSize>1000</queueBufferSize>
      </sourceConnectorProperties>
      <transmissionModeProperties class="com.mirth.connect.plugins.mllpmode.MLLPModeProperties">
        <pluginPointName>MLLP</pluginPointName>
        <startOfMessageBytes>0B</startOfMessageBytes>
        <endOfMessageBytes>1C0D</endOfMessageBytes>
        <useMLLPv2>false</useMLLPv2>
        <ackBytes>06</ackBytes>
        <nackBytes>15</nackBytes>
        <maxRetries>2</maxRetries>
      </transmissionModeProperties>
      <serverMode>true</serverMode>
      <receiveTimeout>0</receiveTimeout>
      <bufferSize>65536</bufferSize>
      <maxConnections>10</maxConnections>
      <keepConnectionOpen>true</keepConnectionOpen>
      <dataTypeBinary>false</dataTypeBinary>
      <charsetEncoding>DEFAULT_ENCODING</charsetEncoding>
      <respondOnNewConnection>0</respondOnNewConnection>
    </properties>
    <transformer version="4.5.0">
      <elements/>
      <inboundDataType>HL7V2</inboundDataType>
      <outboundDataType>HL7V2</outboundDataType>
    </transformer>
    <filter version="4.5.0">
      <elements>
        <com.mirth.connect.plugins.javascriptrule.JavaScriptRule version="4.5.0">
          <name>Only the message types the API maps</name>
          <sequenceNumber>0</sequenceNumber>
          <enabled>true</enabled>
          <operator>NONE</operator>
          <script><![CDATA[
var type = msg['MSH']['MSH.9']['MSH.9.1'].toString();
return type === 'ADT' || type === 'ORM' || type === 'OMG' || type === 'OML' || type === 'ORU' || type === 'MDM';
]]></script>
        </com.mirth.connect.plugins.javascriptrule.JavaScriptRule>
      </elements>
    </filter>
    <transportName>TCP Listener</transportName>
    <mode>SOURCE</mode>
    <enabled>true</enabled>
    <waitForPrevious>true</waitForPrevious>
  </sourceConnector>
  <destinationConnectors>
    <connector version="4.5.0">
      <metaDataId>1</metaDataId>
      <name>Integration API /v1/hl7v2</name>
      <properties class="com.mirth.connect.connectors.js.JavaScriptDispatcherProperties" version="4.5.0">
        <pluginProperties/>
        <destinationConnectorProperties version="4.5.0">
          <queueEnabled>true</queueEnabled>
          <sendFirst>false</sendFirst>
          <retryIntervalMillis>10000</retryIntervalMillis>
          <regenerateTemplate>true</regenerateTemplate>
          <retryCount>0</retryCount>
          <rotate>false</rotate>
          <includeFilterTransformer>false</includeFilterTransformer>
          <threadCount>1</threadCount>
          <threadAssignmentVariable/>
          <validateResponse>false</validateResponse>
          <resourceIds class="linked-hash-map">
            <entry>
              <string>Default Resource</string>
              <string>[Default Resource]</string>
            </entry>
          </resourceIds>
          <queueBufferSize>1000</queueBufferSize>
          <reattachAttachments>true</reattachAttachments>
        </destinationConnectorProperties>
        <script><![CDATA[
// Dispatch-time send (api-platform §5.12): the token is fetched here, never in a transformer.
if (ingestPaused()) {
  return new Response(QUEUED, '', 'Paused for Retry-After', '');
}
var site = ingestCfg('ingest.api.site', true);
var path = '/v1/hl7v2' + (site ? '?site=' + java.net.URLEncoder.encode(site, 'UTF-8') : '');
var er7 = connectorMessage.getEncodedData();
// The engine's message id is a stable idempotency key: a retry of the same message replays.
var key = 'mirth-' + channelId + '-' + connectorMessage.getMessageId();
var result = ingestSend(path, 'application/hl7-v2; charset=utf-8', er7, key);
return ingestResponse(result, 'Integration API errors');
]]></script>
      </properties>
      <transformer version="4.5.0">
        <elements/>
        <inboundDataType>HL7V2</inboundDataType>
        <outboundDataType>HL7V2</outboundDataType>
      </transformer>
      <responseTransformer version="4.5.0">
        <elements/>
        <inboundDataType>HL7V2</inboundDataType>
        <outboundDataType>HL7V2</outboundDataType>
      </responseTransformer>
      <filter version="4.5.0">
        <elements/>
      </filter>
      <transportName>JavaScript Writer</transportName>
      <mode>DESTINATION</mode>
      <enabled>true</enabled>
      <waitForPrevious>true</waitForPrevious>
    </connector>
  </destinationConnectors>
  <preprocessingScript>return message;</preprocessingScript>
  <postprocessingScript>return;</postprocessingScript>
  <deployScript>return;</deployScript>
  <undeployScript>return;</undeployScript>
  <properties version="4.5.0">
    <clearGlobalChannelMap>true</clearGlobalChannelMap>
    <messageStorageMode>DEVELOPMENT</messageStorageMode>
    <encryptData>true</encryptData>
    <removeContentOnCompletion>true</removeContentOnCompletion>
    <removeOnlyFilteredOnCompletion>false</removeOnlyFilteredOnCompletion>
    <removeAttachmentsOnCompletion>true</removeAttachmentsOnCompletion>
    <initialState>STARTED</initialState>
    <storeAttachments>false</storeAttachments>
    <metaDataColumns/>
    <attachmentProperties version="4.5.0">
      <type>None</type>
      <properties/>
    </attachmentProperties>
    <resourceIds class="linked-hash-map">
      <entry>
        <string>Default Resource</string>
        <string>[Default Resource]</string>
      </entry>
    </resourceIds>
  </properties>
</channel>
