protected List<DDMFormFieldValue> getDDMFormFieldValues(
      Map<String, DDMFormFieldValue> ddmFormFieldValuesMap, List<DDMFormField> ddmFormFields) {

    List<DDMFormFieldValue> ddmFormFieldValues = new ArrayList<>();

    int i = 0;

    for (DDMFormField ddmFormField : ddmFormFields) {
      Set<String> entryKeys =
          getEntryKeys(ddmFormFieldValuesMap, StringPool.BLANK, ddmFormField.getName());

      for (String entryKey : entryKeys) {
        DDMFormFieldValue ddmFormFieldValue = ddmFormFieldValuesMap.get(entryKey);

        int index = i + getDDMFormFieldValueIndex(entryKey);

        setNestedDDMFormFieldValues(
            ddmFormFieldValuesMap,
            ddmFormField.getNestedDDMFormFields(),
            ddmFormFieldValue,
            entryKey);

        setDDMFormFieldValueAtIndex(ddmFormFieldValues, ddmFormFieldValue, index);
      }

      i = ddmFormFieldValues.size();
    }

    return ddmFormFieldValues;
  }
  protected void validateDDMFormFields(
      List<DDMFormField> ddmFormFields,
      Set<String> ddmFormFieldNames,
      Set<Locale> ddmFormAvailableLocales,
      Locale ddmFormDefaultLocale)
      throws DDMFormValidationException {

    for (DDMFormField ddmFormField : ddmFormFields) {
      validateDDMFormFieldName(ddmFormField, ddmFormFieldNames);

      validateDDMFormFieldType(ddmFormField);

      validateDDMFormFieldIndexType(ddmFormField);

      validateDDMFormFieldOptions(ddmFormField, ddmFormAvailableLocales, ddmFormDefaultLocale);

      validateOptionalDDMFormFieldLocalizedProperty(
          ddmFormField, "label", ddmFormAvailableLocales, ddmFormDefaultLocale);

      validateOptionalDDMFormFieldLocalizedProperty(
          ddmFormField, "predefinedValue", ddmFormAvailableLocales, ddmFormDefaultLocale);

      validateOptionalDDMFormFieldLocalizedProperty(
          ddmFormField, "tip", ddmFormAvailableLocales, ddmFormDefaultLocale);

      validateDDMFormFieldVisibilityExpression(ddmFormField);

      validateDDMFormFields(
          ddmFormField.getNestedDDMFormFields(),
          ddmFormFieldNames,
          ddmFormAvailableLocales,
          ddmFormDefaultLocale);
    }
  }
  protected void setNestedDDMFormFieldValues(
      Map<String, DDMFormFieldValue> ddmFormFieldValuesMap,
      List<DDMFormField> nestedDDMFormFields,
      DDMFormFieldValue parentDDMFormFieldValue,
      String parentEntryKey) {

    int i = 0;

    for (DDMFormField nestedDDMFormField : nestedDDMFormFields) {
      Set<String> entryKeys =
          getEntryKeys(ddmFormFieldValuesMap, parentEntryKey, nestedDDMFormField.getName());

      for (String entryKey : entryKeys) {
        DDMFormFieldValue ddmFormFieldValue = ddmFormFieldValuesMap.get(entryKey);

        int index = i + getDDMFormFieldValueIndex(entryKey);

        setNestedDDMFormFieldValues(
            ddmFormFieldValuesMap,
            nestedDDMFormField.getNestedDDMFormFields(),
            ddmFormFieldValue,
            entryKey);

        setDDMFormFieldValueAtIndex(
            parentDDMFormFieldValue.getNestedDDMFormFieldValues(), ddmFormFieldValue, index);
      }

      List<DDMFormFieldValue> parentNestedDDMFormFieldValues =
          parentDDMFormFieldValue.getNestedDDMFormFieldValues();

      i = parentNestedDDMFormFieldValues.size();
    }
  }
  @Test
  public void testCreateWithValidDDMFieldValue() throws Exception {
    DDMStructure structure = addStructure(_CLASS_NAME_ID, "Default Structure");

    DDMForm ddmForm = DDMFormTestUtil.createDDMForm();

    DDMFormField ddmFormField = DDMFormTestUtil.createTextDDMFormField("text", false, false, false);

    DDMFormFieldValidation ddmFormFieldValidation = new DDMFormFieldValidation();

    ddmFormFieldValidation.setExpression("!text.isEmpty()");
    ddmFormFieldValidation.setErrorMessage("custom validation error message");

    ddmFormField.setDDMFormFieldValidation(ddmFormFieldValidation);

    ddmForm.addDDMFormField(ddmFormField);

    DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues(ddmForm);

    DDMFormFieldValue ddmFormFieldValue =
        DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue("text", "not empty");

    ddmFormValues.addDDMFormFieldValue(ddmFormFieldValue);

    _jsonStorageAdapter.create(
        TestPropsValues.getCompanyId(),
        structure.getStructureId(),
        ddmFormValues,
        ServiceContextTestUtil.getServiceContext(group.getGroupId()));
  }
