MedRecXMLProcessor.java
001 package com.bea.medrec.xml;
002 
003 import com.bea.medrec.controller.AdminSession;
004 import com.bea.medrec.exceptions.MedRecException;
005 import com.bea.medrec.utils.JNDINames;
006 import com.bea.medrec.utils.MedRecLog4jFactory;
007 import com.bea.medrec.utils.ServiceLocator;
008 import com.bea.medrec.utils.XMLFilter;
009 import com.bea.medrec.value.MedicalRecord;
010 import com.bea.medrec.value.XMLImportFile;
011 import java.io.BufferedInputStream;
012 import java.io.File;
013 import java.io.FileInputStream;
014 import java.io.FilenameFilter;
015 import java.rmi.RemoteException;
016 import java.text.SimpleDateFormat;
017 import java.util.*;
018 import javax.ejb.EJBException;
019 import javax.naming.NamingException;
020 import org.apache.log4j.Logger;
021 
022 public class MedRecXMLProcessor {
023 
024   private static Logger logger =
025       MedRecLog4jFactory.getLogger(MedRecXMLProcessor.class.getName());
026 
027   private String xmlDirLoc;
028 
029   private MedRecXMLProcessor() {
030     // Incoming XML medical record location
031     xmlDirLoc = System.getProperty("com.bea.medrec.xml.incoming");
032     logger.info("XML directory: " + xmlDirLoc);
033   }
034 
035   public static MedRecXMLProcessor getInstance() {
036     return new MedRecXMLProcessor();
037   }
038 
039   //  G E T   P E N D I N G ,   I N C O M I N G   X M L   F I L E S
040   /**
041    <p>Get pending, incoming xml files containing new patients and
042    * their medical record.</p>
043    *
044    @return Collection  Collection of XMLImportFile objects.
045    @throws javax.naming.NamingException
046    @throws java.rmi.RemoteException
047    @throws Exception
048    */
049   public Collection<Object> getIncomingXMLFiles()
050       throws RemoteException, NamingException, Exception {
051     // Declare local variables.
052     Collection<Object> col = new ArrayList<Object>();
053 
054     // if the log4j-init-file is not set, then no point in trying
055     if (xmlDirLoc != null) {
056       File xmlDir = new File(xmlDirLoc);
057       if (xmlDir.isDirectory()) {
058         FilenameFilter filter = new XMLFilter();
059         File[] files = xmlDir.listFiles(filter);
060         for (int i = 0; i < files.length; i++) {
061           Calendar cal = new GregorianCalendar();
062           cal.setTimeInMillis(files[i].lastModified());
063           XMLImportFile xmlFile = new XMLImportFile(files[i].getName(),
064               xmlDirLoc, cal, files[i].length());
065           logger.debug(xmlFile.toString());
066           col.add(xmlFile);
067         }
068       }
069     }
070     return col;
071   }
072 
073   //   S A V E   X M L    R E C O R D
074   /**
075    <p>Parses XML stream into MedRec value objects.
076    * Stores resultant objects.</p>
077    *
078    @param pBis
079    @throws MedRecException
080    */
081   public void saveXMLRecord(BufferedInputStream pBis)
082       throws MedRecException, Exception {
083     logger.info("Parsing xml and saving resultant.");
084 
085     // Declare local variables.
086     AdminSession adminSession = null;
087     //XMLInputStreamFactory factory = null;
088     //XMLInputStream stream = null;
089     RecordXMLToVO parser = null;
090     Collection<MedicalRecord> medicalRecordCol = null;
091 
092     try {
093       // Session bean homes.
094       adminSession = getAdminSession();
095       parser = RecordXMLToVO.getInstance();
096       logger.debug("Parsing xml doc.");
097       parser.parse(pBis);
098 
099       logger.debug("Getting MedicalRecords collection.");
100       medicalRecordCol = parser.getMedicalRecords();
101       if (logger.isDebugEnabled()) printMedicalRecords(medicalRecordCol);
102       adminSession.insertMedicalRecord(medicalRecordCol);
103     catch (NamingException ne) {
104       logger.error(ne.getLocalizedMessage(), ne);
105       throw new MedRecException(ne);
106     catch (MedRecException me) {
107       logger.error(me.getLocalizedMessage(), me);
108       throw me;
109     catch (EJBException ejbe) {
110       logger.error(ejbe.getLocalizedMessage(), ejbe);
111       throw new MedRecException(ejbe);
112     catch (Exception e) {
113       logger.error(e.getLocalizedMessage(), e);
114       throw e;
115     }
116   }
117 
118   /**
119    <p>Parses given XML file found in xml.incoming System property
120    * into MedRec value objects.  Stores resultant objects.</p>
121    *
122    @param pFilename
123    @throws MedRecException
124    */
125   public void saveXMLRecord(String pFilename)
126       throws MedRecException, Exception {
127     logger.info("Parsing xml file: " + pFilename);
128 
129     // Declare local variables.
130     File xmlFile = null;
131     File archivedFile = null;
132     FileInputStream fis = null;
133 
134     xmlFile = new File(xmlDirLoc + "/" + pFilename);
135     logger.debug("XML filepath: " + xmlFile.getAbsolutePath());
136 
137     if (!xmlFile.exists() || !xmlFile.isFile())
138       throw new MedRecException("File not found.");
139 
140     try {
141       fis = new FileInputStream(xmlFile);
142       saveXMLRecord(new BufferedInputStream(fis));
143       archivedFile = new File(xmlDirLoc + "/" + pFilename + "_" + getDateTimeStr());
144       xmlFile.renameTo(archivedFile);
145     catch (Exception e) {
146       throw e;
147     finally {
148       fis.close();
149     }
150   }
151 
152   //   P R I V A T E   M E T H O D S
153   /**
154    <p>Prints contents of medical record collection.</p>
155    */
156   private void printMedicalRecords(Collection<MedicalRecord> pMedRecCol) {
157     // Declare local variables.
158     Iterator itr = null;
159     MedicalRecord medicalRecord = null;
160 
161     itr = pMedRecCol.iterator();
162     while (itr.hasNext()) {
163       medicalRecord = (MedicalRecorditr.next();
164       logger.debug("******* Medical Record **********");
165       logger.debug(medicalRecord.toString());
166     }
167   }
168 
169   /**
170    <p>Returns date string - yyMMddHHmmss.</p>
171    */
172   private String getDateTimeStr() {
173     SimpleDateFormat dateFormat = null;
174     dateFormat = new SimpleDateFormat("yyMMddHHmmss");
175     return dateFormat.format(new Date());
176 
177   }
178 
179   /**
180    * Get AdminSession
181    */
182   private AdminSession getAdminSession()
183       throws NamingException {
184     ServiceLocator locator = ServiceLocator.getInstance();
185     Object obj = locator.getObj(JNDINames.ADMIN_SESSION_REMOTE_HOME,
186         com.bea.medrec.controller.AdminSessionHome.class);
187     return (AdminSessionobj;
188   }
189 }