[507] | 1 | /*
|
---|
| 2 | * To change this template, choose Tools | Templates
|
---|
| 3 | * and open the template in the editor.
|
---|
| 4 | */
|
---|
| 5 | package gov.hhs.fha.nhinc.mpilib;
|
---|
| 6 |
|
---|
| 7 | import java.beans.XMLDecoder;
|
---|
| 8 | import java.beans.XMLEncoder;
|
---|
| 9 | import java.io.File;
|
---|
| 10 | import java.io.FileInputStream;
|
---|
| 11 | import java.io.FileNotFoundException;
|
---|
| 12 | import java.io.FileOutputStream;
|
---|
| 13 | import java.io.IOException;
|
---|
| 14 | import java.util.logging.Level;
|
---|
| 15 | import java.util.logging.Logger;
|
---|
| 16 | import org.apache.commons.logging.Log;
|
---|
| 17 | import org.apache.commons.logging.LogFactory;
|
---|
| 18 |
|
---|
| 19 | public class MpiDataSaver {
|
---|
| 20 | private static Log log = LogFactory.getLog(MpiDataSaver.class);
|
---|
| 21 | private static String filename = "mpi.xml";
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | public static void SaveMpi(Patients patientList, String file)
|
---|
| 25 | {
|
---|
| 26 | log.info("Saving " + patientList.size() + " patient(s)");
|
---|
| 27 |
|
---|
| 28 | // Create output stream.
|
---|
| 29 | log.info("Filename=" + file);
|
---|
| 30 |
|
---|
| 31 | FileOutputStream fos;
|
---|
| 32 |
|
---|
| 33 | try {
|
---|
| 34 | fos = new FileOutputStream(file);
|
---|
| 35 | } catch (FileNotFoundException ex) {
|
---|
| 36 | Logger.getLogger(MpiDataSaver.class.getName()).log(Level.SEVERE, null, ex);
|
---|
| 37 | throw new UnableToInitializeMpi("Error accessing mpi storage", ex);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | try {
|
---|
| 41 | // Create XML encoder.
|
---|
| 42 | XMLEncoder xenc = new XMLEncoder(fos);
|
---|
| 43 | try {
|
---|
| 44 | // Write object.
|
---|
| 45 | xenc.writeObject(patientList);
|
---|
| 46 | xenc.flush();
|
---|
| 47 | } finally {
|
---|
| 48 | xenc.close();
|
---|
| 49 | }
|
---|
| 50 | } finally {
|
---|
| 51 | try {
|
---|
| 52 | fos.close();
|
---|
| 53 | } catch (IOException ex) {
|
---|
| 54 | Logger.getLogger(MpiDataSaver.class.getName()).log(Level.SEVERE, null, ex);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | log.info("Save complete");
|
---|
| 58 | }
|
---|
| 59 | public static void SaveMpi(Patients patientList) {
|
---|
| 60 | if ((patientList == null)) {
|
---|
| 61 | log.info("Patiet List is null");
|
---|
| 62 | patientList = new Patients();
|
---|
| 63 | }
|
---|
| 64 | SaveMpi(patientList, filename);
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public static Patients LoadMpi(String file)
|
---|
| 70 | {
|
---|
| 71 | log.info("Loading patients");
|
---|
| 72 |
|
---|
| 73 | Patients patientList;
|
---|
| 74 | File f;
|
---|
| 75 |
|
---|
| 76 | // Create input stream.
|
---|
| 77 | log.info("Filename=" + file);
|
---|
| 78 | log.info("user.dir: " + System.getProperty("user.dir"));
|
---|
| 79 |
|
---|
| 80 | f = new File(file);
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | if (!f.exists()) {
|
---|
| 84 | //file does not exist, so create it
|
---|
| 85 | //i would like to replace this with ability to create a default mpi
|
---|
| 86 | //for testing purposes
|
---|
| 87 | try
|
---|
| 88 | {
|
---|
| 89 | f.createNewFile();
|
---|
| 90 | SaveMpi(new Patients(), file);
|
---|
| 91 | }
|
---|
| 92 | catch (Exception ex)
|
---|
| 93 | {
|
---|
| 94 | throw new UnableToInitializeMpi("Error accessing mpi storage", ex);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | FileInputStream fis;
|
---|
| 100 | try {
|
---|
| 101 | fis = new FileInputStream(file);
|
---|
| 102 | } catch (FileNotFoundException ex) {
|
---|
| 103 | Logger.getLogger(MpiDataSaver.class.getName()).log(Level.SEVERE, null, ex);
|
---|
| 104 | throw new UnableToInitializeMpi("Error accessing mpi storage", ex);
|
---|
| 105 | }
|
---|
| 106 | try {
|
---|
| 107 | // Create XML decode.
|
---|
| 108 | XMLDecoder xdec = new XMLDecoder(fis);
|
---|
| 109 | try {
|
---|
| 110 | // Write object.
|
---|
| 111 | log.info("Loading object");
|
---|
| 112 | Object o = xdec.readObject();
|
---|
| 113 | log.info("casting object to 'patients'");
|
---|
| 114 | patientList = (Patients) o;
|
---|
| 115 | } finally {
|
---|
| 116 | xdec.close();
|
---|
| 117 | }
|
---|
| 118 | } finally {
|
---|
| 119 | try {
|
---|
| 120 | fis.close();
|
---|
| 121 | } catch (IOException ex) {
|
---|
| 122 | Logger.getLogger(MpiDataSaver.class.getName()).log(Level.SEVERE, null, ex);
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | log.info("Loaded " + patientList.size() + " patient(s)");
|
---|
| 127 | return patientList;
|
---|
| 128 | }
|
---|
| 129 | public static Patients LoadMpi() {
|
---|
| 130 | return LoadMpi(filename);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|