Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public void addChildViews(ElementsResponse res, Map<String, String> params) {
for (ElementJson element: res.getElements()) {
if (cameoHelper.isView(element)) {
List<String> ownedAttributeIds = (List) element.get(CameoConstants.OWNEDATTRIBUTEIDS);
if (ownedAttributeIds == null) {
ownedAttributeIds = new ArrayList<>();
}
ElementsResponse ownedAttributes = this.read(element.getProjectId(), element.getRefId(),
buildRequest(ownedAttributeIds), params);
List<ElementJson> sorted = nodeGetHelper.sort(ownedAttributeIds, ownedAttributes.getElements());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ElementsRequest extends BaseRequest {

private List<ElementJson> deletes;

private String lastCommitId;

public List<ElementJson> getElements() {
return elements;
}
Expand All @@ -27,4 +29,12 @@ public List<ElementJson> getDeletes() {
public void setDeletes(List<ElementJson> deletes) {
this.deletes = deletes;
}

public String getLastCommitId() {
return lastCommitId;
}

public void setLastCommitId(String lastCommitId) {
this.lastCommitId = lastCommitId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class ElementsResponse extends BaseResponse<ElementsResponse> {

private List<ElementJson> elements;

private String commitId;
public ElementsResponse() {
this.elements = new ArrayList<>();
}
Expand All @@ -21,4 +21,11 @@ public ElementsResponse setElements(List<ElementJson> elements) {
this.elements = elements;
return this;
}
public String getCommitId() {
return commitId;
}

public void setCommitId(String commitId) {
this.commitId = commitId;
}
}
10 changes: 10 additions & 0 deletions core/src/main/java/org/openmbee/mms/core/services/NodeGetInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class NodeGetInfo {

Map<String, Rejection> rejected;

String commitId;


public Set<String> getReqIndexIds() {
return reqIndexIds;
Expand Down Expand Up @@ -87,4 +89,12 @@ public void addRejection(String id, Rejection rejection) {
this.rejected.put(id, rejection);
}

public void setCommitId(String commitId) {
this.commitId = commitId;
}

public String getCommitId() {
return this.commitId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,21 @@ public void readAsStream(String projectId, String refId,
String commitId = params.getOrDefault("commitId", null);
ContextHolder.setContext(projectId, refId);
List<Node> nodes;
String resCommitId;
if (commitId != null && !commitId.isEmpty()) {
if (!commitRepository.findByCommitId(commitId).isPresent()) {
throw new BadRequestException("commit id is invalid");
}
nodes = nodeRepository.findAll();
resCommitId = commitId;
} else {
nodes = nodeRepository.findAllByDeleted(false);
resCommitId = nodeGetHelper.getLatestRefCommitId();
}
String separator = "\n";
if (!"application/x-ndjson".equals(accept)) {
stream.write("{\"elements\":[".getBytes(StandardCharsets.UTF_8));
String intro = "{\"commitId\":" + (resCommitId == null ? "null" : "\"" + resCommitId + "\"") + ",\"elements\":[";
stream.write(intro.getBytes(StandardCharsets.UTF_8));
separator = ",";
}
final String sep = separator;
Expand Down Expand Up @@ -162,6 +166,11 @@ public ElementsResponse read(String projectId, String refId, String id,
ElementsResponse response = new ElementsResponse();
String commitId = params.getOrDefault("commitId", null);
response.getElements().addAll(nodeGetHelper.processGetAll(commitId, this));
if (commitId != null) {
response.setCommitId(commitId);
} else {
response.setCommitId(nodeGetHelper.getLatestRefCommitId());
}
return response;
}
}
Expand All @@ -178,6 +187,7 @@ public ElementsResponse read(String projectId, String refId, ElementsRequest req
ElementsResponse response = new ElementsResponse();
response.getElements().addAll(info.getActiveElementMap().values());
response.setRejected(new ArrayList<>(info.getRejected().values()));
response.setCommitId(info.getCommitId());
return response;
}

Expand All @@ -189,10 +199,11 @@ public ElementsCommitResponse createOrUpdate(String projectId, String refId, Ele
boolean overwriteJson = Boolean.parseBoolean(params.get("overwrite"));
nodePostHelper.setPreserveTimestamps(Boolean.parseBoolean(params.get("preserveTimestamps")));
String commitId = params.get("commitId");
String lastCommitId = req.getLastCommitId();

NodeChangeInfo info = nodePostHelper
.processPostJson(req.getElements(), overwriteJson,
createCommit(user, refId, projectId, req, commitId), this);
createCommit(user, refId, projectId, req, commitId), this, lastCommitId);

if (req.getDeletes() != null && !req.getDeletes().isEmpty()) {
NodeChangeInfo delete = nodeDeleteHelper.processDeleteJson(req.getDeletes(), createCommit(user, refId, projectId, req, info.getCommitJson().getCommitId()), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private NodeGetInfo processLatest(NodeGetInfo info, NodeService service) {
}
info.getActiveElementMap().put(nodeId, indexElement);
}
info.setCommitId(getLatestRefCommitId());
return info;
}

Expand All @@ -76,6 +77,7 @@ private NodeGetInfo processCommit(NodeGetInfo info, String commitId, NodeService
if (!commit.isPresent() ) {
throw new BadRequestException("commitId is invalid");
}
info.setCommitId(commitId);
Instant time = commit.get().getTimestamp(); //time of commit
List<String> refCommitIds = null; //get it later if needed
for (String nodeId : info.getReqElementMap().keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.openmbee.mms.core.services.NodeGetInfo;
import org.openmbee.mms.core.dao.BranchDAO;
import org.openmbee.mms.core.dao.CommitDAO;
import org.openmbee.mms.data.domains.scoped.Branch;
import org.openmbee.mms.data.domains.scoped.Commit;
import org.openmbee.mms.data.domains.scoped.Node;
import org.openmbee.mms.core.dao.NodeDAO;
import org.openmbee.mms.core.dao.NodeIndexDAO;
Expand Down Expand Up @@ -274,4 +276,14 @@ public boolean isPreserveTimestamps() {
public void setPreserveTimestamps(boolean preserveTimestamps) {
this.preserveTimestamps = preserveTimestamps;
}

public String getLatestRefCommitId() {
Optional<Branch> branch = branchRepository.findByBranchId(ContextHolder.getContext().getBranchId());
Optional<Commit> commit = commitRepository.findLatestByRef(branch.get());
if (commit.isPresent()) {
return commit.get().getCommitId();
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Optional;
import java.util.UUID;
import org.openmbee.mms.core.config.Formats;
import org.openmbee.mms.core.exceptions.ConflictException;
import org.openmbee.mms.core.objects.Rejection;
import org.openmbee.mms.core.services.NodeChangeInfo;
import org.openmbee.mms.core.services.NodeService;
Expand Down Expand Up @@ -54,8 +55,12 @@ public boolean diffUpdateJson(BaseJson element, Map<String, Object> existing,

// create new elastic id for all element json, update modified time, modifier (use dummy for now), set _projectId, _refId, _inRefIds
public NodeChangeInfo processPostJson(List<ElementJson> elements, boolean overwriteJson,
CommitJson cmjs, NodeService service) {

CommitJson cmjs, NodeService service, String lastCommitId) {
if (lastCommitId != null && !lastCommitId.isEmpty()) {
if (!lastCommitId.equals(getLatestRefCommitId())) {
throw new ConflictException("Given commitId " + lastCommitId + " is not the latest");
}
}
NodeChangeInfo info = initInfo(elements, cmjs);

// Logic for update/add
Expand Down
89 changes: 89 additions & 0 deletions example/crud.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,95 @@
},
"response": []
},
{
"name": "get all elements returns last commitId",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"response has 3 elements and correct commitId\", function () {",
" var jsonData = pm.response.json();",
" pm.expect(jsonData.elements.length).to.eql(3);",
" pm.expect(jsonData.commitId).to.eql(pm.environment.get(\"commitId1\"))",
"});",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text",
"disabled": true
}
],
"url": {
"raw": "{{host}}/projects/aa/refs/master/elements",
"host": [
"{{host}}"
],
"path": [
"projects",
"aa",
"refs",
"master",
"elements"
]
}
},
"response": []
},
{
"name": "post elements with wrong lastCommitId results in 409",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"response code is 409\", function () {",
" pm.response.to.have.status(409);",
"});",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{ \"lastCommitId\": \"blah\",\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/aa/refs/master/elements",
"host": [
"{{host}}"
],
"path": [
"projects",
"aa",
"refs",
"master",
"elements"
]
}
},
"response": []
},
{
"name": "update x",
"event": [
Expand Down