001package ca.cdr.api.pub.hl7v2.out;
002
003/*-
004 * #%L
005 * Smile CDR - CDR
006 * %%
007 * Copyright (C) 2016 - 2024 Smile CDR, Inc.
008 * %%
009 * All rights reserved.
010 * #L%
011 */
012
013import ca.uhn.hl7v2.HL7Exception;
014import ca.uhn.hl7v2.model.Message;
015import ca.uhn.hl7v2.model.v25.message.*;
016import ca.uhn.hl7v2.model.v25.segment.*;
017import jakarta.annotation.Nonnull;
018import jakarta.annotation.Nullable;
019import org.hl7.fhir.instance.model.api.IBaseResource;
020import org.hl7.fhir.r4.model.*;
021
022/**
023 * This service is used to generate and populate HL7 v2.x outbound (from Smile CDR) messages using
024 * information from FHIR resources as the source of data.
025 * <p>
026 * To create a new HL7 v2.x message to transmit, the first call should be to
027 * {@link #newTarget(String, String, Class)}. You can then use methods such as
028 * {@link #populateMessageAdtA01FromEncounter(IBaseResource, MappingTarget, OutboundMappingInstructions)}
029 * to populate the entire message according to Smile CDR's mapping rules, or use
030 * individual segment methods such as {@link #populateSegmentPidFromPatient(Patient, Encounter, PID, MappingTarget, OutboundMappingInstructions)}
031 * to populate individual segments. You can also simply populate the message yourself.
032 * </p>
033 */
034public interface IHl7V2OutboundMapperSvc {
035
036        /**
037         * Create a new {@link MappingTarget} with a target {@link Message} instance that is appropriate for the
038         * given {@literal theMessageCode} and {@literal theMessageTrigger} values. The MSH segment in the message
039         * is populated with initial values (which may subsequently be modified if needed), but no other segments
040         * are populated.
041         *
042         * @param theMessageCode    The message code, e.g. <code>ADT</code>
043         * @param theMessageTrigger The message trigger, e.h. <code>A01</code>
044         * @param theStructureType  The HAPI-HL7v2 type class corresponding to the given code and trigger. Note that these
045         *                          will often agree (e.g. Code/Type of "ADT"/"A01" should use a structure type of
046         *                          {@link ADT_A01 ADT_A01.class}) but there are also many cases in the HL7 v2.x
047         *                          specification where a structure type gets used for multiple triggers (e.g. Code/Type of
048         *                          "ADT"/"A31" uses the structure type of {@link ADT_A05 ADT_A05.class}). At this time, an
049         *                          HL7 v2.5 structure class must be used.
050         * @throws IllegalArgumentException If {@literal theStructureType} is inappropriate for the given
051         *                                  {@literal theMessageCode} and {@literal theMessageTrigger} values.
052         */
053        <T extends Message> MappingTarget<T> newTarget(
054                        @Nonnull String theMessageCode, @Nonnull String theMessageTrigger, @Nonnull Class<T> theStructureType)
055                        throws IllegalArgumentException;
056
057        /**
058         * Populate a complete DFT_P03 structure using a ChargeItem resource as the focal resource. This can be
059         * used to create messages of type DFT^P03.
060         * <p>
061         * This method attempts to populate the entire message structure using values from the focal
062         * resource as well as from other resources that are referenced directly or indirectly from the
063         * focal resource.
064         * </p>
065         *
066         * @param theMappingTarget Should contain message of type {@link DFT_P03}
067         */
068        void populateMessageDftP03FromChargeItem(
069                        @Nonnull IBaseResource theFocalChargeItem,
070                        @Nonnull MappingTarget<DFT_P03> theMappingTarget,
071                        OutboundMappingInstructions theOutboundMappingInstructions);
072
073        /**
074         * Populate a complete ORU_R01 structure using a DiagnosticReport resource as the focal resource. This can be
075         * used to create messages of type ORU^R01
076         * <p>
077         * This method attempts to populate the entire message structure using values from the focal
078         * resource as well as from other resources that are referenced directly or indirectly from the
079         * focal resource.
080         * </p>
081         *
082         * @param theMappingTarget Should contain message of type {@link ORU_R01}
083         */
084        void populateMessageOruR01FromDiagnosticReport(
085                        @Nonnull IBaseResource theFocalDiagnosticReport,
086                        @Nonnull MappingTarget<ORU_R01> theMappingTarget,
087                        OutboundMappingInstructions theOutboundMappingInstructions);
088
089        /**
090         * Populate a complete ADT_A01 structure using an Encounter resource as the focal resource. This can be
091         * used to create messages of type ADT^A01, ADT^A04
092         * <p>
093         * This method attempts to populate the entire message structure using values from the focal
094         * resource as well as from other resources that are referenced directly or indirectly from the
095         * focal resource.
096         * </p>
097         *
098         * @param theMappingTarget Should contain message of type {@link ADT_A01}
099         */
100        void populateMessageAdtA01FromEncounter(
101                        @Nonnull IBaseResource theFocalEncounter,
102                        @Nonnull MappingTarget<ADT_A01> theMappingTarget,
103                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions)
104                        throws HL7Exception;
105
106        /**
107         * Populate a complete ADT_A02 structure using an Encounter resource as the focal resource. This can be
108         * used to create messages of type ADT^A02
109         * <p>
110         * This method attempts to populate the entire message structure using values from the focal
111         * resource as well as from other resources that are referenced directly or indirectly from the
112         * focal resource.
113         * </p>
114         *
115         * @param theMappingTarget Should contain message of type {@link ADT_A02}
116         */
117        void populateMessageAdtA02FromEncounter(
118                        @Nonnull IBaseResource theFocalEncounter,
119                        @Nonnull MappingTarget<ADT_A02> theMappingTarget,
120                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions)
121                        throws HL7Exception;
122
123        /**
124         * Populate a complete ADT_A03 structure using an Encounter resource as the focal resource. This can be
125         * used to create messages of type ADT^A03
126         * <p>
127         * This method attempts to populate the entire message structure using values from the focal
128         * resource as well as from other resources that are referenced directly or indirectly from the
129         * focal resource.
130         * </p>
131         *
132         * @param theMappingTarget Should contain message of type {@link ADT_A03}
133         */
134        void populateMessageAdtA03FromEncounter(
135                        @Nonnull IBaseResource theFocalEncounter,
136                        @Nonnull MappingTarget<ADT_A03> theMappingTarget,
137                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions)
138                        throws HL7Exception;
139
140        /**
141         * Populate a complete ADT_A05 structure using a Patient resource as the focal resource. This can be
142         * used to create messages of type ADT^A28, ADT^A31
143         * <p>
144         * This method attempts to populate the entire message structure using values from the focal
145         * resource as well as from other resources that are referenced directly or indirectly from the
146         * focal resource.
147         * </p>
148         *
149         * @param theFocalPatient  A Patient resource to use as the focal resource
150         * @param theMappingTarget Should contain message of type {@link ADT_A05}
151         */
152        void populateMessageAdtA05FromPatient(
153                        @Nonnull IBaseResource theFocalPatient,
154                        @Nonnull MappingTarget<ADT_A05> theMappingTarget,
155                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
156
157        /**
158         * Populate a complete ADT_A05 structure using an Encounter resource as the focal resource. This can be
159         * used to create messages of type ADT^A05, ADT^A08
160         * <p>
161         * This method attempts to populate the entire message structure using values from the focal
162         * resource as well as from other resources that are referenced directly or indirectly from the
163         * focal resource.
164         * </p>
165         *
166         * @param theFocalEncounter An Encounter resource to use as the focal resource
167         * @param theMappingTarget  Should contain message of type {@link ADT_A05}
168         */
169        void populateMessageAdtA05FromEncounter(
170                        @Nonnull IBaseResource theFocalEncounter,
171                        @Nonnull MappingTarget<ADT_A05> theMappingTarget,
172                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions)
173                        throws HL7Exception;
174
175        /**
176         * Populate a complete OMG_O19 structure using a ServiceRequest resource (R4+) or a ProcedureRequest resource (DSTU3)
177         * as the focal resource. This can be used to create messages of type OMG^O19.
178         * <p>
179         * This method attempts to populate the entire message structure using values from the focal
180         * resource as well as from other resources that are referenced directly or indirectly from the
181         * focal resource.
182         * </p>
183         *
184         * @param theMappingTarget Should contain message of type {@link OMG_O19}
185         */
186        void populateMessageOmgO19FromServiceRequest(
187                        @Nonnull IBaseResource theFocalServiceRequest,
188                        @Nonnull MappingTarget<OMG_O19> theMappingTarget,
189                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions)
190                        throws HL7Exception;
191
192        /**
193         * Populate a complete ORM_O01 structure using a ServiceRequest resource (R4+) or a ProcedureRequest resource (DSTU3)
194         * as the focal resource. This can be used to create messages of type ORM^O01.
195         * <p>
196         * This method attempts to populate the entire message structure using values from the focal
197         * resource as well as from other resources that are referenced directly or indirectly from the
198         * focal resource.
199         * </p>
200         *
201         * @param theMappingTarget Should contain message of type {@link ORM_O01}
202         */
203        void populateMessageOrmO01FromServiceRequest(
204                        @Nonnull IBaseResource theFocalServiceRequest,
205                        @Nonnull MappingTarget<ORM_O01> theMappingTarget,
206                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions)
207                        throws HL7Exception;
208
209        /**
210         * Populate a complete ORU_R01 structure using a ServiceRequest resource (R4+) or a ProcedureRequest resource (DSTU3)
211         * as the focal resource. This can be used to create messages of type ORU^R01.
212         * <p>
213         * This method attempts to populate the entire message structure using values from the focal
214         * resource as well as from other resources that are referenced directly or indirectly from the
215         * focal resource.
216         * </p>
217         *
218         * @param theMappingTarget Should contain message of type {@link ORU_R01}
219         */
220        void populateMessageOruR01FromServiceRequest(
221                        @Nonnull IBaseResource theFocalServiceRequest,
222                        @Nonnull MappingTarget<ORU_R01> theMappingTarget,
223                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
224
225        /**
226         * Populates an ROL segment using a single {@literal Encounter.participant} instance.
227         * Because {@literal Encounter.participant} is a repeating field, an index must also
228         * be passed in.
229         *
230         * @param theEncounter                 The source Encounter to draw values from.
231         * @param theEncounterParticipantIndex The index of the {@literal Encounter.participant} repetition to draw values from (0-indexed).
232         * @param theRol                       The ROL segment to populate.
233         * @param theMappingTarget             The target object. This is only used to store any warnings or errors generated as
234         *                                     a part of the mapping.
235         */
236        void populateSegmentRolFromEncounterParticipant(
237                        @Nonnull Encounter theEncounter,
238                        int theEncounterParticipantIndex,
239                        @Nonnull ROL theRol,
240                        @Nonnull MappingTarget<?> theMappingTarget,
241                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
242
243        /**
244         * Populates an AL1 segment using values from an AllergyIntolerance resource instance.
245         *
246         * @param theAllergyIntolerance The source Coverage to draw values from.
247         * @param theSetId              The set ID to populate in AL1-1, or {@literal null}.
248         * @param theAl1                The AL1 segment to populate
249         * @param theMappingTarget      The target object. This is only used to store any warnings or errors generated as
250         *                              a part of the mapping.
251         */
252        void populateSegmentAl1FromAllergyIntolerance(
253                        @Nonnull AllergyIntolerance theAllergyIntolerance,
254                        @Nullable Integer theSetId,
255                        @Nonnull AL1 theAl1,
256                        @Nonnull MappingTarget<?> theMappingTarget,
257                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
258
259        /**
260         * Populates a pair of IN1 and IN2 segments using values from a Coverage resource instance.
261         *
262         * @param theCoverage      The source Coverage to draw values from.
263         * @param theIn1           The IN1 segment to populate
264         * @param theIn2           The IN2 segment to populate
265         * @param theMappingTarget The target object. This is only used to store any warnings or errors generated as
266         *                         a part of the mapping.
267         */
268        void populateSegmentIn1In2FromInsurance(
269                        Coverage theCoverage,
270                        IN1 theIn1,
271                        IN2 theIn2,
272                        MappingTarget<?> theMappingTarget,
273                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
274
275        /**
276         * Populates a PR1 segment using values from a Procedure resource instance.
277         *
278         * @param theProcedure     The source Procedure to draw values from.
279         * @param thePr1           The PR1 segment to populate
280         * @param theMappingTarget The target object. This is only used to store any warnings or errors generated as
281         *                         a part of the mapping.
282         */
283        void populateSegmentPr1FromProcedure(
284                        Procedure theProcedure,
285                        PR1 thePr1,
286                        MappingTarget<?> theMappingTarget,
287                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
288
289        /**
290         * Populates an NK1 segment using a single {@literal Patient.contact} instance.
291         * Because {@literal Patient.context} is a repeating field, an index must also
292         * be passed in.
293         *
294         * @param theEncounter               The source Encounter to draw values from.
295         * @param theEncounterConditionIndex The index of the {@literal Encounter.condition} repetition to draw values from (0-indexed).
296         * @param theMappingTarget           The target object. This is only used to store any warnings or errors generated as
297         *                                   a part of the mapping.
298         * @param theDg1                     The DG1 segment to populate.
299         */
300        void populateSegmentDg1FromEncounterCondition(
301                        Encounter theEncounter,
302                        int theEncounterConditionIndex,
303                        DG1 theDg1,
304                        MappingTarget<?> theMappingTarget,
305                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
306
307        /**
308         * Populates an NK1 segment using a single {@literal Patient.contact} instance.
309         * Because {@literal Patient.context} is a repeating field, an index must also
310         * be passed in.
311         *
312         * @param thePatient             The source Patient to draw values from.
313         * @param thePatientContactIndex The index of the {@literal Patient.contact} repetition to draw values from (0-indexed).
314         * @param theNk1                 The NK1 segment to populate.
315         * @param theMappingTarget       The target object. This is only used to store any warnings or errors generated as
316         *                               a part of the mapping.
317         */
318        void populateSegmentNk1FromPatientContact(
319                        @Nonnull Patient thePatient,
320                        int thePatientContactIndex,
321                        @Nonnull NK1 theNk1,
322                        @Nonnull MappingTarget<?> theMappingTarget,
323                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
324
325        /**
326         * Populates a PID segment using information from a Patient resource and optionally an Encounter resource.
327         *
328         * @param theSubject         The subject patient.
329         * @param theEncounterOrNull The focal encounter, or {@literal null}.
330         * @param thePid             The PID segment to populate.
331         * @param theMappingTarget   The target object. This is only used to store any warnings or errors generated as
332         *                           a part of the mapping.
333         */
334        void populateSegmentPidFromPatient(
335                        @Nonnull Patient theSubject,
336                        @Nullable Encounter theEncounterOrNull,
337                        @Nonnull PID thePid,
338                        @Nonnull MappingTarget<?> theMappingTarget,
339                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
340
341        /**
342         * Populates a PD1 segment using information from a Patient resource.
343         *
344         * @param theSubject       The subject patient.
345         * @param thePd1           The PD1 segment to populate.
346         * @param theMappingTarget The target object. This is only used to store any warnings or errors generated as
347         *                         a part of the mapping.
348         */
349        void populateSegmentPd1FromPatient(
350                        @Nonnull Patient theSubject,
351                        @Nonnull PD1 thePd1,
352                        @Nonnull MappingTarget<?> theMappingTarget,
353                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
354
355        /**
356         * Populates a PV1 segment using information from an Encounter resource.
357         *
358         * @param theEncounter     The encounter resource.
359         * @param thePv1           The PV1 segment to populate.
360         * @param theMappingTarget The target object. This is only used to store any warnings or errors generated as
361         *                         a part of the mapping.
362         */
363        void populateSegmentPv1FromEncounter(
364                        @Nonnull Encounter theEncounter,
365                        @Nonnull PV1 thePv1,
366                        @Nonnull MappingTarget<?> theMappingTarget,
367                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
368
369        /**
370         * Populates a PV2 segment using information from an Encounter resource.
371         *
372         * @param theEncounter     The source Encounter to draw values from.
373         * @param thePv2           The PV2 segment to populate.
374         * @param theMappingTarget The target object. This is only used to store any warnings or errors generated as
375         *                         a part of the mapping.
376         */
377        void populateSegmentPv2FromEncounter(
378                        @Nonnull Encounter theEncounter,
379                        @Nonnull PV2 thePv2,
380                        @Nonnull MappingTarget<?> theMappingTarget,
381                        @Nonnull OutboundMappingInstructions theOutboundMappingInstructions);
382}