Updates to xls generation

modified processing of sections with missing or incorrect template ids
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 75ed50c..1da3c5e 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
@@ -1710,7 +1710,17 @@
 							}
 
 							console.println("Start Section " + section.eClass().getName());
-							pss.doSwitch(section);
+							if (!pss.doSwitch(section)) {
+
+								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("End Section " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
 
 						}
diff --git a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/ProcessSectionSwitch.java b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/ProcessSectionSwitch.java
index 1d89d2b..bd43899 100644
--- a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/ProcessSectionSwitch.java
+++ b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/ProcessSectionSwitch.java
@@ -11,15 +11,26 @@
  *******************************************************************************/
 package org.eclipse.mdht.cda.xml.ui.handlers;
 
+import static org.eclipse.mdht.uml.cda.util.CDAUtil.CDA_ANNOTATION_SOURCE;
+
 import java.io.IOException;
+import java.util.HashMap;
 import java.util.List;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.emf.ecore.EAnnotation;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EClassifier;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.mdht.cda.xml.ui.handlers.CDAValueUtil.DocumentMetadata;
+import org.eclipse.mdht.cda.xml.ui.internal.Logger;
 import org.eclipse.mdht.uml.cda.Act;
+import org.eclipse.mdht.uml.cda.CDAPackage;
 import org.eclipse.mdht.uml.cda.Encounter;
 import org.eclipse.mdht.uml.cda.Entry;
 import org.eclipse.mdht.uml.cda.PatientRole;
@@ -357,7 +368,8 @@
 	 * (non-Javadoc)
 	 *
 	 * @see
-	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseInterventionsSection(org.openhealthtools.mdht.uml.cda.consol.InterventionsSection)
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseInterventionsSection(org.openhealthtools.mdht.uml.cda.consol.
+	 * InterventionsSection)
 	 */
 	@Override
 	public Boolean caseInterventionsSection2(InterventionsSection2 section) {
@@ -421,7 +433,8 @@
 	 * (non-Javadoc)
 	 *
 	 * @see
-	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseHealthConcernsSection(org.openhealthtools.mdht.uml.cda.consol.HealthConcernsSection)
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseHealthConcernsSection(org.openhealthtools.mdht.uml.cda.consol.
+	 * HealthConcernsSection)
 	 */
 	@Override
 	public Boolean caseHealthConcernsSection(HealthConcernsSection section) {
@@ -796,9 +809,12 @@
 	// return super.casePlanOfTreatmentSection2(section);
 	// }
 
+	HashMap<String, EClass> codeToSection = new HashMap<String, EClass>();
+
 	/*
 	 * (non-Javadoc)
 	 *
+	 *
 	 * @see org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseSection(org.eclipse.mdht.uml.cda.Section)
 	 */
 	@Override
@@ -826,4 +842,90 @@
 		return Boolean.TRUE;
 	}
 
+	/*
+	 * (non-Javadoc)
+	 *
+	 * @see org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#defaultCase(org.eclipse.emf.ecore.EObject)
+	 */
+	@Override
+	public Boolean defaultCase(EObject object) {
+
+		// If not found - try using code
+		Section section = (Section) object;
+
+		Logger.log(Logger.ERROR, "Second Chance");
+
+		if (codeToSection.isEmpty()) {
+			for (EClassifier consolPackageClassifier : ConsolPackage.eINSTANCE.getEClassifiers()) {
+				if (CDAPackage.eINSTANCE.getSection().isSuperTypeOf(CDAPackage.eINSTANCE.getSection())) {
+					EAnnotation annotation = consolPackageClassifier.getEAnnotation(CDA_ANNOTATION_SOURCE);
+					String code = annotation.getDetails().get("code.code");
+					if (!codeToSection.containsKey(code)) {
+						codeToSection.put(code, (EClass) consolPackageClassifier);
+					}
+
+				}
+
+			}
+		}
+
+		boolean processed = false;
+		if (section.getCode() != null && !StringUtils.isEmpty(section.getCode().getCode())) {
+
+			String code = section.getCode().getCode();
+			Logger.log(Logger.ERROR, "Second Chance CODE " + code);
+
+			if (codeToSection.containsKey(code)) {
+				Section newSection = (Section) EcoreUtil.create(codeToSection.get(code));
+				newSection.setText(section.getText());
+				newSection.getEntries().addAll(section.getEntries());
+
+				Logger.log(Logger.ERROR, "Second Chance SECTION " + newSection.toString());
+
+				String secondChanceSheetIndex = currentHandler.getSheet(
+					section.getClinicalDocument().eClass(), String.valueOf(newSection.eClass().getClassifierID()),
+					GenerateCDADataHandler.sheetName(newSection.eClass()), splitOption);
+
+				SectionSwitch secondChanceSwitch = new SectionSwitch(
+					query, wb.getSheet(secondChanceSheetIndex), documentMetadata, patientRole, serviceEvent, encounters,
+					file.getName());
+				secondChanceSwitch.doSwitch(newSection);
+
+				if (currentHandler.shouldCountSection(newSection)) {
+					currentHandler.getSectionHash(
+						section.getClinicalDocument().eClass().getClassifierID(), secondChanceSheetIndex,
+						splitOption).add(file);
+
+				}
+
+				processed = true;
+			}
+
+		}
+
+		// if can not find - process as a section process only narrative
+		if (!processed) {
+			String secondChanceSheetIndex = currentHandler.getSheet(
+				section.getClinicalDocument().eClass(), String.valueOf(section.eClass().getClassifierID()),
+				GenerateCDADataHandler.sheetName(section.eClass()), splitOption);
+
+			SectionSwitch secondChanceSwitch = new SectionSwitch(
+				query, wb.getSheet(secondChanceSheetIndex), documentMetadata, patientRole, serviceEvent, encounters,
+				file.getName());
+			secondChanceSwitch.setFirstpass(false);
+			secondChanceSwitch.doSwitch(section);
+
+			if (currentHandler.shouldCountSection(section)) {
+				currentHandler.getSectionHash(
+					section.getClinicalDocument().eClass().getClassifierID(), secondChanceSheetIndex, splitOption).add(
+						file);
+
+			}
+
+		}
+
+		// TODO Auto-generated method stub
+		return Boolean.FALSE;
+	}
+
 }
diff --git a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/SectionSwitch.java b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/SectionSwitch.java
index f448425..648c907 100644
--- a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/SectionSwitch.java
+++ b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/SectionSwitch.java
@@ -80,11 +80,29 @@
 
 class SectionSwitch extends ConsolSwitch<Boolean> {
 
+	boolean firstpass = true;
+
+	/**
+	 * @return the firstpass
+	 */
+	public boolean isFirstpass() {
+		return firstpass;
+	}
+
+	/**
+	 * @param firstpass
+	 *            the firstpass to set
+	 */
+	public void setFirstpass(boolean firstpass) {
+		this.firstpass = firstpass;
+	}
+
 	/*
 	 * (non-Javadoc)
 	 *
 	 * @see
-	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseHospitalAdmissionMedicationsSectionEntriesOptional(org.openhealthtools.mdht.uml.cda.
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseHospitalAdmissionMedicationsSectionEntriesOptional(org.openhealthtools.mdht.uml.
+	 * cda.
 	 * consol.HospitalAdmissionMedicationsSectionEntriesOptional)
 	 */
 	@Override
@@ -140,7 +158,8 @@
 	 * (non-Javadoc)
 	 *
 	 * @see
-	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseComplicationsSection(org.openhealthtools.mdht.uml.cda.consol.ComplicationsSection)
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseComplicationsSection(org.openhealthtools.mdht.uml.cda.consol.
+	 * ComplicationsSection)
 	 */
 	@Override
 	public Boolean caseComplicationsSection(ComplicationsSection section) {
@@ -407,7 +426,8 @@
 	/*
 	 * (non-Javadoc)
 	 *
-	 * @see org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseInstructionsSection(org.openhealthtools.mdht.uml.cda.consol.InstructionsSection)
+	 * @see
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseInstructionsSection(org.openhealthtools.mdht.uml.cda.consol.InstructionsSection)
 	 */
 	@Override
 	public Boolean caseInstructionsSection(InstructionsSection section) {
@@ -436,7 +456,8 @@
 	 * (non-Javadoc)
 	 *
 	 * @see
-	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseHealthStatusEvaluationsAndOutcomesSection(org.openhealthtools.mdht.uml.cda.consol.
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseHealthStatusEvaluationsAndOutcomesSection(org.openhealthtools.mdht.uml.cda.
+	 * consol.
 	 * HealthStatusEvaluationsAndOutcomesSection)
 	 */
 	@Override
@@ -839,7 +860,8 @@
 	 * (non-Javadoc)
 	 *
 	 * @see
-	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseChiefComplaintSection(org.openhealthtools.mdht.uml.cda.consol.ChiefComplaintSection)
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseChiefComplaintSection(org.openhealthtools.mdht.uml.cda.consol.
+	 * ChiefComplaintSection)
 	 */
 	// @Override
 	// public Boolean caseChiefComplaintSection(ChiefComplaintSection section) {
@@ -866,7 +888,8 @@
 	 * (non-Javadoc)
 	 *
 	 * @see
-	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseDischargeMedicationsSectionEntriesOptional2(org.openhealthtools.mdht.uml.cda.consol.
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseDischargeMedicationsSectionEntriesOptional2(org.openhealthtools.mdht.uml.cda.
+	 * consol.
 	 * DischargeMedicationsSectionEntriesOptional2)
 	 */
 	@Override
@@ -924,7 +947,8 @@
 	/*
 	 * (non-Javadoc)
 	 *
-	 * @see org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseChiefComplaintAndReasonForVisitSection(org.openhealthtools.mdht.uml.cda.consol.
+	 * @see
+	 * org.openhealthtools.mdht.uml.cda.consol.util.ConsolSwitch#caseChiefComplaintAndReasonForVisitSection(org.openhealthtools.mdht.uml.cda.consol.
 	 * ChiefComplaintAndReasonForVisitSection)
 	 */
 	@Override
@@ -939,7 +963,12 @@
 	 */
 	@Override
 	public Boolean defaultCase(EObject object) {
+
+		if (this.firstpass) {
+			return Boolean.FALSE;
+		}
 		Section section = (Section) object;
+
 		if (sheet.getPhysicalNumberOfRows() == 0) {
 			Row row1 = null; // sheet.createRow(0);
 			Row row2 = sheet.createRow(0);
@@ -1270,8 +1299,6 @@
 				offset = SpreadsheetSerializer.appendCode(
 					row, offset, allergyProblemAct.getSection(), severityCode, severity.getText());
 			} else {
-				// tick up offset becuase appendcode will not populate the text column
-				offset++;
 				offset = SpreadsheetSerializer.appendCode(row, offset, allergyProblemAct.getSection(), null, null);
 			}
 			offset = SpreadsheetSerializer.appendOrganizationAndAuthor(row, offset, allergyObservation.getAuthors());
@@ -1442,7 +1469,7 @@
 
 		offset = SpreadsheetSerializer.serializeNoEnounterID(row, offset);
 
-		// offset = SpreadsheetSerializer.serializeStrucDocText(row, offset, text);
+		offset = SpreadsheetSerializer.serializeStrucDocText(row, offset, text);
 
 		SpreadsheetSerializer.serializeSectionAndFileName(row, offset, (Section) text.eContainer(), fileName);
 
diff --git a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/SpreadsheetSerializer.java b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/SpreadsheetSerializer.java
index 50f9d4d..b180a20 100644
--- a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/SpreadsheetSerializer.java
+++ b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/SpreadsheetSerializer.java
@@ -19,6 +19,7 @@
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.xssf.streaming.SXSSFSheet;
@@ -31,6 +32,7 @@
 import org.eclipse.mdht.cda.xml.ui.handlers.AnalyzeCDAHandler.CDAAnalaysisInput.CDAMetrics;
 import org.eclipse.mdht.cda.xml.ui.handlers.CDAValueUtil.DocumentMetadata;
 import org.eclipse.mdht.cda.xml.ui.handlers.GenerateCDADataHandler.WrapperForZipEnty;
+import org.eclipse.mdht.cda.xml.ui.handlers.html.HTMLToExcel;
 import org.eclipse.mdht.uml.cda.Act;
 import org.eclipse.mdht.uml.cda.AssignedAuthor;
 import org.eclipse.mdht.uml.cda.Author;
@@ -156,9 +158,9 @@
 
 		if (text != null && !StringUtils.isEmpty(text.getText())) {
 			String narrative = text.getText();
-			// HTMLToExcel hte = new HTMLToExcel();
-			// RichTextString s = hte.fromHtmlToCellValue(narrative, row.getSheet().getWorkbook());
-			row.createCell(offset++).setCellValue(narrative);
+			HTMLToExcel hte = new HTMLToExcel();
+			RichTextString s = hte.fromHtmlToCellValue(narrative, row.getSheet().getWorkbook());
+			row.createCell(offset++).setCellValue(s.getString());
 		} else {
 			row.createCell(offset++).setCellValue("No Narrative");
 		}
diff --git a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/html/HTMLToExcel.java b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/html/HTMLToExcel.java
index 827f0bc..d9f510b 100644
--- a/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/html/HTMLToExcel.java
+++ b/cda/plugins/org.eclipse.mdht.cda.xml.ui/src/org/eclipse/mdht/cda/xml/ui/handlers/html/HTMLToExcel.java
@@ -85,12 +85,12 @@
 		}
 
 		RichTextString richText = new XSSFRichTextString(textBuffer.toString());
-		for (int i = 0; i < textBuffer.length(); i++) {
-			Font currentFont = mergedMap.get(i);
-			if (currentFont != null) {
-				richText.applyFont(i, i + 1, currentFont);
-			}
-		}
+		// for (int i = 0; i < textBuffer.length(); i++) {
+		// Font currentFont = mergedMap.get(i);
+		// if (currentFont != null) {
+		// richText.applyFont(i, i + 1, currentFont);
+		// }
+		// }
 		return richText;
 	}