Example #1
0
  protected List<TemplateNode> getTemplateNodes(
      ThemeDisplay themeDisplay, Element element, long ddmStructureId) throws Exception {

    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(ddmStructureId);

    DDMForm ddmForm = ddmStructure.getDDMForm();

    Map<String, DDMFormField> ddmFormFieldsMap = ddmForm.getDDMFormFieldsMap(true);

    List<TemplateNode> templateNodes = new ArrayList<>();

    Map<String, TemplateNode> prototypeTemplateNodes = new HashMap<>();

    List<Element> dynamicElementElements = element.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
      Element dynamicContentElement = dynamicElementElement.element("dynamic-content");

      String data = StringPool.BLANK;

      if (dynamicContentElement != null) {
        data = dynamicContentElement.getText();
      }

      String name = dynamicElementElement.attributeValue("name", StringPool.BLANK);

      if (name.length() == 0) {
        throw new TransformException("Element missing \"name\" attribute");
      }

      String type = dynamicElementElement.attributeValue("type", StringPool.BLANK);

      Map<String, String> attributes = new HashMap<>();

      if (dynamicContentElement != null) {
        for (Attribute attribute : dynamicContentElement.attributes()) {
          attributes.put(attribute.getName(), attribute.getValue());
        }
      }

      TemplateNode templateNode =
          new TemplateNode(themeDisplay, name, StringUtil.stripCDATA(data), type, attributes);

      if (dynamicElementElement.element("dynamic-element") != null) {
        templateNode.appendChildren(
            getTemplateNodes(themeDisplay, dynamicElementElement, ddmStructureId));
      } else if ((dynamicContentElement != null)
          && (dynamicContentElement.element("option") != null)) {

        List<Element> optionElements = dynamicContentElement.elements("option");

        for (Element optionElement : optionElements) {
          templateNode.appendOption(StringUtil.stripCDATA(optionElement.getText()));
        }
      }

      DDMFormField ddmFormField = ddmFormFieldsMap.get(name);

      if (ddmFormField != null) {
        DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions();

        Map<String, LocalizedValue> options = ddmFormFieldOptions.getOptions();

        for (Entry<String, LocalizedValue> entry : options.entrySet()) {
          String optionValue = StringUtil.stripCDATA(entry.getKey());

          LocalizedValue localizedLabel = entry.getValue();

          String optionLabel = localizedLabel.getString(themeDisplay.getLocale());

          templateNode.appendOptionMap(optionValue, optionLabel);
        }
      }

      TemplateNode prototypeTemplateNode = prototypeTemplateNodes.get(name);

      if (prototypeTemplateNode == null) {
        prototypeTemplateNode = templateNode;

        prototypeTemplateNodes.put(name, prototypeTemplateNode);

        templateNodes.add(templateNode);
      }

      prototypeTemplateNode.appendSibling(templateNode);
    }

    return templateNodes;
  }