Added command line generate xsl

Added new application
modified generation logic
diff --git a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/HeadlessCDA2XLS.java b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/HeadlessCDA2XLS.java
new file mode 100644
index 0000000..d648494
--- /dev/null
+++ b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/HeadlessCDA2XLS.java
@@ -0,0 +1,33 @@
+package org.eclipse.mdht.cda.xml;
+
+import java.util.Map;
+
+import org.eclipse.equinox.app.IApplication;
+import org.eclipse.equinox.app.IApplicationContext;
+import org.eclipse.mdht.cda.xml.ui.handlers.GenerateCDADataHandler;
+
+public class HeadlessCDA2XLS implements IApplication {
+
+	@Override
+	public Object start(IApplicationContext context) throws Exception {
+
+		final Map<?, ?> args = context.getArguments();
+		final String[] appArgs = (String[]) args.get("application.args");
+		String zipFile = "";
+		for (final String arg : appArgs) {
+			System.out.println(arg);
+			if (arg.startsWith("-Z")) {
+				zipFile = arg.replace("-Z", "");
+			}
+		}
+		GenerateCDADataHandler generateCDADataHandler = new GenerateCDADataHandler();
+		generateCDADataHandler.processCommandLine(zipFile);
+		return IApplication.EXIT_OK;
+	}
+
+	@Override
+	public void stop() {
+
+	}
+
+}
diff --git a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/GenerateCDADataHandler.java b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/GenerateCDADataHandler.java
index 7b04b4e..329b2a4 100644
--- a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/GenerateCDADataHandler.java
+++ b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/GenerateCDADataHandler.java
@@ -148,7 +148,6 @@
 import org.eclipse.ui.console.ConsolePlugin;
 import org.eclipse.ui.console.IConsoleManager;
 import org.eclipse.ui.console.MessageConsole;
-import org.eclipse.ui.console.MessageConsoleStream;
 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.openhealthtools.mdht.uml.cda.consol.ConsolPackage;
