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