Example #5
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);
  }
  protected void validateDDMFormFieldIndexType(DDMFormField ddmFormField)
      throws DDMFormValidationException {

    if (!ArrayUtil.contains(_ddmFormFieldIndexTypes, ddmFormField.getIndexType())) {

      throw new MustSetValidIndexType(ddmFormField.getName());
    }
  }
  protected void createNotEmptyValidation(DDMFormField ddmFormField) {
    DDMFormFieldValidation ddmFormFieldValidation = new DDMFormFieldValidation();

    ddmFormFieldValidation.setErrorMessage(
        "Field " + ddmFormField.getName() + " must not be empty.");
    ddmFormFieldValidation.setExpression("!" + ddmFormField.getName() + ".isEmpty()");

    ddmFormField.setDDMFormFieldValidation(ddmFormFieldValidation);
  }
  protected DDMFormField createNestedDDMFormFields(String parentName, String childName) {

    DDMFormField parentDDMFormField = createTextDDMFormField(parentName);

    List<DDMFormField> nestedDDMFormFields = new ArrayList<>();

    nestedDDMFormFields.add(createSelectDDMFormField(childName));

    parentDDMFormField.setNestedDDMFormFields(nestedDDMFormFields);

    return parentDDMFormField;
  }
  protected void validateDDMFormFieldType(DDMFormField ddmFormField)
      throws DDMFormValidationException {

    if (Validator.isNull(ddmFormField.getType())) {
      throw new MustSetFieldType(ddmFormField.getName());
    }

    Matcher matcher = _ddmFormFieldTypePattern.matcher(ddmFormField.getType());

    if (!matcher.matches()) {
      throw new MustSetValidCharactersForFieldType(ddmFormField.getType());
    }
  }
  protected DDMForm getDDMForm(DDMStructure ddmStructure, boolean requireCaptcha) {

    DDMForm ddmForm = ddmStructure.getDDMForm();

    if (requireCaptcha) {
      DDMFormField captchaDDMFormField = new DDMFormField(_DDM_FORM_FIELD_NAME_CAPTCHA, "captcha");

      captchaDDMFormField.setDataType("string");
      captchaDDMFormField.setProperty("url", createCaptchaResourceURL());

      ddmForm.addDDMFormField(captchaDDMFormField);
    }

    return ddmForm;
  }
  protected void validateDDMFormFieldVisibilityExpression(DDMFormField ddmFormField)
      throws DDMFormValidationException {

    String visibilityExpression = ddmFormField.getVisibilityExpression();

    if (Validator.isNull(visibilityExpression)) {
      return;
    }

    try {
      _ddmExpressionFactory.createBooleanDDMExpression(visibilityExpression);
    } catch (DDMExpressionException ddmee) {
      throw new MustSetValidVisibilityExpression(ddmFormField.getName(), visibilityExpression);
    }
  }
  protected Serializable toSimpleValue(
      DDMFormField ddmFormField, DDMFormFieldValue ddmFormFieldValue) {

    Value value = ddmFormFieldValue.getValue();

    return FieldConstants.getSerializable(ddmFormField.getDataType(), value.getString(_locale));
  }
  protected void validateDDMFormFieldName(DDMFormField ddmFormField, Set<String> ddmFormFieldNames)
      throws DDMFormValidationException {

    Matcher matcher = _ddmFormFieldNamePattern.matcher(ddmFormField.getName());

    if (!matcher.matches()) {
      throw new MustSetValidCharactersForFieldName(ddmFormField.getName());
    }

    if (ddmFormFieldNames.contains(StringUtil.toLowerCase(ddmFormField.getName()))) {

      throw new MustNotDuplicateFieldName(ddmFormField.getName());
    }

    ddmFormFieldNames.add(StringUtil.toLowerCase(ddmFormField.getName()));
  }
  @Test
  public void testCreateWithInvalidDDMFieldValue() throws Exception {
    DDMStructure structure = addStructure(_CLASS_NAME_ID, "Default Structure");

    DDMForm ddmForm = DDMFormTestUtil.createDDMForm();

    DDMFormField ddmFormField = DDMFormTestUtil.createTextDDMFormField("text", false, false, false);

    DDMFormFieldValidation ddmFormFieldValidation = new DDMFormFieldValidation();

    ddmFormFieldValidation.setExpression("text.contains(\"test\")");
    ddmFormFieldValidation.setErrorMessage("custom validation error message");

    ddmFormField.setDDMFormFieldValidation(ddmFormFieldValidation);

    ddmForm.addDDMFormField(ddmFormField);

    DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues(ddmForm);

    DDMFormFieldValue ddmFormFieldValue =
        DDMFormValuesTestUtil.createUnlocalizedDDMFormFieldValue("text", "text value");

    ddmFormValues.addDDMFormFieldValue(ddmFormFieldValue);

    try {
      _jsonStorageAdapter.create(
          TestPropsValues.getCompanyId(),
          structure.getStructureId(),
          ddmFormValues,
          ServiceContextTestUtil.getServiceContext(group.getGroupId()));

      Assert.fail();
    } catch (DDMFormValuesValidationException.MustSetValidValues msvv) {
      List<DDMFormFieldEvaluationResult> ddmFormFieldEvaluationResults =
          msvv.getDDMFormFieldEvaluationResults();

      Assert.assertEquals(1, ddmFormFieldEvaluationResults.size());

      DDMFormFieldEvaluationResult ddmFormFieldEvaluationResult =
          ddmFormFieldEvaluationResults.get(0);

      Assert.assertEquals("text", ddmFormFieldEvaluationResult.getName());
      Assert.assertEquals(
          "custom validation error message", ddmFormFieldEvaluationResult.getErrorMessage());
    }
  }
