Commit 3b99eddf authored by Matija Obreza's avatar Matija Obreza

New taxonomy

parent 78fdb4b0
......@@ -21,7 +21,7 @@ import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.hibernate.search.bridge.FieldBridge;
......@@ -51,7 +51,7 @@ public class AccessionBridge implements FieldBridge {
sd.append(countryOfOrigin.getName());
}
Taxonomy taxonomy = accession.getTaxonomy();
Taxonomy2 taxonomy = accession.getTaxonomy();
if (taxonomy != null) {
sd.append(taxonomy.getTaxonName());
}
......
......@@ -135,18 +135,14 @@ public class Accession extends VersionedAuditedModel {
this.accessionName = accessionName;
}
public Taxonomy getTaxonomy() {
public Taxonomy getTaxonomy1() {
return this.taxonomy;
}
public Taxonomy2 getTaxonomy2() {
public Taxonomy2 getTaxonomy() {
return this.taxonomy2;
}
public void setTaxonomy(final Taxonomy taxonomy) {
this.taxonomy = taxonomy;
}
public String getAcquisitionSource() {
return this.acquisitionSource;
}
......@@ -252,7 +248,7 @@ public class Accession extends VersionedAuditedModel {
this.taxSpecies = taxSpecies;
}
public void setTaxonomy2(Taxonomy2 taxonomy2) {
public void setTaxonomy(Taxonomy2 taxonomy2) {
this.taxonomy2 = taxonomy2;
}
......
......@@ -20,6 +20,7 @@ import java.text.MessageFormat;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
......@@ -49,6 +50,14 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
@Column(nullable = true, length = 100)
private String subtAuthor;
@Column(nullable = false)
@Lob
private String taxonName;
private long taxGenus;
private Long taxSpecies;
// @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "taxonomy")
// private List<CropTaxonomy> cropTaxonomies;
......@@ -80,6 +89,11 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
return sb.toString();
}
public void setTaxonName(String taxonName) {
// Ignore what they provide
taxonName = getTaxonName();
}
// public List<CropTaxonomy> getCropTaxonomies() {
// return cropTaxonomies;
// }
......@@ -88,13 +102,53 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
// this.cropTaxonomies = cropTaxonomies;
// }
@Override
public String toString() {
return MessageFormat.format("Tax id={0} taxonName=", id, getTaxonName());
public String getSpAuthor() {
return spAuthor;
}
public void setSpAuthor(String spAuthor) {
this.spAuthor = spAuthor;
}
public String getSubtaxa() {
return subtaxa;
}
public void setSubtaxa(String subtaxa) {
this.subtaxa = subtaxa;
}
public String getSubtAuthor() {
return subtAuthor;
}
public void setSubtAuthor(String subtAuthor) {
this.subtAuthor = subtAuthor;
}
public boolean sameAs(Taxonomy2 taxonomy) {
return taxonomy == null ? false : taxonomy.getId().equals(id);
}
public long getTaxGenus() {
return this.taxGenus;
}
public void setTaxGenus(long taxGenus) {
this.taxGenus = taxGenus;
}
public Long getTaxSpecies() {
return this.taxSpecies;
}
public void setTaxSpecies(Long taxSpecies) {
this.taxSpecies = taxSpecies;
}
@Override
public String toString() {
return MessageFormat.format("Tax id={0} taxonName=", id, getTaxonName());
}
}
......@@ -24,6 +24,7 @@ import javax.persistence.Table;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.genesys.Taxonomy;
// TODO FIXME Need to link to {@link Taxonomy2}
@Entity
@Table(name = "croptaxonomy")
public class CropTaxonomy extends BusinessModel {
......
......@@ -87,6 +87,7 @@ public class FaoInstitute extends BusinessModel implements GeoReferencedEntity,
private Double latitude;
private Double longitude;
private Double elevation;
private boolean uniqueAcceNumbs;
public FaoInstitute() {
}
......@@ -210,4 +211,12 @@ public class FaoInstitute extends BusinessModel implements GeoReferencedEntity,
public void setSettings(Map<String, FaoInstituteSetting> settings) {
this.settings = settings;
}
public boolean hasUniqueAcceNumbs() {
return this.uniqueAcceNumbs;
}
public void setUniqueAcceNumbs(boolean uniqueAcceNumbs) {
this.uniqueAcceNumbs = uniqueAcceNumbs;
}
}
......@@ -96,8 +96,7 @@ public interface AccessionRepository extends JpaRepository<Accession, Long> {
@Query("select a from Accession a where a.taxonomy in ( ?1 )")
Page<Accession> findByTaxonomy(Collection<Taxonomy> taxonomies, Pageable pageable);
// Accession findByInstituteCodeAndAccessionName(String holdingInstitute,
// String accessionName);
Accession findByInstituteCodeAndAccessionName(String instCode, String acceNumb);
Accession findByInstituteCodeAndAccessionNameAndGenus(String holdingInstitute, String accessionName, String genus);
......
/**
* 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.persistence.domain;
import java.util.List;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
@Query("select distinct t.genus from Taxonomy2 t where t.genus like ?1")
List<String> autocompleteGenus(String term, Pageable page);
@Query("select distinct t.taxonName from Taxonomy2 t where t.taxonName like ?1")
List<String> autocompleteTaxonomy(String term, Pageable page);
@Query("select t from Taxonomy2 t where t.genus=?1 and t.species=?2 and t.spAuthor=?3 and t.subtaxa=?4 and t.subtAuthor=?5")
Taxonomy2 findOne(String genus, String species, String spAuthor, String subtaxa, String subtAuthor);
}
......@@ -20,6 +20,7 @@ import java.util.List;
import java.util.Map;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.impl.RESTApiException;
import org.genesys2.server.servlet.controller.rest.model.AccessionJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
......@@ -27,7 +28,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
public interface BatchRESTService {
boolean upsertAccessionData(FaoInstitute institute, Map<AccessionJson, ObjectNode> batch);
boolean upsertAccessionData(FaoInstitute institute, Map<AccessionJson, ObjectNode> batch) throws RESTApiException;
void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch);
......
......@@ -20,6 +20,7 @@ import java.util.List;
import java.util.Locale;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.model.impl.CropTaxonomy;
......@@ -36,6 +37,8 @@ public interface CropService {
List<Crop> getCrops(Taxonomy taxonomy);
List<Crop> getCrops(Taxonomy2 taxonomy2);
void rebuildTaxonomies();
Page<CropTaxonomy> getCropTaxonomies(Crop crop, Pageable pageable);
......
......@@ -95,7 +95,7 @@ public interface GenesysService {
Accession getAccession(AccessionIdentifier3 aid3);
Accession getAccession(String instCode, String genus, String acceNumb);
Accession getAccession(String instCode, String acceNumb, String genus);
Page<Object[]> statisticsGenusByInstitute(FaoInstitute faoInstitute, Pageable pageable);
......@@ -163,4 +163,13 @@ public interface GenesysService {
long countAll();
/**
* For institutes with {@link FaoInstitute#uniqueAcceNumbs}
*
* @param instCode
* @param acceNumb
* @return the 1 accession
*/
Accession getAccession(String instCode, String acceNumb);
}
......@@ -19,6 +19,7 @@ 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 {
......@@ -31,4 +32,10 @@ public interface TaxonomyService {
List<String> autocompleteTaxonomy(String term);
long getTaxonomy2Id(String genus);
long getTaxonomy2Id(String genus, String species);
Taxonomy2 ensureTaxonomy2(String genus, String species, String spAuthor, String subtaxa, String subtAuthor);
}
......@@ -26,6 +26,7 @@ import org.apache.commons.lang.StringUtils;
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.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.model.impl.CropTaxonomy;
......@@ -69,12 +70,11 @@ public class CropServiceImpl implements CropService {
return cropRepository.findByShortName(shortName);
}
@Override
public List<Crop> listCrops() {
return cropRepository.findAll();
}
@Override
public List<Crop> list(final Locale locale) {
List<Crop> crops = cropRepository.findAll();
......@@ -92,6 +92,12 @@ public class CropServiceImpl implements CropService {
return cropTaxonomyRepository.findByTaxonomy(taxonomy);
}
@Override
public List<Crop> getCrops(Taxonomy2 taxonomy2) {
// TODO FIXME get the crops!
return null;
}
@Override
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
@PreAuthorize("hasRole('ADMINISTRATOR')")
......
......@@ -224,9 +224,15 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
public Accession getAccession(AccessionIdentifier3 aid3) {
return accessionRepository.findByInstituteCodeAndAccessionNameAndGenus(aid3.getHoldingInstitute(), aid3.getAccessionName(), aid3.getGenus());
}
@Override
public Accession getAccession(String instCode, String acceNumb) {
return accessionRepository.findByInstituteCodeAndAccessionName(instCode, acceNumb);
}
@Override
public Accession getAccession(String instCode, String genus, String acceNumb) {
public Accession getAccession(String instCode, String acceNumb, String genus) {
return accessionRepository.findByInstituteCodeAndAccessionNameAndGenus(instCode, acceNumb, genus);
}
......
package org.genesys2.server.service.impl;
public class RESTApiDataTypeException extends RESTApiException {
public RESTApiDataTypeException(String message) {
super(message);
}
}
package org.genesys2.server.service.impl;
public class RESTApiException extends Exception {
public RESTApiException(String message) {
super(message);
}
}
package org.genesys2.server.service.impl;
public class RESTApiValueException extends RESTApiException {
public RESTApiValueException(String message) {
super(message);
}
}
......@@ -22,6 +22,8 @@ import org.apache.commons.lang.StringUtils;
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;
......@@ -37,6 +39,8 @@ public class TaxonomyServiceImpl implements TaxonomyService {
@Autowired
private TaxonomyRepository taxonomyRepository;
private Taxonomy2Repository taxonomy2Repository;
@Override
public Taxonomy get(String genus, String species) {
return taxonomyRepository.getByGenusAndSpecies(genus, species);
......@@ -85,4 +89,80 @@ public class TaxonomyServiceImpl implements TaxonomyService {
public List<String> autocompleteTaxonomy(String term) {
return taxonomyRepository.autocompleteTaxonomy("%" + term + "%", new PageRequest(0, 10));
}
@Override
@Transactional
public Taxonomy2 ensureTaxonomy2(String genus, String species, String spAuthor, String subtaxa, String subtAuthor) {
if (species == null && spAuthor != null) {
species = "sp.";
}
if (subtaxa == null && subtAuthor != null) {
subtaxa = "subsp.";
}
Taxonomy2 existing = taxonomy2Repository.findOne(genus, species, spAuthor, subtaxa, subtAuthor);
if (existing == null) {
return internalEnsure(genus, species, spAuthor, subtaxa, subtAuthor);
}
return existing;
}
private synchronized Taxonomy2 internalEnsure(String genus, String species, String spAuthor, String subtaxa, String subtAuthor) {
// Loop it a bit if required
for (int i = 5; i > 0; i--) {
Taxonomy2 taxonomy = taxonomy2Repository.findOne(genus, species, spAuthor, subtaxa, subtAuthor);
if (taxonomy == null) {
taxonomy = new Taxonomy2();
LOG.warn("Inserting new taxonomy");
taxonomy.setGenus(genus);
taxonomy.setSpecies(species);
taxonomy.setSpAuthor(spAuthor);
taxonomy.setSubtaxa(subtaxa);
taxonomy.setSubtAuthor(subtAuthor);
try {
taxonomy = taxonomy2Repository.save(taxonomy);
LOG.warn("Inserted new:" + taxonomy);
if (!StringUtils.equals(species, "sp.") && spAuthor != null && subtaxa != null && subtAuthor != null) {
Taxonomy2 genusTaxa = internalEnsure(genus, "sp.", null, null, null);
taxonomy.setTaxGenus(genusTaxa.getId());
if (species != null && subtaxa != null && !StringUtils.equals(subtaxa, "subsp.") && subtAuthor != null) {
Taxonomy2 speciesTaxa = internalEnsure(genus, species, null, "subsp.", null);
taxonomy.setTaxSpecies(speciesTaxa.getId());
} else {
// No species taxonomy
}
} else {
taxonomy.setTaxGenus(taxonomy.getId());
}
taxonomy = taxonomy2Repository.save(taxonomy);
// TODO Should update crop taxonomy lists?
return taxonomy;
} catch (Throwable e) {
LOG.warn("Error " + e.getMessage() + " :" + taxonomy);
continue;
}
}
}
// Should not get here!
throw new RuntimeException("Could not persist Taxonomy2");
}
@Override
public long getTaxonomy2Id(String genus) {
return taxonomy2Repository.findOne(genus, "sp.", null, null, null).getId();
}
@Override
public long getTaxonomy2Id(String genus, String species) {
return taxonomy2Repository.findOne(genus, species, null, "subsp.", null).getId();
}
}
package org.genesys2.server.service.worker;
import org.genesys2.server.model.impl.AccessionIdentifier3;
class SGSVEntry implements AccessionIdentifier3 {
String instCode;
String acceNumb;
String origCty;
String genus;
String species;
String subtAuthor;
String subtaxa;
String spAuthor;
String fullTaxa;
String boxNo;
String depositDate;
Float quantity;
public SGSVEntry(String[] entry) {
instCode = entry[1];
acceNumb = entry[4];
fullTaxa = entry[5];
origCty = entry[12];
genus = entry[16];
species = entry[17];
spAuthor = entry[25];
subtaxa = entry[26];
depositDate = entry[20];
if (entry[7] != null) {
quantity = Float.parseFloat(entry[7]);
}
boxNo = entry[2];
}
@Override
public String getHoldingInstitute() {
return instCode;
}
@Override
public String getAccessionName() {
return acceNumb;
}
@Override
public String getGenus() {
return genus;
}
@Override
public String toString() {
return "SGSVEntry " + instCode + " " + acceNumb;
}
}
\ No newline at end of file
......@@ -41,7 +41,6 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
......@@ -266,9 +265,9 @@ public class SGSVInsertMissing {
accn.setAccessionName(entry.acceNumb);
accn.setOrigin(entry.origCty);
accn.setCountryOfOrigin(geoService.getCountry(entry.origCty));
accn.setGenus(entry.genus);
accn.setTaxonomy(taxonomyService.ensureTaxonomy(entry.genus, entry.species));
// entry.fullTaxa));
accn.setTaxonomy(taxonomyService.ensureTaxonomy2(entry.genus, entry.species, entry.spAuthor, entry.subtaxa, entry.subtAuthor));
accn.setTaxGenus(taxonomyService.getTaxonomy2Id(entry.genus));
accn.setTaxSpecies(taxonomyService.getTaxonomy2Id(entry.genus, entry.species));
accn.setInstituteCode(entry.instCode);
accn.setInstitute(getFromCache(entry.instCode));
......@@ -302,42 +301,4 @@ public class SGSVInsertMissing {
}
return inst;
}
private class SGSVEntry implements AccessionIdentifier3 {
String instCode;
String acceNumb;
String origCty;
String genus;
String fullTaxa;
String species;
public SGSVEntry(String[] entry) {
instCode = entry[1];
acceNumb = entry[4];
fullTaxa = entry[5];
origCty = entry[12];
genus = entry[16];
species = entry[17];
}
@Override
public String getHoldingInstitute() {
return instCode;
}
@Override
public String getAccessionName() {
return acceNumb;
}
@Override
public String getGenus() {
return genus;
}
@Override
public String toString() {
return "SGSVEntry " + instCode + " " + acceNumb;
}
}
}
......@@ -40,7 +40,6 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.SvalbardData;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.persistence.domain.SvalbardRepository;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
......@@ -275,17 +274,22 @@ public class SGSVUpdate {
updateAccession = true;
}
// // Doesn't have taxonomy due to no species
// if (accn.getTaxonomy() == null && StringUtils.isNotBlank(entry.species)) {
// accn.setTaxonomy(taxonomyService.ensureTaxonomy(entry.genus, entry.species));
// updateAccession = true;