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