Example #15
0
  protected DDMFormLayoutRow getDefaultDDMFormLayoutRow(DDMFormField ddmFormField) {

    DDMFormLayoutRow ddmFormLayoutRow = new DDMFormLayoutRow();

    ddmFormLayoutRow.addDDMFormLayoutColumn(
        new DDMFormLayoutColumn(DDMFormLayoutColumn.FULL, ddmFormField.getName()));

    return ddmFormLayoutRow;
  }
  protected void poupulateDefaultDDMFormFieldParameterNames(
      List<DDMFormField> ddmFormFields,
      String parentDefaultDDMFormFieldParameterName,
      Set<String> defaultDDMFormFieldParameterNames) {

    for (DDMFormField ddmFormField : ddmFormFields) {
      String defaultDDMFormFieldParameterName =
          createDefaultDDMFormFieldParameterName(
              ddmFormField, parentDefaultDDMFormFieldParameterName);

      defaultDDMFormFieldParameterNames.add(defaultDDMFormFieldParameterName);

      poupulateDefaultDDMFormFieldParameterNames(
          ddmFormField.getNestedDDMFormFields(),
          defaultDDMFormFieldParameterName,
          defaultDDMFormFieldParameterNames);
    }
  }
  protected DDMFormField createRadioDDMFormField(String name) {
    DDMFormField ddmFormField = new DDMFormField(name, "radio");

    ddmFormField.setDataType("string");
    ddmFormField.setDDMFormFieldOptions(createDDMFormFieldOptions());
    ddmFormField.setLocalizable(false);
    ddmFormField.setReadOnly(false);
    ddmFormField.setRepeatable(false);
    ddmFormField.setRequired(true);
    ddmFormField.setShowLabel(false);
    ddmFormField.setVisibilityExpression("false");

    createNotEmptyValidation(ddmFormField);

    return ddmFormField;
  }
  protected void checkDDMFormFieldParameterNames(
      List<DDMFormField> ddmFormFields,
      String parentDDMFormFieldParameterName,
      Set<String> ddmFormFieldParameterNames) {

    for (DDMFormField ddmFormField : ddmFormFields) {
      if (ddmFormField.isTransient()) {
        continue;
      }

      if (containsDDMFormFieldParameterName(ddmFormField, ddmFormFieldParameterNames)) {

        continue;
      }

      String defaultDDMFormFieldParameterName =
          createDefaultDDMFormFieldParameterName(ddmFormField, parentDDMFormFieldParameterName);

      ddmFormFieldParameterNames.add(defaultDDMFormFieldParameterName);
    }
  }
  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);
    }
  }
  public Map<String, Object> getProperties() {
    Map<String, Object> properties = new HashMap<>();

    for (DDMFormField ddmFormField : _ddmForm.getDDMFormFields()) {
      String methodName = _ddmFormFieldMethodNameMap.get(ddmFormField.getName());

      Object value = null;

      List<DDMFormFieldValue> ddmFormFieldValues =
          _ddmFormFieldValuesMap.get(ddmFormField.getName());

      if (ddmFormField.isRepeatable()) {
        value = toArrayValue(ddmFormField, ddmFormFieldValues);
      } else {
        value = toSimpleValue(ddmFormField, ddmFormFieldValues.get(0));
      }

      properties.put(methodName, value);
    }

    return properties;
  }
  protected Object toArrayValue(
      DDMFormField ddmFormField, List<DDMFormFieldValue> ddmFormFieldValues) {

    List<Serializable> values = new ArrayList<>();

    for (DDMFormFieldValue ddmFormFieldValue : ddmFormFieldValues) {
      Serializable value = toSimpleValue(ddmFormField, ddmFormFieldValue);

      values.add(value);
    }

    return FieldConstants.getSerializable(ddmFormField.getDataType(), values);
  }
  @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));
  }
  @Test
  public void testGetWithTextField() {
    ExtendedObjectClassDefinition extendedObjectClassDefinition =
        mock(ExtendedObjectClassDefinition.class);

    ExtendedAttributeDefinition extendedAttributeDefinition =
        mock(ExtendedAttributeDefinition.class);

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

    whenGetCardinality(extendedAttributeDefinition, 0);
    whenGetID(extendedAttributeDefinition, "Text");
    whenGetType(extendedAttributeDefinition, ExtendedAttributeDefinition.STRING);

    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("Text");

    Assert.assertNotNull(ddmFormField);
    Assert.assertEquals(DDMFormFieldType.TEXT, ddmFormField.getType());
    Assert.assertEquals("string", ddmFormField.getDataType());
    Assert.assertFalse(ddmFormField.isRepeatable());
    Assert.assertFalse(ddmFormField.isRequired());
  }
  protected boolean containsDDMFormFieldParameterName(
      DDMFormField ddmFormField, Set<String> ddmFormFieldParameterNames) {

    for (String ddmFormFieldParameterName : ddmFormFieldParameterNames) {
      String[] ddmFormFieldParameterNameParts =
          getDDMFormFieldParameterNameParts(ddmFormFieldParameterName);

      String fieldName = getFieldName(ddmFormFieldParameterNameParts);

      if (fieldName.equals(ddmFormField.getName())) {
        return true;
      }
    }

    return false;
  }
