Commit f15c1dff authored by Matija Obreza's avatar Matija Obreza
Browse files

AccessionUploader must roll-back for Throwable

parent 4c797cbd
...@@ -29,7 +29,6 @@ import java.util.UUID; ...@@ -29,7 +29,6 @@ import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator; import javax.validation.Validator;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
...@@ -144,9 +143,9 @@ public class AccessionUploader implements InitializingBean { ...@@ -144,9 +143,9 @@ public class AccessionUploader implements InitializingBean {
* @param updates the updates * @param updates the updates
* @throws IOException * @throws IOException
*/ */
@Transactional(timeout = 250, isolation = Isolation.READ_UNCOMMITTED, rollbackFor = Exception.class) @Transactional(timeout = 250, isolation = Isolation.READ_UNCOMMITTED, rollbackFor = Throwable.class)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE')")
public List<AccessionOpResponse> upsertAccessions(FaoInstitute institute, ArrayNode updates) throws InvalidApiUsageException, ConstraintViolationException { public List<AccessionOpResponse> upsertAccessions(FaoInstitute institute, ArrayNode updates) {
assert (updates.isArray()); assert (updates.isArray());
StopWatch stopWatch = StopWatch.createStarted(); StopWatch stopWatch = StopWatch.createStarted();
ObjectReader reader = objectMapper.readerFor(Accession.class); ObjectReader reader = objectMapper.readerFor(Accession.class);
...@@ -230,6 +229,12 @@ public class AccessionUploader implements InitializingBean { ...@@ -230,6 +229,12 @@ public class AccessionUploader implements InitializingBean {
throw new InvalidApiUsageException("DOI not included for accession with registered DOI=" + accession.getDoi()); throw new InvalidApiUsageException("DOI not included for accession with registered DOI=" + accession.getDoi());
} }
if (accession.getDoi() != null) {
if (! accession.getAccessionNumber().equalsIgnoreCase(updateA.getAccessionNumber())) {
throw new InvalidApiUsageException("Provided ACCENUMB for doi=" + accession.getDoi() + " does not match current " + accession.getAccessionNumber());
}
}
if (! accession.getInstitute().getId().equals(institute.getId())) { if (! accession.getInstitute().getId().equals(institute.getId())) {
throw new InvalidApiUsageException("Accession does not belong to institute " + institute.getCode()); throw new InvalidApiUsageException("Accession does not belong to institute " + institute.getCode());
} }
...@@ -289,7 +294,7 @@ public class AccessionUploader implements InitializingBean { ...@@ -289,7 +294,7 @@ public class AccessionUploader implements InitializingBean {
} else if ("newInstituteCode".equals(fieldName)) { } else if ("newInstituteCode".equals(fieldName)) {
updateInstitute(update.get("newInstituteCode").textValue(), accession); updateInstitute(update.get("newInstituteCode").textValue(), accession);
} else if ("accessionNumber".equals(fieldName) || "newAcceNumb".equals(fieldName)) { } else if ("newAcceNumb".equals(fieldName)) {
updateAccessionNumber(update.get("accessionNumber"), update.get("newAcceNumb"), accession); updateAccessionNumber(update.get("accessionNumber"), update.get("newAcceNumb"), accession);
} else if ("taxonomy".equals(fieldName)) { } else if ("taxonomy".equals(fieldName)) {
...@@ -391,11 +396,6 @@ public class AccessionUploader implements InitializingBean { ...@@ -391,11 +396,6 @@ public class AccessionUploader implements InitializingBean {
// Set provided accessionNumber and ignore newAcceNumb // Set provided accessionNumber and ignore newAcceNumb
accession.setAccessionNumber(accessionNumber); accession.setAccessionNumber(accessionNumber);
} else if (accession.getDoi() != null) {
// DO NOT IMPLICITLY UPDATE ACCESSION NUMBERS
if (! accession.getAccessionNumber().equalsIgnoreCase(accessionNumber)) {
throw new InvalidApiUsageException("ACCENUMB for doi=" + accession.getDoi() + " does not match current " + accession.getAccessionNumber());
}
} }
if (StringUtils.isNotBlank(newAcceNumb)) { if (StringUtils.isNotBlank(newAcceNumb)) {
......
...@@ -363,12 +363,17 @@ public class AccessionUploaderTest extends AbstractServicesTest { ...@@ -363,12 +363,17 @@ public class AccessionUploaderTest extends AbstractServicesTest {
@Test @Test
public void testComplainWrongAccenumbWithDOI() { public void testComplainWrongAccenumbWithDOI() {
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, DOI_1)); accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, DOI_1));
assertThat(accessionRepository.count(), is(1l));
Accession a1 = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1); Accession a1 = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1);
assertThat(a1, notNullValue()); assertThat(a1, notNullValue());
List<AccessionOpResponse> res = accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_2, DOI_1)); List<AccessionOpResponse> res = accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_1, DOI_1));
assertThat(accessionRepository.count(), is(1l));
a1 = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1);
assertThat(a1, notNullValue());
assertThat(res.get(0).getError(), is("Provided ACCENUMB for doi=" + DOI_1 + " does not match current " + ACCENUMB_1));
assertThat(res.get(0).getResult().getAction(), is(UpsertResult.Type.ERROR)); assertThat(res.get(0).getResult().getAction(), is(UpsertResult.Type.ERROR));
assertThat(res.get(0).getError(), is("ACCENUMB for doi=" + DOI_1 + " does not match current " + ACCENUMB_1));
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment