Search in sources :

Example 1 with ResourceInformation

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

the class ApprovalManager method requestApproval.

public <T> T requestApproval(T entity, HttpMethod method) {
    RegistryEntry entry = moduleRegistry.getResourceRegistry().getEntry(entity.getClass());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    Object id = resourceInformation.getId(entity);
    ApprovalProcessInstance resource = newApprovalProcessInstance(entity.getClass());
    resource.setResourceType(resourceInformation.getResourceType());
    resource.setResourceId(id.toString());
    resource.setNewValues(approvalMapper.mapValues(entity));
    T currentEntity = entity;
    if (method != HttpMethod.POST) {
        // you may need  additional logic here, like detaching entities from an entity manager
        // in case of entities, like detaching from entity manager
        currentEntity = (T) get(entry, id.toString());
        PreconditionUtil.assertFalse("posted resource must not be managed", currentEntity == entity);
        if (currentEntity != null) {
            resource.setPreviousValues(approvalMapper.mapValues(currentEntity));
        }
    }
    Map<String, Object> processVariables = resourceMapper.mapToVariables(resource);
    runtimeService.startProcessInstanceByKey("scheduleChange", processVariables);
    return currentEntity;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ScheduleApprovalProcessInstance(io.crnk.activiti.example.model.ScheduleApprovalProcessInstance) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 2 with ResourceInformation

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

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

the class CrnkClient method setProxyFactory.

public void setProxyFactory(ClientProxyFactory proxyFactory) {
    proxyFactory.init(new ClientProxyFactoryContext() {

        @Override
        public ModuleRegistry getModuleRegistry() {
            return moduleRegistry;
        }

        @Override
        public <T> DefaultResourceList<T> getCollection(Class<T> resourceClass, String url) {
            RegistryEntry entry = resourceRegistry.findEntry(resourceClass);
            ResourceInformation resourceInformation = entry.getResourceInformation();
            final ResourceRepositoryStubImpl<T, ?> repositoryStub = new ResourceRepositoryStubImpl<>(CrnkClient.this, resourceClass, resourceInformation, urlBuilder);
            return repositoryStub.findAll(url);
        }
    });
    documentMapper.setProxyFactory(proxyFactory);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryStubImpl(io.crnk.client.internal.ResourceRepositoryStubImpl) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) ModuleRegistry(io.crnk.core.module.ModuleRegistry) ClientProxyFactoryContext(io.crnk.client.internal.proxy.ClientProxyFactoryContext)

Example 4 with ResourceInformation

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

the class CrnkClient method allocateRepository.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <T, I extends Serializable> RegistryEntry allocateRepository(Class<T> resourceClass) {
    ResourceInformationProvider resourceInformationProvider = moduleRegistry.getResourceInformationBuilder();
    ResourceInformation resourceInformation = resourceInformationProvider.build(resourceClass);
    final ResourceRepositoryStub<T, I> repositoryStub = new ResourceRepositoryStubImpl<>(this, resourceClass, resourceInformation, urlBuilder);
    // create interface for it!
    RepositoryInstanceBuilder repositoryInstanceBuilder = new RepositoryInstanceBuilder(null, null) {

        @Override
        public Object buildRepository() {
            return repositoryStub;
        }
    };
    ResourceRepositoryInformation repositoryInformation = new ResourceRepositoryInformationImpl(resourceInformation.getResourceType(), resourceInformation, RepositoryMethodAccess.ALL);
    ResourceEntry resourceEntry = new DirectResponseResourceEntry(repositoryInstanceBuilder, repositoryInformation);
    Map<ResourceField, ResponseRelationshipEntry> relationshipEntries = new HashMap<>();
    RegistryEntry registryEntry = new RegistryEntry(resourceEntry, relationshipEntries);
    registryEntry.initialize(moduleRegistry);
    resourceRegistry.addEntry(resourceClass, registryEntry);
    allocateRepositoryRelations(registryEntry);
    return registryEntry;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryStubImpl(io.crnk.client.internal.ResourceRepositoryStubImpl) ResourceRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.ResourceRepositoryInformationImpl) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) ResourceInformationProvider(io.crnk.core.engine.information.resource.ResourceInformationProvider) RepositoryInstanceBuilder(io.crnk.legacy.registry.RepositoryInstanceBuilder)

Example 5 with ResourceInformation

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

the class ClientResourceUpsert method setRelationsField.

@Override
protected void setRelationsField(Object newResource, RegistryEntry registryEntry, Map.Entry<String, Relationship> property, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
    Relationship relationship = property.getValue();
    if (!relationship.getData().isPresent()) {
        ObjectNode links = relationship.getLinks();
        if (links != null) {
            // create proxy to lazy load relations
            String fieldName = property.getKey();
            ResourceInformation resourceInformation = registryEntry.getResourceInformation();
            ResourceField field = resourceInformation.findRelationshipFieldByName(fieldName);
            Class elementType = field.getElementType();
            Class collectionClass = field.getType();
            JsonNode relatedNode = links.get("related");
            if (relatedNode != null) {
                String url = null;
                if (relatedNode.has(SerializerUtil.HREF)) {
                    JsonNode hrefNode = relatedNode.get(SerializerUtil.HREF);
                    if (hrefNode != null) {
                        url = hrefNode.asText().trim();
                    }
                } else {
                    url = relatedNode.asText().trim();
                }
                Object proxy = proxyFactory.createCollectionProxy(elementType, collectionClass, url);
                field.getAccessor().setValue(newResource, proxy);
            }
        }
    } else {
        // set elements
        super.setRelationsField(newResource, registryEntry, property, queryAdapter, parameterProvider);
    }
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Relationship(io.crnk.core.engine.document.Relationship) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)167 Test (org.junit.Test)79 ResourceField (io.crnk.core.engine.information.resource.ResourceField)76 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)60 QuerySpec (io.crnk.core.queryspec.QuerySpec)16 Resource (io.crnk.core.engine.document.Resource)15 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)15 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)9 Task (io.crnk.core.mock.models.Task)9 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)9 HashSet (java.util.HashSet)9 Collection (java.util.Collection)8 Relationship (io.crnk.core.engine.document.Relationship)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)7 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)7 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)7 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)7 Set (java.util.Set)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6