Search in sources :

Example 1 with Operation

use of io.crnk.operations.Operation in project crnk-framework by crnk-project.

the class OperationsModule method fetchUpToDateResponses.

protected void fetchUpToDateResponses(List<OrderedOperation> orderedOperations, OperationResponse[] responses) {
    RequestDispatcher requestDispatcher = moduleContext.getRequestDispatcher();
    // get current set of resources after all the updates have been applied
    for (OrderedOperation orderedOperation : orderedOperations) {
        Operation operation = orderedOperation.getOperation();
        OperationResponse operationResponse = responses[orderedOperation.getOrdinal()];
        boolean isPost = operation.getOp().equalsIgnoreCase(HttpMethod.POST.toString());
        boolean isPatch = operation.getOp().equalsIgnoreCase(HttpMethod.PATCH.toString());
        if (isPost || isPatch) {
            Resource resource = operationResponse.getSingleData().get();
            String path = resource.getType() + "/" + resource.getId();
            String method = HttpMethod.GET.toString();
            RepositoryMethodParameterProvider parameterProvider = null;
            Map<String, Set<String>> parameters = new HashMap<>();
            parameters.put("include", getLoadedRelationshipNames(resource));
            Response response = requestDispatcher.dispatchRequest(path, method, parameters, parameterProvider, null);
            copyDocument(operationResponse, response.getDocument());
            operationResponse.setIncluded(null);
        }
    }
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) OperationResponse(io.crnk.operations.OperationResponse) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Resource(io.crnk.core.engine.document.Resource) OrderedOperation(io.crnk.operations.server.order.OrderedOperation) OrderedOperation(io.crnk.operations.server.order.OrderedOperation) Operation(io.crnk.operations.Operation) OperationResponse(io.crnk.operations.OperationResponse) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher)

Example 2 with Operation

use of io.crnk.operations.Operation in project crnk-framework by crnk-project.

the class OperationsModule method enrichTypeIdInformation.

private void enrichTypeIdInformation(List<Operation> operations) {
    ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
    for (Operation operation : operations) {
        if (operation.getOp().equalsIgnoreCase(HttpMethod.DELETE.toString())) {
            String path = OperationParameterUtils.parsePath(operation.getPath());
            JsonPath jsonPath = (new PathBuilder(resourceRegistry)).build(path);
            Resource resource = new Resource();
            resource.setType(jsonPath.getResourceType());
            resource.setId(jsonPath.getIds().getIds().get(0));
            operation.setValue(resource);
        }
    }
}
Also used : PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) Resource(io.crnk.core.engine.document.Resource) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) OrderedOperation(io.crnk.operations.server.order.OrderedOperation) Operation(io.crnk.operations.Operation) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath)

Example 3 with Operation

use of io.crnk.operations.Operation in project crnk-framework by crnk-project.

the class OperationsCall method execute.

public void execute() {
    List<Operation> operations = new ArrayList<>();
    for (QueuedOperation queuedOperation : queuedOperations) {
        operations.add(queuedOperation.operation);
    }
    HttpAdapter adapter = client.getCrnk().getHttpAdapter();
    ObjectMapper mapper = client.getCrnk().getObjectMapper();
    try {
        String operationsJson = mapper.writer().writeValueAsString(operations.toArray(new Operation[operations.size()]));
        String url = client.getCrnk().getServiceUrlProvider().getUrl() + "/operations";
        HttpAdapterRequest request = adapter.newRequest(url, HttpMethod.PATCH, operationsJson);
        request.header(HttpHeaders.HTTP_CONTENT_TYPE, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
        request.header(HttpHeaders.HTTP_HEADER_ACCEPT, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
        HttpAdapterResponse response = request.execute();
        int status = response.code();
        if (status != 200) {
            // general issue, status of individual operations is important.
            throw new InternalServerErrorException("patch execution failed with status " + status);
        }
        String json = response.body();
        responses = Arrays.asList(mapper.readValue(json, OperationResponse[].class));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : HttpAdapterRequest(io.crnk.client.http.HttpAdapterRequest) HttpAdapter(io.crnk.client.http.HttpAdapter) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ArrayList(java.util.ArrayList) Operation(io.crnk.operations.Operation) IOException(java.io.IOException) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) OperationResponse(io.crnk.operations.OperationResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with Operation

use of io.crnk.operations.Operation in project crnk-framework by crnk-project.

the class DependencyOrderStrategy method order.

@Override
public List<OrderedOperation> order(List<Operation> operations) {
    Map<String, GraphUtils.Node> nodeMap = new HashMap<>();
    List<GraphUtils.Node> nodeList = new ArrayList<>();
    for (int i = 0; i < operations.size(); i++) {
        Operation operation = operations.get(i);
        String key = toKey(operation.getValue());
        if (nodeMap.containsKey(key)) {
            throw new UnsupportedOperationException("cannot modify same resource with multiple operations: type=" + operation.getValue().getType() + " id=" + operation.getValue().getId());
        }
        GraphUtils.Node node = new GraphUtils.Node(key, new OrderedOperation(operation, i));
        nodeMap.put(key, node);
        nodeList.add(node);
    }
    buildDependencyGraph(operations, nodeMap);
    List<GraphUtils.Node> sortedNodes = GraphUtils.sort(nodeList);
    List<OrderedOperation> dependencySortedOperations = new ArrayList<>();
    for (GraphUtils.Node node : sortedNodes) {
        dependencySortedOperations.add((OrderedOperation) node.getValue());
    }
    return moveDeletionsToEnd(dependencySortedOperations);
}
Also used : Operation(io.crnk.operations.Operation) GraphUtils(io.crnk.operations.internal.GraphUtils)

Example 5 with Operation

use of io.crnk.operations.Operation in project crnk-framework by crnk-project.

the class DependencyOrderStrategy method checkDependency.

private void checkDependency(Map<String, GraphUtils.Node> nodes, Operation operation, ResourceIdentifier dependencyId) {
    String dependencyKey = toKey(dependencyId);
    GraphUtils.Node node = nodes.get(toKey(operation.getValue()));
    if (nodes.containsKey(dependencyKey)) {
        GraphUtils.Node dependentNode = nodes.get(dependencyKey);
        Operation dependentOperation = ((OrderedOperation) dependentNode.getValue()).getOperation();
        if (HttpMethod.POST.name().equalsIgnoreCase(dependentOperation.getOp())) {
            dependentNode.addEdge(node);
        }
    }
}
Also used : Operation(io.crnk.operations.Operation) GraphUtils(io.crnk.operations.internal.GraphUtils)

Aggregations

Operation (io.crnk.operations.Operation)7 Resource (io.crnk.core.engine.document.Resource)3 OperationResponse (io.crnk.operations.OperationResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 GraphUtils (io.crnk.operations.internal.GraphUtils)2 OrderedOperation (io.crnk.operations.server.order.OrderedOperation)2 IOException (java.io.IOException)2 HttpAdapter (io.crnk.client.http.HttpAdapter)1 HttpAdapterRequest (io.crnk.client.http.HttpAdapterRequest)1 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)1 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)1 Response (io.crnk.core.engine.dispatcher.Response)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)1 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)1 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)1 RepositoryMethodParameterProvider (io.crnk.legacy.internal.RepositoryMethodParameterProvider)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1