Commit 2f32b0d4 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'fix-taxonomy-update' into 'master'

Fix: Taxonomy should not be updated when only "genus" is provided

See merge request genesys-pgr/genesys-server!285
parents f100a059 42c401dd
......@@ -33,6 +33,7 @@ import javax.persistence.UniqueConstraint;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.GlobalVersionedAuditedModel;
import org.genesys2.server.model.impl.CropTaxonomy;
import org.genesys2.server.model.json.Api1Constants;
import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
......@@ -40,6 +41,7 @@ import org.springframework.data.elasticsearch.annotations.FieldIndex;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@Cacheable
@Entity
......@@ -51,18 +53,23 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
private static final long serialVersionUID = 8881324404490162933L;
@Column(nullable = false, length = 100)
@JsonProperty(value = Api1Constants.Accession.GENUS)
private String genus;
@Column(nullable = false, length = 100)
@JsonProperty(value = Api1Constants.Accession.SPECIES)
private String species;
@Column(nullable = false, length = 100)
@JsonProperty(value = Api1Constants.Accession.SPAUTHOR)
private String spAuthor;
@Column(nullable = false, length = 100)
@JsonProperty(value = Api1Constants.Accession.SUBTAXA)
private String subtaxa;
@Column(nullable = false, length = 100)
@JsonProperty(value = Api1Constants.Accession.SUBTAUTHOR)
private String subtAuthor;
@Column(nullable = false)
......
......@@ -44,6 +44,7 @@ import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.json.Api1Constants;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.CropService;
......@@ -429,9 +430,36 @@ public class AccessionUploader implements InitializingBean {
} else if (taxonomy.equalTo(source)) {
// NOOP
} else {
LOG.trace("Ensuring taxonomy {}", jsonNode);
taxonomy = taxonomyService.ensureTaxonomy(source);
accession.setTaxonomy(taxonomy);
LOG.trace("Updating taxonomy of {} with {}", accession.getAccessionNumber(), jsonNode);
if (jsonNode.has(Api1Constants.Accession.GENUS) && jsonNode.has(Api1Constants.Accession.SPECIES)) {
Taxonomy2 updated = new Taxonomy2(taxonomy);
updated.setGenus(source.getGenus());
updated.setSpecies(source.getSpecies());
if (jsonNode.has(Api1Constants.Accession.SPAUTHOR)) {
updated.setSpAuthor(source.getSpAuthor());
} else {
updated.setSpAuthor(null);
}
if (jsonNode.has(Api1Constants.Accession.SUBTAXA)) {
updated.setSubtaxa(source.getSubtaxa());
if (jsonNode.has(Api1Constants.Accession.SUBTAUTHOR)) {
updated.setSubtAuthor(source.getSubtAuthor());
} else {
updated.setSubtAuthor(null);
}
} else {
updated.setSubtaxa(null);
updated.setSubtAuthor(null);
}
if (! updated.equalTo(taxonomy)) {
LOG.trace("Ensuring taxonomy {}", updated);
updated = taxonomyService.ensureTaxonomy(updated);
accession.setTaxonomy(updated);
}
}
}
}
......
......@@ -22,6 +22,7 @@ import java.util.List;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.json.Api1Constants;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
......@@ -243,9 +244,33 @@ public class AccessionUploaderTest extends AbstractServicesTest {
Accession a2 = accessionRepository.findByDoi(DOI_1);
assertThat(a2.getId(), equalTo(a1.getId()));
assertThat(a2.getUuid(), equalTo(a1.getUuid()));
assertThat(a2.getGenus(), is(GENUS_2));
// Genus not updated, missing species
assertThat(a2.getGenus(), is(GENUS_1));
assertThat(a2.getAccessionNumber(), is(ACCENUMB_2));
}
/**
* Updating accession number and taxonomy must work with DOI only.
*/
@Test
public void testUpdateTaxonomysUsingDOI() {
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, DOI_1));
// update taxonomy
ArrayNode json = upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, DOI_1);
ObjectNode taxa = (ObjectNode) json.get(0).get("taxonomy");
taxa.put(Api1Constants.Accession.GENUS, GENUS_2);
taxa.put(Api1Constants.Accession.SPECIES, "species");
taxa.put(Api1Constants.Accession.SPAUTHOR, "M.");
accessionUploader.upsertAccessions(institute, json);
assertThat(accessionRepository.count(), is(1l));
Accession a2 = accessionRepository.findByDoi(DOI_1);
assertThat(a2.getGenus(), is(GENUS_2));
assertThat(a2.getTaxonomy().getSpecies(), is("species"));
assertThat(a2.getTaxonomy().getSpAuthor(), is("M."));
assertThat(a2.getTaxonomy().getSubtAuthor(), is(""));
}
/**
......@@ -261,6 +286,26 @@ public class AccessionUploaderTest extends AbstractServicesTest {
assertThat(op.get(0).getError(), is("Accession does not belong to institute " + institute2.getCode()));
}
@Test
public void testTaxonomyNotModified() {
// upload accesion with species
ArrayNode json = upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, DOI_1);
ObjectNode taxa = (ObjectNode) json.get(0).get("taxonomy");
taxa.put(Api1Constants.Accession.SPECIES, "species");
taxa.put(Api1Constants.Accession.SPAUTHOR, "M.");
accessionUploader.upsertAccessions(institute, json);
Accession a1 = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1);
assertThat(a1.getTaxonomy().getSpecies(), equalTo("species"));
assertThat(a1.getTaxonomy().getSpAuthor(), equalTo("M."));
// Update without providing taxonomy details
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, DOI_1));
Accession a1a = accessionRepository.findOne(institute, DOI_1, ACCENUMB_1, GENUS_1);
assertThat(a1a.getTaxonomy().getSpecies(), equalTo("species"));
assertThat(a1a.getTaxonomy().getSpAuthor(), equalTo("M."));
}
/**
* Report invalid field in JSON
......
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