001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2024 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.api.pub.hl7v2.common.abstraction;
011
012import ca.uhn.hl7v2.HL7Exception;
013import ca.uhn.hl7v2.model.AbstractMessage;
014import ca.uhn.hl7v2.model.Group;
015import ca.uhn.hl7v2.model.v25.group.ADT_A01_INSURANCE;
016import ca.uhn.hl7v2.model.v25.group.ADT_A01_PROCEDURE;
017import ca.uhn.hl7v2.model.v25.group.ADT_A03_INSURANCE;
018import ca.uhn.hl7v2.model.v25.group.ADT_A03_PROCEDURE;
019import ca.uhn.hl7v2.model.v25.group.ADT_A05_INSURANCE;
020import ca.uhn.hl7v2.model.v25.group.ADT_A05_PROCEDURE;
021import ca.uhn.hl7v2.model.v25.group.ADT_A06_INSURANCE;
022import ca.uhn.hl7v2.model.v25.group.ADT_A06_PROCEDURE;
023import ca.uhn.hl7v2.model.v25.group.BAR_P12_PROCEDURE;
024import ca.uhn.hl7v2.model.v25.group.DFT_P03_INSURANCE;
025import ca.uhn.hl7v2.model.v25.group.OMG_O19_INSURANCE;
026import ca.uhn.hl7v2.model.v25.group.OMG_O19_ORDER;
027import ca.uhn.hl7v2.model.v25.group.ORM_O01_INSURANCE;
028import ca.uhn.hl7v2.model.v25.group.ORM_O01_ORDER;
029import ca.uhn.hl7v2.model.v25.message.ADT_A01;
030import ca.uhn.hl7v2.model.v25.message.ADT_A02;
031import ca.uhn.hl7v2.model.v25.message.ADT_A03;
032import ca.uhn.hl7v2.model.v25.message.ADT_A05;
033import ca.uhn.hl7v2.model.v25.message.ADT_A06;
034import ca.uhn.hl7v2.model.v25.message.ADT_A09;
035import ca.uhn.hl7v2.model.v25.message.ADT_A12;
036import ca.uhn.hl7v2.model.v25.message.ADT_A15;
037import ca.uhn.hl7v2.model.v25.message.ADT_A17;
038import ca.uhn.hl7v2.model.v25.message.ADT_A21;
039import ca.uhn.hl7v2.model.v25.message.ADT_A24;
040import ca.uhn.hl7v2.model.v25.message.ADT_A30;
041import ca.uhn.hl7v2.model.v25.message.ADT_A38;
042import ca.uhn.hl7v2.model.v25.message.ADT_A39;
043import ca.uhn.hl7v2.model.v25.message.ADT_A60;
044import ca.uhn.hl7v2.model.v25.message.BAR_P12;
045import ca.uhn.hl7v2.model.v25.message.DFT_P03;
046import ca.uhn.hl7v2.model.v25.message.MDM_T01;
047import ca.uhn.hl7v2.model.v25.message.MDM_T02;
048import ca.uhn.hl7v2.model.v25.message.OMG_O19;
049import ca.uhn.hl7v2.model.v25.message.ORM_O01;
050import ca.uhn.hl7v2.model.v25.message.ORU_R01;
051import ca.uhn.hl7v2.model.v25.message.RAS_O17;
052import ca.uhn.hl7v2.model.v25.message.RDE_O11;
053import ca.uhn.hl7v2.model.v25.message.VXU_V04;
054import ca.uhn.hl7v2.model.v25.segment.DG1;
055import ca.uhn.hl7v2.model.v25.segment.IN1;
056import ca.uhn.hl7v2.model.v25.segment.IN2;
057import ca.uhn.hl7v2.model.v25.segment.PR1;
058import ca.uhn.hl7v2.model.v25.segment.PV1;
059import ca.uhn.hl7v2.model.v25.segment.PV2;
060
061import java.util.ArrayList;
062import java.util.List;
063import java.util.function.Function;
064import java.util.stream.Collectors;
065
066/**
067 * Wraps the relevant portion of an HL7 V2 message to an {@link IVisitMessage}
068 */
069public class VisitMessageFactory {
070
071        public IVisitMessage asVisitMessage(AbstractMessage theMsg) throws HL7Exception {
072                if (theMsg instanceof ADT_A01) {
073                        return asVisitMessage((ADT_A01) theMsg);
074                } else if (theMsg instanceof ADT_A02) {
075                        return asVisitMessage((ADT_A02) theMsg);
076                } else if (theMsg instanceof ADT_A03) {
077                        return asVisitMessage((ADT_A03) theMsg);
078                } else if (theMsg instanceof ADT_A05) {
079                        return asVisitMessage((ADT_A05) theMsg);
080                } else if (theMsg instanceof ADT_A06) {
081                        return asVisitMessage((ADT_A06) theMsg);
082                } else if (theMsg instanceof ADT_A09) {
083                        return asVisitMessage((ADT_A09) theMsg);
084                } else if (theMsg instanceof ADT_A12) {
085                        return asVisitMessage((ADT_A12) theMsg);
086                } else if (theMsg instanceof ADT_A15) {
087                        return asVisitMessage((ADT_A15) theMsg);
088                } else if (theMsg instanceof ADT_A17) {
089                        return asVisitMessage((ADT_A17) theMsg);
090                } else if (theMsg instanceof ADT_A21) {
091                        return asVisitMessage((ADT_A21) theMsg);
092                } else if (theMsg instanceof ADT_A24) {
093                        return asVisitMessage((ADT_A24) theMsg);
094                } else if (theMsg instanceof ADT_A30) {
095                        return asVisitMessage((ADT_A30) theMsg);
096                } else if (theMsg instanceof ADT_A38) {
097                        return asVisitMessage((ADT_A38) theMsg);
098                } else if (theMsg instanceof ADT_A39) {
099                        return asVisitMessage((ADT_A39) theMsg);
100                } else if (theMsg instanceof ADT_A60) {
101                        return asVisitMessage((ADT_A60) theMsg);
102                } else if (theMsg instanceof DFT_P03) {
103                        return asVisitMessage((DFT_P03) theMsg);
104                } else if (theMsg instanceof OMG_O19) {
105                        return asVisitMessage((OMG_O19) theMsg);
106                } else if (theMsg instanceof ORM_O01) {
107                        return asVisitMessage((ORM_O01) theMsg);
108                } else if (theMsg instanceof ORU_R01) {
109                        return asVisitMessage((ORU_R01) theMsg);
110                } else if (theMsg instanceof RAS_O17) {
111                        return asVisitMessage((RAS_O17) theMsg);
112                } else if (theMsg instanceof RDE_O11) {
113                        return asVisitMessage((RDE_O11) theMsg);
114                } else if (theMsg instanceof VXU_V04) {
115                        return asVisitMessage((VXU_V04) theMsg);
116                } else if (theMsg instanceof BAR_P12) {
117                        return asVisitMessage((BAR_P12) theMsg);
118                } else if (theMsg instanceof MDM_T01) {
119                        return asVisitMessage((MDM_T01) theMsg);
120                } else if (theMsg instanceof MDM_T02) {
121                        return asVisitMessage((MDM_T02) theMsg);
122                } else {
123                        return null;
124                }
125        }
126
127        private VisitMessage getDefaultVisitMessageAndSetPV1PV2(PV1 thePv1, PV2 thePv2) {
128                return new VisitMessage()
129                                .setPV1(thePv1)
130                                .setPV2(thePv2)
131                                .setDG1(null, null)
132                                .setROL(null, null)
133                                .setGT1(null)
134                                .setOBX(null);
135        }
136
137        private <T> List<IVisitMessage.IVisitMessageInsurance> getInsurances(
138                        List<T> theInsurances, Function<T, IN1> theIn1Function, Function<T, IN2> theIn2Function) {
139                return theInsurances.stream()
140                                .map(t -> new VisitMessageInsurance<>(t, theIn1Function, theIn2Function))
141                                .collect(Collectors.toList());
142        }
143
144        private <T> List<IVisitMessage.IVisitMessageProcedure> getProcedures(
145                        List<T> theProcedures, Function<T, PR1> thePr1Function) {
146                return theProcedures.stream()
147                                .map(t -> new VisitMessageProcedure((Group) t, thePr1Function))
148                                .collect(Collectors.toList());
149        }
150
151        private IVisitMessage asVisitMessage(ADT_A39 theMsg) {
152                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPATIENT().getPV1(), null);
153        }
154
155        private IVisitMessage asVisitMessage(ADT_A60 theMsg) {
156                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2());
157        }
158
159        private IVisitMessage asVisitMessage(ADT_A15 theMsg) {
160                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2());
161        }
162
163        private IVisitMessage asVisitMessage(DFT_P03 theMsg) throws HL7Exception {
164                VisitMessage visitMessage = getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
165                                .setDG1(theMsg.getDG1All(), () -> theMsg.insertDG1(theMsg.getDG1Reps()))
166                                .setGT1(theMsg.getGT1All());
167
168                List<IVisitMessage.IVisitMessageInsurance> insurances =
169                                getInsurances(theMsg.getINSURANCEAll(), DFT_P03_INSURANCE::getIN1, DFT_P03_INSURANCE::getIN2);
170                visitMessage.setInsurances(
171                                insurances,
172                                () -> new VisitMessageInsurance<>(
173                                                theMsg.insertINSURANCE(theMsg.getINSURANCEReps()),
174                                                DFT_P03_INSURANCE::getIN1,
175                                                DFT_P03_INSURANCE::getIN2));
176                return visitMessage;
177        }
178
179        private IVisitMessage asVisitMessage(ORU_R01 theMsg) {
180                return getDefaultVisitMessageAndSetPV1PV2(
181                                theMsg.getPATIENT_RESULT().getPATIENT().getVISIT().getPV1(),
182                                theMsg.getPATIENT_RESULT().getPATIENT().getVISIT().getPV2());
183        }
184
185        private IVisitMessage asVisitMessage(RAS_O17 theMsg) {
186                return getDefaultVisitMessageAndSetPV1PV2(
187                                theMsg.getPATIENT().getPATIENT_VISIT().getPV1(),
188                                theMsg.getPATIENT().getPATIENT_VISIT().getPV2());
189        }
190
191        private IVisitMessage asVisitMessage(RDE_O11 theMsg) {
192                return getDefaultVisitMessageAndSetPV1PV2(
193                                theMsg.getPATIENT().getPATIENT_VISIT().getPV1(),
194                                theMsg.getPATIENT().getPATIENT_VISIT().getPV2());
195        }
196
197        private IVisitMessage asVisitMessage(ADT_A01 theMsg) throws HL7Exception {
198                VisitMessage visitMessage = getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
199                                .setDG1(theMsg.getDG1All(), () -> theMsg.insertDG1(theMsg.getDG1Reps()))
200                                .setROL(theMsg.getROL2All(), () -> theMsg.insertROL2(theMsg.getROL2Reps()))
201                                .setAL1(theMsg.getAL1All(), () -> theMsg.insertAL1(theMsg.getAL1Reps()))
202                                .setGT1(theMsg.getGT1All())
203                                .setOBX(theMsg.getOBXAll());
204
205                List<IVisitMessage.IVisitMessageProcedure> procedures =
206                                getProcedures(theMsg.getPROCEDUREAll(), ADT_A01_PROCEDURE::getPR1);
207                visitMessage.setProcedures(
208                                procedures,
209                                () -> new VisitMessageProcedure<>(
210                                                theMsg.insertPROCEDURE(theMsg.getPROCEDUREReps()), ADT_A01_PROCEDURE::getPR1));
211
212                List<IVisitMessage.IVisitMessageInsurance> insurances =
213                                getInsurances(theMsg.getINSURANCEAll(), ADT_A01_INSURANCE::getIN1, ADT_A01_INSURANCE::getIN2);
214                visitMessage.setInsurances(
215                                insurances,
216                                () -> new VisitMessageInsurance<>(
217                                                theMsg.insertINSURANCE(theMsg.getINSURANCEReps()),
218                                                ADT_A01_INSURANCE::getIN1,
219                                                ADT_A01_INSURANCE::getIN2));
220
221                return visitMessage;
222        }
223
224        private IVisitMessage asVisitMessage(ADT_A02 theMsg) throws HL7Exception {
225                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
226                                .setROL(theMsg.getROL2All(), () -> theMsg.insertROL2(theMsg.getROL2Reps()))
227                                .setOBX(theMsg.getOBXAll());
228        }
229
230        private IVisitMessage asVisitMessage(ADT_A03 theMsg) throws HL7Exception {
231                VisitMessage visitMessage = getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
232                                .setDG1(theMsg.getDG1All(), () -> theMsg.insertDG1(theMsg.getDG1Reps()))
233                                .setROL(theMsg.getROL2All(), () -> theMsg.insertROL2(theMsg.getROL2Reps()))
234                                .setAL1(theMsg.getAL1All(), () -> theMsg.insertAL1(theMsg.getAL1Reps()))
235                                .setGT1(theMsg.getGT1All())
236                                .setOBX(theMsg.getOBXAll());
237
238                List<IVisitMessage.IVisitMessageProcedure> procedures =
239                                getProcedures(theMsg.getPROCEDUREAll(), ADT_A03_PROCEDURE::getPR1);
240                visitMessage.setProcedures(
241                                procedures,
242                                () -> new VisitMessageProcedure<>(
243                                                theMsg.insertPROCEDURE(theMsg.getPROCEDUREReps()), ADT_A03_PROCEDURE::getPR1));
244
245                List<IVisitMessage.IVisitMessageInsurance> insurances =
246                                getInsurances(theMsg.getINSURANCEAll(), ADT_A03_INSURANCE::getIN1, ADT_A03_INSURANCE::getIN2);
247                visitMessage.setInsurances(
248                                insurances,
249                                () -> new VisitMessageInsurance<>(
250                                                theMsg.insertINSURANCE(theMsg.getINSURANCEReps()),
251                                                ADT_A03_INSURANCE::getIN1,
252                                                ADT_A03_INSURANCE::getIN2));
253
254                return visitMessage;
255        }
256
257        private IVisitMessage asVisitMessage(ADT_A05 theMsg) throws HL7Exception {
258                VisitMessage visitMessage = getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
259                                .setDG1(theMsg.getDG1All(), () -> theMsg.insertDG1(theMsg.getDG1Reps()))
260                                .setROL(theMsg.getROL2All(), () -> theMsg.insertROL2(theMsg.getROL2Reps()))
261                                .setAL1(theMsg.getAL1All(), () -> theMsg.insertAL1(theMsg.getAL1Reps()))
262                                .setGT1(theMsg.getGT1All())
263                                .setOBX(theMsg.getOBXAll());
264
265                List<IVisitMessage.IVisitMessageProcedure> procedures =
266                                getProcedures(theMsg.getPROCEDUREAll(), ADT_A05_PROCEDURE::getPR1);
267                visitMessage.setProcedures(
268                                procedures,
269                                () -> new VisitMessageProcedure<>(
270                                                theMsg.insertPROCEDURE(theMsg.getPROCEDUREReps()), ADT_A05_PROCEDURE::getPR1));
271
272                List<IVisitMessage.IVisitMessageInsurance> insurances =
273                                getInsurances(theMsg.getINSURANCEAll(), ADT_A05_INSURANCE::getIN1, ADT_A05_INSURANCE::getIN2);
274                visitMessage.setInsurances(
275                                insurances,
276                                () -> new VisitMessageInsurance<>(
277                                                theMsg.insertINSURANCE(theMsg.getINSURANCEReps()),
278                                                ADT_A05_INSURANCE::getIN1,
279                                                ADT_A05_INSURANCE::getIN2));
280
281                return visitMessage;
282        }
283
284        private IVisitMessage asVisitMessage(ADT_A06 theMsg) throws HL7Exception {
285                VisitMessage visitMessage = getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
286                                .setDG1(theMsg.getDG1All(), () -> theMsg.insertDG1(theMsg.getDG1Reps()))
287                                .setROL(theMsg.getROL2All(), () -> theMsg.insertROL2(theMsg.getROL2Reps()))
288                                .setAL1(theMsg.getAL1All(), () -> theMsg.insertAL1(theMsg.getAL1Reps()))
289                                .setGT1(theMsg.getGT1All())
290                                .setOBX(theMsg.getOBXAll());
291
292                List<IVisitMessage.IVisitMessageProcedure> procedures =
293                                getProcedures(theMsg.getPROCEDUREAll(), ADT_A06_PROCEDURE::getPR1);
294                visitMessage.setProcedures(
295                                procedures,
296                                () -> new VisitMessageProcedure<>(
297                                                theMsg.insertPROCEDURE(theMsg.getPROCEDUREReps()), ADT_A06_PROCEDURE::getPR1));
298
299                List<IVisitMessage.IVisitMessageInsurance> insurances =
300                                getInsurances(theMsg.getINSURANCEAll(), ADT_A06_INSURANCE::getIN1, ADT_A06_INSURANCE::getIN2);
301                visitMessage.setInsurances(
302                                insurances,
303                                () -> new VisitMessageInsurance<>(
304                                                theMsg.insertINSURANCE(theMsg.getINSURANCEReps()),
305                                                ADT_A06_INSURANCE::getIN1,
306                                                ADT_A06_INSURANCE::getIN2));
307                return visitMessage;
308        }
309
310        private IVisitMessage asVisitMessage(ADT_A09 theMsg) throws HL7Exception {
311                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
312                                .setDG1(theMsg.getDG1All(), () -> theMsg.insertDG1(theMsg.getDG1Reps()))
313                                .setOBX(theMsg.getOBXAll());
314        }
315
316        private IVisitMessage asVisitMessage(ADT_A12 theMsg) throws HL7Exception {
317                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
318                                .setOBX(theMsg.getOBXAll());
319        }
320
321        private IVisitMessage asVisitMessage(ADT_A17 theMsg) throws HL7Exception {
322                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
323                                .setOBX(theMsg.getOBXAll());
324        }
325
326        private IVisitMessage asVisitMessage(ADT_A21 theMsg) throws HL7Exception {
327                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
328                                .setOBX(theMsg.getOBXAll());
329        }
330
331        private IVisitMessage asVisitMessage(ADT_A38 theMsg) throws HL7Exception {
332                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), theMsg.getPV2())
333                                .setDG1(theMsg.getDG1All(), () -> theMsg.insertDG1(theMsg.getDG1Reps()))
334                                .setOBX(theMsg.getOBXAll());
335        }
336
337        @SuppressWarnings("unused")
338        private IVisitMessage asVisitMessage(ADT_A30 theMsg) {
339                return getDefaultVisitMessageAndSetPV1PV2(null, null);
340        }
341
342        private IVisitMessage asVisitMessage(ADT_A24 theMsg) {
343                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), null);
344        }
345
346        private IVisitMessage asVisitMessage(VXU_V04 theMsg) {
347                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPATIENT().getPV1(), null);
348        }
349
350        private IVisitMessage asVisitMessage(ORM_O01 theMsg) throws HL7Exception {
351                List<DG1> dg1List = new ArrayList<>();
352                for (ORM_O01_ORDER nextOrder : theMsg.getORDERAll()) {
353                        dg1List.addAll(nextOrder.getORDER_DETAIL().getDG1All());
354                }
355
356                VisitMessage visitMessage = getDefaultVisitMessageAndSetPV1PV2(
357                                                theMsg.getPATIENT().getPATIENT_VISIT().getPV1(),
358                                                theMsg.getPATIENT().getPATIENT_VISIT().getPV2())
359                                .setDG1(dg1List, null);
360
361                List<IVisitMessage.IVisitMessageInsurance> insurances = getInsurances(
362                                theMsg.getPATIENT().getINSURANCEAll(), ORM_O01_INSURANCE::getIN1, ORM_O01_INSURANCE::getIN2);
363                visitMessage.setInsurances(
364                                insurances,
365                                () -> new VisitMessageInsurance<>(
366                                                theMsg.getPATIENT().insertINSURANCE(theMsg.getPATIENT().getINSURANCEReps()),
367                                                ORM_O01_INSURANCE::getIN1,
368                                                ORM_O01_INSURANCE::getIN2));
369
370                return visitMessage;
371        }
372
373        private IVisitMessage asVisitMessage(OMG_O19 theMsg) throws HL7Exception {
374                List<DG1> dg1List = new ArrayList<>();
375                for (OMG_O19_ORDER nextOrder : theMsg.getORDERAll()) {
376                        dg1List.addAll(nextOrder.getDG1All());
377                }
378
379                VisitMessage visitMessage = getDefaultVisitMessageAndSetPV1PV2(
380                                                theMsg.getPATIENT().getPATIENT_VISIT().getPV1(),
381                                                theMsg.getPATIENT().getPATIENT_VISIT().getPV2())
382                                .setDG1(dg1List, null);
383
384                List<IVisitMessage.IVisitMessageInsurance> insurances = getInsurances(
385                                theMsg.getPATIENT().getINSURANCEAll(), OMG_O19_INSURANCE::getIN1, OMG_O19_INSURANCE::getIN2);
386                visitMessage.setInsurances(
387                                insurances,
388                                () -> new VisitMessageInsurance<>(
389                                                theMsg.getPATIENT().insertINSURANCE(theMsg.getPATIENT().getINSURANCEReps()),
390                                                OMG_O19_INSURANCE::getIN1,
391                                                OMG_O19_INSURANCE::getIN2));
392
393                return visitMessage;
394        }
395
396        private IVisitMessage asVisitMessage(BAR_P12 theMsg) throws HL7Exception {
397                VisitMessage visitMessage = getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), null)
398                                .setDG1(theMsg.getDG1All(), () -> theMsg.insertDG1(theMsg.getDG1Reps()));
399
400                List<IVisitMessage.IVisitMessageProcedure> procedures =
401                                getProcedures(theMsg.getPROCEDUREAll(), BAR_P12_PROCEDURE::getPR1);
402                visitMessage.setProcedures(
403                                procedures,
404                                () -> new VisitMessageProcedure<>(
405                                                theMsg.insertPROCEDURE(theMsg.getPROCEDUREReps()), BAR_P12_PROCEDURE::getPR1));
406
407                return visitMessage;
408        }
409
410        private IVisitMessage asVisitMessage(MDM_T01 theMsg) {
411                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), null);
412        }
413
414        private IVisitMessage asVisitMessage(MDM_T02 theMsg) {
415                return getDefaultVisitMessageAndSetPV1PV2(theMsg.getPV1(), null);
416        }
417}