示例#1
0
  protected void addDDMFormFieldOptions(
      JSONObject jsonObject,
      DDMFormField ddmFormField,
      Set<Locale> availableLocales,
      Locale defaultLocale) {

    String type = ddmFormField.getType();

    if (!(type.equals(DDMImpl.TYPE_RADIO) || type.equals(DDMImpl.TYPE_SELECT))) {

      return;
    }

    String fieldName = ddmFormField.getName();

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions();

    for (String optionValue : ddmFormFieldOptions.getOptionsValues()) {
      JSONObject optionJSONObject = JSONFactoryUtil.createJSONObject();

      String name = fieldName.concat(StringUtil.randomString());

      optionJSONObject.put("id", name);
      optionJSONObject.put("name", name);
      optionJSONObject.put("type", "option");
      optionJSONObject.put("value", optionValue);

      addDDMFormFieldLocalizedProperty(
          optionJSONObject,
          "label",
          ddmFormFieldOptions.getOptionLabels(optionValue),
          defaultLocale,
          defaultLocale,
          "option");

      JSONObject localizationMapJSONObject = JSONFactoryUtil.createJSONObject();

      for (Locale availableLocale : availableLocales) {
        JSONObject localeMap = JSONFactoryUtil.createJSONObject();

        addDDMFormFieldLocalizedProperty(
            localeMap,
            "label",
            ddmFormFieldOptions.getOptionLabels(optionValue),
            availableLocale,
            defaultLocale,
            "option");

        localizationMapJSONObject.put(LocaleUtil.toLanguageId(availableLocale), localeMap);
      }

      optionJSONObject.put("localizationMap", localizationMapJSONObject);

      jsonArray.put(optionJSONObject);
    }

    jsonObject.put("options", jsonArray);
  }
  @Test
  public void testGetWithSelectField() {
    ExtendedObjectClassDefinition extendedObjectClassDefinition =
        mock(ExtendedObjectClassDefinition.class);

    ExtendedAttributeDefinition extendedAttributeDefinition =
        mock(ExtendedAttributeDefinition.class);

    whenGetAttributeDefinitions(
        extendedObjectClassDefinition,
        new ExtendedAttributeDefinition[] {extendedAttributeDefinition},
        ExtendedObjectClassDefinition.REQUIRED);

    whenGetCardinality(extendedAttributeDefinition, 0);
    whenGetID(extendedAttributeDefinition, "Select");
    whenGetType(extendedAttributeDefinition, ExtendedAttributeDefinition.STRING);
    whenGetOptionLabels(extendedAttributeDefinition, new String[] {"Label 1", "Label 2"});
    whenGetOptionValues(extendedAttributeDefinition, new String[] {"Value 1", "Value 2"});

    ConfigurationModel configurationModel =
        new ConfigurationModel(extendedObjectClassDefinition, null, null, null, false);

    ConfigurationModelToDDMFormConverter configurationModelToDDMFormConverter =
        new ConfigurationModelToDDMFormConverter(
            configurationModel, _enLocale, new EmptyResourceBundle());

    DDMForm ddmForm = configurationModelToDDMFormConverter.getDDMForm();

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

    DDMFormField ddmFormField = ddmFormFieldsMap.get("Select");

    Assert.assertNotNull(ddmFormField);
    Assert.assertEquals(DDMFormFieldType.SELECT, ddmFormField.getType());
    Assert.assertEquals("string", ddmFormField.getDataType());
    Assert.assertFalse(ddmFormField.isRepeatable());
    Assert.assertTrue(ddmFormField.isRequired());

    DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions();

    Assert.assertEquals(_enLocale, ddmFormFieldOptions.getDefaultLocale());

    Set<String> optionValues = ddmFormFieldOptions.getOptionsValues();

    Assert.assertTrue(optionValues.contains("Value 1"));
    Assert.assertTrue(optionValues.contains("Value 2"));

    LocalizedValue value1Labels = ddmFormFieldOptions.getOptionLabels("Value 1");

    Assert.assertEquals(_enLocale, value1Labels.getDefaultLocale());
    Assert.assertEquals("Label 1", value1Labels.getString(_enLocale));

    LocalizedValue value2Labels = ddmFormFieldOptions.getOptionLabels("Value 2");

    Assert.assertEquals(_enLocale, value2Labels.getDefaultLocale());
    Assert.assertEquals("Label 2", value2Labels.getString(_enLocale));
  }
  @Override
  public String get(DDMFormFieldValue ddmFormFieldValue) {
    String optionValue = _ddmFormFieldValueAccessor.get(ddmFormFieldValue);

    DDMFormFieldOptions ddmFormFieldOptions = getDDMFormFieldOptions(ddmFormFieldValue);

    LocalizedValue optionLabel = ddmFormFieldOptions.getOptionLabels(optionValue);

    return optionLabel.getString(_ddmFormFieldValueAccessor.getLocale());
  }
  protected void validateDDMFormFieldOptions(
      DDMFormField ddmFormField, Set<Locale> ddmFormAvailableLocales, Locale ddmFormDefaultLocale)
      throws DDMFormValidationException {

    String fieldType = ddmFormField.getType();

    if (!fieldType.equals(DDMFormFieldType.RADIO) && !fieldType.equals(DDMFormFieldType.SELECT)) {

      return;
    }

    String dataSourceType = (String) ddmFormField.getProperty("dataSourceType");

    if (!Objects.equals(dataSourceType, "manual")) {
      return;
    }

    DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions();

    Set<String> optionValues = Collections.emptySet();

    if (ddmFormFieldOptions != null) {
      optionValues = ddmFormFieldOptions.getOptionsValues();
    }

    if (optionValues.isEmpty()) {
      throw new MustSetOptionsForField(ddmFormField.getName());
    }

    for (String optionValue : ddmFormFieldOptions.getOptionsValues()) {
      LocalizedValue localizedValue = ddmFormFieldOptions.getOptionLabels(optionValue);

      validateDDMFormFieldPropertyValue(
          ddmFormField.getName(),
          "options",
          localizedValue,
          ddmFormAvailableLocales,
          ddmFormDefaultLocale);
    }
  }
  protected DDMFormFieldOptions createDDMFormFieldOptions() {
    DDMFormFieldOptions ddmFormFieldOptions = new DDMFormFieldOptions();

    ddmFormFieldOptions.addOption("Value 1");

    ddmFormFieldOptions.addOptionLabel("Value 1", LocaleUtil.BRAZIL, "Opcao 1");
    ddmFormFieldOptions.addOptionLabel("Value 1", LocaleUtil.US, "Option 1");

    ddmFormFieldOptions.addOption("Value 2");

    ddmFormFieldOptions.addOptionLabel("Value 2", LocaleUtil.BRAZIL, "Opcao 2");
    ddmFormFieldOptions.addOptionLabel("Value 2", LocaleUtil.US, "Option 2");

    return ddmFormFieldOptions;
  }
示例#6
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;
  }