Commit a5bca192 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Update of Accession Aliases

parent e532e16b
......@@ -20,6 +20,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
......@@ -292,7 +293,12 @@ public class AccessionUploader implements InitializingBean {
updateColl(update.get("coll"), updateA.getAccessionId().getColl(), accession.getAccessionId());
LOG.debug("Updated coll for 1 accession in {}ms", stopWatch.getSplitTime());
} else if ("acceName".equals(fieldName) || "otherNumb".equals(fieldName)) {
} else if ("donorNumb".equals(fieldName)) {
stopWatch.split();
updateDonorNumbAlias(updateA.getDonorCode(), updateA.getDonorNumb(), accession.getAccessionId());
LOG.debug("Updated donorNumb alias for 1 accession in {}ms", stopWatch.getSplitTime());
} else if (Arrays.stream(AliasType.values()).anyMatch(aliasType -> aliasType.name().equals(fieldName.toUpperCase()))) {
stopWatch.split();
updateAliases(fieldName, update.get(fieldName), accession.getAccessionId());
LOG.debug("Updated aliases for 1 accession in {}ms", stopWatch.getSplitTime());
......@@ -369,6 +375,16 @@ public class AccessionUploader implements InitializingBean {
}
}
private void updateDonorNumbAlias(String donorCode, String donorNumb, AccessionId accession) {
accession.getAliases().stream().filter(al -> AliasType.DONORNUMB.equals(al.getAliasType())).findFirst().ifPresent(toRemove -> accession.getAliases().remove(toRemove));
if (donorNumb != null && donorCode != null) {
AccessionAlias alias = new AccessionAlias(AliasType.DONORNUMB, donorCode + ":" + donorNumb);
alias.setAccession(accession);
accession.getAliases().add(alias);
}
}
private void updateAliases(String fieldName, JsonNode jsonNode, AccessionId accession) {
if (jsonNode == null) {
return;
......
......@@ -386,6 +386,83 @@ public class AccessionControllerTest extends AbstractApiTest {
upsertWithErrors(accessionJson);
}
@Test
public void testAliasesWithArrayValue() throws Exception {
ObjectNode accessionJson = setUpAccession();
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
ArrayNode bredNumbValues = objectMapper.createArrayNode();
bredNumbValues.add("BredNumb1");
bredNumbValues.add("BredNumb2");
accessionJson.set("bredNumb", bredNumbValues);
/*@formatter:off*/
upsert(accessionJson)
.andExpect(jsonPath("$[0].result.action", equalTo("UPDATE")));
/*@formatter:on*/
assertThat(accessionRepository.count(), is(1L));
UUID uuid = accessionRepository.findAll().get(0).getUuid();
Accession result = fetch(uuid);
assertThat(result.getAccessionId().getAliases().size(), is(2));
assertThat(result.getAccessionId().getAliases().get(0).getName(), is("BredNumb1"));
assertThat(result.getAccessionId().getAliases().get(1).getName(), is("BredNumb2"));
assertThat(result.getAccessionId().getAliases().get(0).getAliasType(), is(AliasType.BREDNUMB));
accessionJson.set("bredNumb", null);
upsert(accessionJson);
result = fetch(uuid);
assertThat("bredNumb must be an empty array", result.getAccessionId().getAliases().size(), is(0));
}
@Test
public void testDonorNumbAliases() throws Exception {
ObjectNode accessionJson = setUpAccession();
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
accessionJson.put("donorNumb", "IPV0018");
accessionJson.put("donorCode", "ALB004");
/*@formatter:off*/
upsert(accessionJson)
.andExpect(jsonPath("$[0].result.action", equalTo("UPDATE")));
/*@formatter:on*/
assertThat(accessionRepository.count(), is(1L));
UUID uuid = accessionRepository.findAll().get(0).getUuid();
Accession result = fetch(uuid);
assertThat(result.getAccessionId().getAliases().size(), is(1));
assertThat(result.getAccessionId().getAliases().get(0).getName(), is("IPV0018"));
assertThat(result.getAccessionId().getAliases().get(0).getUsedBy(), is("ALB004"));
assertThat(result.getAccessionId().getAliases().get(0).getAliasType(), is(AliasType.DONORNUMB));
accessionJson.put("donorNumb", "IPV0021");
accessionJson.put("donorCode", "ALB005");
/*@formatter:off*/
upsert(accessionJson)
.andExpect(jsonPath("$[0].result.action", equalTo("UPDATE")));
/*@formatter:on*/
assertThat(accessionRepository.count(), is(1L));
uuid = accessionRepository.findAll().get(0).getUuid();
result = fetch(uuid);
assertThat(result.getAccessionId().getAliases().size(), is(1));
assertThat(result.getAccessionId().getAliases().get(0).getName(), is("IPV0021"));
assertThat(result.getAccessionId().getAliases().get(0).getUsedBy(), is("ALB005"));
assertThat(result.getAccessionId().getAliases().get(0).getAliasType(), is(AliasType.DONORNUMB));
accessionJson.set("donorNumb", null);
upsert(accessionJson);
result = fetch(uuid);
assertThat("donorNumb must be an empty array", result.getAccessionId().getAliases().size(), is(0));
}
@Test
public void testCollNumb() throws Exception {
ObjectNode accessionJson = setUpAccession();
......
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