001package ca.cdr.api.pub.hl7v2.common.abstraction; 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.fhir.rest.server.exceptions.InternalErrorException; 014import ca.uhn.hl7v2.HL7Exception; 015import ca.uhn.hl7v2.model.v25.segment.*; 016import org.apache.commons.collections4.list.UnmodifiableList; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.List; 021import java.util.function.Function; 022 023import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; 024 025public class VisitMessage implements IVisitMessage { 026 027 private List<DG1> myDG1; 028 private List<GT1> myGT1; 029 private List<IVisitMessageInsurance> myInsurance; 030 private List<IVisitMessageProcedure> myProcedure; 031 private PV1 myPV1; 032 private PV2 myPV2; 033 private List<OBX> myOBX; 034 private List<ROL> myROL; 035 private IAddCallback<ROL> myAddRolFunction; 036 private IAddCallback<DG1> myAddDg1Function; 037 private IAddCallback<IVisitMessageInsurance> myAddInsuranceFunction; 038 private IAddCallback<IVisitMessageProcedure> myAddProcedureFunction; 039 private List<AL1> myAL1; 040 private IAddCallback<AL1> myAddAL1Function; 041 042 public VisitMessage setInsurances( 043 List<IVisitMessageInsurance> theInsurances, IAddCallback<IVisitMessageInsurance> theAddFunction) { 044 myInsurance = defaultIfNull(theInsurances, Collections.emptyList()); 045 myAddInsuranceFunction = theAddFunction; 046 return this; 047 } 048 049 public VisitMessage setProcedures( 050 List<IVisitMessageProcedure> theProcedures, IAddCallback<IVisitMessageProcedure> theAddProcedureFunction) { 051 myProcedure = defaultIfNull(theProcedures, Collections.emptyList()); 052 myAddProcedureFunction = theAddProcedureFunction; 053 return this; 054 } 055 056 @Override 057 public ROL addROL() { 058 if (myAddRolFunction == null) { 059 return null; 060 } 061 return call(myAddRolFunction); 062 } 063 064 @Override 065 public List<DG1> getDG1() { 066 if (myDG1 == null) { 067 myDG1 = new ArrayList<>(); 068 } 069 return new UnmodifiableList<>(myDG1); 070 } 071 072 public VisitMessage setDG1(List<DG1> theDG1, IAddCallback<DG1> theAddDg1Function) { 073 myDG1 = theDG1; 074 myAddDg1Function = theAddDg1Function; 075 return this; 076 } 077 078 @Override 079 public List<GT1> getGT1() { 080 return myGT1; 081 } 082 083 public VisitMessage setGT1(List<GT1> theGT1) { 084 myGT1 = theGT1; 085 return this; 086 } 087 088 @Override 089 public List<IVisitMessageInsurance> getInsurance() { 090 return myInsurance; 091 } 092 093 @Override 094 public List<OBX> getOBX() { 095 return myOBX; 096 } 097 098 public VisitMessage setOBX(List<OBX> theOBX) { 099 myOBX = theOBX; 100 return this; 101 } 102 103 @Override 104 public List<IVisitMessageProcedure> getProcedure() { 105 return myProcedure; 106 } 107 108 @Override 109 public PV1 getPV1() { 110 return myPV1; 111 } 112 113 public VisitMessage setPV1(PV1 thePV1) { 114 myPV1 = thePV1; 115 return this; 116 } 117 118 @Override 119 public PV2 getPV2() { 120 return myPV2; 121 } 122 123 public VisitMessage setPV2(PV2 thePV2) { 124 myPV2 = thePV2; 125 return this; 126 } 127 128 @Override 129 public List<ROL> getROL() { 130 return myROL == null ? Collections.emptyList() : myROL; 131 } 132 133 public VisitMessage setROL(List<ROL> theROL, IAddCallback<ROL> theAddRolFunction) { 134 myROL = theROL; 135 myAddRolFunction = theAddRolFunction; 136 return this; 137 } 138 139 @Override 140 public DG1 addDG1() { 141 IAddCallback<DG1> function = myAddDg1Function; 142 return call(function); 143 } 144 145 @Override 146 public IVisitMessageProcedure addProcedure() { 147 return call(myAddProcedureFunction); 148 } 149 150 @Override 151 public IVisitMessageInsurance addInsurance() { 152 return call(myAddInsuranceFunction); 153 } 154 155 @Override 156 public boolean canAddDg1() { 157 return myAddDg1Function != null; 158 } 159 160 @Override 161 public boolean canAddProcedure() { 162 return myAddProcedureFunction != null; 163 } 164 165 @Override 166 public boolean canAddInsurance() { 167 return myAddInsuranceFunction != null; 168 } 169 170 @Override 171 public List<AL1> getAL1() { 172 return myAL1; 173 } 174 175 @Override 176 public boolean canAddAL1() { 177 return myAddAL1Function != null; 178 } 179 180 @Override 181 public AL1 addAL1() { 182 return call(myAddAL1Function); 183 } 184 185 public VisitMessage setAL1(List<AL1> theAl1, IAddCallback<AL1> theAddAL1Function) { 186 myAL1 = theAl1; 187 myAddAL1Function = theAddAL1Function; 188 return this; 189 } 190 191 static <P, T> T call(P theInput, Function<P, T> theFunction) { 192 try { 193 return theFunction.apply(theInput); 194 } catch (Exception e) { 195 throw new InternalErrorException(e); 196 } 197 } 198 199 static <T> T call(IAddCallback<T> function) { 200 try { 201 return function.add(); 202 } catch (HL7Exception e) { 203 throw new InternalErrorException(e); 204 } 205 } 206}