public void addNodeClinicalData(boolean header, boolean footer) {
   String ODMVersion = this.getODMVersion();
   // when collecting data, only item with value has been collected.
   StringBuffer xml = this.getXmlOutput();
   String indent = this.getIndent();
   String nls = System.getProperty("line.separator");
   if (header) {
     xml.append(
         indent
             + "<ClinicalData StudyOID=\""
             + StringEscapeUtils.escapeXml(clinicalData.getStudyOID())
             + "\" MetaDataVersionOID=\""
             + StringEscapeUtils.escapeXml(this.clinicalData.getMetaDataVersionOID())
             + "\">");
     xml.append(nls);
   }
   ArrayList<ExportSubjectDataBean> subs =
       (ArrayList<ExportSubjectDataBean>) this.clinicalData.getExportSubjectData();
   for (ExportSubjectDataBean sub : subs) {
     xml.append(
         indent
             + indent
             + "<SubjectData SubjectKey=\""
             + StringEscapeUtils.escapeXml(sub.getSubjectOID()));
     if ("oc1.2".equalsIgnoreCase(ODMVersion) || "oc1.3".equalsIgnoreCase(ODMVersion)) {
       xml.append(
           "\" OpenClinica:StudySubjectId=\""
               + StringEscapeUtils.escapeXml(sub.getStudySubjectId()));
       String uniqueIdentifier = sub.getUniqueIdentifier();
       if (uniqueIdentifier != null && uniqueIdentifier.length() > 0) {
         xml.append(
             "\" OpenClinica:UniqueIdentifier=\"" + StringEscapeUtils.escapeXml(uniqueIdentifier));
       }
       String status = sub.getStatus();
       if (status != null && status.length() > 0) {
         xml.append("\" OpenClinica:Status=\"" + StringEscapeUtils.escapeXml(status));
       }
       String secondaryId = sub.getSecondaryId();
       if (secondaryId != null && secondaryId.length() > 0) {
         xml.append("\"  OpenClinica:SecondaryId=\"" + StringEscapeUtils.escapeXml(secondaryId));
       }
       Integer year = sub.getYearOfBirth();
       if (year != null) {
         xml.append("\" OpenClinica:YearOfBirth=\"" + sub.getYearOfBirth());
       } else {
         if (sub.getDateOfBirth() != null) {
           xml.append("\" OpenClinica:DateOfBirth=\"" + sub.getDateOfBirth());
         }
       }
       String gender = sub.getSubjectGender();
       if (gender != null && gender.length() > 0) {
         xml.append("\" OpenClinica:Sex=\"" + StringEscapeUtils.escapeXml(gender));
       }
     }
     xml.append("\">");
     xml.append(nls);
     //
     ArrayList<ExportStudyEventDataBean> ses =
         (ArrayList<ExportStudyEventDataBean>) sub.getExportStudyEventData();
     for (ExportStudyEventDataBean se : ses) {
       xml.append(
           indent
               + indent
               + indent
               + "<StudyEventData StudyEventOID=\""
               + StringEscapeUtils.escapeXml(se.getStudyEventOID()));
       if ("oc1.2".equalsIgnoreCase(ODMVersion) || "oc1.3".equalsIgnoreCase(ODMVersion)) {
         String location = se.getLocation();
         if (location != null && location.length() > 0) {
           xml.append(
               "\" OpenClinica:StudyEventLocation=\"" + StringEscapeUtils.escapeXml(location));
         }
         String startDate = se.getStartDate();
         if (startDate != null && startDate.length() > 0) {
           xml.append("\" OpenClinica:StartDate=\"" + startDate);
         }
         String endDate = se.getEndDate();
         if (endDate != null && endDate.length() > 0) {
           xml.append("\" OpenClinica:EndDate=\"" + endDate);
         }
         String status = se.getStatus();
         if (status != null && status.length() > 0) {
           xml.append("\" OpenClinica:Status=\"" + StringEscapeUtils.escapeXml(status));
         }
         if (se.getAgeAtEvent() != null) {
           xml.append("\" OpenClinica:SubjectAgeAtEvent=\"" + se.getAgeAtEvent());
         }
       }
       xml.append("\"");
       if (!"-1".equals(se.getStudyEventRepeatKey())) {
         xml.append(" StudyEventRepeatKey=\"" + se.getStudyEventRepeatKey() + "\"");
       }
       xml.append(">");
       xml.append(nls);
       //
       ArrayList<ExportFormDataBean> forms = se.getExportFormData();
       for (ExportFormDataBean form : forms) {
         xml.append(
             indent
                 + indent
                 + indent
                 + indent
                 + "<FormData FormOID=\""
                 + StringEscapeUtils.escapeXml(form.getFormOID()));
         if ("oc1.2".equalsIgnoreCase(ODMVersion) || "oc1.3".equalsIgnoreCase(ODMVersion)) {
           String crfVersion = form.getCrfVersion();
           if (crfVersion != null && crfVersion.length() > 0) {
             xml.append("\" OpenClinica:Version=\"" + StringEscapeUtils.escapeXml(crfVersion));
           }
           String interviewerName = form.getInterviewerName();
           if (interviewerName != null && interviewerName.length() > 0) {
             xml.append(
                 "\" OpenClinica:InterviewerName=\""
                     + StringEscapeUtils.escapeXml(interviewerName));
           }
           if (form.getInterviewDate() != null) {
             xml.append("\" OpenClinica:InterviewDate=\"" + form.getInterviewDate());
           }
           String status = form.getStatus();
           if (status != null && status.length() > 0) {
             xml.append("\" OpenClinica:Status=\"" + StringEscapeUtils.escapeXml(status));
           }
         }
         xml.append("\">");
         xml.append(nls);
         //
         ArrayList<ImportItemGroupDataBean> igs = form.getItemGroupData();
         for (ImportItemGroupDataBean ig : igs) {
           xml.append(
               indent
                   + indent
                   + indent
                   + indent
                   + indent
                   + "<ItemGroupData ItemGroupOID=\""
                   + StringEscapeUtils.escapeXml(ig.getItemGroupOID())
                   + "\" ");
           if (!"-1".equals(ig.getItemGroupRepeatKey())) {
             xml.append("ItemGroupRepeatKey=\"" + ig.getItemGroupRepeatKey() + "\" ");
           }
           xml.append("TransactionType=\"Insert\">");
           xml.append(nls);
           ArrayList<ImportItemDataBean> items = ig.getItemData();
           for (ImportItemDataBean item : items) {
             if ("Yes".equals(item.getIsNull())) {
               xml.append(
                   indent
                       + indent
                       + indent
                       + indent
                       + indent
                       + indent
                       + "<ItemData ItemOID=\""
                       + StringEscapeUtils.escapeXml(item.getItemOID())
                       + "\" IsNull=\"Yes\"");
               if ("oc1.2".equalsIgnoreCase(ODMVersion) || "oc1.3".equalsIgnoreCase(ODMVersion)) {
                 xml.append(
                     " OpenClinica:ReasonForNull=\""
                         + StringEscapeUtils.escapeXml(item.getReasonForNull())
                         + "\"");
               }
               xml.append("/>");
               xml.append(nls);
             } else {
               String muRefOid = item.getMeasurementUnitRef().getElementDefOID();
               if (muRefOid != null && muRefOid.length() > 0) {
                 xml.append(
                     indent
                         + indent
                         + indent
                         + indent
                         + indent
                         + indent
                         + "<ItemData ItemOID=\""
                         + StringEscapeUtils.escapeXml(item.getItemOID())
                         + "\" Value=\""
                         + StringEscapeUtils.escapeXml(item.getValue())
                         + "\">");
                 xml.append(nls);
                 xml.append(
                     indent
                         + indent
                         + indent
                         + indent
                         + indent
                         + indent
                         + indent
                         + "<MeasurementUnitRef MeasurementUnitOID=\""
                         + StringEscapeUtils.escapeXml(muRefOid)
                         + "\"/>");
                 xml.append(nls);
                 xml.append(indent + indent + indent + indent + indent + indent + "</ItemData>");
                 xml.append(nls);
               } else {
                 xml.append(
                     indent
                         + indent
                         + indent
                         + indent
                         + indent
                         + indent
                         + "<ItemData ItemOID=\""
                         + StringEscapeUtils.escapeXml(item.getItemOID())
                         + "\" Value=\""
                         + StringEscapeUtils.escapeXml(item.getValue())
                         + "\"/>");
                 xml.append(nls);
               }
             }
           }
           xml.append(indent + indent + indent + indent + indent + "</ItemGroupData>");
           xml.append(nls);
         }
         xml.append(indent + indent + indent + indent + "</FormData>");
         xml.append(nls);
       }
       xml.append(indent + indent + indent + "</StudyEventData>");
       xml.append(nls);
     }
     if ("oc1.2".equalsIgnoreCase(ODMVersion) || "oc1.3".equalsIgnoreCase(ODMVersion)) {
       ArrayList<SubjectGroupDataBean> sgddata =
           (ArrayList<SubjectGroupDataBean>) sub.getSubjectGroupData();
       if (sgddata.size() > 0) {
         for (SubjectGroupDataBean sgd : sgddata) {
           String cid =
               sgd.getStudyGroupClassId() != null
                   ? "OpenClinica:StudyGroupClassID=\""
                       + StringEscapeUtils.escapeXml(sgd.getStudyGroupClassId())
                       + "\" "
                   : "";
           if (cid.length() > 0) {
             String cn =
                 sgd.getStudyGroupClassName() != null
                     ? "OpenClinica:StudyGroupClassName=\""
                         + StringEscapeUtils.escapeXml(sgd.getStudyGroupClassName())
                         + "\" "
                     : "";
             String gn =
                 sgd.getStudyGroupName() != null
                     ? "OpenClinica:StudyGroupName=\""
                         + StringEscapeUtils.escapeXml(sgd.getStudyGroupName())
                         + "\" "
                     : "";
             xml.append(
                 indent + indent + indent + "<OpenClinica:SubjectGroupData " + cid + cn + gn);
           }
           xml.append(" />");
           xml.append(nls);
         }
       }
     }
     xml.append(indent + indent + "</SubjectData>");
     xml.append(nls);
   }
   if (footer) {
     xml.append(indent + "</ClinicalData>");
     xml.append(nls);
   }
 }
 public String generateHardValidationErrorMessage(
     ArrayList<SubjectDataBean> subjectData,
     HashMap<String, String> hardValidationErrors,
     boolean isValid) {
   StringBuffer sb = new StringBuffer();
   String studyEventRepeatKey = "1";
   String groupRepeatKey = "1";
   sb.append("<table border=\'0\' cellpadding=\'0\' cellspacing=\'0\' width=\'100%\'>");
   for (SubjectDataBean subjectDataBean : subjectData) {
     sb.append(
         "<tr valign=\'top\'> <td class=\'table_header_row\' colspan=\'4\'>Study Subject: "
             + subjectDataBean.getSubjectOID()
             + "</td> </tr>");
     // next step here
     ArrayList<StudyEventDataBean> studyEventDataBeans = subjectDataBean.getStudyEventData();
     for (StudyEventDataBean studyEventDataBean : studyEventDataBeans) {
       sb.append(
           "<tr valign=\'top\'> <td class=\'table_header_row\'>Event CRF OID</td> <td class=\'table_header_row\' colspan=\'3\'></td>");
       sb.append("</tr> <tr valign=\'top\'> <td class=\'table_cell_left\'>");
       sb.append(studyEventDataBean.getStudyEventOID());
       if (studyEventDataBean.getStudyEventRepeatKey() != null) {
         studyEventRepeatKey = studyEventDataBean.getStudyEventRepeatKey();
         sb.append(" (Repeat key " + studyEventDataBean.getStudyEventRepeatKey() + ")");
       } else {
         // reset
         studyEventRepeatKey = "1";
       }
       sb.append("</td> <td class=\'table_cell\' colspan=\'3\'></td> </tr>");
       ArrayList<FormDataBean> formDataBeans = studyEventDataBean.getFormData();
       for (FormDataBean formDataBean : formDataBeans) {
         sb.append("<tr valign=\'top\'> <td class=\'table_header_row\'></td> ");
         sb.append(
             "<td class=\'table_header_row\'>CRF Version OID</td> <td class=\'table_header_row\' colspan=\'2\'></td></tr>");
         sb.append(
             "<tr valign=\'top\'> <td class=\'table_cell_left\'></td> <td class=\'table_cell\'>");
         sb.append(formDataBean.getFormOID());
         sb.append("</td> <td class=\'table_cell\' colspan=\'2\'></td> </tr>");
         ArrayList<ImportItemGroupDataBean> itemGroupDataBeans = formDataBean.getItemGroupData();
         for (ImportItemGroupDataBean itemGroupDataBean : itemGroupDataBeans) {
           sb.append("<tr valign=\'top\'> <td class=\'table_header_row\'></td>");
           sb.append(
               "<td class=\'table_header_row\'></td> <td class=\'table_header_row\' colspan=\'2\'>");
           sb.append(itemGroupDataBean.getItemGroupOID());
           if (itemGroupDataBean.getItemGroupRepeatKey() != null) {
             groupRepeatKey = itemGroupDataBean.getItemGroupRepeatKey();
             sb.append(" (Repeat key " + itemGroupDataBean.getItemGroupRepeatKey() + ")");
           } else {
             groupRepeatKey = "1";
           }
           sb.append("</td></tr>");
           ArrayList<ImportItemDataBean> itemDataBeans = itemGroupDataBean.getItemData();
           for (ImportItemDataBean itemDataBean : itemDataBeans) {
             String oidKey =
                 itemDataBean.getItemOID()
                     + "_"
                     + studyEventRepeatKey
                     + "_"
                     + groupRepeatKey
                     + "_"
                     + subjectDataBean.getSubjectOID();
             if (!isValid) {
               if (hardValidationErrors.containsKey(oidKey)) {
                 sb.append("<tr valign=\'top\'> <td class=\'table_cell_left\'></td>");
                 sb.append(
                     "<td class=\'table_cell\'></td> <td class=\'table_cell\'><font color=\'red\'>");
                 sb.append(itemDataBean.getItemOID());
                 sb.append("</font></td> <td class=" + "\'table_cell\'>");
                 sb.append(itemDataBean.getValue() + "<br/>");
                 sb.append(hardValidationErrors.get(oidKey));
                 sb.append("</td></tr>");
                 /*
                  * <tr valign="top"> <td
                  * class="table_cell_left"></td> <td
                  * class="table_cell"></td> <td
                  * class="table_cell"><font
                  * color="red"><c:out
                  * value="${itemData.itemOID}"/></font></td>
                  * <td class="table_cell"> <c:out
                  * value="${itemData.value}"/><br/> <c:out
                  * value="${hardValidationErrors[oidKey]}"/>
                  * </td> </tr>
                  */
               }
             } else {
               if (!hardValidationErrors.containsKey(oidKey)) {
                 sb.append("<tr valign=\'top\'> <td class=\'table_cell_left\'></td>");
                 sb.append("<td class=\'table_cell\'></td> <td class=\'table_cell\'>");
                 sb.append(itemDataBean.getItemOID());
                 sb.append("</td> <td class=" + "\'table_cell\'>");
                 sb.append(itemDataBean.getValue());
                 sb.append("</td></tr>");
               }
             }
           }
         }
       }
     }
   }
   sb.append("</table>");
   return sb.toString();
 }