Search in sources :

Example 1 with ResourceFieldImpl

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

the class ResourceRegistryTest method onExistingResourceShouldReturnUrl.

@Test
public void onExistingResourceShouldReturnUrl() {
    Task task = new Task();
    task.setId(1L);
    ResourceField idField = new ResourceFieldImpl("id", "id", ResourceFieldType.ID, Long.class, Long.class, null);
    ResourceField valueField = new ResourceFieldImpl("value", "value", ResourceFieldType.RELATIONSHIP, String.class, String.class, "projects");
    ResourceInformation resourceInformation = new ResourceInformation(moduleRegistry.getTypeParser(), Task.class, "tasks", null, Arrays.asList(idField, valueField), new OffsetLimitPagingBehavior());
    RegistryEntry registryEntry = new RegistryEntry(new DirectResponseResourceEntry(null, new ResourceRepositoryInformationImpl("tasks", resourceInformation, RepositoryMethodAccess.ALL)));
    resourceRegistry.addEntry(Task.class, registryEntry);
    String resourceUrl = resourceRegistry.getResourceUrl(task);
    assertThat(resourceUrl).isEqualTo(TEST_MODELS_URL + "/tasks/1");
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) ResourceRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.ResourceRepositoryInformationImpl) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 2 with ResourceFieldImpl

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

the class TestResourceInformationProvider method build.

@Override
public ResourceInformation build(Class<?> resourceClass) {
    ResourceField idField = new ResourceFieldImpl("testId", "id", ResourceFieldType.ID, Integer.class, null, null);
    List<ResourceField> fields = Arrays.asList(idField);
    TypeParser typeParser = context.getTypeParser();
    ResourceInformation info = new ResourceInformation(typeParser, resourceClass, resourceClass.getSimpleName(), null, fields, new OffsetLimitPagingBehavior());
    return info;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) TypeParser(io.crnk.core.engine.parser.TypeParser)

Example 3 with ResourceFieldImpl

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

the class ResourceInformationTest method setup.

@Before
public void setup() throws NoSuchFieldException {
    ResourceField idField = new ResourceFieldImpl("id", "id", ResourceFieldType.ID, Long.class, Long.class, null);
    ResourceField valueField = new ResourceFieldImpl("value", "value", ResourceFieldType.RELATIONSHIP, String.class, String.class, "projects");
    TypeParser typeParser = new TypeParser();
    sut = new ResourceInformation(typeParser, Task.class, "tasks", null, Arrays.asList(idField, valueField), new OffsetLimitPagingBehavior());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Task(io.crnk.core.mock.models.Task) OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) TypeParser(io.crnk.core.engine.parser.TypeParser) Before(org.junit.Before)

Example 4 with ResourceFieldImpl

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

the class FieldOrderedComparatorTest method setUp.

@Before
public void setUp() throws Exception {
    fieldA = new ResourceFieldImpl("a", "a", ResourceFieldType.ATTRIBUTE, String.class, String.class, null);
    fieldB = new ResourceFieldImpl("b", "b", ResourceFieldType.ATTRIBUTE, String.class, String.class, null);
}
Also used : ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) Before(org.junit.Before)

Example 5 with ResourceFieldImpl

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

Aggregations

ResourceFieldImpl (io.crnk.core.engine.internal.information.resource.ResourceFieldImpl)5 ResourceField (io.crnk.core.engine.information.resource.ResourceField)3 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)3 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)3 TypeParser (io.crnk.core.engine.parser.TypeParser)2 Task (io.crnk.core.mock.models.Task)2 Before (org.junit.Before)2 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 ResourceRepositoryInformationImpl (io.crnk.core.engine.internal.information.repository.ResourceRepositoryInformationImpl)1 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 JpaResourceInformationProvider (io.crnk.jpa.internal.JpaResourceInformationProvider)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 DirectResponseResourceEntry (io.crnk.legacy.internal.DirectResponseResourceEntry)1 List (java.util.List)1