@@ -416,7 +415,10 @@
 				if (object instanceof IFolder) {
 					IFolder folder = (IFolder) object;
 					codeMetricsFile = folder.getFile("codemetrics.cfg");
-					monitor.beginTask("Generate Spreadsheet", folder.members().length);
+					monitor.beginTask(
+						"Generate Spreadsheet", (folder != null
+								? folder.members().length
+								: 1));
 
 					int worksheetCache = 1000;
 					if (System.getProperty("org.eclipse.mdht.cda.xls.worksheetcache") != null) {
@@ -424,7 +426,8 @@
 					}
 
 					final SXSSFWorkbook rollupStats = new SXSSFWorkbook(worksheetCache);
-					processSelection(folder, null, monitor, splitOption, theSections, theSectionCache, rollupStats);
+					processSelection(
+						folder, null, null, monitor, splitOption, theSections, theSectionCache, rollupStats);
 
 					final StringBuffer fileLocation = new StringBuffer();
 					fileLocation.append(
@@ -444,7 +447,8 @@
 					IFile zipFile = (IFile) object;
 
 					final SXSSFWorkbook rollupStats = new SXSSFWorkbook(10);
-					processSelection(null, zipFile, monitor, splitOption, theSections, theSectionCache, rollupStats);
+					processSelection(
+						null, zipFile, null, monitor, splitOption, theSections, theSectionCache, rollupStats);
 
 					final StringBuffer fileLocation = new StringBuffer();
 					fileLocation.append(
@@ -650,7 +654,10 @@
 									if (o instanceof IFolder) {
 										IFolder folder = (IFolder) o;
 										codeMetricsFile = folder.getFile("codemetrics.cfg");
-										monitor.beginTask("Generate Spreadsheet", folder.members().length);
+										monitor.beginTask(
+											"Generate Spreadsheet", (folder != null
+													? folder.members().length
+													: 1));
 
 										if (fileLocation.length() == 0) {
 											fileLocation.append(
@@ -661,7 +668,7 @@
 										}
 
 										processSelection(
-											folder, null, monitor, splitOption, theSections, theSectionCache,
+											folder, null, null, monitor, splitOption, theSections, theSectionCache,
 											rollupStats);
 									}
 
@@ -677,7 +684,7 @@
 										}
 
 										processSelection(
-											null, zipFile, monitor, splitOption, theSections, theSectionCache,
+											null, zipFile, null, monitor, splitOption, theSections, theSectionCache,
 											rollupStats);
 
 									}
@@ -1373,13 +1380,24 @@
 
 	}
 
-	void processSelection(IFolder selectedFolder, IFile selectedFile, IProgressMonitor monitor, String splitOption,
-			HashSet<EClass> sectionFilter, HashMap<EClass, HashSet<EClass>> theSectionCache, SXSSFWorkbook rollupStats)
-			throws Exception {
+	public void processCommandLine(String selectedFile2) throws Exception {
 
-		MessageConsole mdhtConsole = findConsole(MDHTCONSOLE);
-		mdhtConsole.setWaterMarks(1000, 18000);
-		MessageConsoleStream console = mdhtConsole.newMessageStream();
+		HashSet<EClass> sectionFilter = new HashSet<EClass>();
+		final HashMap<EClass, HashSet<EClass>> theSectionCache = new HashMap<EClass, HashSet<EClass>>();
+		final SXSSFWorkbook rollupStats = new SXSSFWorkbook(100);
+		processSelection(
+			null, null, selectedFile2, new NullProgressMonitor(), CONSOLIDATED, sectionFilter, theSectionCache,
+			rollupStats);
+
+	}
+
+	void processSelection(IFolder selectedFolder, IFile selectedFile, String selectedFile2, IProgressMonitor monitor,
+			String splitOption, HashSet<EClass> sectionFilter, HashMap<EClass, HashSet<EClass>> theSectionCache,
+			SXSSFWorkbook rollupStats) throws Exception {
+
+		// MessageConsole mdhtConsole = findConsole(MDHTCONSOLE);
+		// mdhtConsole.setWaterMarks(1000, 18000);
+		// MessageConsoleStream console = mdhtConsole.newMessageStream();
 
 		/*
 		 * Set Ratio low as to prevent Zip Bomb Detection
@@ -1453,8 +1471,13 @@
 
 		IFolder folder = null;
 		Iterator<IFile> documentsIterator = null;
+
+		String folderTarget = null;
+		String folderName = null;
 		if (selectedFolder != null) {
 			folder = selectedFolder;
+			folderTarget = folder.getParent().getLocation().toOSString() + System.getProperty("file.separator");
+			folderName = folder.getName().toUpperCase();
 			for (IResource resource : folder.members()) {
 				if (resource instanceof IFile) {
 					documents.add((IFile) resource);
@@ -1466,17 +1489,27 @@
 
 		if (selectedFile != null) {
 			folder = (IFolder) selectedFile.getParent();
+			folderTarget = folder.getParent().getLocation().toOSString() + System.getProperty("file.separator");
+			folderName = folder.getName().toUpperCase();
 			documentsIterator = new ZIPIterator(
 				selectedFile.getName(), new ZipInputStream(selectedFile.getContents()), fileNameFilters);
+
+		}
+
+		if (!StringUtils.isEmpty(selectedFile2)) {
+			java.nio.file.Path zipfile = Paths.get(selectedFile2);
+			folderTarget = zipfile.getParent().toAbsolutePath().toString() + System.getProperty("file.separator");
+			folderName = zipfile.getFileName().toString().toUpperCase().replace(".ZIP", "");
+			documentsIterator = new ZIPIterator(
+				zipfile.getFileName().toString(), new ZipInputStream(Files.newInputStream(zipfile)), fileNameFilters);
 		}
 
 		fileCount = documents.size() + 50;
 
-		String fileLocation2 = folder.getParent().getLocation().toOSString() + System.getProperty("file.separator") +
-				CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + folder.getName().toUpperCase() + "performance.log";
+		String fileLocation2 = folderTarget + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + folderName +
+				"performance.log";
 
-		String fileLocation3 = folder.getParent().getLocation().toOSString() + System.getProperty("file.separator") +
-				CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + folder.getName().toUpperCase() +
+		String fileLocation3 = folderTarget + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + folderName +
 				"validatation.csv";
 
 		BufferedWriter performance = Files.newBufferedWriter(Paths.get(fileLocation2));
@@ -1539,6 +1572,11 @@
 			skipSwitch = true;
 		}
 
+		boolean ignoreFilter = false;
+		if (System.getProperty("org.eclipse.mdht.cda.xls.ignorefilter") != null) {
+			ignoreFilter = true;
+		}
+
 		int statfilectr = 0;
 		int consoldocumentsprocessed = 0;
 		int outpatientprogressnoteprocessed = 0;
@@ -1549,7 +1587,9 @@
 
 		int nonxmldocuments = 0;
 		int documenterrors = 0;
-		int totalFiles = folder.members().length;
+		int totalFiles = (folder != null
+				? folder.members().length
+				: 1);
 		while (documentsIterator.hasNext()) {
 			statfilectr++;
 			IFile file = documentsIterator.next();
@@ -1569,8 +1609,9 @@
 			if ("XML".equalsIgnoreCase(file.getFileExtension())) {
 				files.add(file);
 				monitor.worked(1);
-				double estimatedTimeLeft = ((folder.members().length - filectr) * (currentProcessingTime / filectr)) /
-						1000.0;
+				double estimatedTimeLeft = (((folder != null
+						? folder.members().length
+						: 1) - filectr) * (currentProcessingTime / filectr)) / 1000.0;
 
 				if (estimatedTimeLeft > 60) {
 					monitor.setTaskName(
@@ -1586,8 +1627,8 @@
 
 				try {
 
-					console.println(getMemoryUssage());
-					console.println("file : " + file.getName() + "  size : " + fileSize);
+					// console.println(getMemoryUssage());
+					// console.println("file : " + file.getName() + " size : " + fileSize);
 
 					ClinicalDocument clinicalDocument = null;
 
@@ -1632,7 +1673,7 @@
 
 						}
 
-						console.println("End Load " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
+						// console.println("End Load " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
 						is.close();
 
 						if (!loaded) {
@@ -1714,7 +1755,7 @@
 					 */
 
 					if (clinicalDocument instanceof GeneralHeaderConstraints) {
-						console.println("Start Processing ");
+						// console.println("Start Processing ");
 
 						consoldocumentsprocessed++;
 
@@ -1758,7 +1799,7 @@
 						for (Section section : clinicalDocument.getSections()) {
 
 							// This is when a section template is not found
-							if (!sectionFilter.isEmpty()) {
+							if (!ignoreFilter && !sectionFilter.isEmpty()) {
 
 								if (CDAPackage.eINSTANCE.getSection().isInstance(section)) {
 
@@ -1801,22 +1842,22 @@
 								}
 							}
 
-							console.println("Start Section " + section.eClass().getName());
+							// console.println("Start Section " + section.eClass().getName());
 
-							if (!skipSwitch) {
+							if (!skipSwitch && section != null) {
 								if (!pss.doSwitch(section)) {
 
-									console.println("Error processing section Unknown Template ID ");
+									// console.println("Error processing section Unknown Template ID ");
 
 									for (II templateId : section.getTemplateIds()) {
-										console.println(
-											"Error processing section Unknown Template ID root :  " +
-													templateId.getRoot() + " extension : " + templateId.getExtension());
+										// console.println(
+										// "Error processing section Unknown Template ID root : " +
+										// templateId.getRoot() + " extension : " + templateId.getExtension());
 									}
 
 								}
 							}
-							console.println("End Section " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
+							// console.println("End Section " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
 
 						}
 
@@ -1830,7 +1871,7 @@
 						performance.newLine();
 						performance.flush();
 
-						console.println("End Processing " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
+						// console.println("End Processing " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
 					} else {
 
 						c32documentsprocessed++;
@@ -1906,18 +1947,6 @@
 				nonxmldocuments++;
 			}
 
-			// final int STATSFILENAME = 0;
-			// final int STATSTOTAL = 1;
-			// final int STATSCONSOLIDATED = 2;
-			// final int STATSCONTINUITYOFCARE = 3;
-			// final int STATSPROGRESSNOTE = 4;
-			// final int STATSEPISODENOTE = 5;
-			// final int STATSDISCHARGESUMMARY = 6;
-			// final int STATSOTHER = 7;
-			// final int STATSC32 = 8;
-			// final int STATSNONXML = 9;
-			// final int STATSERRORS = 10;
-
 			statrow.createCell(STATSTOTAL).setCellValue(statfilectr);
 			statrow.createCell(STATSCONSOLIDATED).setCellValue(consoldocumentsprocessed);
 			statrow.createCell(STATSCONTINUITYOFCARE).setCellValue(continuityofcareprocessed);
@@ -1931,7 +1960,10 @@
 			stopwatch.stop();
 		}
 
-		monitor.beginTask("Generate Spreadsheet", folder.members().length);
+		monitor.beginTask(
+			"Generate Spreadsheet", (folder != null
+					? folder.members().length
+					: 1));
 
 		for (Integer eClass : workbooks.keySet()) {
 
@@ -2030,15 +2062,18 @@
 			// cell.setCellFormula(strFormula);
 			// }
 
+			// folderTarget = zipfile.getParent().toAbsolutePath().toString() + System.getProperty("file.separator");
+			// folderName = zipfile.getFileName().toString().toUpperCase();
+
 			String fileLocation = "";
 			if (selectedFile != null) {
-				fileLocation = selectedFile.getParent().getLocation().toOSString() +
-						System.getProperty("file.separator") + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" +
+				fileLocation = folderTarget + System.getProperty("file.separator") +
+						CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" +
 						selectedFile.getName().toUpperCase().replace(".ZIP", "") + getFileName(eClass, splitOption) +
 						"_SA.xlsx";
 			} else {
-				fileLocation = folder.getParent().getLocation().toOSString() + System.getProperty("file.separator") +
-						CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + folder.getName().toUpperCase() +
+				fileLocation = folderTarget + System.getProperty("file.separator") +
+						CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + folderName +
 						getFileName(eClass, splitOption) + "_SA.xlsx";
 			}
 
@@ -2050,9 +2085,35 @@
 				org.apache.commons.io.FileUtils.touch(theFile);
 			}
 
-			monitor.subTask(
-				"Serializing  " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + folder.getName().toUpperCase() +
-						"_SA.xlsx");
+			monitor.subTask("Serializing  " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + fileLocation);
+
+			// String fileLocation = "";
+			// if (selectedFile != null) {
+			// fileLocation = selectedFile.getParent().getLocation().toOSString() +
+			// System.getProperty("file.separator") + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" +
+			// selectedFile.getName().toUpperCase().replace(".ZIP", "") + getFileName(eClass, splitOption) +
+			// "_SA.xlsx";
+			//
+			// } else {
+			// fileLocation = folder.getParent().getLocation().toOSString() + System.getProperty("file.separator") +
+			// CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + (folder != null
+			// ? folder.getName().toUpperCase()
+			// : "XXX") +
+			// getFileName(eClass, splitOption) + "_SA.xlsx";
+			// }
+			//
+			// File theFile = new File(fileLocation);
+
+			// If the file exists, check to see if we can open it
+			// this is the excel
+			if (theFile.exists()) {
+				org.apache.commons.io.FileUtils.touch(theFile);
+			}
+
+			// monitor.subTask(
+			// "Serializing " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + (folder != null
+			// ? folder.getName().toUpperCase()
+			// : "XXX") + "_SA.xlsx");
 
 			if (!sectionFilter.isEmpty()) {
 				/*
@@ -2068,7 +2129,7 @@
 				wb.removeSheetAt(wb.getSheetIndex("Encounters"));
 
 			}
-			console.println("Start Saving " + currentProcessingTime);
+			// console.println("Start Saving " + currentProcessingTime);
 
 			try (OutputStream fileOut = Files.newOutputStream(Paths.get(fileLocation))) {
 				wb.write(fileOut);
@@ -2079,25 +2140,30 @@
 
 			// FileOutputStream = new FileOutputStream();
 
-			console.println("End Saving " + currentProcessingTime);
+			// console.println("End Saving " + currentProcessingTime);
 
 			monitor.subTask(
-				"Flushing Memory  " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" +
-						folder.getName().toUpperCase() + "_SA.xlsx");
+				"Flushing Memory  " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + (folder != null
+						? folder.getName().toUpperCase()
+						: "XXX") + "_SA.xlsx");
 
 			monitor.subTask(
-				"Reloading  " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + folder.getName().toUpperCase() +
-						"_SA.xlsx");
+				"Reloading  " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + (folder != null
+						? folder.getName().toUpperCase()
+						: "XXX") + "_SA.xlsx");
 
 			/**
 			 * @TODO Add preference for formatting
 			 */
-			if (false && folder.members().length < 50) {
+			if (false && (folder != null
+					? folder.members().length
+					: 1) < 50) {
 				format(fileLocation, monitor);
 			}
 			monitor.subTask(
-				"Completed Saving  " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" +
-						folder.getName().toUpperCase() + "_SA.xlsx");
+				"Completed Saving  " + CDAValueUtil.DATE_FORMAT3.format(new Date()) + "_" + (folder != null
+						? folder.getName().toUpperCase()
+						: "XXX") + "_SA.xlsx");
 
 		}