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 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();
    }
  }
Example #3
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 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 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()));
  }
  protected void validateDDMFormFieldIndexType(DDMFormField ddmFormField)
      throws DDMFormValidationException {

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

      throw new MustSetValidIndexType(ddmFormField.getName());
    }
  }
Example #7
0
  protected JSONArray getDDMFormFieldsJSONArray(
      List<DDMFormField> ddmFormFields, Set<Locale> availableLocales, Locale defaultLocale) {

    JSONArray ddmFormFieldsJSONArray = JSONFactoryUtil.createJSONArray();

    for (DDMFormField ddmFormField : ddmFormFields) {
      JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

      jsonObject.put("dataType", ddmFormField.getDataType());
      jsonObject.put("id", ddmFormField.getName());
      jsonObject.put("indexType", ddmFormField.getIndexType());
      jsonObject.put("localizable", ddmFormField.isLocalizable());
      jsonObject.put("multiple", ddmFormField.isMultiple());
      jsonObject.put("name", ddmFormField.getName());
      jsonObject.put("repeatable", ddmFormField.isRepeatable());
      jsonObject.put("required", ddmFormField.isRequired());
      jsonObject.put("showLabel", ddmFormField.isShowLabel());
      jsonObject.put("type", ddmFormField.getType());

      addDDMFormFieldLocalizedProperties(jsonObject, ddmFormField, defaultLocale, defaultLocale);

      addDDMFormFieldOptions(jsonObject, ddmFormField, availableLocales, defaultLocale);

      JSONObject localizationMapJSONObject = JSONFactoryUtil.createJSONObject();

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

        addDDMFormFieldLocalizedProperties(localeMap, ddmFormField, availableLocale, defaultLocale);

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

      jsonObject.put("localizationMap", localizationMapJSONObject);

      jsonObject.put(
          "fields",
          getDDMFormFieldsJSONArray(
              ddmFormField.getNestedDDMFormFields(), availableLocales, defaultLocale));

      ddmFormFieldsJSONArray.put(jsonObject);
    }

    return ddmFormFieldsJSONArray;
  }
Example #8
0
  protected DDMFormLayoutRow getDefaultDDMFormLayoutRow(DDMFormField ddmFormField) {

    DDMFormLayoutRow ddmFormLayoutRow = new DDMFormLayoutRow();

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

    return ddmFormLayoutRow;
  }
  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 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 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 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;
  }
  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);
  }