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.v25.message.ADT_A01;
015import ca.uhn.hl7v2.model.v25.message.ADT_A02;
016import ca.uhn.hl7v2.model.v25.message.ADT_A03;
017import ca.uhn.hl7v2.model.v25.message.ADT_A05;
018import ca.uhn.hl7v2.model.v25.message.ADT_A06;
019import ca.uhn.hl7v2.model.v25.message.ADT_A09;
020import ca.uhn.hl7v2.model.v25.message.ADT_A12;
021import ca.uhn.hl7v2.model.v25.message.ADT_A15;
022import ca.uhn.hl7v2.model.v25.message.ADT_A17;
023import ca.uhn.hl7v2.model.v25.message.ADT_A21;
024import ca.uhn.hl7v2.model.v25.message.ADT_A24;
025import ca.uhn.hl7v2.model.v25.message.ADT_A30;
026import ca.uhn.hl7v2.model.v25.message.ADT_A38;
027import ca.uhn.hl7v2.model.v25.message.ADT_A39;
028import ca.uhn.hl7v2.model.v25.message.ADT_A60;
029import ca.uhn.hl7v2.model.v25.message.BAR_P12;
030import ca.uhn.hl7v2.model.v25.message.DFT_P03;
031import ca.uhn.hl7v2.model.v25.message.MDM_T01;
032import ca.uhn.hl7v2.model.v25.message.MDM_T02;
033import ca.uhn.hl7v2.model.v25.message.OMG_O19;
034import ca.uhn.hl7v2.model.v25.message.ORM_O01;
035import ca.uhn.hl7v2.model.v25.message.ORU_R01;
036import ca.uhn.hl7v2.model.v25.message.RAS_O17;
037import ca.uhn.hl7v2.model.v25.message.RDE_O11;
038import ca.uhn.hl7v2.model.v25.message.VXU_V04;
039
040/**
041 * Wraps the relevant portion of an HL7 V2 message to an {@link IPatientMessage}
042 */
043public class PatientMessageFactory {
044
045        public IPatientMessage asPatientMessage(AbstractMessage theMsg) throws HL7Exception {
046                if (theMsg instanceof ADT_A01 msg) {
047                        return asPatientMessage(msg);
048                } else if (theMsg instanceof ADT_A02 msg) {
049                        return asPatientMessage(msg);
050                } else if (theMsg instanceof ADT_A03 msg) {
051                        return asPatientMessage(msg);
052                } else if (theMsg instanceof ADT_A05 msg) {
053                        return asPatientMessage(msg);
054                } else if (theMsg instanceof ADT_A06 msg) {
055                        return asPatientMessage(msg);
056                } else if (theMsg instanceof ADT_A09 msg) {
057                        return asPatientMessage(msg);
058                } else if (theMsg instanceof ADT_A12 msg) {
059                        return asPatientMessage(msg);
060                } else if (theMsg instanceof ADT_A15 msg) {
061                        return asPatientMessage(msg);
062                } else if (theMsg instanceof ADT_A17 msg) {
063                        return asPatientMessage(msg);
064                } else if (theMsg instanceof ADT_A21 msg) {
065                        return asPatientMessage(msg);
066                } else if (theMsg instanceof ADT_A24 msg) {
067                        return asPatientMessage(msg);
068                } else if (theMsg instanceof ADT_A30 msg) {
069                        return asPatientMessage(msg);
070                } else if (theMsg instanceof ADT_A38 msg) {
071                        return asPatientMessage(msg);
072                } else if (theMsg instanceof ADT_A39 msg) {
073                        return asPatientMessage(msg);
074                } else if (theMsg instanceof ADT_A60 msg) {
075                        return asPatientMessage(msg);
076                } else if (theMsg instanceof DFT_P03 msg) {
077                        return asPatientMessage(msg);
078                } else if (theMsg instanceof OMG_O19 msg) {
079                        return asPatientMessage(msg);
080                } else if (theMsg instanceof ORM_O01 msg) {
081                        return asPatientMessage(msg);
082                } else if (theMsg instanceof ORU_R01 msg) {
083                        return asPatientMessage(msg);
084                } else if (theMsg instanceof RAS_O17 msg) {
085                        return asPatientMessage(msg);
086                } else if (theMsg instanceof RDE_O11 msg) {
087                        return asPatientMessage(msg);
088                } else if (theMsg instanceof VXU_V04 msg) {
089                        return asPatientMessage(msg);
090                } else if (theMsg instanceof BAR_P12 msg) {
091                        return asPatientMessage(msg);
092                } else if (theMsg instanceof MDM_T01 msg) {
093                        return asPatientMessage(msg);
094                } else if (theMsg instanceof MDM_T02 msg) {
095                        return asPatientMessage(msg);
096                } else {
097                        return null;
098                }
099        }
100
101        private IPatientMessage asPatientMessage(DFT_P03 theMsg) throws HL7Exception {
102                return new PatientMessage()
103                                .setPID(theMsg.getPID())
104                                .setPD1(theMsg.getPD1())
105                                .setIAM(null)
106                                .setNK1(null, null)
107                                .setROL(theMsg.getROLAll(), () -> theMsg.insertROL(theMsg.getROLReps()));
108        }
109
110        private IPatientMessage asPatientMessage(ORU_R01 theMsg) throws HL7Exception {
111                return new PatientMessage()
112                                .setPID(theMsg.getPATIENT_RESULT().getPATIENT().getPID())
113                                .setPD1(theMsg.getPATIENT_RESULT().getPATIENT().getPD1())
114                                .setIAM(null)
115                                .setNK1(theMsg.getPATIENT_RESULT().getPATIENT().getNK1All(), () -> theMsg.getPATIENT_RESULT()
116                                                .getPATIENT()
117                                                .insertNK1(theMsg.getPATIENT_RESULT().getPATIENT().getNK1Reps()))
118                                .setROL(null, null);
119        }
120
121        private IPatientMessage asPatientMessage(RAS_O17 theMsg) {
122                return new PatientMessage()
123                                .setPID(theMsg.getPATIENT().getPID())
124                                .setPD1(theMsg.getPATIENT().getPD1())
125                                .setIAM(null)
126                                .setNK1(null, null)
127                                .setROL(null, null);
128        }
129
130        private IPatientMessage asPatientMessage(RDE_O11 theMsg) {
131                return new PatientMessage()
132                                .setPID(theMsg.getPATIENT().getPID())
133                                .setPD1(theMsg.getPATIENT().getPD1())
134                                .setIAM(null)
135                                .setNK1(null, null)
136                                .setROL(null, null);
137        }
138
139        private IPatientMessage asPatientMessage(ADT_A01 theMsg) throws HL7Exception {
140                return new PatientMessage()
141                                .setPID(theMsg.getPID())
142                                .setPD1(theMsg.getPD1())
143                                .setIAM(null)
144                                .setNK1(theMsg.getNK1All(), () -> theMsg.insertNK1(theMsg.getNK1Reps()))
145                                .setROL(theMsg.getROLAll(), () -> theMsg.insertROL(theMsg.getROLReps()));
146        }
147
148        private IPatientMessage asPatientMessage(ADT_A02 theMsg) throws HL7Exception {
149                return new PatientMessage()
150                                .setPID(theMsg.getPID())
151                                .setPD1(theMsg.getPD1())
152                                .setIAM(null)
153                                .setNK1(null, null)
154                                .setROL(theMsg.getROLAll(), () -> theMsg.insertROL(theMsg.getROLReps()));
155        }
156
157        private IPatientMessage asPatientMessage(ADT_A03 theMsg) throws HL7Exception {
158                return new PatientMessage()
159                                .setPID(theMsg.getPID())
160                                .setPD1(theMsg.getPD1())
161                                .setIAM(null)
162                                .setNK1(theMsg.getNK1All(), () -> theMsg.insertNK1(theMsg.getNK1Reps()))
163                                .setROL(theMsg.getROLAll(), () -> theMsg.insertROL(theMsg.getROLReps()));
164        }
165
166        private IPatientMessage asPatientMessage(ADT_A05 theMsg) throws HL7Exception {
167                return new PatientMessage()
168                                .setPID(theMsg.getPID())
169                                .setPD1(theMsg.getPD1())
170                                .setIAM(null)
171                                .setNK1(theMsg.getNK1All(), () -> theMsg.insertNK1(theMsg.getNK1Reps()))
172                                .setROL(theMsg.getROLAll(), () -> theMsg.insertROL(theMsg.getROLReps()));
173        }
174
175        private IPatientMessage asPatientMessage(ADT_A06 theMsg) throws HL7Exception {
176                return new PatientMessage()
177                                .setPID(theMsg.getPID())
178                                .setPD1(theMsg.getPD1())
179                                .setIAM(null)
180                                .setNK1(theMsg.getNK1All(), () -> theMsg.insertNK1(theMsg.getNK1Reps()))
181                                .setROL(theMsg.getROLAll(), () -> theMsg.insertROL(theMsg.getROLReps()));
182        }
183
184        private IPatientMessage asPatientMessage(ADT_A09 theMsg) {
185                return new PatientMessage()
186                                .setPID(theMsg.getPID())
187                                .setPD1(theMsg.getPD1())
188                                .setIAM(null)
189                                .setNK1(null, null)
190                                .setROL(null, null);
191        }
192
193        private IPatientMessage asPatientMessage(ADT_A12 theMsg) {
194                return new PatientMessage()
195                                .setPID(theMsg.getPID())
196                                .setPD1(theMsg.getPD1())
197                                .setIAM(null)
198                                .setNK1(null, null)
199                                .setROL(null, null);
200        }
201
202        private IPatientMessage asPatientMessage(ADT_A17 theMsg) {
203                return new PatientMessage()
204                                .setPID(theMsg.getPID())
205                                .setPD1(theMsg.getPD1())
206                                .setIAM(null)
207                                .setNK1(null, null)
208                                .setROL(null, null);
209        }
210
211        private IPatientMessage asPatientMessage(ADT_A21 theMsg) {
212                return new PatientMessage()
213                                .setPID(theMsg.getPID())
214                                .setPD1(theMsg.getPD1())
215                                .setIAM(null)
216                                .setNK1(null, null)
217                                .setROL(null, null);
218        }
219
220        private IPatientMessage asPatientMessage(ADT_A38 theMsg) {
221                return new PatientMessage()
222                                .setPID(theMsg.getPID())
223                                .setPD1(theMsg.getPD1())
224                                .setIAM(null)
225                                .setNK1(null, null)
226                                .setROL(null, null);
227        }
228
229        private IPatientMessage asPatientMessage(ADT_A30 theMsg) {
230                return new PatientMessage()
231                                .setPID(theMsg.getPID())
232                                .setPD1(theMsg.getPD1())
233                                .setIAM(null)
234                                .setNK1(null, null)
235                                .setROL(null, null);
236        }
237
238        private IPatientMessage asPatientMessage(ADT_A60 theMsg) throws HL7Exception {
239                return new PatientMessage()
240                                .setPID(theMsg.getPID())
241                                // No PD1 in this structure.
242                                .setPD1(null)
243                                .setIAM(theMsg.getIAMAll())
244                                .setNK1(null, null)
245                                .setROL(null, null);
246        }
247
248        private IPatientMessage asPatientMessage(ADT_A24 theMsg) {
249                return new PatientMessage()
250                                .setPID(theMsg.getPID())
251                                .setPD1(theMsg.getPD1())
252                                .setIAM(null)
253                                .setNK1(null, null)
254                                .setROL(null, null);
255        }
256
257        private IPatientMessage asPatientMessage(ADT_A15 theMsg) {
258                return new PatientMessage()
259                                .setPID(theMsg.getPID())
260                                .setPD1(theMsg.getPD1())
261                                .setIAM(null)
262                                .setNK1(null, null)
263                                .setROL(null, null);
264        }
265
266        private IPatientMessage asPatientMessage(ADT_A39 theMsg) {
267                return new PatientMessage()
268                                // PATIENT has multiple reps actually.. need to deal with that
269                                // if we ever implement A39
270                                .setPID(theMsg.getPATIENT().getPID())
271                                .setPD1(theMsg.getPATIENT().getPD1())
272                                .setIAM(null)
273                                .setNK1(null, null)
274                                .setROL(null, null);
275        }
276
277        private IPatientMessage asPatientMessage(ORM_O01 theMsg) {
278                return new PatientMessage()
279                                .setPID(theMsg.getPATIENT().getPID())
280                                .setIAM(null)
281                                .setPD1(theMsg.getPATIENT().getPD1())
282                                .setNK1(null, null)
283                                .setROL(null, null);
284        }
285
286        private IPatientMessage asPatientMessage(OMG_O19 theMsg) {
287                return new PatientMessage()
288                                .setPID(theMsg.getPATIENT().getPID())
289                                .setIAM(null)
290                                .setPD1(theMsg.getPATIENT().getPD1())
291                                .setNK1(null, null)
292                                .setROL(null, null);
293        }
294
295        private IPatientMessage asPatientMessage(VXU_V04 theMsg) {
296                return new PatientMessage()
297                                .setPID(theMsg.getPID())
298                                .setPD1(theMsg.getPD1())
299                                .setIAM(null)
300                                .setNK1(null, null)
301                                .setROL(null, null);
302        }
303
304        private IPatientMessage asPatientMessage(BAR_P12 theMsg) {
305                return new PatientMessage()
306                                .setPID(theMsg.getPID())
307                                .setPD1(null)
308                                .setIAM(null)
309                                .setNK1(null, null)
310                                .setROL(null, null);
311        }
312
313        private IPatientMessage asPatientMessage(MDM_T01 theMsg) {
314                return new PatientMessage().setPID(theMsg.getPID());
315        }
316
317        private IPatientMessage asPatientMessage(MDM_T02 theMsg) {
318                return new PatientMessage().setPID(theMsg.getPID());
319        }
320}