Search in sources :

Example 6 with FormOptionSetOption

use of com.enonic.xp.form.FormOptionSetOption in project xp by enonic.

the class FormDefaultValuesProcessorImpl method processFormItems.

private void processFormItems(final Iterable<FormItem> formItems, final PropertySet dataSet) {
    StreamSupport.stream(formItems.spliterator(), false).forEach(formItem -> {
        if (formItem.getType() == INPUT) {
            Input input = formItem.toInput();
            if (input.getDefaultValue() != null) {
                try {
                    final Value defaultValue = InputTypes.BUILTIN.resolve(input.getInputType()).createDefaultValue(input);
                    final PropertyPath propertyPath = PropertyPath.from(input.getName());
                    if (defaultValue != null && dataSet.getProperty(propertyPath) == null) {
                        if (input.getOccurrences().getMinimum() > 0) {
                            for (int i = 0; i < input.getOccurrences().getMinimum(); i++) {
                                dataSet.setProperty(input.getName(), i, defaultValue);
                            }
                        } else {
                            dataSet.setProperty(input.getName(), defaultValue);
                        }
                    }
                } catch (IllegalArgumentException ex) {
                    LOG.warn("Invalid default value for " + input.getInputType() + " input type with name '" + input.getName() + "': '" + input.getDefaultValue().getRootValue() + "'" + (ex.getMessage() == null ? "" : " - " + ex.getMessage()));
                }
            }
        } else if (formItem.getType() == FORM_ITEM_SET) {
            processFormItems(formItem.getName(), formItem.toFormItemSet().getFormItems(), dataSet, formItem.toFormItemSet().getOccurrences().getMinimum());
        } else if (formItem.getType() == LAYOUT && formItem.toLayout() instanceof FieldSet) {
            processFormItems((FieldSet) formItem.toLayout(), dataSet);
        } else if (formItem.getType() == FORM_OPTION_SET_OPTION) {
            FormOptionSetOption option = formItem.toFormOptionSetOption();
            if (option.isDefaultOption()) {
                dataSet.setProperty("_selected", ValueFactory.newString(formItem.getName()));
                if (dataSet.getProperty(formItem.getName()) == null) {
                    Property property = dataSet.setProperty(formItem.getName(), ValueFactory.newPropertySet(new PropertySet()));
                    processFormItems(option.getFormItems(), property.getSet());
                }
            }
        } else if (formItem.getType() == FORM_OPTION_SET) {
            processFormItems(formItem.getName(), formItem.toFormOptionSet().getFormItems(), dataSet, formItem.toFormOptionSet().getOccurrences().getMinimum());
        }
    });
}
Also used : Input(com.enonic.xp.form.Input) FieldSet(com.enonic.xp.form.FieldSet) Value(com.enonic.xp.data.Value) PropertyPath(com.enonic.xp.data.PropertyPath) PropertySet(com.enonic.xp.data.PropertySet) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) Property(com.enonic.xp.data.Property)

Example 7 with FormOptionSetOption

use of com.enonic.xp.form.FormOptionSetOption in project xp by enonic.

the class MixinServiceImpl method transformFormItems.

private List<FormItem> transformFormItems(final Iterable<FormItem> iterable, final Set<MixinName> inlineMixinStack) {
    final List<FormItem> formItems = new ArrayList<>();
    for (final FormItem formItem : iterable) {
        if (formItem instanceof InlineMixin) {
            final InlineMixin inline = (InlineMixin) formItem;
            final MixinName mixinName = inline.getMixinName();
            final Mixin mixin = getByName(mixinName);
            if (mixin != null) {
                if (inlineMixinStack.contains(mixinName)) {
                    final String error = "Cycle detected in mixin [" + mixin.getName() + "]. It contains an inline mixin that references itself.";
                    LOG.error(error);
                    throw new IllegalArgumentException(error);
                }
                inlineMixinStack.add(mixinName);
                final Form mixinForm = doInlineFormItems(mixin.getForm(), inlineMixinStack);
                inlineMixinStack.remove(mixinName);
                for (final FormItem mixinFormItem : mixinForm) {
                    formItems.add(mixinFormItem.copy());
                }
            } else {
                throw new IllegalArgumentException("Inline mixin [" + mixinName + "] not found");
            }
        } else if (formItem instanceof FormItemSet) {
            final FormItemSet.Builder formItemSetBuilder = FormItemSet.create((FormItemSet) formItem);
            formItemSetBuilder.clearFormItems();
            formItemSetBuilder.addFormItems(transformFormItems((FormItemSet) formItem, inlineMixinStack));
            formItems.add(formItemSetBuilder.build());
        } else if (formItem instanceof FieldSet) {
            final FieldSet.Builder formItemSetBuilder = FieldSet.create((FieldSet) formItem);
            formItemSetBuilder.clearFormItems();
            formItemSetBuilder.addFormItems(transformFormItems((FieldSet) formItem, inlineMixinStack));
            formItems.add(formItemSetBuilder.build());
        } else if (formItem instanceof FormOptionSet) {
            final FormOptionSet.Builder formOptionSetBuilder = FormOptionSet.create((FormOptionSet) formItem);
            formOptionSetBuilder.clearOptions();
            for (FormOptionSetOption option : (FormOptionSet) formItem) {
                final FormOptionSetOption.Builder optionBuilder = FormOptionSetOption.create(option);
                optionBuilder.clearFormItems();
                optionBuilder.addFormItems(transformFormItems(option.getFormItems(), inlineMixinStack));
                formOptionSetBuilder.addOptionSetOption(optionBuilder.build());
            }
            formItems.add(formOptionSetBuilder.build());
        } else {
            formItems.add(formItem.copy());
        }
    }
    return formItems;
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) Form(com.enonic.xp.form.Form) FormItem(com.enonic.xp.form.FormItem) ArrayList(java.util.ArrayList) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) Mixin(com.enonic.xp.schema.mixin.Mixin) InlineMixin(com.enonic.xp.form.InlineMixin) MixinName(com.enonic.xp.schema.mixin.MixinName) FieldSet(com.enonic.xp.form.FieldSet) InlineMixin(com.enonic.xp.form.InlineMixin) FormItemSet(com.enonic.xp.form.FormItemSet)

Aggregations

FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)7 FormItem (com.enonic.xp.form.FormItem)4 FormOptionSet (com.enonic.xp.form.FormOptionSet)4 Input (com.enonic.xp.form.Input)4 FieldSet (com.enonic.xp.form.FieldSet)3 FormItemSet (com.enonic.xp.form.FormItemSet)3 ContentType (com.enonic.xp.schema.content.ContentType)3 Property (com.enonic.xp.data.Property)2 PropertySet (com.enonic.xp.data.PropertySet)2 Occurrences (com.enonic.xp.form.Occurrences)2 Test (org.junit.jupiter.api.Test)2 PropertyPath (com.enonic.xp.data.PropertyPath)1 Value (com.enonic.xp.data.Value)1 Form (com.enonic.xp.form.Form)1 FormItems (com.enonic.xp.form.FormItems)1 InlineMixin (com.enonic.xp.form.InlineMixin)1 Mixin (com.enonic.xp.schema.mixin.Mixin)1 MixinName (com.enonic.xp.schema.mixin.MixinName)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1