[507] | 1 | package gov.hhs.fha.nhinc.repository.util;
|
---|
| 2 |
|
---|
| 3 | import gov.hhs.fha.nhinc.repository.model.EventCode;
|
---|
| 4 | import gov.hhs.fha.nhinc.repository.service.DocumentService;
|
---|
| 5 | import java.io.File;
|
---|
| 6 | import java.io.FileInputStream;
|
---|
| 7 | import java.io.FileOutputStream;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.io.InputStream;
|
---|
| 10 | import java.io.OutputStream;
|
---|
| 11 | import java.text.SimpleDateFormat;
|
---|
| 12 | import java.util.Date;
|
---|
| 13 | import java.util.HashSet;
|
---|
| 14 | import java.util.PropertyResourceBundle;
|
---|
| 15 | import java.util.Set;
|
---|
| 16 | import org.apache.commons.logging.Log;
|
---|
| 17 | import org.apache.commons.logging.LogFactory;
|
---|
| 18 | import org.w3c.dom.Document;
|
---|
| 19 | import org.w3c.dom.Element;
|
---|
| 20 | import org.w3c.dom.NodeList;
|
---|
| 21 | import org.w3c.dom.Node;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Utility to load documents from an XML file into the database.
|
---|
| 25 | *
|
---|
| 26 | * @author Neil Webb
|
---|
| 27 | */
|
---|
| 28 | public class DocumentLoadUtil
|
---|
| 29 | {
|
---|
| 30 | private static final String DATE_FORMAT_STRING = "yyyyMMddhhmmssZ";
|
---|
| 31 | private static Log log = LogFactory.getLog(DocumentLoadUtil.class);
|
---|
| 32 |
|
---|
| 33 | private static void loadData(String absoluteFilePath) throws Exception
|
---|
| 34 | {
|
---|
| 35 | // Load file into XML
|
---|
| 36 | Document doc = XmlUtil.getDocumentFromFile(absoluteFilePath);
|
---|
| 37 | if (doc == null)
|
---|
| 38 | {
|
---|
| 39 | throw new Exception("Could not load document from file for: " + absoluteFilePath);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | // Traverse the DOM and load documents into database
|
---|
| 43 | NodeList docNodes = doc.getElementsByTagName("Document");
|
---|
| 44 | if ((docNodes != null) && (docNodes.getLength() > 0))
|
---|
| 45 | {
|
---|
| 46 | for (int i = 0; i < docNodes.getLength(); i++)
|
---|
| 47 | {
|
---|
| 48 | Node node = docNodes.item(i);
|
---|
| 49 | if (node instanceof Element)
|
---|
| 50 | {
|
---|
| 51 | storeDocument((Element)node);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | private static void storeDocument(Element documentElement) throws Exception
|
---|
| 58 | {
|
---|
| 59 | if (documentElement == null)
|
---|
| 60 | {
|
---|
| 61 | throw new Exception("Document element was null.");
|
---|
| 62 | }
|
---|
| 63 | gov.hhs.fha.nhinc.repository.model.Document doc = new gov.hhs.fha.nhinc.repository.model.Document();
|
---|
| 64 | doc.setDocumentid(getChildElementLongValue(documentElement, "documentId"));
|
---|
| 65 | doc.setDocumentUniqueId(getChildElementStringValue(documentElement, "documentUniqueId"));
|
---|
| 66 | doc.setDocumentTitle(getChildElementStringValue(documentElement, "documentTitle"));
|
---|
| 67 | doc.setAuthorPerson(getChildElementStringValue(documentElement, "authorPerson"));
|
---|
| 68 | doc.setAuthorInstitution(getChildElementStringValue(documentElement, "authorInstitution"));
|
---|
| 69 | doc.setAuthorRole(getChildElementStringValue(documentElement, "authorRole"));
|
---|
| 70 | doc.setAuthorSpecialty(getChildElementStringValue(documentElement, "authorSpecialty"));
|
---|
| 71 | doc.setAvailablityStatus(getChildElementStringValue(documentElement, "availablityStatus"));
|
---|
| 72 | doc.setClassCode(getChildElementStringValue(documentElement, "classCode"));
|
---|
| 73 | doc.setClassCodeScheme(getChildElementStringValue(documentElement, "classCodeScheme"));
|
---|
| 74 | doc.setClassCodeDisplayName(getChildElementStringValue(documentElement, "classCodeDisplayName"));
|
---|
| 75 | doc.setConfidentialityCode(getChildElementStringValue(documentElement, "confidentialityCode"));
|
---|
| 76 | doc.setConfidentialityCodeScheme(getChildElementStringValue(documentElement, "confidentialityCodeScheme"));
|
---|
| 77 | doc.setConfidentialityCodeDisplayName(getChildElementStringValue(documentElement, "confidentialityCodeDisplayName"));
|
---|
| 78 | doc.setFormatCode(getChildElementStringValue(documentElement, "formatCode"));
|
---|
| 79 | doc.setFormatCodeScheme(getChildElementStringValue(documentElement, "formatCodeScheme"));
|
---|
| 80 | doc.setFormatCodeDisplayName(getChildElementStringValue(documentElement, "formatCodeDisplayName"));
|
---|
| 81 | doc.setPatientId(getChildElementStringValue(documentElement, "patientId"));
|
---|
| 82 | doc.setStatus(getChildElementStringValue(documentElement, "status"));
|
---|
| 83 | doc.setComments(getChildElementStringValue(documentElement, "comments"));
|
---|
| 84 | doc.setFacilityCode(getChildElementStringValue(documentElement, "facilityCode"));
|
---|
| 85 | doc.setFacilityCodeScheme(getChildElementStringValue(documentElement, "facilityCodeScheme"));
|
---|
| 86 | doc.setFacilityCodeDisplayName(getChildElementStringValue(documentElement, "facilityCodeDisplayName"));
|
---|
| 87 | doc.setIntendedRecipientPerson(getChildElementStringValue(documentElement, "intendedRecipientPerson"));
|
---|
| 88 | doc.setIntendedRecipientOrganization(getChildElementStringValue(documentElement, "intendedRecipientOrganization"));
|
---|
| 89 | doc.setLanguageCode(getChildElementStringValue(documentElement, "languageCode"));
|
---|
| 90 | doc.setLegalAuthenticator(getChildElementStringValue(documentElement, "legalAuthenticator"));
|
---|
| 91 | doc.setMimeType(getChildElementStringValue(documentElement, "mimeType"));
|
---|
| 92 | doc.setParentDocumentId(getChildElementStringValue(documentElement, "parentDocumentId"));
|
---|
| 93 | doc.setParentDocumentRelationship(getChildElementStringValue(documentElement, "parentDocumentRelationship"));
|
---|
| 94 | doc.setPracticeSetting(getChildElementStringValue(documentElement, "practiceSetting"));
|
---|
| 95 | doc.setPracticeSettingScheme(getChildElementStringValue(documentElement, "practiceSettingScheme"));
|
---|
| 96 | doc.setPracticeSettingDisplayName(getChildElementStringValue(documentElement, "practiceSettingDisplayName"));
|
---|
| 97 | doc.setSize(getChildElementIntegerValue(documentElement, "size"));
|
---|
| 98 | doc.setSourcePatientId(getChildElementStringValue(documentElement, "sourcePatientId"));
|
---|
| 99 | doc.setPid3(getChildElementStringValue(documentElement, "pid3"));
|
---|
| 100 | doc.setPid5(getChildElementStringValue(documentElement, "pid5"));
|
---|
| 101 | doc.setPid8(getChildElementStringValue(documentElement, "pid8"));
|
---|
| 102 | doc.setPid7(getChildElementStringValue(documentElement, "pid7"));
|
---|
| 103 | doc.setPid11(getChildElementStringValue(documentElement, "pid11"));
|
---|
| 104 | doc.setTypeCode(getChildElementStringValue(documentElement, "typeCode"));
|
---|
| 105 | doc.setTypeCodeScheme(getChildElementStringValue(documentElement, "typeCodeScheme"));
|
---|
| 106 | doc.setTypeCodeDisplayName(getChildElementStringValue(documentElement, "typeCodeDisplayName"));
|
---|
| 107 | doc.setDocumentUri(getChildElementStringValue(documentElement, "documentUri"));
|
---|
| 108 | doc.setRawData(getChildElementByteArray(documentElement, "rawData"));
|
---|
| 109 |
|
---|
| 110 | // Date fields
|
---|
| 111 | doc.setCreationTime(parseDate(getChildElementStringValue(documentElement, "creationTime"),DATE_FORMAT_STRING));
|
---|
| 112 | doc.setServiceStartTime(parseDate(getChildElementStringValue(documentElement, "serviceStartTime"),DATE_FORMAT_STRING));
|
---|
| 113 | doc.setServiceStopTime(parseDate(getChildElementStringValue(documentElement, "serviceStopTime"),DATE_FORMAT_STRING));
|
---|
| 114 | // doc.setServiceStartTime(parseDate(getChildElementStringValue(documentElement, "serviceStartTime"), "yyyyMMddhhmmss"));
|
---|
| 115 | // doc.setServiceStopTime(parseDate(getChildElementStringValue(documentElement, "serviceStopTime"), "yyyyMMddhhmmss"));
|
---|
| 116 |
|
---|
| 117 | loadEventCodes(documentElement, doc);
|
---|
| 118 |
|
---|
| 119 | DocumentService docService = new DocumentService();
|
---|
| 120 | docService.saveDocument(doc);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private static Date parseDate(String value, String dateFormat)
|
---|
| 124 | {
|
---|
| 125 | Date parsedDate = null;
|
---|
| 126 | if ((value != null) && (dateFormat != null))
|
---|
| 127 | {
|
---|
| 128 | try
|
---|
| 129 | {
|
---|
| 130 | String formatString = prepareDateFormatString(dateFormat, value);
|
---|
| 131 | SimpleDateFormat dateFormatter = new SimpleDateFormat(formatString);
|
---|
| 132 | parsedDate = dateFormatter.parse(value);
|
---|
| 133 | }
|
---|
| 134 | catch (Throwable t)
|
---|
| 135 | {
|
---|
| 136 | log.error("Error parsing date '" + value + "' using format: '" +
|
---|
| 137 | dateFormat + "': " + t.getMessage(), t);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | return parsedDate;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * Prepare a date format string based on the length of the date string
|
---|
| 145 | * to be parsed.
|
---|
| 146 | * @param dateFormat Date format string (ex. yyyyMMddhhmmssZ)
|
---|
| 147 | * @param dateString Date string to be parsed (ex. 19990205)
|
---|
| 148 | * @return Modified format string based on the date string length (ex. yyyyMMdd)
|
---|
| 149 | */
|
---|
| 150 | public static String prepareDateFormatString(String dateFormat, String dateString)
|
---|
| 151 | {
|
---|
| 152 | String formatString = dateFormat;
|
---|
| 153 | if ((dateString != null) && (dateFormat != null) && (dateString.length() > 0) && (dateString.length() < dateFormat.length()))
|
---|
| 154 | {
|
---|
| 155 | formatString = dateFormat.substring(0, dateString.length());
|
---|
| 156 | if(log.isDebugEnabled())
|
---|
| 157 | {
|
---|
| 158 | log.debug("New dateFormat: " + dateFormat);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | return formatString;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | private static void loadEventCodes(Element documentElement, gov.hhs.fha.nhinc.repository.model.Document doc)
|
---|
| 165 | {
|
---|
| 166 | if (documentElement != null)
|
---|
| 167 | {
|
---|
| 168 | NodeList nodes = documentElement.getElementsByTagName("eventCode");
|
---|
| 169 | if ((nodes != null) && (nodes.getLength() > 0))
|
---|
| 170 | {
|
---|
| 171 | Set<EventCode> eventCodes = new HashSet<EventCode>();
|
---|
| 172 | for (int i = 0; i < nodes.getLength(); i++)
|
---|
| 173 | {
|
---|
| 174 | // eventCode element - load data
|
---|
| 175 | Node node = nodes.item(i);
|
---|
| 176 | if (node instanceof Element)
|
---|
| 177 | {
|
---|
| 178 | Element eventCodeElement = (Element)node;
|
---|
| 179 | EventCode eventCode = new EventCode();
|
---|
| 180 | eventCode.setDocument(doc);
|
---|
| 181 | eventCode.setEventCodeId(getChildElementLongValue(eventCodeElement, "codeId"));
|
---|
| 182 | eventCode.setEventCode(getChildElementStringValue(eventCodeElement, "code"));
|
---|
| 183 | eventCode.setEventCodeScheme(getChildElementStringValue(eventCodeElement, "codeSchema"));
|
---|
| 184 | eventCode.setEventCodeDisplayName(getChildElementStringValue(eventCodeElement, "codeDisplayName"));
|
---|
| 185 | eventCodes.add(eventCode);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | doc.setEventCodes(eventCodes);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | private static String getChildElementStringValue(Element element, String childElementName)
|
---|
| 194 | {
|
---|
| 195 | String value = null;
|
---|
| 196 | if ((element != null) && (childElementName != null))
|
---|
| 197 | {
|
---|
| 198 | if(log.isDebugEnabled())
|
---|
| 199 | {
|
---|
| 200 | log.debug("Extracting child element '" + childElementName + "' from '" + element.getTagName() + "'");
|
---|
| 201 | }
|
---|
| 202 | NodeList nodes = element.getElementsByTagName(childElementName);
|
---|
| 203 | if ((nodes != null) && (nodes.getLength() > 0))
|
---|
| 204 | {
|
---|
| 205 | Node node = nodes.item(0);
|
---|
| 206 | if (node instanceof Element)
|
---|
| 207 | {
|
---|
| 208 | Element childElement = (Element)node;
|
---|
| 209 | value = childElement.getTextContent();
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | return value;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | private static Long getChildElementLongValue(Element element, String childElementName)
|
---|
| 217 | {
|
---|
| 218 | Long value = null;
|
---|
| 219 | if ((element != null) && (childElementName != null))
|
---|
| 220 | {
|
---|
| 221 | if(log.isDebugEnabled())
|
---|
| 222 | {
|
---|
| 223 | log.debug("Extracting child element '" + childElementName + "' from '" + element.getTagName() + "'");
|
---|
| 224 | }
|
---|
| 225 | NodeList nodes = element.getElementsByTagName(childElementName);
|
---|
| 226 | if ((nodes != null) && (nodes.getLength() > 0))
|
---|
| 227 | {
|
---|
| 228 | Node node = nodes.item(0);
|
---|
| 229 | if (node instanceof Element)
|
---|
| 230 | {
|
---|
| 231 | Element childElement = (Element)node;
|
---|
| 232 | String strVal = childElement.getTextContent();
|
---|
| 233 | if (strVal != null)
|
---|
| 234 | {
|
---|
| 235 | try
|
---|
| 236 | {
|
---|
| 237 | value = Long.parseLong(strVal);
|
---|
| 238 | }
|
---|
| 239 | catch (Throwable t)
|
---|
| 240 | {
|
---|
| 241 | log.error("Failed to parse long from '" + strVal + "'", t);
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | return value;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | private static Integer getChildElementIntegerValue(Element element, String childElementName)
|
---|
| 251 | {
|
---|
| 252 | Integer value = null;
|
---|
| 253 | if ((element != null) && (childElementName != null))
|
---|
| 254 | {
|
---|
| 255 | if(log.isDebugEnabled())
|
---|
| 256 | {
|
---|
| 257 | log.debug("Extracting child element '" + childElementName + "' from '" + element.getTagName() + "'");
|
---|
| 258 | }
|
---|
| 259 | NodeList nodes = element.getElementsByTagName(childElementName);
|
---|
| 260 | if ((nodes != null) && (nodes.getLength() > 0))
|
---|
| 261 | {
|
---|
| 262 | Node node = nodes.item(0);
|
---|
| 263 | if (node instanceof Element)
|
---|
| 264 | {
|
---|
| 265 | Element childElement = (Element)node;
|
---|
| 266 | String strVal = childElement.getTextContent();
|
---|
| 267 | if (strVal != null)
|
---|
| 268 | {
|
---|
| 269 | try
|
---|
| 270 | {
|
---|
| 271 | value = new Integer(strVal);
|
---|
| 272 | }
|
---|
| 273 | catch (Throwable t)
|
---|
| 274 | {
|
---|
| 275 | log.error("Failed to parse integer from '" + strVal + "'",t);
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 | return value;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | private static byte[] getChildElementByteArray(Element element, String childElementName)
|
---|
| 285 | {
|
---|
| 286 | byte[] value = null;
|
---|
| 287 | if ((element != null) && (childElementName != null))
|
---|
| 288 | {
|
---|
| 289 | if(log.isDebugEnabled())
|
---|
| 290 | {
|
---|
| 291 | log.debug("Extracting child element '" + childElementName + "' from '" + element.getTagName() + "'");
|
---|
| 292 | }
|
---|
| 293 | NodeList nodes = element.getElementsByTagName(childElementName);
|
---|
| 294 | if ((nodes != null) && (nodes.getLength() > 0))
|
---|
| 295 | {
|
---|
| 296 | Node node = nodes.item(0);
|
---|
| 297 | if (node instanceof Element)
|
---|
| 298 | {
|
---|
| 299 | Element childElement = (Element)node;
|
---|
| 300 | String encStrVal = childElement.getTextContent();
|
---|
| 301 | if (encStrVal != null)
|
---|
| 302 | {
|
---|
| 303 | value = encStrVal.getBytes();
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | return value;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | /**
|
---|
| 312 | * This method copies files from input folder to output folder...
|
---|
| 313 | * @param source
|
---|
| 314 | * @param dest
|
---|
| 315 | * @throws java.io.IOException
|
---|
| 316 | */
|
---|
| 317 | private static void copyFile(File source, File dest) throws IOException {
|
---|
| 318 | if (!dest.exists()) {
|
---|
| 319 | dest.createNewFile();
|
---|
| 320 | }
|
---|
| 321 | InputStream in = null;
|
---|
| 322 | OutputStream out = null;
|
---|
| 323 | try {
|
---|
| 324 | in = new FileInputStream(source);
|
---|
| 325 | out = new FileOutputStream(dest);
|
---|
| 326 | byte[] buf = new byte[1024];
|
---|
| 327 | int len;
|
---|
| 328 | while ((len = in.read(buf)) > 0) {
|
---|
| 329 | out.write(buf, 0, len);
|
---|
| 330 | }
|
---|
| 331 | } finally {
|
---|
| 332 | in.close();
|
---|
| 333 | out.close();
|
---|
| 334 | source.deleteOnExit();
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | /**
|
---|
| 339 | * Method reads bunch of files kept under a folder and move them to different folder after processing..
|
---|
| 340 | */
|
---|
| 341 | public static void readFiles() {
|
---|
| 342 | String propFileName = "filespath";
|
---|
| 343 | PropertyResourceBundle prop = (PropertyResourceBundle) PropertyResourceBundle.getBundle(propFileName);
|
---|
| 344 | String inFilePath = prop.getString("inFilePath");
|
---|
| 345 | String outFilePath = prop.getString("outFilePath");
|
---|
| 346 | log.info("In Put File Path -> " + inFilePath);
|
---|
| 347 | log.info("Out Put File Path -> " + outFilePath);
|
---|
| 348 | File inputfolder = new File(inFilePath);
|
---|
| 349 | File files[] = inputfolder.listFiles();
|
---|
| 350 | if (files.length != 0) {
|
---|
| 351 | String inFileName = "";
|
---|
| 352 | File destFile = null;
|
---|
| 353 | String absolutePath = "";
|
---|
| 354 | for (File inFile : files) {
|
---|
| 355 | if (inFile.isFile()) {
|
---|
| 356 | try {
|
---|
| 357 | absolutePath = inFile.getAbsolutePath();
|
---|
| 358 | try {
|
---|
| 359 | log.info("absolutePath" + absolutePath);
|
---|
| 360 | loadData(absolutePath);
|
---|
| 361 | } catch (Throwable t) {
|
---|
| 362 | log.debug("Failed to load documents: " + t.getMessage());
|
---|
| 363 | t.printStackTrace();
|
---|
| 364 | }
|
---|
| 365 | Thread.sleep(500);
|
---|
| 366 | } catch (InterruptedException ex) {
|
---|
| 367 | log.error(ex.getMessage());
|
---|
| 368 | }
|
---|
| 369 | inFileName = inFile.getName();
|
---|
| 370 | destFile = new File(outFilePath, inFileName);
|
---|
| 371 | try {
|
---|
| 372 | copyFile(inFile, destFile);
|
---|
| 373 | //inFile.delete();
|
---|
| 374 | } catch (IOException ex) {
|
---|
| 375 | log.error(ex.getMessage());
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 | } else {
|
---|
| 380 | log.info("Input files Not found under the specific directory, Please verify filespath.properties and run again");
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /**
|
---|
| 385 | * Entry point for the document load utility. The absolute path to the
|
---|
| 386 | * source file is expected as the only parameter.
|
---|
| 387 | *
|
---|
| 388 | * @param args Absolute path to source file as only parameter.
|
---|
| 389 | */
|
---|
| 390 | public static void main(String[] args)
|
---|
| 391 | {
|
---|
| 392 | log.debug("Begin DocumentLoad");
|
---|
| 393 | try {
|
---|
| 394 | if (args.length == 0) {
|
---|
| 395 | readFiles();
|
---|
| 396 | }
|
---|
| 397 | if (args.length == 1) {
|
---|
| 398 | loadData(args[0]);
|
---|
| 399 | }
|
---|
| 400 | if (args.length > 1) {
|
---|
| 401 | throw new Exception("Only one argument of the absolute path to a " +
|
---|
| 402 | "source file is expected or bunch files expected using a propery file, review ReadMe.tx before uploading");
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | } catch (Throwable t) {
|
---|
| 406 | log.debug("Failed to load documents: " + t.getMessage());
|
---|
| 407 | t.printStackTrace();
|
---|
| 408 | }
|
---|
| 409 | log.debug("End DocumentLoad");
|
---|
| 410 | System.exit(0);
|
---|
| 411 | }
|
---|
| 412 | }
|
---|