Search in sources :

Example 1 with ObjectProxy

use of io.crnk.client.internal.proxy.ObjectProxy in project crnk-framework by crnk-project.

the class JpaQuerySpecEndToEndTest method testLazyManyRelation.

@Test
public void testLazyManyRelation() throws InstantiationException, IllegalAccessException {
    addTestWithManyRelations();
    QuerySpec querySpec = new QuerySpec(TestEntity.class);
    List<TestEntity> list = testRepo.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    TestEntity testEntity = list.get(0);
    List<RelatedEntity> manyRelatedValues = testEntity.getManyRelatedValues();
    Assert.assertNotNull(manyRelatedValues);
    ObjectProxy proxy = (ObjectProxy) manyRelatedValues;
    Assert.assertFalse(proxy.isLoaded());
    Assert.assertEquals(3, manyRelatedValues.size());
    for (RelatedEntity relatedEntity : manyRelatedValues) {
        Assert.assertNotNull(relatedEntity.getStringValue());
    }
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 2 with ObjectProxy

use of io.crnk.client.internal.proxy.ObjectProxy in project crnk-framework by crnk-project.

the class BasicProxyFactoryTest method testWrappedCollectionProxy.

@Test
public void testWrappedCollectionProxy() {
    Collection<Schedule> col = factory.createCollectionProxy(Schedule.class, ScheduleRepository.ScheduleList.class, "http://127.0.0.1:99999");
    Assert.assertTrue(col instanceof ScheduleRepository.ScheduleList);
    ScheduleRepository.ScheduleList list = (ScheduleRepository.ScheduleList) col;
    Assert.assertTrue(list.getWrappedList() instanceof ObjectProxy);
    ObjectProxy proxy = (ObjectProxy) list.getWrappedList();
    Assert.assertFalse(proxy.isLoaded());
    Assert.assertEquals("http://127.0.0.1:99999", proxy.getUrl());
}
Also used : Schedule(io.crnk.test.mock.models.Schedule) ScheduleRepository(io.crnk.test.mock.repository.ScheduleRepository) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy) Test(org.junit.Test)

Example 3 with ObjectProxy

use of io.crnk.client.internal.proxy.ObjectProxy in project crnk-framework by crnk-project.

the class AbstractProxiedObjectsClientTest method saveDoesNotTriggerLazyLoad.

@Test
public void saveDoesNotTriggerLazyLoad() {
    Schedule schedule = new Schedule();
    schedule.setId(1L);
    schedule.setName("project");
    scheduleRepo.create(schedule);
    QuerySpec querySpec = new QuerySpec(Schedule.class);
    schedule = scheduleRepo.findOne(1L, querySpec);
    Collection<Task> proxiedTasks = schedule.getTasks();
    ObjectProxy proxy = (ObjectProxy) proxiedTasks;
    Assert.assertFalse(proxy.isLoaded());
    // update primitive field
    schedule.setName("newValue");
    scheduleRepo.save(schedule);
    // save should not trigger a load of the relation
    Assert.assertFalse(proxy.isLoaded());
    Assert.assertSame(proxy, schedule.getTasks());
    // data should be saved
    schedule = scheduleRepo.findOne(1L, querySpec);
    Assert.assertEquals("newValue", schedule.getName());
}
Also used : Task(io.crnk.test.mock.models.Task) Schedule(io.crnk.test.mock.models.Schedule) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 4 with ObjectProxy

use of io.crnk.client.internal.proxy.ObjectProxy in project crnk-framework by crnk-project.

the class BasicProxyFactoryTest method testCollectionProxy.

@Test
public void testCollectionProxy() {
    Collection<Task> col = factory.createCollectionProxy(Task.class, ResourceList.class, "http://127.0.0.1:99999");
    Assert.assertTrue(col instanceof ResourceList);
    Assert.assertTrue(col instanceof ObjectProxy);
    ObjectProxy proxy = (ObjectProxy) col;
    Assert.assertFalse(proxy.isLoaded());
    Assert.assertEquals("http://127.0.0.1:99999", proxy.getUrl());
}
Also used : Task(io.crnk.test.mock.models.Task) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) ResourceList(io.crnk.core.resource.list.ResourceList) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy) Test(org.junit.Test)

