use of com.enonic.xp.form.Input in project xp by enonic.
the class XmlContentTypeParserTest method testI18nParseOmittingElementValue.
@Test
public void testI18nParseOmittingElementValue() throws Exception {
parse(this.parser, "-i18n-only-attribute.xml");
final ContentType result = this.builder.build();
assertEquals("translated.display-name", result.getDisplayNameI18nKey());
assertEquals("translated.description", result.getDescriptionI18nKey());
// input type
final Input input = (Input) result.getForm().getFormItem("textLine");
assertEquals("translated.label", input.getLabelI18nKey());
assertEquals("translated.label", input.getLabel());
assertEquals("translated.help-text", input.getHelpTextI18nKey());
assertEquals("translated.help-text", input.getHelpText());
// option set
final FormItem item = result.getForm().getFormItem("radioOptionSet");
assertNotNull(item);
final FormOptionSet radioOptionSet = (FormOptionSet) item;
assertEquals(FormItemType.FORM_OPTION_SET, radioOptionSet.getType());
assertEquals("translated.help-text", radioOptionSet.getHelpTextI18nKey());
assertEquals("translated.help-text", radioOptionSet.getHelpText());
assertEquals("translated.label", radioOptionSet.getLabelI18nKey());
assertEquals("translated.label", radioOptionSet.getLabel());
final Input inputInsideOption = radioOptionSet.getFormItems().getInput(FormItemPath.from("option_1.text-input"));
assertEquals("translated.help-text", inputInsideOption.getHelpTextI18nKey());
assertEquals("translated.help-text", inputInsideOption.getHelpText());
assertEquals("translated.label", inputInsideOption.getLabelI18nKey());
assertEquals("translated.label", inputInsideOption.getLabel());
final FormOptionSetOption radioOption = radioOptionSet.getFormItems().getItemByName("option_1").toFormOptionSetOption();
assertEquals("translated.help-text", radioOption.getHelpTextI18nKey());
assertEquals("translated.help-text", radioOption.getHelpText());
assertEquals("translated.label", radioOption.getLabelI18nKey());
assertEquals("translated.label", radioOption.getLabel());
// field set
FormItem fieldSetItem = null;
for (final FormItem next : result.getForm().getFormItems()) {
if (next.getName().startsWith("fieldSet")) {
fieldSetItem = next;
}
}
assertNotNull(fieldSetItem);
final FieldSet fieldSet = (FieldSet) fieldSetItem;
assertEquals("translated.label", fieldSet.getLabelI18nKey());
assertEquals("translated.label", fieldSet.getLabel());
final Input inputInsideFieldSet = fieldSet.getFormItems().getInput(FormItemPath.from("textLine2"));
assertEquals("translated.help-text", inputInsideFieldSet.getHelpTextI18nKey());
assertEquals("translated.help-text", inputInsideFieldSet.getHelpText());
assertEquals("translated.label", inputInsideFieldSet.getLabelI18nKey());
assertEquals("translated.label", inputInsideFieldSet.getLabel());
// item set
final FormItem itemSet = result.getForm().getFormItem("item-set");
assertNotNull(item);
final FormItemSet formItemSet = (FormItemSet) itemSet;
assertEquals("translated.label", formItemSet.getLabelI18nKey());
assertEquals("translated.label", formItemSet.getLabel());
assertEquals("translated.help-text", formItemSet.getHelpTextI18nKey());
assertEquals("translated.help-text", formItemSet.getHelpText());
final Input inputInsideFormItemSet = formItemSet.getFormItems().getInput(FormItemPath.from("textLine1"));
assertEquals("translated.help-text", inputInsideFormItemSet.getHelpTextI18nKey());
assertEquals("translated.help-text", inputInsideFormItemSet.getHelpText());
assertEquals("translated.label", inputInsideFormItemSet.getLabelI18nKey());
assertEquals("translated.label", inputInsideFormItemSet.getLabel());
}
use of com.enonic.xp.form.Input in project xp by enonic.
the class OccurrenceValidator method validateOccurrences.
private static void validateOccurrences(final FormItem formItem, final List<PropertySet> parentDataSets, final ValidationErrors.Builder validationErrorsBuilder) {
final Occurrences occurrences;
if (formItem instanceof Input) {
occurrences = ((Input) formItem).getOccurrences();
} else if (formItem instanceof FormItemSet) {
occurrences = ((FormItemSet) formItem).getOccurrences();
} else if (formItem instanceof FormOptionSet) {
occurrences = ((FormOptionSet) formItem).getOccurrences();
} else {
return;
}
for (final PropertySet parentDataSet : parentDataSets) {
final int entryCount = parentDataSet.countProperties(formItem.getName());
final PropertyPath parentPath = Optional.ofNullable(parentDataSet.getProperty()).map(Property::getPath).orElseGet(() -> PropertyPath.from(formItem.getPath().getParent().toString()));
final PropertyPath path = PropertyPath.from(parentPath, formItem.getName());
final int minOccurrences = occurrences.getMinimum();
if (occurrences.impliesRequired() && entryCount < minOccurrences) {
validationErrorsBuilder.add(ValidationError.dataError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "cms.occurrencesInvalid"), path).i18n("system.cms.validation.minOccurrencesInvalid").args(formItem.getPath(), minOccurrences, entryCount).build());
}
final int maxOccurrences = occurrences.getMaximum();
if (maxOccurrences > 0 && entryCount > maxOccurrences) {
validationErrorsBuilder.add(ValidationError.dataError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "cms.occurrencesInvalid"), path).i18n("system.cms.validation.maxOccurrencesInvalid").args(formItem.getPath(), occurrences.getMaximum(), entryCount).build());
}
}
}
use of com.enonic.xp.form.Input in project xp by enonic.
the class ImageContentProcessor method extractGeoLocation.
private ExtraData extractGeoLocation(final MediaInfo mediaInfo, final XDatas xDatas) {
final ImmutableMultimap<String, String> mediaItems = mediaInfo.getMetadata();
final Double geoLat = parseDouble(mediaItems.get(GEO_LATITUDE).stream().findFirst().orElse(null));
final Double geoLong = parseDouble(mediaItems.get(GEO_LONGITUDE).stream().findFirst().orElse(null));
if (geoLat == null || geoLong == null) {
return null;
}
final XData geoMixin = xDatas.getXData(MediaInfo.GPS_INFO_METADATA_NAME);
if (geoMixin == null) {
return null;
}
final ExtraData extraData = new ExtraData(geoMixin.getName(), new PropertyTree());
final FormItem formItem = geoMixin.getForm().getFormItems().getItemByName(MediaInfo.GPS_INFO_GEO_POINT);
if (FormItemType.INPUT.equals(formItem.getType())) {
final Input input = (Input) formItem;
if (InputTypeName.GEO_POINT.equals(input.getInputType())) {
final GeoPoint geoPoint = new GeoPoint(geoLat, geoLong);
extraData.getData().addGeoPoint(formItem.getName(), ValueTypes.GEO_POINT.convert(geoPoint));
}
}
return extraData;
}
use of com.enonic.xp.form.Input in project xp by enonic.
the class ImageContentProcessor method extractMetadata.
private ExtraDatas extractMetadata(final MediaInfo mediaInfo, final XDatas xDatas, final CreateAttachment sourceAttachment) {
final ExtraDatas.Builder extradatasBuilder = ExtraDatas.create();
final Map<XDataName, ExtraData> metadataMap = new HashMap<>();
final ExtraData geoData = extractGeoLocation(mediaInfo, xDatas);
if (geoData != null) {
metadataMap.put(MediaInfo.GPS_INFO_METADATA_NAME, geoData);
extradatasBuilder.add(geoData);
}
for (Map.Entry<String, Collection<String>> entry : mediaInfo.getMetadata().asMap().entrySet()) {
for (XData xData : xDatas) {
final String formItemName = getConformityName(entry.getKey());
final FormItem formItem = xData.getForm().getFormItems().getItemByName(formItemName);
if (formItem == null) {
continue;
}
ExtraData extraData = metadataMap.get(xData.getName());
if (extraData == null) {
extraData = new ExtraData(xData.getName(), new PropertyTree());
metadataMap.put(xData.getName(), extraData);
extradatasBuilder.add(extraData);
}
if (FormItemType.INPUT.equals(formItem.getType())) {
Input input = (Input) formItem;
if (InputTypeName.DATE_TIME.equals(input.getInputType())) {
extraData.getData().addLocalDateTime(formItemName, ValueTypes.LOCAL_DATE_TIME.convert(entry.getValue().toArray()[0]));
} else if (InputTypeName.LONG.equals(input.getInputType())) {
final Long[] longValues = entry.getValue().stream().map(Long::parseLong).toArray(Long[]::new);
extraData.getData().addLongs(formItemName, longValues);
} else {
extraData.getData().addStrings(formItemName, entry.getValue());
}
}
}
}
fillComputedFormItems(metadataMap.values(), mediaInfo, sourceAttachment);
return extradatasBuilder.build();
}
use of com.enonic.xp.form.Input in project xp by enonic.
the class FormDefaultValuesProcessorImplTest method testItemSetWithMinOccurrencesMoreEqualZero.
@Test
public void testItemSetWithMinOccurrencesMoreEqualZero() {
FormItemSet.Builder checkItemSet = FormItemSet.create().name("checkItemSet").occurrences(Occurrences.create(0, 3));
Input input = Input.create().name("testInput").label("testInput").inputType(InputTypeName.TEXT_LINE).defaultValue(InputTypeDefault.create().property(InputTypeProperty.create("default", "Default Value").build()).build()).occurrences(Occurrences.create(3, 3)).build();
checkItemSet.addFormItem(input);
final Form form = Form.create().addFormItem(checkItemSet.build()).build();
final FormDefaultValuesProcessor defaultValuesProcessor = new FormDefaultValuesProcessorImpl();
final PropertyTree data = new PropertyTree();
defaultValuesProcessor.setDefaultValues(form, data);
assertEquals(0, data.getTotalSize());
}
Aggregations