Example #25
0
  @Test
  public void testCreateCheckboxDDMFormFieldTypeSettingsDDMForm() {
    DDMForm ddmForm = DDMFormFactory.create(CheckboxDDMFormFieldTypeSettings.class);

    List<DDMFormRule> ddmFormRules = ddmForm.getDDMFormRules();

    Assert.assertEquals(1, ddmFormRules.size());

    DDMFormRule ddmFormRule = ddmFormRules.get(0);

    Assert.assertEquals("TRUE", ddmFormRule.getCondition());

    List<String> ddmFormRuleActions = ddmFormRule.getActions();

    Assert.assertArrayEquals(
        new String[] {
          "set(fieldAt(\"repeatable\", 0), \"visible\", false)",
          "set(fieldAt(\"validation\", 0), \"visible\", false)"
        },
        ddmFormRuleActions.toArray());

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

    DDMFormField predefinedValueDDMFormField = ddmFormFieldsMap.get("predefinedValue");

    Assert.assertNotNull(predefinedValueDDMFormField);
    Assert.assertEquals("boolean", predefinedValueDDMFormField.getDataType());
    Assert.assertEquals("checkbox", predefinedValueDDMFormField.getType());
    Assert.assertEquals(true, predefinedValueDDMFormField.isLocalizable());
    Assert.assertEquals("true", predefinedValueDDMFormField.getProperty("showAsSwitcher"));

    DDMFormField showAsSwitcherDDMFormField = ddmFormFieldsMap.get("showAsSwitcher");

    Assert.assertNotNull(showAsSwitcherDDMFormField);
    Assert.assertEquals("checkbox", showAsSwitcherDDMFormField.getType());
    Assert.assertEquals("boolean", showAsSwitcherDDMFormField.getDataType());
    Assert.assertEquals("true", showAsSwitcherDDMFormField.getProperty("showAsSwitcher"));
  }
  protected String createDefaultDDMFormFieldParameterName(
      DDMFormField ddmFormField, String parentDefaultDDMFormFieldParameterName) {

    StringBundler sb = new StringBundler(7);

    if (Validator.isNotNull(parentDefaultDDMFormFieldParameterName)) {
      sb.append(parentDefaultDDMFormFieldParameterName);
      sb.append(DDMFormRendererConstants.DDM_FORM_FIELDS_SEPARATOR);
    }

    sb.append(ddmFormField.getName());
    sb.append(DDMFormRendererConstants.DDM_FORM_FIELD_PARTS_SEPARATOR);
    sb.append(StringUtil.randomString());
    sb.append(DDMFormRendererConstants.DDM_FORM_FIELD_PARTS_SEPARATOR);
    sb.append(0);

    return sb.toString();
  }
  protected void validateOptionalDDMFormFieldLocalizedProperty(
      DDMFormField ddmFormField,
      String propertyName,
      Set<Locale> ddmFormAvailableLocales,
      Locale ddmFormDefaultLocale)
      throws DDMFormValidationException {

    LocalizedValue propertyValue =
        (LocalizedValue) BeanPropertiesUtil.getObject(ddmFormField, propertyName);

    if (MapUtil.isEmpty(propertyValue.getValues())) {
      return;
    }

    validateDDMFormFieldPropertyValue(
        ddmFormField.getName(),
        propertyName,
        propertyValue,
        ddmFormAvailableLocales,
        ddmFormDefaultLocale);
  }
  protected String renderCaptchaTag(
      DDMFormField ddmFormField, DDMFormFieldRenderingContext ddmFormFieldRenderingContext)
      throws Exception {

    CaptchaTag captchaTag = new CaptchaTag();

    captchaTag.setUrl(GetterUtil.getString(ddmFormField.getProperty("url")));

    JspFactory jspFactory = JspFactory.getDefaultFactory();

    HttpServletRequest httpServletRequest = ddmFormFieldRenderingContext.getHttpServletRequest();
    HttpServletResponse httpServletResponse = ddmFormFieldRenderingContext.getHttpServletResponse();

    PageContext pageContext =
        jspFactory.getPageContext(
            new JSPSupportServlet(httpServletRequest.getServletContext()),
            httpServletRequest,
            httpServletResponse,
            null,
            false,
            0,
            false);

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    unsyncStringWriter.append("<div class=\"form-group\" data-fieldname=\"");
    unsyncStringWriter.append(ddmFormFieldRenderingContext.getName());
    unsyncStringWriter.append("\">");

    captchaTag.setPageContext(new PipingPageContext(pageContext, unsyncStringWriter));

    captchaTag.runTag();

    unsyncStringWriter.append("</div>");

    StringBundler sb = unsyncStringWriter.getStringBundler();

    return sb.toString();
  }
  protected DDMFormFieldValue createDDMFormFieldValue(
      HttpServletRequest httpServletRequest,
      String ddmFormFieldParameterName,
      Map<String, DDMFormField> ddmFormFieldsMap) {

    DDMFormFieldValue ddmFormFieldValue = new DDMFormFieldValue();

    String[] lastDDMFormFieldParameterNameParts =
        getLastDDMFormFieldParameterNameParts(ddmFormFieldParameterName);

    String fieldName = getFieldName(lastDDMFormFieldParameterNameParts);

    ddmFormFieldValue.setName(fieldName);
    ddmFormFieldValue.setInstanceId(getFieldInstanceId(lastDDMFormFieldParameterNameParts));

    DDMFormField ddmFormField = ddmFormFieldsMap.get(fieldName);

    if (ddmFormField.isTransient()) {
      return ddmFormFieldValue;
    }

    if (ddmFormField.isLocalizable()) {
      setDDMFormFieldValueLocalizedValue(
          httpServletRequest,
          ddmFormField.getType(),
          ddmFormFieldParameterName,
          ddmFormField.getPredefinedValue(),
          ddmFormFieldValue);
    } else {
      setDDMFormFieldValueUnlocalizedValue(
          httpServletRequest,
          ddmFormField.getType(),
          ddmFormFieldParameterName,
          ddmFormField.getPredefinedValue(),
          ddmFormFieldValue);
    }

    return ddmFormFieldValue;
  }
Example #30
0
  protected void addDDMFormFieldLocalizedProperties(
      JSONObject jsonObject, DDMFormField ddmFormField, Locale locale, Locale defaultLocale) {

    addDDMFormFieldLocalizedProperty(
        jsonObject,
        "label",
        ddmFormField.getLabel(),
        locale,
        defaultLocale,
        ddmFormField.getType());
    addDDMFormFieldLocalizedProperty(
        jsonObject,
        "predefinedValue",
        ddmFormField.getPredefinedValue(),
        locale,
        defaultLocale,
        ddmFormField.getType());
    addDDMFormFieldLocalizedProperty(
        jsonObject, "tip", ddmFormField.getTip(), locale, defaultLocale, ddmFormField.getType());
  }