Search in sources :

Example 31 with Input

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());
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) Input(com.enonic.xp.form.Input) FieldSet(com.enonic.xp.form.FieldSet) ContentType(com.enonic.xp.schema.content.ContentType) FormItem(com.enonic.xp.form.FormItem) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) FormItemSet(com.enonic.xp.form.FormItemSet) Test(org.junit.jupiter.api.Test)

Example 32 with Input

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());
        }
    }
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) Input(com.enonic.xp.form.Input) PropertyPath(com.enonic.xp.data.PropertyPath) Occurrences(com.enonic.xp.form.Occurrences) PropertySet(com.enonic.xp.data.PropertySet) FormItemSet(com.enonic.xp.form.FormItemSet)

Example 33 with Input

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;
}
Also used : GeoPoint(com.enonic.xp.util.GeoPoint) Input(com.enonic.xp.form.Input) FormItem(com.enonic.xp.form.FormItem) PropertyTree(com.enonic.xp.data.PropertyTree) XData(com.enonic.xp.schema.xdata.XData) ExtraData(com.enonic.xp.content.ExtraData)

Example 34 with Input

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();
}
Also used : HashMap(java.util.HashMap) FormItem(com.enonic.xp.form.FormItem) ExtraData(com.enonic.xp.content.ExtraData) XDataName(com.enonic.xp.schema.xdata.XDataName) Input(com.enonic.xp.form.Input) PropertyTree(com.enonic.xp.data.PropertyTree) ExtraDatas(com.enonic.xp.content.ExtraDatas) Collection(java.util.Collection) XData(com.enonic.xp.schema.xdata.XData) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 35 with Input

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());
}
Also used : Input(com.enonic.xp.form.Input) Form(com.enonic.xp.form.Form) FormDefaultValuesProcessor(com.enonic.xp.form.FormDefaultValuesProcessor) PropertyTree(com.enonic.xp.data.PropertyTree) FormItemSet(com.enonic.xp.form.FormItemSet) Test(org.junit.jupiter.api.Test)

Aggregations

Input (com.enonic.xp.form.Input)62 Test (org.junit.jupiter.api.Test)47 Value (com.enonic.xp.data.Value)24 Form (com.enonic.xp.form.Form)15 PropertyTree (com.enonic.xp.data.PropertyTree)11 FormItem (com.enonic.xp.form.FormItem)9 FormItemSet (com.enonic.xp.form.FormItemSet)9 FormDefaultValuesProcessor (com.enonic.xp.form.FormDefaultValuesProcessor)7 ContentType (com.enonic.xp.schema.content.ContentType)7 FormOptionSet (com.enonic.xp.form.FormOptionSet)5 FieldSet (com.enonic.xp.form.FieldSet)4 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)4 PatternIndexConfigDocument (com.enonic.xp.index.PatternIndexConfigDocument)4 Property (com.enonic.xp.data.Property)3 Occurrences (com.enonic.xp.form.Occurrences)3 Content (com.enonic.xp.content.Content)2 ExtraData (com.enonic.xp.content.ExtraData)2 ValidationErrors (com.enonic.xp.content.ValidationErrors)2 PropertyPath (com.enonic.xp.data.PropertyPath)2 PropertySet (com.enonic.xp.data.PropertySet)2