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 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)); }
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()); }
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; }
@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 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; }
@Test public void testGetWithIntegerField() { ExtendedObjectClassDefinition extendedObjectClassDefinition = mock(ExtendedObjectClassDefinition.class); ExtendedAttributeDefinition extendedAttributeDefinition = mock(ExtendedAttributeDefinition.class); whenGetAttributeDefinitions( extendedObjectClassDefinition, new ExtendedAttributeDefinition[] {extendedAttributeDefinition}, ExtendedObjectClassDefinition.REQUIRED); whenGetCardinality(extendedAttributeDefinition, 0); whenGetID(extendedAttributeDefinition, "Integer"); whenGetType(extendedAttributeDefinition, ExtendedAttributeDefinition.INTEGER); 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("Integer"); Assert.assertNotNull(ddmFormField); Assert.assertEquals(DDMFormFieldType.TEXT, ddmFormField.getType()); Assert.assertEquals("integer", ddmFormField.getDataType()); Assert.assertFalse(ddmFormField.isRepeatable()); Assert.assertTrue(ddmFormField.isRequired()); LocalizedValue predefinedValue = ddmFormField.getPredefinedValue(); Assert.assertEquals(_enLocale, predefinedValue.getDefaultLocale()); Assert.assertEquals("0", predefinedValue.getString(_enLocale)); }
@Test public void testGetWithCheckboxField() { ExtendedObjectClassDefinition extendedObjectClassDefinition = mock(ExtendedObjectClassDefinition.class); ExtendedAttributeDefinition extendedAttributeDefinition = mock(ExtendedAttributeDefinition.class); whenGetAttributeDefinitions( extendedObjectClassDefinition, new ExtendedAttributeDefinition[] {extendedAttributeDefinition}, ExtendedObjectClassDefinition.REQUIRED); whenGetCardinality(extendedAttributeDefinition, 0); whenGetID(extendedAttributeDefinition, "Boolean"); whenGetType(extendedAttributeDefinition, ExtendedAttributeDefinition.BOOLEAN); 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("Boolean"); Assert.assertNotNull(ddmFormField); Assert.assertEquals(DDMFormFieldType.CHECKBOX, ddmFormField.getType()); Assert.assertEquals("boolean", ddmFormField.getDataType()); Assert.assertFalse(ddmFormField.isRepeatable()); Assert.assertFalse(ddmFormField.isRequired()); Value predefinedValue = ddmFormField.getPredefinedValue(); Assert.assertEquals("false", predefinedValue.getString(_enLocale)); }
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); } }
@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 String getDDMFormFieldType(String fieldName) { DDMFormField ddmFormField = _ddmFormFieldsMap.get(fieldName); return ddmFormField.getType(); }
protected List<Serializable> getFieldValues( DDMStructure ddmStructure, String fieldName, String fieldNamespace, ServiceContext serviceContext) throws PortalException { DDMFormField ddmFormField = ddmStructure.getDDMFormField(fieldName); String fieldDataType = ddmFormField.getDataType(); String fieldType = ddmFormField.getType(); LocalizedValue predefinedValue = ddmFormField.getPredefinedValue(); List<String> fieldNames = getFieldNames(fieldNamespace, fieldName, serviceContext); List<Serializable> fieldValues = new ArrayList<>(fieldNames.size()); for (String fieldNameValue : fieldNames) { Serializable fieldValue = serviceContext.getAttribute(fieldNameValue); if (Validator.isNull(fieldValue)) { fieldValue = predefinedValue.getString(serviceContext.getLocale()); } if (fieldType.equals(DDMImpl.TYPE_CHECKBOX) && Validator.isNull(fieldValue)) { fieldValue = "false"; } else if (fieldDataType.equals(FieldConstants.DATE)) { Date fieldValueDate = null; if (Validator.isNull(fieldValue)) { int fieldValueMonth = GetterUtil.getInteger(serviceContext.getAttribute(fieldNameValue + "Month")); int fieldValueDay = GetterUtil.getInteger(serviceContext.getAttribute(fieldNameValue + "Day")); int fieldValueYear = GetterUtil.getInteger(serviceContext.getAttribute(fieldNameValue + "Year")); fieldValueDate = PortalUtil.getDate(fieldValueMonth, fieldValueDay, fieldValueYear); } else { try { fieldValueDate = DateUtil.parseDate(String.valueOf(fieldValue), serviceContext.getLocale()); } catch (ParseException pe) { if (_log.isErrorEnabled()) { _log.error("Unable to parse date " + fieldValue); } } } if (fieldValueDate != null) { fieldValue = String.valueOf(fieldValueDate.getTime()); } } else if (fieldDataType.equals(FieldConstants.IMAGE) && Validator.isNull(fieldValue)) { HttpServletRequest request = serviceContext.getRequest(); if (request instanceof UploadRequest) { String imageFieldValue = getImageFieldValue((UploadRequest) request, fieldNameValue); if (Validator.isNotNull(imageFieldValue)) { fieldValue = imageFieldValue; } } } if (Validator.isNull(fieldValue)) { return null; } if (DDMImpl.TYPE_RADIO.equals(fieldType) || DDMImpl.TYPE_SELECT.equals(fieldType)) { String predefinedValueString = predefinedValue.getString(serviceContext.getLocale()); if (!fieldValue.equals(predefinedValueString) && (fieldValue instanceof String)) { fieldValue = new String[] {String.valueOf(fieldValue)}; fieldValue = JSONFactoryUtil.serialize(fieldValue); } } Serializable fieldValueSerializable = FieldConstants.getSerializable(fieldDataType, GetterUtil.getString(fieldValue)); fieldValues.add(fieldValueSerializable); } return fieldValues; }