RecordXMLToVO.java
001 package com.bea.medrec.xml;
002 
003 import com.bea.medrec.utils.MedRecLog4jFactory;
004 import com.bea.medrec.value.*;
005 import com.bea.medrec.xml.types.*;
006 import java.io.InputStream;
007 import java.util.ArrayList;
008 import java.util.Collection;
009 import java.util.List;
010 import org.apache.log4j.Logger;
011 
012 /**
013  <p>This class reads in an XML set of medical records with XMLBeans
014  * and then simply converts them to the value objects necessary to
015  * save the data to the database.  In the future it would be nice
016  * if the XMLBean itself could be the value object.</p>
017  *
018  @author Copyright (c) 2006 by BEA Systems, Inc. All Rights Reserved.
019  */
020 public class RecordXMLToVO {
021 
022   private static Logger logger =
023       MedRecLog4jFactory.getLogger(RecordXMLToVO.class.getName());
024 
025   private MedicalRecordsDocument medrecDoc;
026 
027   /**
028    <p>Constructor is private
029    */
030   private RecordXMLToVO() {
031   }
032 
033   /**
034    <p>Parses and validates the document that will be converted.</p>
035    *
036    @param stream InputStream
037    @throws Exception
038    */
039   public void parse(InputStream streamthrows Exception {
040     if (stream == null) {
041       throw new Exception("InputStream for XMLBean is null.");
042     else {
043       medrecDoc = MedicalRecordsDocument.Factory.parse(stream);
044     }
045 
046     if (!medrecDoc.validate()) {
047       logger.info("Document not valid");
048     else {
049       logger.info("Document is valid");
050     }
051   }
052 
053   /**
054    <p>Primary method of this class used to convert one type of bean
055    * to another.</p>
056    *
057    @return Collection
058    @throws Exception
059    */
060   public Collection<MedicalRecord> getMedicalRecords() throws Exception {
061     MedicalRecordType medrec = medrecDoc.getMedicalRecords();
062     Collection<MedicalRecord> medRecVOCol = new ArrayList<MedicalRecord>();
063 
064     String srcId = medrec.getSrcId();
065     String srcName = medrec.getSrcName();
066 
067     MedicalVisitType[] visits = medrec.getMedicalVisitArray();
068 
069     for (int i = 0; i < visits.length; i++) {
070       MedicalRecord medicalRecordVO = convertPatientVisit(visits[i],
071           srcId, srcName);
072       medRecVOCol.add(medicalRecordVO);
073     }
074 
075     logger.debug(medRecVOCol.toString());
076     return medRecVOCol;
077   }
078 
079   /**
080    <p>Converts a MedicalVisit to a MedicalRecord with all the trimmings.</p>
081    *
082    @param visit   MedicalVisitType
083    @param srcId   String
084    @param srcName String
085    @return MedicalRecord
086    @throws Exception
087    */
088   private MedicalRecord convertPatientVisit(MedicalVisitType visit,
089                                             String srcId, String srcName)
090       throws Exception {
091     logger.debug("convertPatientVisit called...");
092     MedicalRecord medicalRecordVO = new MedicalRecord(Integer.valueOf(srcId),
093         srcName);
094     Patient patientVO = null;
095     User userVO = null;
096 
097     patientVO = convertPatient(visit.getPatient());
098     userVO = new User(patientVO.getEmail());
099     medicalRecordVO.setPatient(patientVO);
100     medicalRecordVO.setUser(userVO);
101 
102     RecordType[] records = visit.getRecordArray();
103 
104     for (int i = 0; i < records.length; i++) {
105       Record recordVO = convertRecord(records[i]);
106       medicalRecordVO.addRecord(recordVO);
107     }
108 
109     return medicalRecordVO;
110   }
111 
112   /**
113    <p>Converts the Patient bean.</p>
114    *
115    @param patient PatientInfoType
116    @return Patient
117    @throws Exception
118    */
119   private Patient convertPatient(PatientInfoType patient)
120       throws Exception {
121 
122     logger.debug("convertPatient called...");
123     Patient patientVO = new Patient();
124 
125     patientVO.setSsn(patient.getSsn());
126     patientVO.setFirstName(patient.getPatientName().getFirstName());
127     patientVO.setMiddleName(patient.getPatientName()
128         .getMiddleName());
129     patientVO.setLastName(patient.getPatientName().getLastName());
130     patientVO.setDateOfBirth(patient.getDob());
131     patientVO.setPhone(patient.getPhone());
132     patientVO.setEmail(patient.getEmail());
133     logger.debug("Gender ="+patient.getGender()+".");
134     String gender = (patient.getGender().toString().equals("male""Male"
135         "Female");
136     patientVO.setGender(gender);
137 
138     Address addrVO = convertAddress(patient.getAddress());
139     patientVO.setAddress(addrVO);
140 
141     return patientVO;
142   }
143 
144   /**
145    <p>Converts the Record bean.</p>
146    *
147    @param record RecordType
148    @return Record
149    @throws Exception
150    */
151   private Record convertRecord(RecordType recordthrows Exception {
152     logger.debug("convertRecord called...");
153     Record recordVO = new Record();
154     List<Object> prescriptionList = new ArrayList<Object>();
155 
156     recordVO.setDate(record.getDate());
157     recordVO.setSymptoms(record.getSymptoms());
158     recordVO.setDiagnosis(record.getDiagnosis());
159     recordVO.setNotes(record.getNotes());
160 
161     Physician physician = convertPhysician(record.getPhysician());
162     recordVO.setPhysician(physician);
163 
164     VitalSigns vitals = convertVitals(record.getVitalSigns());
165     recordVO.setVitalSigns(vitals);
166 
167     PrescriptionType[] prescriptions = record.getPrescriptionArray();
168 
169     for (int i=0; i<prescriptions.length; i++) {
170       Prescription prescription = convertPrescription(prescriptions[i]);
171       prescriptionList.add(prescription);
172     }
173 
174     recordVO.setPrescriptions((Prescription[])prescriptionList.toArray(
175         new Prescription[0]));
176 
177     return recordVO;
178   }
179 
180   /**
181    <p>Converts the Physician bean.</p>
182    *
183    @param physician VitalSignsType
184    @return VitalSigns
185    @throws Exception
186    */
187   private Physician convertPhysician(PhysicianInfoType physicianthrows Exception {
188     logger.debug("convertPhysician called...");
189     Physician physicianVO = new Physician();
190     physicianVO.setFirstName(physician.getFirstName());
191     physicianVO.setMiddleName(physician.getMiddleName());
192     physicianVO.setLastName(physician.getLastName());
193     physicianVO.setPhone(physician.getPhone());
194     physicianVO.setEmail(physician.getEmail());
195     return physicianVO;
196   }
197 
198   /**
199    <p>Converts the VitalSigns bean.</p>
200    *
201    @param vitals VitalSignsType
202    @return VitalSigns
203    @throws Exception
204    */
205   private VitalSigns convertVitals(VitalSignsType vitals)
206       throws Exception {
207     logger.debug("convertVitals called...");
208     VitalSigns vitalsVO = new VitalSigns();
209 
210     vitalsVO.setWeight(new Integer(vitals.getWeight().getAmount()
211         .toString()));
212     vitalsVO.setHeight(new Integer(vitals.getHeight().getAmount()
213         .toString()));
214     vitalsVO.setTemperature(String.valueOf(vitals.getTemperature()
215         .getAmount()));
216     vitalsVO.setBloodPressure(vitals.getBloodPressure()
217         .getSystolic()+"/"
218        +vitals.getBloodPressure().getDiastolic());
219     vitalsVO.setPulse(vitals.getPulse().toString());
220 
221     return vitalsVO;
222   }
223 
224   /**
225    <p>Converts the Prescription bean.</p>
226    *
227    @param prescription PrescriptionType
228    @return Prescription
229    @throws Exception
230    */
231   private Prescription convertPrescription(PrescriptionType prescription)
232       throws Exception {
233     logger.debug("convertPrescription called...");
234     Prescription prescriptionVO = new Prescription();
235 
236     prescriptionVO.setDatePrescribed(prescription.getDate());
237     prescriptionVO.setDrug(prescription.getDrug());
238     prescriptionVO.setDosage(prescription.getDosage());
239     prescriptionVO.setFrequency(prescription.getFrequency());
240     prescriptionVO.setRefillsRemaining(
241         new Integer(prescription.getRefills().toString()));
242     prescriptionVO.setInstructions(prescription.getInstructions());
243 
244     return prescriptionVO;
245   }
246 
247   /**
248    <p>Converts the Address bean.</p>
249    *
250    @param address AddressType
251    @return Address
252    @throws Exception
253    */
254   private Address convertAddress(AddressType address)
255       throws Exception {
256     logger.debug("convertAddress called...");
257     Address addrVO = new Address();
258 
259     addrVO.setStreetName1(address.getStreetName1());
260     addrVO.setStreetName2(address.getStreetName2());
261     addrVO.setCity(address.getCity());
262     addrVO.setState(address.getState());
263     addrVO.setZipCode(address.getZip());
264     addrVO.setCountry(address.getCountry());
265 
266     return addrVO;
267   }
268 
269   /**
270    <p>Creates a new instance of RecordXMLToVO.</p>
271    *
272    @return RecordXMLToVO
273    */
274   public static RecordXMLToVO getInstance() {
275     return new RecordXMLToVO();
276   }
277 
278   /**
279    <p>For testing.</p>
280    *
281    @param args String[]
282    */
283   public static void main(String[] args) {
284     RecordXMLToVO b2b = new RecordXMLToVO();
285     try {
286       b2b.parse(null);
287       b2b.getMedicalRecords();
288     catch (Exception e) {
289       e.printStackTrace();
290     }
291   }
292 }