diff --git a/core/src/main/java/org/openmbee/mms/core/objects/ElementsCommitResponse.java b/core/src/main/java/org/openmbee/mms/core/objects/ElementsCommitResponse.java index 4a0a42732..ab3d365e7 100644 --- a/core/src/main/java/org/openmbee/mms/core/objects/ElementsCommitResponse.java +++ b/core/src/main/java/org/openmbee/mms/core/objects/ElementsCommitResponse.java @@ -1,12 +1,16 @@ package org.openmbee.mms.core.objects; import io.swagger.v3.oas.annotations.media.Schema; +import org.openmbee.mms.json.ElementJson; + +import java.util.List; public class ElementsCommitResponse extends ElementsResponse { @Schema(nullable = true) private String commitId; + private List deleted; public String getCommitId() { return commitId; } @@ -14,4 +18,13 @@ public String getCommitId() { public void setCommitId(String commitId) { this.commitId = commitId; } + + public List getDeleted() { + return deleted; + } + + public ElementsResponse setDeleted(List deleted) { + this.deleted = deleted; + return this; + } } diff --git a/core/src/main/java/org/openmbee/mms/core/objects/ElementsRequest.java b/core/src/main/java/org/openmbee/mms/core/objects/ElementsRequest.java index 1450473b7..9b26dd8fa 100644 --- a/core/src/main/java/org/openmbee/mms/core/objects/ElementsRequest.java +++ b/core/src/main/java/org/openmbee/mms/core/objects/ElementsRequest.java @@ -10,6 +10,8 @@ public class ElementsRequest extends BaseRequest { @Schema(required = true) private List elements; + private List deletes; + public List getElements() { return elements; } @@ -17,4 +19,12 @@ public List getElements() { public void setElements(List elements) { this.elements = elements; } + + public List getDeletes() { + return deletes; + } + + public void setDeletes(List deletes) { + this.deletes = deletes; + } } diff --git a/crud/src/main/java/org/openmbee/mms/crud/services/DefaultNodeService.java b/crud/src/main/java/org/openmbee/mms/crud/services/DefaultNodeService.java index 59b860992..564cc6f71 100644 --- a/crud/src/main/java/org/openmbee/mms/crud/services/DefaultNodeService.java +++ b/crud/src/main/java/org/openmbee/mms/crud/services/DefaultNodeService.java @@ -194,12 +194,22 @@ public ElementsCommitResponse createOrUpdate(String projectId, String refId, Ele .processPostJson(req.getElements(), overwriteJson, createCommit(user, refId, projectId, req, commitId), this); + if (req.getDeletes() != null && !req.getDeletes().isEmpty()) { + NodeChangeInfo delete = nodeDeleteHelper.processDeleteJson(req.getDeletes(), createCommit(user, refId, projectId, req, info.getCommitJson().getCommitId()), this); + info.getCommitJson().getDeleted().addAll(delete.getCommitJson().getDeleted()); + info.getDeletedMap().putAll(delete.getDeletedMap()); + info.getToSaveNodeMap().putAll(delete.getToSaveNodeMap()); + info.getOldDocIds().addAll(delete.getOldDocIds()); + info.getRejected().putAll(delete.getRejected()); + } + commitChanges(info); ElementsCommitResponse response = new ElementsCommitResponse(); response.getElements().addAll(info.getUpdatedMap().values()); response.setRejected(new ArrayList<>(info.getRejected().values())); - if(!info.getUpdatedMap().isEmpty()) { + response.setDeleted(new ArrayList<>(info.getDeletedMap().values())); + if(!info.getUpdatedMap().isEmpty() || !info.getDeletedMap().isEmpty()) { response.setCommitId(info.getCommitJson().getId()); } return response; diff --git a/crud/src/main/java/org/openmbee/mms/crud/services/NodePostHelper.java b/crud/src/main/java/org/openmbee/mms/crud/services/NodePostHelper.java index e5cf967c5..34f9a3112 100644 --- a/crud/src/main/java/org/openmbee/mms/crud/services/NodePostHelper.java +++ b/crud/src/main/java/org/openmbee/mms/crud/services/NodePostHelper.java @@ -91,6 +91,10 @@ public NodeChangeInfo processPostJson(List elements, boolean overwr } } else { updated = true; + String ARTIFACTS = "_artifacts"; + if (indexElement.containsKey(ARTIFACTS) && !element.containsKey(ARTIFACTS)) { + element.put(ARTIFACTS, indexElement.get(ARTIFACTS)); + } element.setCreated(indexElement.getCreated()); element.setCreator(indexElement.getCreator()); } diff --git a/example/artifacts.postman_collection.json b/example/artifacts.postman_collection.json index f6300eb4d..3e7027937 100644 --- a/example/artifacts.postman_collection.json +++ b/example/artifacts.postman_collection.json @@ -670,13 +670,64 @@ }, "response": [] }, + { + "name": "overwriting element json without _artifacts won't remove it", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"element artifact persists\", function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.elements[0]._artifacts.length).to.eql(1);", + "});", + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{\n\t\"elements\": [\n\t\t{\n\t\t\t\"id\": \"x\",\n\t\t\t\"name\": \"overwrite x\"\n\t\t\t \n\t\t}\n\t]\n}" + }, + "url": { + "raw": "{{host}}/projects/arta/refs/master/elements?overwrite=true", + "host": [ + "{{host}}" + ], + "path": [ + "projects", + "arta", + "refs", + "master", + "elements" + ], + "query": [ + { + "key": "overwrite", + "value": "true" + } + ] + } + }, + "response": [] + }, { "name": "create other user", "event": [ { "listen": "test", "script": { - "id": "3e68a393-9a9f-4759-b8ec-a7e51917ee62", "exec": [ "pm.test(\"Status code is 200\", function () {", " pm.response.to.have.status(200);", diff --git a/example/crud.postman_collection.json b/example/crud.postman_collection.json index e51faf156..0fa48976b 100644 --- a/example/crud.postman_collection.json +++ b/example/crud.postman_collection.json @@ -3563,6 +3563,237 @@ } }, "response": [] + }, + { + "name": "create project zz", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{\n\t\"projects\": [\n\t\t{\n\t\t\t\"id\": \"zz\", \n\t\t\t\"name\": \"zz\",\n\t\t\t\"orgId\": \"a\",\n\t\t\t\"schema\": \"default\"\n\t\t}\n\t]\n}" + }, + "url": { + "raw": "{{host}}/projects", + "host": [ + "{{host}}" + ], + "path": [ + "projects" + ] + } + }, + "response": [] + }, + { + "name": "add elements to project zz", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{\n\t\"elements\": [\n\t\t{\n\t\t\t\"id\": \"x\",\n\t\t\t\"name\": \"x\"\n\t\t}, {\n\t\t\t\"id\": \"y\", \n\t\t\t\"name\": \"y\"\n\t\t}, {\n\t\t\t\"id\": \"z\",\n\t\t\t\"name\": \"z\"\n\t\t}\n\t]\n}" + }, + "url": { + "raw": "{{host}}/projects/zz/refs/master/elements", + "host": [ + "{{host}}" + ], + "path": [ + "projects", + "zz", + "refs", + "master", + "elements" + ] + } + }, + "response": [] + }, + { + "name": "update and delete elements in same commit", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"response has elements\", function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.elements.length).to.eql(2);", + " pm.expect(jsonData.deleted.length).to.eql(1);", + "});", + "", + "pm.environment.set(\"hybridCommit\", pm.response.json().commitId);" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{\n\t\"elements\": [\n\t\t{\n\t\t\t\"id\": \"x\",\n\t\t\t\"name\": \"xx\"\n\t\t}, {\n\t\t\t\"id\": \"y\", \n\t\t\t\"name\": \"yy\"\n\t\t}\n\t],\n \"deletes\": [{\"id\": \"z\"}]\n}" + }, + "url": { + "raw": "{{host}}/projects/zz/refs/master/elements", + "host": [ + "{{host}}" + ], + "path": [ + "projects", + "zz", + "refs", + "master", + "elements" + ] + } + }, + "response": [] + }, + { + "name": "get deleted element", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 410\", function () {", + " pm.response.to.have.status(410);", + "});" + ], + "type": "text/javascript" + } + } + ], + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{\n\t\"elements\": [\n\t\t{\n\t\t\t\"id\": \"x\",\n\t\t\t\"name\": \"xx\"\n\t\t}, {\n\t\t\t\"id\": \"y\", \n\t\t\t\"name\": \"yy\"\n\t\t}\n\t],\n \"deletes\": [{\"id\": \"z\"}]\n}" + }, + "url": { + "raw": "{{host}}/projects/zz/refs/master/elements/z", + "host": [ + "{{host}}" + ], + "path": [ + "projects", + "zz", + "refs", + "master", + "elements", + "z" + ] + } + }, + "response": [] + }, + { + "name": "get commit object", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"commit has right data\", function () {", + " var jsonData = pm.response.json();", + " pm.expect(jsonData.commits[0].updated.length).to.eql(2);", + " pm.expect(jsonData.commits[0].deleted.length).to.eql(1);", + "", + "});", + "", + "" + ], + "type": "text/javascript" + } + } + ], + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "type": "text", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{\n\t\"elements\": [\n\t\t{\n\t\t\t\"id\": \"x\",\n\t\t\t\"name\": \"xx\"\n\t\t}, {\n\t\t\t\"id\": \"y\", \n\t\t\t\"name\": \"yy\"\n\t\t}\n\t],\n \"deletes\": [{\"id\": \"z\"}]\n}" + }, + "url": { + "raw": "{{host}}/projects/zz/commits/{{hybridCommit}}", + "host": [ + "{{host}}" + ], + "path": [ + "projects", + "zz", + "commits", + "{{hybridCommit}}" + ] + } + }, + "response": [] } ], "auth": { diff --git a/gradle.properties b/gradle.properties index 6fcfb3d8f..08d176e97 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -version=4.0.12 +version=4.0.13 group=org.openmbee.mms springBootVersion=2.6.7