protected void validateDDMFormFieldPropertyValue(
      String fieldName,
      String propertyName,
      LocalizedValue propertyValue,
      Set<Locale> ddmFormAvailableLocales,
      Locale ddmFormDefaultLocale)
      throws DDMFormValidationException {

    if (!ddmFormDefaultLocale.equals(propertyValue.getDefaultLocale())) {
      throw new DDMFormValidationException(
          "Invalid default locale set for property \""
              + propertyName
              + "\" of field name "
              + fieldName);
    }

    if (!ddmFormAvailableLocales.equals(propertyValue.getAvailableLocales())) {

      throw new DDMFormValidationException(
          "Invalid available locales set for property \""
              + propertyName
              + "\" of field name "
              + fieldName);
    }
  }
  protected void assertEquals(
      LocalizedValue expectedLocalizedValue, LocalizedValue actualLocalizedValue) {

    Set<Locale> expectedAvailableLocales = expectedLocalizedValue.getAvailableLocales();

    for (Locale expectedLocale : expectedAvailableLocales) {
      String expectedValue = expectedLocalizedValue.getString(expectedLocale);

      String actualValue = actualLocalizedValue.getString(expectedLocale);

      Assert.assertEquals(expectedValue, actualValue);
    }
  }
  protected Map<Locale, Map<String, String>> getOptionLabelsMap(LocalizedValue optionLabels) {

    Map<Locale, Map<String, String>> optionLabelsMap = new HashMap<Locale, Map<String, String>>();

    for (Locale availableLocale : optionLabels.getAvailableLocales()) {
      Map<String, String> optionMetadataEntries = new HashMap<String, String>();

      optionMetadataEntries.put("label", optionLabels.getValue(availableLocale));

      optionLabelsMap.put(availableLocale, optionMetadataEntries);
    }

    return optionLabelsMap;
  }
  protected DDMFormField createTextDDMFormField(
      String name, String label, boolean localizable, boolean repeatable, boolean required) {

    DDMFormField ddmFormField = new DDMFormField(name, "text");

    ddmFormField.setDataType("string");
    ddmFormField.setLocalizable(localizable);
    ddmFormField.setRepeatable(repeatable);
    ddmFormField.setRequired(required);

    LocalizedValue localizedValue = ddmFormField.getLabel();

    localizedValue.addValue(LocaleUtil.US, label);

    return ddmFormField;
  }
  protected void setDDMFormFieldValueLocalizedValue(
      HttpServletRequest httpServletRequest,
      String ddmFormFieldParameterName,
      LocalizedValue predefinedValue,
      DDMFormFieldValue ddmFormFieldValue) {

    Locale defaultLocale = getDefaultLocale(httpServletRequest);

    Value value = new LocalizedValue(defaultLocale);

    Set<Locale> availableLocales = getAvailableLocales(httpServletRequest);

    for (Locale availableLocale : availableLocales) {
      String ddmFormFieldParameterValue =
          getDDMFormFieldParameterValue(
              httpServletRequest,
              ddmFormFieldParameterName,
              availableLocale,
              predefinedValue.getString(availableLocale));

      value.addString(availableLocale, ddmFormFieldParameterValue);
    }

    ddmFormFieldValue.setValue(value);
  }
  protected void addMetadataEntryValues(
      Map<Locale, Map<String, String>> ddmFormFieldMetadataMap,
      String entryName,
      LocalizedValue localizedValue) {

    for (Locale availableLocale : localizedValue.getAvailableLocales()) {
      Map<String, String> metadataMap = ddmFormFieldMetadataMap.get(availableLocale);

      if (metadataMap == null) {
        metadataMap = new HashMap<String, String>();

        ddmFormFieldMetadataMap.put(availableLocale, metadataMap);
      }

      metadataMap.put(entryName, localizedValue.getValue(availableLocale));
    }
  }
  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 void setDDMFormFieldValueUnlocalizedValue(
      HttpServletRequest httpServletRequest,
      String ddmFormFieldParameterName,
      LocalizedValue predefinedValue,
      DDMFormFieldValue ddmFormFieldValue) {

    Locale defaultLocale = getDefaultLocale(httpServletRequest);

    String ddmFormFieldParameterValue =
        getDDMFormFieldParameterValue(
            httpServletRequest,
            ddmFormFieldParameterName,
            defaultLocale,
            predefinedValue.getString(defaultLocale));

    Value value = new UnlocalizedValue(ddmFormFieldParameterValue);

    ddmFormFieldValue.setValue(value);
  }