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);
}
}
}
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);
}
}
}
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);
}
}
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);
}
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);
}
}
}
Aggregations