Search in sources :

Example 1 with ResourceFieldAccessor

use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.

the class HistoryFieldContributor method getResourceFields.

@Override
public List<ResourceField> getResourceFields(ResourceFieldContributorContext context) {
    // this method could be omitted if the history field is added regularly to Project and Task resource. This would be
    // simpler and recommended, but may not always be possible. Here we demonstrate doing it dynamically.
    InformationBuilder.Field fieldBuilder = context.getInformationBuilder().createResourceField();
    fieldBuilder.name("history");
    fieldBuilder.genericType(new TypeToken<List<History>>() {
    }.getType());
    fieldBuilder.oppositeResourceType("history");
    fieldBuilder.fieldType(ResourceFieldType.RELATIONSHIP);
    // field values are "null" on resource and we make use of automated lookup to the relationship repository
    // instead:
    fieldBuilder.lookupIncludeBehavior(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS);
    fieldBuilder.accessor(new ResourceFieldAccessor() {

        @Override
        public Object getValue(Object resource) {
            return null;
        }

        @Override
        public void setValue(Object resource, Object fieldValue) {
        }
    });
    return Arrays.asList(fieldBuilder.build());
}
Also used : InformationBuilder(io.crnk.core.engine.information.InformationBuilder) ResourceFieldAccessor(io.crnk.core.engine.information.resource.ResourceFieldAccessor) TypeToken(com.google.common.reflect.TypeToken) History(io.crnk.example.springboot.domain.model.History)

Example 2 with ResourceFieldAccessor

use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.

the class CustomResourceFieldTest method setupModule.

@Override
protected void setupModule(final JpaModule module, boolean server) {
    super.setupModule(module, server);
    if (server) {
        module.setResourceInformationProvider(new JpaResourceInformationProvider(new NullPropertiesProvider()) {

            @Override
            protected List<ResourceField> getResourceFields(Class clazz) {
                List<ResourceField> fields = super.getResourceFields(clazz);
                if (clazz == CountryEntity.class) {
                    List<String> languages = Arrays.asList("en", "de");
                    for (final String language : languages) {
                        ResourceFieldType resourceFieldType = ResourceFieldType.ATTRIBUTE;
                        String name = language + "Text";
                        Class<?> type = String.class;
                        ResourceFieldAccess access = new ResourceFieldAccess(true, true, true, false, false);
                        ResourceFieldImpl field = new ResourceFieldImpl(name, name, resourceFieldType, type, type, null, null, SerializeType.LAZY, LookupIncludeBehavior.NONE, access, null, null, null, RelationshipRepositoryBehavior.DEFAULT);
                        field.setAccessor(new ResourceFieldAccessor() {

                            @Override
                            public String getValue(Object resource) {
                                CountryEntity country = (CountryEntity) resource;
                                List<CountryTranslationEntity> translations = country.getTranslations();
                                CountryTranslationEntity translation = getTranslation(translations, language);
                                return translation != null ? translation.getTxt() : null;
                            }

                            @Override
                            public void setValue(Object resource, Object fieldValue) {
                                CountryEntity country = (CountryEntity) resource;
                                List<CountryTranslationEntity> translations = country.getTranslations();
                                CountryTranslationEntity translation = getTranslation(translations, language);
                                if (translation == null) {
                                    EntityManager em = module.getEntityManager();
                                    LangEntity langEntity = em.find(LangEntity.class, language);
                                    if (langEntity == null) {
                                        throw new IllegalStateException("language not found: " + language);
                                    }
                                    translation = new CountryTranslationEntity();
                                    CountryTranslationPK pk = new CountryTranslationPK();
                                    pk.setCountry(country);
                                    pk.setLang(langEntity);
                                    translation.setCountryTranslationPk(pk);
                                    translations.add(translation);
                                }
                                translation.setTxt((String) fieldValue);
                            }

                            private CountryTranslationEntity getTranslation(List<CountryTranslationEntity> translations, String language) {
                                for (CountryTranslationEntity translation : translations) {
                                    CountryTranslationPK translationPk = translation.getCountryTranslationPk();
                                    String langCd = translationPk.getLang().getLangCd();
                                    if (langCd.equals(language)) {
                                        return translation;
                                    }
                                }
                                return null;
                            }
                        });
                        fields.add(field);
                    }
                }
                return fields;
            }
        });
    }
}
Also used : ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) CountryTranslationPK(io.crnk.jpa.model.CountryTranslationPK) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess) LangEntity(io.crnk.jpa.model.LangEntity) JpaResourceInformationProvider(io.crnk.jpa.internal.JpaResourceInformationProvider) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) ResourceFieldAccessor(io.crnk.core.engine.information.resource.ResourceFieldAccessor) CountryTranslationEntity(io.crnk.jpa.model.CountryTranslationEntity) EntityManager(javax.persistence.EntityManager) List(java.util.List) CountryEntity(io.crnk.jpa.model.CountryEntity)

