/**
   * Generates the xml representation and mount in rootNode the structure to be included. Fills the
   * XMLNode structure with the form representation and returns the string.
   */
  public String generateFormXML(Form form, XMLNode rootNode) throws Exception {
    rootNode.addAttribute(ATTR_ID, form.getId().toString());

    addXMLNode("subject", form.getSubject(), rootNode);
    addXMLNode("name", form.getName(), rootNode);
    addXMLNode("displayMode", form.getDisplayMode(), rootNode);
    addXMLNode("labelMode", form.getLabelMode(), rootNode);
    addXMLNode("showMode", form.getShowMode(), rootNode);
    addXMLNode(
        "status", (form.getStatus() != null ? String.valueOf(form.getStatus()) : null), rootNode);
    addXMLNode("formTemplate", form.getFormTemplate(), rootNode);

    for (Field field : form.getFormFields()) {
      generateFieldXML(field, rootNode);
    }

    for (DataHolder dataHolder : form.getHolders()) {
      generateDataHolderXML(dataHolder, rootNode);
    }

    StringWriter sw = new StringWriter();
    rootNode.writeXML(sw, true);

    return sw.toString();
  }
 private void addXMLNode(String propName, String value, XMLNode parent) {
   if (!StringUtils.isEmpty(value)) {
     XMLNode propertyNode = new XMLNode(NODE_PROPERTY, parent);
     propertyNode.addAttribute(ATTR_NAME, propName);
     propertyNode.addAttribute(ATTR_VALUE, value);
     parent.addChild(propertyNode);
   }
 }
  @Override
  public String generateHeaderFormFormId(long formId) throws IOException {
    XMLNode rootNode = new XMLNode(NODE_FORM, null);
    rootNode.addAttribute(ATTR_ID, String.valueOf(formId));
    StringWriter sw = new StringWriter();
    rootNode.writeXML(sw, true);

    return sw.toString().replace("/", "").trim();
  }
  public void generateDataHolderXML(DataHolder dataHolder, XMLNode parent) {
    XMLNode rootNode = new XMLNode(NODE_DATA_HOLDER, parent);
    rootNode.addAttribute(ATTR_ID, dataHolder.getUniqeId());
    rootNode.addAttribute(ATTR_INPUT_ID, dataHolder.getInputId());
    rootNode.addAttribute(ATTR_OUT_ID, dataHolder.getOuputId());
    rootNode.addAttribute(ATTR_TYPE, dataHolder.getTypeCode());
    rootNode.addAttribute(ATTR_VALUE, dataHolder.getInfo());
    rootNode.addAttribute(ATTR_NAME, dataHolder.getRenderColor());

    if (!StringUtils.isEmpty(dataHolder.getSupportedType()))
      rootNode.addAttribute(ATTR_SUPPORTED_TYPE, dataHolder.getSupportedType());

    parent.addChild(rootNode);
  }
  public void generateFieldXML(Field field, XMLNode parent) {
    XMLNode rootNode = new XMLNode(NODE_FIELD, parent);
    rootNode.addAttribute(ATTR_ID, String.valueOf(field.getId()));
    rootNode.addAttribute(ATTR_POSITION, String.valueOf(field.getPosition()));
    rootNode.addAttribute(ATTR_NAME, field.getFieldName());
    if (field.getFieldType() != null) {
      rootNode.addAttribute(ATTR_TYPE, field.getFieldType().getCode());
    }

    if (!StringUtils.isEmpty(field.getBag())) {
      rootNode.addAttribute(ATTR_BAG_TYPE, field.getBag());
    }

    addXMLNode(
        "fieldRequired",
        (field.getFieldRequired() != null ? String.valueOf(field.getFieldRequired()) : null),
        rootNode);
    addXMLNode(
        "groupWithPrevious",
        (field.getGroupWithPrevious() != null
            ? String.valueOf(field.getGroupWithPrevious())
            : null),
        rootNode);
    addXMLNode("height", field.getHeight(), rootNode);
    addXMLNode("labelCSSClass", field.getLabelCSSClass(), rootNode);
    addXMLNode("labelCSSStyle", field.getLabelCSSStyle(), rootNode);
    addXMLNode(
        "label", (field.getLabel() != null ? serializeI18nSet(field.getLabel()) : null), rootNode);
    addXMLNode(
        "errorMessage",
        (field.getErrorMessage() != null ? serializeI18nSet(field.getErrorMessage()) : null),
        rootNode);
    addXMLNode(
        "title", (field.getTitle() != null ? serializeI18nSet(field.getTitle()) : null), rootNode);
    addXMLNode(
        "readonly",
        (field.getReadonly() != null ? String.valueOf(field.getReadonly()) : null),
        rootNode);
    addXMLNode(
        "size", (field.getSize() != null ? String.valueOf(field.getSize()) : null), rootNode);
    addXMLNode("formula", field.getFormula(), rootNode);
    addXMLNode("rangeFormula", field.getRangeFormula(), rootNode);
    addXMLNode("pattern", field.getPattern(), rootNode);
    addXMLNode(
        "maxlength",
        (field.getMaxlength() != null ? String.valueOf(field.getMaxlength()) : null),
        rootNode);
    addXMLNode("styleclass", field.getStyleclass(), rootNode);
    addXMLNode("cssStyle", field.getCssStyle(), rootNode);
    addXMLNode(
        "tabindex",
        (field.getTabindex() != null ? String.valueOf(field.getTabindex()) : null),
        rootNode);
    addXMLNode("accesskey", field.getAccesskey(), rootNode);
    addXMLNode(
        "isHTML", (field.getIsHTML() != null ? String.valueOf(field.getIsHTML()) : null), rootNode);
    addXMLNode(
        "hideContent",
        (field.getHideContent() != null ? String.valueOf(field.getHideContent()) : null),
        rootNode);
    addXMLNode("defaultValueFormula", field.getDefaultValueFormula(), rootNode);
    addXMLNode("inputBinding", field.getInputBinding(), rootNode);
    addXMLNode("outputBinding", field.getOutputBinding(), rootNode);
    addXMLNode(
        "htmlContent",
        (field.getHtmlContent() != null ? serializeI18nSet(field.getHtmlContent()) : null),
        rootNode);

    addXMLNode("defaultSubform", field.getDefaultSubform(), rootNode);
    addXMLNode("previewSubform", field.getPreviewSubform(), rootNode);
    addXMLNode("tableSubform", field.getTableSubform(), rootNode);
    addXMLNode(
        "newItemText",
        (field.getNewItemText() != null ? serializeI18nSet(field.getNewItemText()) : null),
        rootNode);
    addXMLNode(
        "addItemText",
        (field.getAddItemText() != null ? serializeI18nSet(field.getAddItemText()) : null),
        rootNode);
    addXMLNode(
        "cancelItemText",
        (field.getCancelItemText() != null ? serializeI18nSet(field.getCancelItemText()) : null),
        rootNode);
    addXMLNode(
        "deleteItems",
        (field.getDeleteItems() != null ? String.valueOf(field.getDeleteItems()) : null),
        rootNode);
    addXMLNode(
        "updateItems",
        (field.getUpdateItems() != null ? String.valueOf(field.getUpdateItems()) : null),
        rootNode);
    addXMLNode(
        "visualizeItems",
        (field.getVisualizeItem() != null ? String.valueOf(field.getVisualizeItem()) : null),
        rootNode);
    addXMLNode(
        "hideCreateItem",
        (field.getHideCreateItem() != null ? String.valueOf(field.getHideCreateItem()) : null),
        rootNode);
    addXMLNode(
        "expanded",
        (field.getExpanded() != null ? String.valueOf(field.getExpanded()) : null),
        rootNode);
    addXMLNode(
        "enableTableEnterData",
        (field.getEnableTableEnterData() != null
            ? String.valueOf(field.getEnableTableEnterData())
            : null),
        rootNode);
    addXMLNode("customFieldType", field.getCustomFieldType(), rootNode);
    addXMLNode("fieldClass", field.getFieldType().getFieldClass(), rootNode);
    addXMLNode("param1", field.getParam1(), rootNode);
    addXMLNode("param2", field.getParam2(), rootNode);
    addXMLNode("param3", field.getParam3(), rootNode);
    addXMLNode("param4", field.getParam4(), rootNode);
    addXMLNode("param5", field.getParam5(), rootNode);
    addXMLNode("onChangeScript", field.getOnChangeScript(), rootNode);

    parent.addChild(rootNode);
  }