Search in sources :

Example 1 with Mixin

use of com.enonic.xp.schema.mixin.Mixin in project xp by enonic.

the class MixinLoader method load.

@Override
protected Mixin load(final MixinName name, final Resource resource) {
    final Mixin.Builder builder = Mixin.create();
    parseXml(resource, builder);
    final Instant modifiedTime = Instant.ofEpochMilli(resource.getTimestamp());
    builder.modifiedTime(modifiedTime);
    builder.createdTime(modifiedTime);
    builder.icon(loadIcon(name));
    return builder.name(name).build();
}
Also used : Instant(java.time.Instant) Mixin(com.enonic.xp.schema.mixin.Mixin)

Example 2 with Mixin

use of com.enonic.xp.schema.mixin.Mixin in project xp by enonic.

the class ContentServiceImplTest_update method update_with_metadata.

@Test
public void update_with_metadata() throws Exception {
    final PropertyTree data = new PropertyTree();
    data.setString("testString", "value");
    data.setString("testString2", "value");
    final Mixin mixin = Mixin.create().name("myapplication:my_mixin").addFormItem(Input.create().name("inputToBeMixedIn").label("Mixed in").inputType(InputTypeName.TEXT_LINE).build()).build();
    Mockito.when(this.mixinService.getByName(Mockito.isA(MixinName.class))).thenReturn(mixin);
    final ExtraData extraData = new ExtraData(XDataName.from("myapplication:my_mixin"), new PropertyTree());
    ExtraDatas extraDatas = ExtraDatas.from(List.of(extraData));
    final CreateContentParams createContentParams = CreateContentParams.create().contentData(data).displayName("This is my content").parent(ContentPath.ROOT).permissions(AccessControlList.empty()).type(ContentTypeName.folder()).extraDatas(extraDatas).build();
    final Content content = this.contentService.create(createContentParams);
    assertTrue(content.hasExtraData());
    final UpdateContentParams updateContentParams = new UpdateContentParams();
    updateContentParams.contentId(content.getId()).editor(edit -> {
        final PropertyTree editData = edit.data;
        editData.setString("testString", "value-updated");
    });
    this.contentService.update(updateContentParams);
    final Content storedContent = this.contentService.getById(content.getId());
    assertEquals("This is my content", storedContent.getDisplayName());
    assertEquals("value-updated", storedContent.getData().getString("testString"));
    assertEquals("value", storedContent.getData().getString("testString2"));
}
Also used : MixinName(com.enonic.xp.schema.mixin.MixinName) UpdateContentParams(com.enonic.xp.content.UpdateContentParams) CreateContentParams(com.enonic.xp.content.CreateContentParams) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) ExtraDatas(com.enonic.xp.content.ExtraDatas) ExtraData(com.enonic.xp.content.ExtraData) Mixin(com.enonic.xp.schema.mixin.Mixin) Test(org.junit.jupiter.api.Test)

Example 3 with Mixin

use of com.enonic.xp.schema.mixin.Mixin in project xp by enonic.

the class MixinServiceImpl method getAll.

@Override
public Mixins getAll() {
    final Set<Mixin> list = new LinkedHashSet<>();
    for (final Application application : this.applicationService.getInstalledApplications()) {
        final Mixins types = getByApplication(application.getKey());
        list.addAll(types.getList());
    }
    return Mixins.from(list);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Mixins(com.enonic.xp.schema.mixin.Mixins) Application(com.enonic.xp.app.Application) Mixin(com.enonic.xp.schema.mixin.Mixin) InlineMixin(com.enonic.xp.form.InlineMixin)

Example 4 with Mixin

use of com.enonic.xp.schema.mixin.Mixin 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)

Example 5 with Mixin

use of com.enonic.xp.schema.mixin.Mixin in project xp by enonic.

the class MixinServiceImplTest method testEmpty.

@Test
public void testEmpty() {
    final Mixins types1 = this.service.getAll();
    assertNotNull(types1);
    assertEquals(0, types1.getSize());
    final Mixins types2 = this.service.getByApplication(ApplicationKey.from("other"));
    assertNotNull(types2);
    assertEquals(0, types2.getSize());
    final Mixin mixin = service.getByName(MixinName.from("other:mytype"));
    assertEquals(null, mixin);
}
Also used : Mixins(com.enonic.xp.schema.mixin.Mixins) InlineMixin(com.enonic.xp.form.InlineMixin) Mixin(com.enonic.xp.schema.mixin.Mixin) Test(org.junit.jupiter.api.Test) AbstractSchemaTest(com.enonic.xp.core.impl.schema.AbstractSchemaTest)

Aggregations

Mixin (com.enonic.xp.schema.mixin.Mixin)7 InlineMixin (com.enonic.xp.form.InlineMixin)4 Mixins (com.enonic.xp.schema.mixin.Mixins)3 Test (org.junit.jupiter.api.Test)3 AbstractSchemaTest (com.enonic.xp.core.impl.schema.AbstractSchemaTest)2 MixinName (com.enonic.xp.schema.mixin.MixinName)2 Application (com.enonic.xp.app.Application)1 Content (com.enonic.xp.content.Content)1 CreateContentParams (com.enonic.xp.content.CreateContentParams)1 ExtraData (com.enonic.xp.content.ExtraData)1 ExtraDatas (com.enonic.xp.content.ExtraDatas)1 UpdateContentParams (com.enonic.xp.content.UpdateContentParams)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 FieldSet (com.enonic.xp.form.FieldSet)1 Form (com.enonic.xp.form.Form)1 FormItem (com.enonic.xp.form.FormItem)1 FormItemSet (com.enonic.xp.form.FormItemSet)1 FormOptionSet (com.enonic.xp.form.FormOptionSet)1 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)1 Instant (java.time.Instant)1