Search in sources :

Example 21 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class ApprovalManager method get.

private Object get(RegistryEntry entry, String idString) {
    ResourceInformation resourceInformation = entry.getResourceInformation();
    Object id = resourceInformation.parseIdString(idString);
    ResourceRepositoryV2 resourceRepository = entry.getResourceRepositoryFacade();
    QuerySpec querySpec = new QuerySpec(resourceInformation.getResourceType());
    return resourceRepository.findOne((Serializable) id, querySpec);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 22 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class ApprovalRelationshipRepository method findOneTarget.

@Override
public P findOneTarget(Serializable sourceId, String fieldName, QuerySpec querySpec) {
    if (relationshipName.equals(fieldName)) {
        RegistryEntry resourceEntry = resourceRegistry.getEntry(resourceClass);
        RegistryEntry processEntry = resourceRegistry.getEntry(processInfoClass);
        String resourceType = resourceEntry.getResourceInformation().getResourceType();
        ResourceRepositoryV2 processRepository = (ResourceRepositoryV2) processEntry.getResourceRepository(null).getResourceRepository();
        QuerySpec processQuerySpec = querySpec.duplicate();
        processQuerySpec.addFilter(new FilterSpec(Arrays.asList(RESOURCE_ID_FIELD), FilterOperator.EQ, sourceId.toString()));
        processQuerySpec.addFilter(new FilterSpec(Arrays.asList(RESOURCE_TYPE_FIELD), FilterOperator.EQ, resourceType));
        baseFilters.forEach(processQuerySpec::addFilter);
        ResourceList list = processRepository.findAll(querySpec);
        PreconditionUtil.assertTrue("unique result expected", list.size() <= 1);
        return list.isEmpty() ? null : (P) list.get(0);
    } else {
        throw new UnsupportedOperationException("unknown fieldName '" + fieldName + "'");
    }
}
Also used : ResourceList(io.crnk.core.resource.list.ResourceList) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 23 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class JpaEntityRepository method findOne.

@Override
public final T findOne(I id, QuerySpec querySpec) {
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(primaryKeyAttr.getName()), FilterOperator.EQ, id));
    List<T> results = findAll(idQuerySpec);
    return getUnique(results, id);
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Example 24 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class JpaEntityRepository method findAll.

@Override
public final ResourceList<T> findAll(Iterable<I> ids, QuerySpec querySpec) {
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(primaryKeyAttr.getName()), FilterOperator.EQ, ids));
    return findAll(idQuerySpec);
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Example 25 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class JpaEntityRepository method saveInternal.

@SuppressWarnings("unchecked")
private <S extends T> S saveInternal(S resource) {
    JpaMapper<Object, T> mapper = repositoryConfig.getMapper();
    Object entity = mapper.unmap(resource);
    // PATCH reads, updates and saves entities, needs reattachment during
    // save since reads do a detach
    EntityManager em = module.getEntityManager();
    em.persist(entity);
    I id = (I) em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(entity);
    // fetch again since we may have to fetch tuple data and do DTO mapping
    QuerySpec querySpec = new QuerySpec(repositoryConfig.getResourceClass());
    PreconditionUtil.verify(id != null, "id not available for entity %s", resource);
    return (S) findOne(id, querySpec);
}
Also used : EntityManager(javax.persistence.EntityManager) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Aggregations

QuerySpec (io.crnk.core.queryspec.QuerySpec)306 Test (org.junit.Test)233 FilterSpec (io.crnk.core.queryspec.FilterSpec)51 Document (io.crnk.core.engine.document.Document)45 Resource (io.crnk.core.engine.document.Resource)43 Set (java.util.Set)39 HashMap (java.util.HashMap)37 HashSet (java.util.HashSet)36 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)34 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)32 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)32 Task (io.crnk.test.mock.models.Task)32 Project (io.crnk.core.mock.models.Project)28 Relationship (io.crnk.core.engine.document.Relationship)26 Task (io.crnk.core.mock.models.Task)26 TestEntity (io.crnk.jpa.model.TestEntity)26 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)25 Serializable (java.io.Serializable)24 RelatedEntity (io.crnk.jpa.model.RelatedEntity)21 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)20