Search in sources :

Example 1 with JpaResourceInformationProvider

use of io.crnk.jpa.internal.JpaResourceInformationProvider in project crnk-framework by crnk-project.

the class JpaPartialEntityExposureTest method testInformationBuilder.

@Test
public void testInformationBuilder() {
    EntityManager em = null;
    JpaResourceInformationProvider builder = new JpaResourceInformationProvider(new NullPropertiesProvider());
    ResourceInformation info = builder.build(TestEntity.class);
    List<ResourceField> relationshipFields = info.getRelationshipFields();
    Assert.assertEquals(0, relationshipFields.size());
}
Also used : JpaResourceInformationProvider(io.crnk.jpa.internal.JpaResourceInformationProvider) ResourceField(io.crnk.core.engine.information.resource.ResourceField) EntityManager(javax.persistence.EntityManager) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider)

Example 2 with JpaResourceInformationProvider

use of io.crnk.jpa.internal.JpaResourceInformationProvider 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 JpaResourceInformationProvider

use of io.crnk.jpa.internal.JpaResourceInformationProvider in project crnk-framework by crnk-project.

the class JpaResourceInformationProviderTest method setup.

@Before
public void setup() {
    jpaMetaProvider = new JpaMetaProvider(Collections.<Class>emptySet());
    lookup = new MetaLookup();
    lookup.addProvider(jpaMetaProvider);
    builder = new JpaResourceInformationProvider(new NullPropertiesProvider());
    builder.init(new DefaultResourceInformationProviderContext(builder, new DefaultInformationBuilder(new TypeParser()), new TypeParser(), new ObjectMapper()));
}
Also used : JpaResourceInformationProvider(io.crnk.jpa.internal.JpaResourceInformationProvider) MetaLookup(io.crnk.meta.MetaLookup) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) TypeParser(io.crnk.core.engine.parser.TypeParser) JpaMetaProvider(io.crnk.jpa.meta.JpaMetaProvider) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) DefaultResourceInformationProviderContext(io.crnk.legacy.registry.DefaultResourceInformationProviderContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Aggregations

NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)3 JpaResourceInformationProvider (io.crnk.jpa.internal.JpaResourceInformationProvider)3 EntityManager (javax.persistence.EntityManager)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceFieldAccess (io.crnk.core.engine.information.resource.ResourceFieldAccess)1 ResourceFieldAccessor (io.crnk.core.engine.information.resource.ResourceFieldAccessor)1 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 DefaultInformationBuilder (io.crnk.core.engine.internal.information.DefaultInformationBuilder)1 ResourceFieldImpl (io.crnk.core.engine.internal.information.resource.ResourceFieldImpl)1 TypeParser (io.crnk.core.engine.parser.TypeParser)1 JpaMetaProvider (io.crnk.jpa.meta.JpaMetaProvider)1 CountryEntity (io.crnk.jpa.model.CountryEntity)1 CountryTranslationEntity (io.crnk.jpa.model.CountryTranslationEntity)1 CountryTranslationPK (io.crnk.jpa.model.CountryTranslationPK)1 LangEntity (io.crnk.jpa.model.LangEntity)1 DefaultResourceInformationProviderContext (io.crnk.legacy.registry.DefaultResourceInformationProviderContext)1 MetaLookup (io.crnk.meta.MetaLookup)1 List (java.util.List)1