Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#949 add param updateId to assertions add service #950

Merged
merged 1 commit into from
Mar 5, 2025
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
17 changes: 16 additions & 1 deletion src/main/java/au/org/ala/biocache/dto/UserAssertions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package au.org.ala.biocache.dto;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

public class UserAssertions extends ArrayList<QualityAssertion> {
Expand All @@ -19,4 +21,17 @@ public boolean deleteUuid(String uuid) {

return changed;
}
}

public boolean updateComment(String uuid, String comment) {
boolean changed = false;
for (int i = 0; i < this.size(); i++) {
if (this.get(i).getUuid().equals(uuid) && StringUtils.compare(this.get(i).comment, comment) != 0) {
this.get(i).comment = comment;
changed = true;
break;
}
}

return changed;
}
}
22 changes: 22 additions & 0 deletions src/main/java/au/org/ala/biocache/service/AssertionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ public Optional<QualityAssertion> addAssertion(
return Optional.empty();
}

public boolean editAssertion(
String recordUuid,
String assertionId,
String comment
) throws IOException {
logger.debug("Editing assertion to: " + recordUuid + ", assertionId: " + assertionId + ", comment: " + comment);

SolrDocument sd = searchDAO.getOcc(recordUuid);
// only when record uuid is valid
if (sd != null) {
UserAssertions userAssertions = store.get(UserAssertions.class, recordUuid).orElse(new UserAssertions());
// if there's any change made
if (userAssertions.updateComment(assertionId, comment)) {
// Update assertions
updateUserAssertions(recordUuid, userAssertions);
return true;
}
}

return false;
}


public boolean deleteAssertion(String recordUuid, String assertionUuid) throws IOException {
SolrDocument sd = searchDAO.getOcc(recordUuid);
Expand Down
36 changes: 24 additions & 12 deletions src/main/java/au/org/ala/biocache/web/AssertionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.entity.ContentType;
import org.apache.log4j.Logger;
Expand Down Expand Up @@ -156,10 +157,11 @@ public ResponseEntity addAssertionWithParams(
@RequestParam(value = "assertionUuid", required = false) String assertionUuid,
@RequestParam(value = "relatedRecordId", required = false) String relatedRecordId,
@RequestParam(value = "relatedRecordReason", required = false) String relatedRecordReason,
@RequestParam(value = "updateId", required = false) String updateId,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

return addAssertion(recordUuid, code, userId, userDisplayName, comment, userAssertionStatus, assertionUuid, relatedRecordId, relatedRecordReason, request, response);
return addAssertion(recordUuid, code, userId, userDisplayName, comment, userAssertionStatus, assertionUuid, relatedRecordId, relatedRecordReason, updateId, request, response);
}

/**
Expand Down Expand Up @@ -243,6 +245,7 @@ public ResponseEntity addAssertion(
@RequestParam(value = "assertionUuid", required = false) String assertionUuid,
@RequestParam(value = "relatedRecordId", required = false) String relatedRecordId,
@RequestParam(value = "relatedRecordReason", required = false) String relatedRecordReason,
@RequestParam(value = "updateId", required = false) String updateId,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

Expand All @@ -251,17 +254,26 @@ public ResponseEntity addAssertion(
}

try {
Optional<QualityAssertion> qa = assertionService.addAssertion(
recordUuid, code, comment, userId, userDisplayName,
userAssertionStatus, assertionUuid, relatedRecordId, relatedRecordReason
);
if (qa.isPresent()) {

String server = request.getSession().getServletContext().getInitParameter("serverName");
return ResponseEntity
.created(new URI(server + "/occurrences/" + recordUuid + "/assertions/" + qa.get().getUuid()))
.contentLength(0)
.build();
// if the updateId is provided then we are editing an existing assertion
if (StringUtils.isEmpty(updateId)) {
// add the assertion
Optional<QualityAssertion> qa = assertionService.addAssertion(
recordUuid, code, comment, userId, userDisplayName,
userAssertionStatus, assertionUuid, relatedRecordId, relatedRecordReason
);
if (qa.isPresent()) {

String server = request.getSession().getServletContext().getInitParameter("serverName");
return ResponseEntity
.created(new URI(server + "/occurrences/" + recordUuid + "/assertions/" + qa.get().getUuid()))
.contentLength(0)
.build();
}
} else {
// update the assertion
if (assertionService.editAssertion(recordUuid, updateId, comment)) {
return ResponseEntity.ok().contentLength(0).build();
}
}

throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
Expand Down