Commit 8065598c authored by Matija Obreza's avatar Matija Obreza
Browse files

Fixed unique constraint on Taxonomy2, using '' instead of null

parent e07aa6a9
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.listener.sample;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.TaxonomyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Upgrades taxonomy1 to taxonomy2
*
* @author mobreza
*/
@Component
public class TaxonomyUpgrade extends RunAsAdminListener {
@Autowired
GenesysService genesysService;
@Autowired
GenesysLowlevelRepository genesysLowlevelRepository;
@Autowired
TaxonomyService taxonomyService;
@Override
public void init() throws Exception {
_logger.info("Checking if default content exists");
long count = taxonomyService.countTaxonomy2();
_logger.warn("Taxonomy2 count=" + count);
if (count != 0) {
_logger.warn("Not changing taxonomy2");
return;
}
final AtomicLong counter = new AtomicLong(0);
List<Taxonomy> oldTaxa = taxonomyService.findAll();
for (Taxonomy t1 : oldTaxa) {
String genus = t1.getGenus();
if (StringUtils.isBlank(genus)) {
_logger.warn("Genus is blank at " + t1);
continue;
}
String species = t1.getSpecies();
String spAuthor = null;
String subtaxa = null;
String subtAuthor = null;
String x = genus + ' ' + species;
if (t1.getTaxonName().startsWith(x)) {
String foo = t1.getTaxonName().substring(x.length()).trim();
if (StringUtils.isNotBlank(foo)) {
subtaxa = foo;
}
}
// Make new taxonomy
Taxonomy2 t2 = taxonomyService.ensureTaxonomy2(genus, species, spAuthor, subtaxa, subtAuthor);
counter.incrementAndGet();
// Update all accessions
count = taxonomyService.upgradeTaxonomy(t1, t2);
_logger.info("Upgraded " + t1 + " to " + t2 + " on accessions=" + count);
}
_logger.warn("Updated Taxonomy2 count=" + counter);
}
}
......@@ -28,6 +28,7 @@ import javax.persistence.PrePersist;
import javax.persistence.Table;
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.hibernate.search.annotations.Indexed;
......@@ -46,13 +47,13 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
@Column(nullable = false, length = 100)
private String species;
@Column(nullable = true, length = 100)
@Column(nullable = false, length = 100)
private String spAuthor;
@Column(nullable = true, length = 100)
@Column(nullable = false, length = 100)
private String subtaxa;
@Column(nullable = true, length = 100)
@Column(nullable = false, length = 100)
private String subtAuthor;
@Column(nullable = false)
......@@ -103,11 +104,11 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
public String getTaxonName() {
StringBuilder sb = new StringBuilder();
sb.append(genus).append(" ").append(species);
if (spAuthor != null)
if (StringUtils.isNotBlank(spAuthor))
sb.append(" ").append(spAuthor);
if (subtaxa != null)
if (StringUtils.isNotBlank(subtaxa))
sb.append(" ").append(subtaxa);
if (subtAuthor != null)
if (StringUtils.isNotBlank(subtAuthor))
sb.append(" ").append(subtAuthor);
return sb.toString();
}
......
......@@ -18,15 +18,10 @@ package org.genesys2.server.service;
import java.util.List;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.genesys.Taxonomy2;
public interface TaxonomyService {
Taxonomy get(String genus, String species);
Taxonomy ensureTaxonomy(String genus, String species);
List<String> autocompleteGenus(String term);
List<String> autocompleteTaxonomy(String term);
......@@ -39,10 +34,6 @@ public interface TaxonomyService {
long countTaxonomy2();
List<Taxonomy> findAll();
long upgradeTaxonomy(Taxonomy t1, Taxonomy2 t2);
Taxonomy2 get(Long id);
}
......@@ -596,7 +596,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
if (toSave.size() > 0) {
LOG.info("Saving aliases count=" + toSave.size());
// LOG.info("Saving aliases count=" + toSave.size());
genesysService.saveAliases(toSave);
}
if (toRemove.size() > 0) {
......
......@@ -838,7 +838,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
methodsWithValues.add(method.getId());
}
}
@Override
public int countAccessions(String jsonFilter) {
try {
......@@ -999,8 +999,8 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
private void writeAccessionsCore(final ObjectNode filter, ZipOutputStream zos) throws IOException {
@SuppressWarnings("resource")
final CSVWriter csv = new CSVWriter(new BufferedWriter(new OutputStreamWriter(zos)), ',', '"', '\\', "\n");
csv.writeNext(new String[] { "genesysId", "uuid", "instCode", "acceNumb", "genus", "species", "fullTaxa", "orgCty", "acqSrc", "acqDate", "mlsStat", "available",
"storage", "sampStat", "duplSite", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate" });
csv.writeNext(new String[] { "genesysId", "uuid", "instCode", "acceNumb", "genus", "species", "fullTaxa", "orgCty", "acqSrc", "acqDate", "mlsStat",
"available", "storage", "sampStat", "duplSite", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate" });
final ResultSetHelper csvResultsetHelper = new ResultSetHelperService();
......@@ -1242,13 +1242,15 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override
@Transactional
public void saveAliases(List<AccessionAlias> aliases) {
accessionAliasRepository.save(aliases);
if (aliases.size() > 0)
accessionAliasRepository.save(aliases);
}
@Override
@Transactional
public void removeAliases(List<AccessionAlias> aliases) {
accessionAliasRepository.delete(aliases);
if (aliases.size() > 0)
accessionAliasRepository.delete(aliases);
}
@Override
......
......@@ -22,14 +22,13 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.persistence.domain.Taxonomy2Repository;
import org.genesys2.server.persistence.domain.TaxonomyRepository;
import org.genesys2.server.service.TaxonomyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
......@@ -37,9 +36,6 @@ import org.springframework.transaction.annotation.Transactional;
public class TaxonomyServiceImpl implements TaxonomyService {
public static final Log LOG = LogFactory.getLog(TaxonomyServiceImpl.class);
@Autowired
private TaxonomyRepository taxonomyRepository;
@Autowired
private Taxonomy2Repository taxonomy2Repository;
......@@ -48,47 +44,6 @@ public class TaxonomyServiceImpl implements TaxonomyService {
return taxonomy2Repository.findOne(id);
}
@Override
public Taxonomy get(String genus, String species) {
return taxonomyRepository.getByGenusAndSpecies(genus, species);
}
@Override
@Transactional(readOnly = false)
public Taxonomy ensureTaxonomy(String genus, String species) {
if (genus == null)
return null;
Taxonomy existing = taxonomyRepository.getByGenusAndSpecies(genus, species);
if (existing == null) {
return internalEnsure(genus, species);
}
return existing;
}
private synchronized Taxonomy internalEnsure(String genus, String species) {
Taxonomy taxonomy = taxonomyRepository.getByGenusAndSpecies(genus, species);
if (taxonomy == null) {
taxonomy = new Taxonomy();
if (LOG.isDebugEnabled())
LOG.debug("Inserting " + genus + " " + species);
taxonomy.setGenus(genus);
taxonomy.setSpecies(species);
taxonomy.setTaxonName((genus + " " + StringUtils.defaultIfEmpty(species, "")).trim());
try {
taxonomyRepository.save(taxonomy);
LOG.info("Inserted new:" + taxonomy);
// TODO Should update crop taxonomy lists?
} catch (Exception e) {
LOG.warn("Error " + e.getMessage() + " :" + taxonomy);
}
}
return taxonomy;
}
@Override
public List<String> autocompleteGenus(String term) {
return taxonomy2Repository.autocompleteGenus(term + "%", new PageRequest(0, 10));
......@@ -101,25 +56,25 @@ public class TaxonomyServiceImpl implements TaxonomyService {
@Override
// @Transactional(noRollbackFor = AssertionFailure.class)
@Transactional
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Taxonomy2 ensureTaxonomy2(String genus, String species, String spAuthor, String subtaxa, String subtAuthor) {
if (StringUtils.isBlank(genus)) {
throw new RuntimeException("Genus can't be blank");
}
// Capitalize
genus = WordUtils.capitalizeFully(genus);
genus = WordUtils.capitalizeFully(genus).trim();
species = StringUtils.defaultIfBlank(species, "sp.");
species = species.toLowerCase();
species = species.trim().toLowerCase();
spAuthor = StringUtils.defaultIfBlank(spAuthor, null);
subtaxa = StringUtils.defaultIfBlank(subtaxa, null);
spAuthor = StringUtils.defaultIfBlank(spAuthor, "").trim();
subtaxa = StringUtils.defaultIfBlank(subtaxa, "").trim();
if (subtaxa != null)
subtaxa = subtaxa.toLowerCase();
subtAuthor = StringUtils.defaultIfBlank(subtAuthor, null);
subtAuthor = StringUtils.defaultIfBlank(subtAuthor, "").trim();
if (StringUtils.equalsIgnoreCase(species.trim(), "sp")) {
species = "sp.";
......@@ -133,13 +88,16 @@ public class TaxonomyServiceImpl implements TaxonomyService {
}
private synchronized Taxonomy2 internalEnsure(String genus, String species, String spAuthor, String subtaxa, String subtAuthor) {
Taxonomy2 genusTaxa = null;
Taxonomy2 speciesTaxa = null;
if (!StringUtils.equals(species, "sp.") || (subtaxa != null || spAuthor != null || subtAuthor != null)) {
genusTaxa = internalEnsure(genus, "sp.", null, null, null);
}
if (!StringUtils.equals(species, "sp.") && (subtaxa != null || spAuthor != null || subtAuthor != null)) {
speciesTaxa = internalEnsure(genus, species, null, null, null);
Long taxSpeciesId = null, taxGenusId = null;
if (!StringUtils.equals(species, "sp.") && !(subtaxa.equals("") && spAuthor.equals("") && subtAuthor.equals(""))) {
Taxonomy2 speciesTaxa = internalEnsure(genus, species, "", "", "");
taxSpeciesId = speciesTaxa.getId();
taxGenusId = speciesTaxa.getTaxGenus();
} else if (! StringUtils.equals(species, "sp.") && (subtaxa.equals("") && spAuthor.equals("") && subtAuthor.equals(""))) {
Taxonomy2 genusTaxa = internalEnsure(genus, "sp.", "", "", "");
taxGenusId = genusTaxa.getId();
taxSpeciesId=genusTaxa.getId();
}
// Loop it a bit if required
......@@ -152,29 +110,28 @@ public class TaxonomyServiceImpl implements TaxonomyService {
e.printStackTrace();
}
if (taxonomy == null) {
LOG.info("Adding new taxonomic name: " + genus + " " + species + " " + spAuthor + " " + subtaxa + " " + subtAuthor);
taxonomy = new Taxonomy2();
taxonomy.setGenus(genus);
taxonomy.setSpecies(species);
taxonomy.setSpAuthor(spAuthor);
taxonomy.setSubtaxa(subtaxa);
taxonomy.setSubtAuthor(subtAuthor);
taxonomy.setTaxGenus(taxGenusId);
taxonomy.setTaxSpecies(taxSpeciesId);
try {
if (genusTaxa != null) {
taxonomy.setTaxGenus(genusTaxa.getId());
} else {
taxonomy = taxonomy2Repository.save(taxonomy);
taxonomy.setTaxGenus(taxonomy.getId());
}
taxonomy = taxonomy2Repository.save(taxonomy);
if (speciesTaxa != null) {
taxonomy.setTaxSpecies(speciesTaxa.getId());
} else {
if (taxGenusId == null) {
taxonomy.setTaxGenus(taxonomy.getId());
taxonomy = taxonomy2Repository.save(taxonomy);
}
if (taxSpeciesId == null) {
taxonomy.setTaxSpecies(taxonomy.getId());
taxonomy = taxonomy2Repository.save(taxonomy);
}
taxonomy = taxonomy2Repository.save(taxonomy);
return taxonomy;
// TODO Should update crop taxonomy lists?
......@@ -206,23 +163,4 @@ public class TaxonomyServiceImpl implements TaxonomyService {
public long countTaxonomy2() {
return taxonomy2Repository.count();
}
/**
* @deprecated Will be removed when {@link Taxonomy} is removed.
*/
// TODO FIXME Remove
@Override
public List<Taxonomy> findAll() {
return taxonomyRepository.findAll();
}
/**
* @deprecated Will be removed when {@link Taxonomy} is removed.
*/
// TODO FIXME Remove
@Override
@Transactional
public long upgradeTaxonomy(Taxonomy t1, Taxonomy2 t2) {
return taxonomyRepository.upgrade(t1, t2, t2.getTaxGenus(), t2.getTaxSpecies());
}
}
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