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