Example 3 with ResourceFieldAccessor

use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.

the class ApprovalRelationshipRepository method getResourceFields.

@Override
public List<ResourceField> getResourceFields(ResourceFieldContributorContext context) {
    InformationBuilder.Field fieldBuilder = context.getInformationBuilder().createResourceField();
    fieldBuilder.name(relationshipName);
    fieldBuilder.oppositeResourceType(oppositeResourceType);
    fieldBuilder.lookupIncludeBehavior(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS);
    fieldBuilder.fieldType(ResourceFieldType.RELATIONSHIP);
    fieldBuilder.access(new ResourceFieldAccess(false, false, false, false, false));
    fieldBuilder.accessor(new ResourceFieldAccessor() {

        @Override
        public Object getValue(Object resource) {
            return null;
        }

        @Override
        public void setValue(Object resource, Object fieldValue) {
        }
    });
    fieldBuilder.type(processInfoClass);
    fieldBuilder.genericType(processInfoClass);
    return Lists.newArrayList(fieldBuilder.build());
}
Also used : InformationBuilder(io.crnk.core.engine.information.InformationBuilder) ResourceFieldAccessor(io.crnk.core.engine.information.resource.ResourceFieldAccessor) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess)

Example 4 with ResourceFieldAccessor

use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.

the class AbstractQuerySpecTest method setup.

@Before
public void setup() {
    ResourceInformationProvider resourceInformationProvider = new DefaultResourceInformationProvider(new NullPropertiesProvider(), ImmutableList.of(new OffsetLimitPagingBehavior(), new CustomOffsetLimitPagingBehavior()), new DefaultResourceFieldInformationProvider(), new JacksonResourceFieldInformationProvider()) {

        @Override
        protected List<ResourceField> getResourceFields(Class<?> resourceClass) {
            List<ResourceField> fields = super.getResourceFields(resourceClass);
            if (resourceClass == Task.class) {
                // add additional field that is not defined on the class
                String name = "computedAttribute";
                ResourceFieldAccess access = new ResourceFieldAccess(true, true, true, true, true);
                InformationBuilder informationBuilder = new DefaultInformationBuilder(new TypeParser());
                InformationBuilder.Field fieldBuilder = informationBuilder.createResourceField();
                fieldBuilder.type(Integer.class);
                fieldBuilder.jsonName(name);
                fieldBuilder.underlyingName(name);
                fieldBuilder.access(access);
                fieldBuilder.accessor(new ResourceFieldAccessor() {

                    public Object getValue(Object resource) {
                        return 13;
                    }

                    public void setValue(Object resource, Object fieldValue) {
                    }
                });
                fields.add(fieldBuilder.build());
            }
            return fields;
        }
    };
    SimpleModule testModule = new SimpleModule("test");
    testModule.addResourceInformationProvider(resourceInformationProvider);
    CrnkBoot boot = new CrnkBoot();
    boot.setServiceUrlProvider(new ConstantServiceUrlProvider("http://127.0.0.1"));
    boot.addModule(testModule);
    boot.setServiceDiscovery(new ReflectionsServiceDiscovery(getResourceSearchPackage()));
    boot.boot();
    moduleRegistry = boot.getModuleRegistry();
    querySpecConverter = new DefaultQuerySpecConverter(moduleRegistry);
    resourceRegistry = boot.getResourceRegistry();
}
Also used : OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) CustomOffsetLimitPagingBehavior(io.crnk.core.queryspec.repository.CustomOffsetLimitPagingBehavior) TypeParser(io.crnk.core.engine.parser.TypeParser) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) DefaultResourceInformationProvider(io.crnk.core.engine.internal.information.resource.DefaultResourceInformationProvider) JacksonResourceFieldInformationProvider(io.crnk.core.engine.internal.jackson.JacksonResourceFieldInformationProvider) DefaultQuerySpecConverter(io.crnk.legacy.internal.DefaultQuerySpecConverter) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess) CustomOffsetLimitPagingBehavior(io.crnk.core.queryspec.repository.CustomOffsetLimitPagingBehavior) InformationBuilder(io.crnk.core.engine.information.InformationBuilder) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceFieldAccessor(io.crnk.core.engine.information.resource.ResourceFieldAccessor) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) CrnkBoot(io.crnk.core.boot.CrnkBoot) ResourceInformationProvider(io.crnk.core.engine.information.resource.ResourceInformationProvider) DefaultResourceInformationProvider(io.crnk.core.engine.internal.information.resource.DefaultResourceInformationProvider) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) SimpleModule(io.crnk.core.module.SimpleModule) DefaultResourceFieldInformationProvider(io.crnk.core.engine.internal.information.resource.DefaultResourceFieldInformationProvider) Before(org.junit.Before)

