Search in sources :

Example 1 with ResourceFieldAccess

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

the class ResourceFieldAccessTest method and.

@Test
public void and() {
    ResourceFieldAccess all = new ResourceFieldAccess(true, true, true, true, true);
    ResourceFieldAccess none = new ResourceFieldAccess(false, false, false, false, false);
    ResourceFieldAccess a1 = new ResourceFieldAccess(false, true, false, false, false);
    ResourceFieldAccess a2 = new ResourceFieldAccess(false, false, true, false, false);
    ResourceFieldAccess a3 = new ResourceFieldAccess(false, false, false, true, false);
    ResourceFieldAccess a4 = new ResourceFieldAccess(false, false, false, false, true);
    ResourceFieldAccess a5 = new ResourceFieldAccess(true, false, false, false, false);
    Assert.assertEquals(a1, a1.and(all));
    Assert.assertEquals(a1, all.and(a1));
    Assert.assertEquals(none, none.and(a1));
    Assert.assertEquals(a2, a2.and(all));
    Assert.assertEquals(a2, all.and(a2));
    Assert.assertEquals(none, none.and(a2));
    Assert.assertEquals(a3, a3.and(all));
    Assert.assertEquals(a3, all.and(a3));
    Assert.assertEquals(none, none.and(a3));
    Assert.assertEquals(a4, a4.and(all));
    Assert.assertEquals(a4, all.and(a4));
    Assert.assertEquals(none, none.and(a4));
    Assert.assertEquals(none, none.and(all));
    Assert.assertEquals(none, none.and(none));
    Assert.assertEquals(all, all.and(all));
    Assert.assertEquals(a5, a5.and(all));
    Assert.assertEquals(a5, all.and(a5));
    Assert.assertEquals(none, none.and(a5));
}
Also used : ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess) Test(org.junit.Test)

Example 2 with ResourceFieldAccess

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

the class DefaultRegistryEntryBuilder method setupImplicitRelationshipRepository.

private ResponseRelationshipEntry setupImplicitRelationshipRepository(ResourceField relationshipField) {
    RelationshipRepositoryBehavior behavior = relationshipField.getRelationshipRepositoryBehavior();
    if (behavior == RelationshipRepositoryBehavior.DEFAULT) {
        if (relationshipField.hasIdField() || relationshipField.getLookupIncludeAutomatically() == LookupIncludeBehavior.NONE) {
            behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
        } else {
            behavior = RelationshipRepositoryBehavior.CUSTOM;
        }
    }
    if (behavior == RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER) {
        behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
    }
    if (behavior == RelationshipRepositoryBehavior.IMPLICIT_GET_OPPOSITE_MODIFY_OWNER) {
        behavior = RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER;
    }
    if (behavior != RelationshipRepositoryBehavior.CUSTOM) {
        ResourceInformation sourceInformation = relationshipField.getParentResourceInformation();
        ResourceFieldAccess fieldAccess = relationshipField.getAccess();
        RepositoryMethodAccess access = new RepositoryMethodAccess(fieldAccess.isPostable(), fieldAccess.isPatchable(), fieldAccess.isReadable(), fieldAccess.isPatchable());
        RelationshipMatcher matcher = new RelationshipMatcher().rule().field(relationshipField).add();
        RelationshipRepositoryInformationImpl implicitRepoInformation = new RelationshipRepositoryInformationImpl(matcher, access);
        ForwardingRelationshipRepository repository;
        if (behavior == RelationshipRepositoryBehavior.FORWARD_OWNER) {
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OWNER, ForwardingDirection.OWNER);
        } else if (behavior == RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER) {
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OWNER);
        } else {
            PreconditionUtil.assertEquals("unknown behavior", RelationshipRepositoryBehavior.FORWARD_OPPOSITE, behavior);
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
        }
        repository.setResourceRegistry(moduleRegistry.getResourceRegistry());
        return setupRelationship(relationshipField, implicitRepoInformation, repository);
    } else {
        return null;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RelationshipRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl) RepositoryMethodAccess(io.crnk.core.engine.information.repository.RepositoryMethodAccess) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) RelationshipRepositoryBehavior(io.crnk.core.resource.annotations.RelationshipRepositoryBehavior) RelationshipMatcher(io.crnk.core.repository.RelationshipMatcher) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess)

Example 3 with ResourceFieldAccess

use of io.crnk.core.engine.information.resource.ResourceFieldAccess 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 4 with ResourceFieldAccess

use of io.crnk.core.engine.information.resource.ResourceFieldAccess 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 5 with ResourceFieldAccess

use of io.crnk.core.engine.information.resource.ResourceFieldAccess 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)

Aggregations

ResourceFieldAccess (io.crnk.core.engine.information.resource.ResourceFieldAccess)7 ResourceFieldAccessor (io.crnk.core.engine.information.resource.ResourceFieldAccessor)5 InformationBuilder (io.crnk.core.engine.information.InformationBuilder)4 ResourceField (io.crnk.core.engine.information.resource.ResourceField)3 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)3 Test (org.junit.Test)3 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)2 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 RepositoryMethodAccess (io.crnk.core.engine.information.repository.RepositoryMethodAccess)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 RelationshipRepositoryInformationImpl (io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl)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