Example 5 with ObjectProxy

use of io.crnk.client.internal.proxy.ObjectProxy in project crnk-framework by crnk-project.

the class ClientDocumentMapper method newResourceMapper.

@Override
protected ResourceMapper newResourceMapper(final DocumentMapperUtil util, boolean client, ObjectMapper objectMapper) {
    return new ResourceMapper(util, client, objectMapper, null) {

        @Override
        protected void setRelationship(Resource resource, ResourceField field, Object entity, ResourceInformation resourceInformation, QueryAdapter queryAdapter, ResourceMappingConfig mappingConfig) {
            // we also include relationship data if it is not null and not a
            // unloaded proxy
            boolean includeRelation = true;
            Object relationshipId = null;
            if (field.hasIdField()) {
                Object relationshipValue = field.getIdAccessor().getValue(entity);
                ResourceInformation oppositeInformation = resourceRegistry.getEntry(field.getOppositeResourceType()).getResourceInformation();
                if (relationshipValue instanceof Collection) {
                    List ids = new ArrayList();
                    for (Object elem : (Collection<?>) relationshipValue) {
                        ids.add(oppositeInformation.toResourceIdentifier(elem));
                    }
                    relationshipId = ids;
                } else if (relationshipValue != null) {
                    relationshipId = oppositeInformation.toResourceIdentifier(relationshipValue);
                }
                includeRelation = relationshipId != null || field.getSerializeType() != SerializeType.LAZY;
            } else {
                Object relationshipValue = field.getAccessor().getValue(entity);
                if (relationshipValue instanceof ObjectProxy) {
                    includeRelation = ((ObjectProxy) relationshipValue).isLoaded();
                } else {
                    // TODO for fieldSets handling in the future the lazy
                    // handling must be different
                    includeRelation = relationshipValue != null || field.getSerializeType() != SerializeType.LAZY && !field.isCollection();
                }
                if (relationshipValue != null && includeRelation) {
                    if (relationshipValue instanceof Collection) {
                        relationshipId = util.toResourceIds((Collection<?>) relationshipValue);
                    } else {
                        relationshipId = util.toResourceId(relationshipValue);
                    }
                }
            }
            if (includeRelation) {
                Relationship relationship = new Relationship();
                relationship.setData(Nullable.of((Object) relationshipId));
                resource.getRelationships().put(field.getJsonName(), relationship);
            }
        }
    };
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceMappingConfig(io.crnk.core.engine.internal.document.mapper.ResourceMappingConfig) Relationship(io.crnk.core.engine.document.Relationship) ResourceMapper(io.crnk.core.engine.internal.document.mapper.ResourceMapper) Collection(java.util.Collection) ArrayList(java.util.ArrayList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy)

Aggregations

ObjectProxy (io.crnk.client.internal.proxy.ObjectProxy)8 QuerySpec (io.crnk.core.queryspec.QuerySpec)5 Task (io.crnk.test.mock.models.Task)5 Test (org.junit.Test)5 Schedule (io.crnk.test.mock.models.Schedule)4 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)2 Relationship (io.crnk.core.engine.document.Relationship)1 Resource (io.crnk.core.engine.document.Resource)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 ResourceMapper (io.crnk.core.engine.internal.document.mapper.ResourceMapper)1 ResourceMappingConfig (io.crnk.core.engine.internal.document.mapper.ResourceMappingConfig)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 ResourceList (io.crnk.core.resource.list.ResourceList)1 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)1 OtherRelatedEntity (io.crnk.jpa.model.OtherRelatedEntity)1 RelatedEntity (io.crnk.jpa.model.RelatedEntity)1 TestEntity (io.crnk.jpa.model.TestEntity)1 Project (io.crnk.test.mock.models.Project)1 ScheduleRepository (io.crnk.test.mock.repository.ScheduleRepository)1