Example 5 with ResourceFieldAccessor

use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.

the class DefaultInformationBuilderTest method resource.

@Test
public void resource() {
    InformationBuilder.Resource resource = builder.createResource(Task.class, "tasks");
    resource.superResourceType("superTask");
    resource.resourceType("changedTasks");
    resource.resourceClass(Project.class);
    InformationBuilder.Field idField = resource.addField("id", ResourceFieldType.ID, String.class);
    idField.serializeType(SerializeType.EAGER);
    idField.access(new ResourceFieldAccess(true, true, true, false, false));
    ResourceFieldAccessor accessor = Mockito.mock(ResourceFieldAccessor.class);
    InformationBuilder.Field projectField = resource.addField("project", ResourceFieldType.RELATIONSHIP, Project.class);
    projectField.serializeType(SerializeType.EAGER);
    projectField.access(new ResourceFieldAccess(true, false, true, false, false));
    projectField.oppositeName("tasks");
    projectField.relationshipRepositoryBehavior(RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER);
    projectField.lookupIncludeBehavior(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS);
    projectField.accessor(accessor);
    ResourceInformation info = resource.build();
    Assert.assertEquals("changedTasks", info.getResourceType());
    Assert.assertEquals(Project.class, info.getResourceClass());
    Assert.assertEquals("superTask", info.getSuperResourceType());
    ResourceField idInfo = info.findFieldByName("id");
    Assert.assertEquals("id", idInfo.getUnderlyingName());
    Assert.assertEquals(String.class, idInfo.getType());
    Assert.assertFalse(idInfo.getAccess().isFilterable());
    Assert.assertFalse(idInfo.getAccess().isSortable());
    Assert.assertTrue(idInfo.getAccess().isPostable());
    Assert.assertTrue(idInfo.getAccess().isPatchable());
    Assert.assertEquals(SerializeType.EAGER, idInfo.getSerializeType());
    Assert.assertFalse(idInfo.isCollection());
    ResourceField projectInfo = info.findFieldByName("project");
    Assert.assertEquals("project", projectInfo.getUnderlyingName());
    Assert.assertEquals("tasks", projectInfo.getOppositeName());
    Assert.assertEquals(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS, projectInfo.getLookupIncludeAutomatically());
    Assert.assertEquals(Project.class, projectInfo.getType());
    Assert.assertSame(accessor, projectInfo.getAccessor());
    Assert.assertFalse(projectInfo.getAccess().isFilterable());
    Assert.assertFalse(projectInfo.getAccess().isSortable());
    Assert.assertFalse(projectInfo.getAccess().isPostable());
    Assert.assertTrue(projectInfo.getAccess().isPatchable());
    Assert.assertEquals(SerializeType.EAGER, projectInfo.getSerializeType());
    Assert.assertEquals(RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER, projectInfo.getRelationshipRepositoryBehavior());
    Assert.assertFalse(projectInfo.isCollection());
}
Also used : InformationBuilder(io.crnk.core.engine.information.InformationBuilder) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceFieldAccessor(io.crnk.core.engine.information.resource.ResourceFieldAccessor) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess) Test(org.junit.Test)

Aggregations

ResourceFieldAccessor (io.crnk.core.engine.information.resource.ResourceFieldAccessor)6 InformationBuilder (io.crnk.core.engine.information.InformationBuilder)5 ResourceFieldAccess (io.crnk.core.engine.information.resource.ResourceFieldAccess)5 ResourceField (io.crnk.core.engine.information.resource.ResourceField)3 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)2 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)2 Test (org.junit.Test)2 TypeToken (com.google.common.reflect.TypeToken)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)1 ResourceInformationProvider (io.crnk.core.engine.information.resource.ResourceInformationProvider)1 DefaultInformationBuilder (io.crnk.core.engine.internal.information.DefaultInformationBuilder)1 DefaultResourceFieldInformationProvider (io.crnk.core.engine.internal.information.resource.DefaultResourceFieldInformationProvider)1 DefaultResourceInformationProvider (io.crnk.core.engine.internal.information.resource.DefaultResourceInformationProvider)1 ResourceFieldImpl (io.crnk.core.engine.internal.information.resource.ResourceFieldImpl)1 JacksonResourceFieldInformationProvider (io.crnk.core.engine.internal.jackson.JacksonResourceFieldInformationProvider)1 TypeParser (io.crnk.core.engine.parser.TypeParser)1 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)1 SimpleModule (io.crnk.core.module.SimpleModule)1 ReflectionsServiceDiscovery (io.crnk.core.module.discovery.ReflectionsServiceDiscovery)1