/**
     *
     * Generate the header row for the csv file.
     *
     * @param form
     * @param extraCols
     * @return
     * @throws Exception
     */
    public static String generateColumnHeadersFromHtmlForm(HtmlForm form, List<String> extraCols, StringBuffer sb, List<PatientIdentifierType> pitList) throws Exception {
        FormEntrySession session = new FormEntrySession(HtmlFormEntryUtil.getFakePerson(), form, null); // session gets a null HttpSession
        session.getHtmlToDisplay();
        HtmlFormSchema hfs = session.getContext().getSchema();

        sb.append(DEFAULT_QUOTE).append("ENCOUNTER_ID").append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR)
        .append(DEFAULT_QUOTE).append("ENCOUNTER_DATE").append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR)
        .append(DEFAULT_QUOTE).append("ENCOUNTER_LOCATION").append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR)
        .append(DEFAULT_QUOTE).append("ENCOUNTER_PROVIDER").append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR)
        .append(DEFAULT_QUOTE).append("INTERNAL_PATIENT_ID").append(DEFAULT_QUOTE).append(DEFAULT_COLUMN_SEPARATOR);
        int index = 1;
        for (PatientIdentifierType pit :  pitList){
            sb.append(DEFAULT_QUOTE).append(pit.getName()).append(DEFAULT_QUOTE);
            if (index < pitList.size())
                sb.append(DEFAULT_COLUMN_SEPARATOR);
            index ++;
        }

        Set<HtmlFormField> fields = hfs.getAllFields();

        for (HtmlFormField hfsec : fields) {
            sb = generateColumnHeadersFromHtmlFormHelper(hfsec, extraCols, sb);
        }

        session = null;
        sb.append(DEFAULT_LINE_SEPARATOR);
        return sb.toString();
    }
 /**
  * returns a Map<Integer, String> of all form sections, in order,
  * where the String value is either the specified name, or an arbitrary one if none was given.
  * and the integer is the numeric index of the sections, starting with 0.
  *
  * @param HtmlForm htmlForm
  * @return
  */
 public static Map<Integer, String> getSectionIndex(HtmlForm htmlForm) throws Exception{
     Document doc = HtmlFormEntryUtil.stringToDocument(htmlForm.getXmlData());
     FormEntrySession session = new FormEntrySession(HtmlFormEntryUtil.getFakePerson(), htmlForm, null); // session gets a null HttpSession
     NodeList nl = doc.getElementsByTagName("section");
     Map<Integer, String> ret = new LinkedHashMap<Integer, String>();
     for (int i = 0; i < nl.getLength(); i++){
         Node sectionNode = nl.item(i);
         NamedNodeMap map = sectionNode.getAttributes();
         String headerLabel = "no name specified";
         for (int j = 0; j < map.getLength(); ++j) {
             Node attribute = map.item(j);
             if (attribute.getNodeName().equals("headerLabel")) {
                 headerLabel = attribute.getNodeValue();
             }
             if (attribute.getNodeName().equals("headerCode")) {
                 Translator trans = session.getContext().getTranslator();
                 headerLabel = trans.translate(Context.getLocale().toString(), attribute.getNodeValue());
             }
         }
         ret.put(i, headerLabel);
     }
     return ret;
 }