Commit 85e789a5 authored by Matija Obreza's avatar Matija Obreza

CropTaxonomy linked to Taxonomy2

parent b52fcf56
...@@ -17,17 +17,13 @@ ...@@ -17,17 +17,13 @@
package org.genesys2.server.model.genesys; package org.genesys2.server.model.genesys;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.List;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.BusinessModel; import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.impl.CropTaxonomy;
import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store; import org.hibernate.search.annotations.Store;
...@@ -50,9 +46,6 @@ public class Taxonomy extends BusinessModel { ...@@ -50,9 +46,6 @@ public class Taxonomy extends BusinessModel {
@Field(name = "title", store = Store.NO) @Field(name = "title", store = Store.NO)
private String taxonName; private String taxonName;
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "taxonomy")
private List<CropTaxonomy> cropTaxonomies;
public String getGenus() { public String getGenus() {
return this.genus; return this.genus;
} }
...@@ -77,14 +70,6 @@ public class Taxonomy extends BusinessModel { ...@@ -77,14 +70,6 @@ public class Taxonomy extends BusinessModel {
this.taxonName = taxonName; this.taxonName = taxonName;
} }
public List<CropTaxonomy> getCropTaxonomies() {
return cropTaxonomies;
}
public void setCropTaxonomies(List<CropTaxonomy> cropTaxonomies) {
this.cropTaxonomies = cropTaxonomies;
}
@Override @Override
public String toString() { public String toString() {
return MessageFormat.format("Tax id={0} genus={1} sp={2}, full={3}", id, genus, species, taxonName); return MessageFormat.format("Tax id={0} genus={1} sp={2}, full={3}", id, genus, species, taxonName);
......
...@@ -17,15 +17,19 @@ ...@@ -17,15 +17,19 @@
package org.genesys2.server.model.genesys; package org.genesys2.server.model.genesys;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.List;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Lob; import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.GlobalVersionedAuditedModel; import org.genesys2.server.model.GlobalVersionedAuditedModel;
import org.genesys2.server.model.impl.CropTaxonomy;
import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.Indexed;
@Entity @Entity
...@@ -59,8 +63,8 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel { ...@@ -59,8 +63,8 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
private Long taxSpecies; private Long taxSpecies;
// @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "taxonomy") @OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "taxonomy")
// private List<CropTaxonomy> cropTaxonomies; private List<CropTaxonomy> cropTaxonomies;
@PrePersist @PrePersist
private void prePersist() { private void prePersist() {
...@@ -152,6 +156,14 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel { ...@@ -152,6 +156,14 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
this.taxSpecies = taxSpecies; this.taxSpecies = taxSpecies;
} }
public List<CropTaxonomy> getCropTaxonomies() {
return cropTaxonomies;
}
public void setCropTaxonomies(List<CropTaxonomy> cropTaxonomies) {
this.cropTaxonomies = cropTaxonomies;
}
@Override @Override
public String toString() { public String toString() {
return MessageFormat.format("Tax id={0} taxonName={1}", id, getTaxonName()); return MessageFormat.format("Tax id={0} taxonName={1}", id, getTaxonName());
......
...@@ -22,9 +22,8 @@ import javax.persistence.ManyToOne; ...@@ -22,9 +22,8 @@ import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import org.genesys2.server.model.BusinessModel; import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.genesys.Taxonomy; import org.genesys2.server.model.genesys.Taxonomy2;
// TODO FIXME Need to link to {@link Taxonomy2}
@Entity @Entity
@Table(name = "croptaxonomy") @Table(name = "croptaxonomy")
public class CropTaxonomy extends BusinessModel { public class CropTaxonomy extends BusinessModel {
...@@ -36,7 +35,7 @@ public class CropTaxonomy extends BusinessModel { ...@@ -36,7 +35,7 @@ public class CropTaxonomy extends BusinessModel {
@ManyToOne(cascade = {}, optional = false) @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "taxonomyId") @JoinColumn(name = "taxonomyId")
private Taxonomy taxonomy; private Taxonomy2 taxonomy;
public Crop getCrop() { public Crop getCrop() {
return crop; return crop;
...@@ -46,11 +45,11 @@ public class CropTaxonomy extends BusinessModel { ...@@ -46,11 +45,11 @@ public class CropTaxonomy extends BusinessModel {
this.crop = crop; this.crop = crop;
} }
public Taxonomy getTaxonomy() { public Taxonomy2 getTaxonomy() {
return taxonomy; return taxonomy;
} }
public void setTaxonomy(Taxonomy taxonomy) { public void setTaxonomy(Taxonomy2 taxonomy) {
this.taxonomy = taxonomy; this.taxonomy = taxonomy;
} }
......
...@@ -18,7 +18,7 @@ package org.genesys2.server.persistence.domain; ...@@ -18,7 +18,7 @@ package org.genesys2.server.persistence.domain;
import java.util.List; import java.util.List;
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.Crop;
import org.genesys2.server.model.impl.CropTaxonomy; import org.genesys2.server.model.impl.CropTaxonomy;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
...@@ -39,12 +39,11 @@ public interface CropTaxonomyRepository extends JpaRepository<CropTaxonomy, Long ...@@ -39,12 +39,11 @@ public interface CropTaxonomyRepository extends JpaRepository<CropTaxonomy, Long
Page<CropTaxonomy> findByCrop(Crop crop, Pageable pageable); Page<CropTaxonomy> findByCrop(Crop crop, Pageable pageable);
// TODO FIXME todo
@Query("select distinct t from CropTaxonomy ct inner join ct.taxonomy t where ct.crop = ?1") @Query("select distinct t from CropTaxonomy ct inner join ct.taxonomy t where ct.crop = ?1")
List<Taxonomy> findTaxonomiesByCrop(Crop crop); List<Taxonomy2> findTaxonomiesByCrop(Crop crop);
@Query("select distinct ct.crop from CropTaxonomy ct where ct.taxonomy= ?1") @Query("select distinct ct.crop from CropTaxonomy ct where ct.taxonomy= ?1")
List<Crop> findByTaxonomy(Taxonomy taxonomy); List<Crop> findByTaxonomy(Taxonomy2 taxonomy);
@Modifying @Modifying
@Query("delete from CropTaxonomy ct where ct.crop = ?1") @Query("delete from CropTaxonomy ct where ct.crop = ?1")
......
...@@ -32,4 +32,10 @@ public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> { ...@@ -32,4 +32,10 @@ public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
List<String> autocompleteTaxonomy(String term, Pageable page); List<String> autocompleteTaxonomy(String term, Pageable page);
Taxonomy2 findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(String genus, String species, String spAuthor, String subtaxa, String subtAuthor); Taxonomy2 findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(String genus, String species, String spAuthor, String subtaxa, String subtAuthor);
List<Taxonomy2> findByGenus(String genus);
List<Taxonomy2> findByGenusAndSpecies(String genus, String species);
List<Taxonomy2> findByGenusAndSpeciesAndSubtaxa(String genus, String species, String subtaxa);
} }
...@@ -19,7 +19,6 @@ package org.genesys2.server.service; ...@@ -19,7 +19,6 @@ package org.genesys2.server.service;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.genesys.Taxonomy2; import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule; import org.genesys2.server.model.impl.CropRule;
...@@ -35,8 +34,6 @@ public interface CropService { ...@@ -35,8 +34,6 @@ public interface CropService {
List<Crop> list(Locale locale); List<Crop> list(Locale locale);
List<Crop> getCrops(Taxonomy taxonomy);
List<Crop> getCrops(Taxonomy2 taxonomy2); List<Crop> getCrops(Taxonomy2 taxonomy2);
void rebuildTaxonomies(); void rebuildTaxonomies();
......
...@@ -22,10 +22,11 @@ import java.util.Comparator; ...@@ -22,10 +22,11 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule; import org.genesys2.server.model.impl.CropRule;
...@@ -33,7 +34,7 @@ import org.genesys2.server.model.impl.CropTaxonomy; ...@@ -33,7 +34,7 @@ import org.genesys2.server.model.impl.CropTaxonomy;
import org.genesys2.server.persistence.domain.CropRepository; import org.genesys2.server.persistence.domain.CropRepository;
import org.genesys2.server.persistence.domain.CropRuleRepository; import org.genesys2.server.persistence.domain.CropRuleRepository;
import org.genesys2.server.persistence.domain.CropTaxonomyRepository; import org.genesys2.server.persistence.domain.CropTaxonomyRepository;
import org.genesys2.server.persistence.domain.TaxonomyRepository; import org.genesys2.server.persistence.domain.Taxonomy2Repository;
import org.genesys2.server.service.CropService; import org.genesys2.server.service.CropService;
import org.genesys2.server.service.HtmlSanitizer; import org.genesys2.server.service.HtmlSanitizer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -60,10 +61,10 @@ public class CropServiceImpl implements CropService { ...@@ -60,10 +61,10 @@ public class CropServiceImpl implements CropService {
CropTaxonomyRepository cropTaxonomyRepository; CropTaxonomyRepository cropTaxonomyRepository;
@Autowired @Autowired
TaxonomyRepository taxonomyRepository; private HtmlSanitizer htmlSanitizer;
@Autowired @Autowired
private HtmlSanitizer htmlSanitizer; private Taxonomy2Repository taxonomy2Repository;
@Override @Override
public Crop getCrop(String shortName) { public Crop getCrop(String shortName) {
...@@ -87,15 +88,9 @@ public class CropServiceImpl implements CropService { ...@@ -87,15 +88,9 @@ public class CropServiceImpl implements CropService {
return crops; return crops;
} }
@Override
public List<Crop> getCrops(Taxonomy taxonomy) {
return cropTaxonomyRepository.findByTaxonomy(taxonomy);
}
@Override @Override
public List<Crop> getCrops(Taxonomy2 taxonomy2) { public List<Crop> getCrops(Taxonomy2 taxonomy2) {
// TODO FIXME get the crops! return cropTaxonomyRepository.findByTaxonomy(taxonomy2);
return null;
} }
@Override @Override
...@@ -135,35 +130,35 @@ public class CropServiceImpl implements CropService { ...@@ -135,35 +130,35 @@ public class CropServiceImpl implements CropService {
LOG.info("Included: " + includedTaxa.size()); LOG.info("Included: " + includedTaxa.size());
LOG.info("Excluded: " + excludedTaxa.size()); LOG.info("Excluded: " + excludedTaxa.size());
List<Taxonomy> taxa = new ArrayList<Taxonomy>(); List<Taxonomy2> taxa = new ArrayList<Taxonomy2>();
for (CropRule cr : includedTaxa) { for (CropRule cr : includedTaxa) {
if (cr.getSpecies() == null) { if (cr.getSpecies() == null && cr.getSubtaxa() == null) {
taxa.addAll(taxonomyRepository.findByGenus(cr.getGenus())); taxa.addAll(taxonomy2Repository.findByGenus(cr.getGenus()));
} else if (cr.getSubtaxa() == null) {
taxa.addAll(taxonomy2Repository.findByGenusAndSpecies(cr.getGenus(), cr.getSpecies()));
} else { } else {
taxa.add(taxonomyRepository.getByGenusAndSpecies(cr.getGenus(), cr.getSpecies())); taxa.addAll(taxonomy2Repository.findByGenusAndSpeciesAndSubtaxa(cr.getGenus(), cr.getSpecies(), cr.getSubtaxa()));
} }
} }
for (CropRule cr : excludedTaxa) { for (final CropRule cr : excludedTaxa) {
if (cr.getSpecies() == null) { CollectionUtils.filterInverse(taxa, new Predicate<Taxonomy2>() {
for (Taxonomy notincluded : taxonomyRepository.findByGenus(cr.getGenus())) { @Override
for (int i = taxa.size() - 1; i >= 0; i--) { public boolean evaluate(Taxonomy2 taxonomy) {
if (taxa.get(i).getId().equals(notincluded.getId())) { if (cr.getSpecies() == null && cr.getSubtaxa() == null) {
taxa.remove(i); return StringUtils.equalsIgnoreCase(taxonomy.getSubtaxa(), cr.getSubtaxa())
} && StringUtils.equalsIgnoreCase(taxonomy.getSpecies(), cr.getSpecies());
} else if (cr.getSubtaxa() == null) {
return StringUtils.equalsIgnoreCase(taxonomy.getSpecies(), cr.getSpecies());
} else {
return StringUtils.equalsIgnoreCase(taxonomy.getGenus(), cr.getGenus());
} }
} }
} else { });
Taxonomy notincluded = taxonomyRepository.getByGenusAndSpecies(cr.getGenus(), cr.getSpecies());
for (int i = taxa.size() - 1; i >= 0; i--) {
if (taxa.get(i).getId().equals(notincluded.getId())) {
taxa.remove(i);
}
}
}
} }
LOG.warn("Clearing crop taxonomy for " + crop.getName()); LOG.warn("Clearing crop taxonomy for " + crop.getName());
// To check // To check
...@@ -176,7 +171,7 @@ public class CropServiceImpl implements CropService { ...@@ -176,7 +171,7 @@ public class CropServiceImpl implements CropService {
long taxonomyId = ct.getTaxonomy().getId(); long taxonomyId = ct.getTaxonomy().getId();
boolean found = false; boolean found = false;
for (Taxonomy taxonomy : taxa) { for (Taxonomy2 taxonomy : taxa) {
if (taxonomyId == taxonomy.getId()) { if (taxonomyId == taxonomy.getId()) {
found = true; found = true;
break; break;
...@@ -191,7 +186,7 @@ public class CropServiceImpl implements CropService { ...@@ -191,7 +186,7 @@ public class CropServiceImpl implements CropService {
List<CropTaxonomy> toAdd = new ArrayList<CropTaxonomy>(); List<CropTaxonomy> toAdd = new ArrayList<CropTaxonomy>();
// To add // To add
for (Taxonomy taxonomy : taxa) { for (Taxonomy2 taxonomy : taxa) {
boolean found = false; boolean found = false;
for (CropTaxonomy ct : existing) { for (CropTaxonomy ct : existing) {
......
...@@ -70,7 +70,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -70,7 +70,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
@Service @Service
@Transactional(readOnly = true) @Transactional(readOnly = true)
// FIXME TODO Change taxonomy to taxonomy2
public class GenesysFilterServiceImpl implements GenesysFilterService { public class GenesysFilterServiceImpl implements GenesysFilterService {
private static final Log LOG = LogFactory.getLog(GenesysFilterServiceImpl.class); private static final Log LOG = LogFactory.getLog(GenesysFilterServiceImpl.class);
...@@ -215,7 +214,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -215,7 +214,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append(" from accession a "); sb.append(" from accession a ");
if (jsonTree.has("crop") || jsonTree.has("genus") || jsonTree.has("taxon")) { if (jsonTree.has("crop") || jsonTree.has("genus") || jsonTree.has("taxon")) {
sb.append(" inner join taxonomy t on t.id=a.taxonomyId "); sb.append(" inner join taxonomy2 t on t.id=a.taxonomyId2 ");
if (jsonTree.has("crop")) { if (jsonTree.has("crop")) {
sb.append(" inner join croptaxonomy ct on ct.taxonomyId=t.id inner join crop on crop.id=ct.cropId "); sb.append(" inner join croptaxonomy ct on ct.taxonomyId=t.id inner join crop on crop.id=ct.cropId ");
} }
...@@ -682,7 +681,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -682,7 +681,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
final StringBuffer sb = new StringBuffer(); final StringBuffer sb = new StringBuffer();
sb.append(" from accessiongeo geo inner join accession a on a.id=geo.accessionId "); sb.append(" from accessiongeo geo inner join accession a on a.id=geo.accessionId ");
if (jsonTree.has("crop") || jsonTree.has("genus") || jsonTree.has("taxon")) { if (jsonTree.has("crop") || jsonTree.has("genus") || jsonTree.has("taxon")) {
sb.append(" inner join taxonomy t on t.id=a.taxonomyId "); sb.append(" inner join taxonomy2 t on t.id=a.taxonomyId2 ");
if (jsonTree.has("crop")) { if (jsonTree.has("crop")) {
sb.append(" inner join croptaxonomy ct on ct.taxonomyId=t.id inner join crop on crop.id=ct.cropId "); sb.append(" inner join croptaxonomy ct on ct.taxonomyId=t.id inner join crop on crop.id=ct.cropId ");
} }
......
...@@ -389,7 +389,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -389,7 +389,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override @Override
public Page<Accession> listAccessionsByCrop(Crop crop, Pageable pageable) { public Page<Accession> listAccessionsByCrop(Crop crop, Pageable pageable) {
List<Taxonomy2> taxonomies = null;// FIXME TODO cropTaxonomyRepository.findTaxonomiesByCrop(crop); List<Taxonomy2> taxonomies = cropTaxonomyRepository.findTaxonomiesByCrop(crop);
if (taxonomies == null || taxonomies.size() == 0) { if (taxonomies == null || taxonomies.size() == 0) {
return null; return null;
} }
......
...@@ -86,12 +86,12 @@ public class TaxonomyServiceImpl implements TaxonomyService { ...@@ -86,12 +86,12 @@ public class TaxonomyServiceImpl implements TaxonomyService {
@Override @Override
public List<String> autocompleteGenus(String term) { public List<String> autocompleteGenus(String term) {
return taxonomyRepository.autocompleteGenus(term + "%", new PageRequest(0, 10)); return taxonomy2Repository.autocompleteGenus(term + "%", new PageRequest(0, 10));
} }
@Override @Override
public List<String> autocompleteTaxonomy(String term) { public List<String> autocompleteTaxonomy(String term) {
return taxonomyRepository.autocompleteTaxonomy("%" + term + "%", new PageRequest(0, 10)); return taxonomy2Repository.autocompleteTaxonomy("%" + term + "%", new PageRequest(0, 10));
} }
@Override @Override
...@@ -207,11 +207,19 @@ public class TaxonomyServiceImpl implements TaxonomyService { ...@@ -207,11 +207,19 @@ public class TaxonomyServiceImpl implements TaxonomyService {
return taxonomy2Repository.count(); return taxonomy2Repository.count();
} }
/**
* @deprecated Will be removed when {@link Taxonomy} is removed.
*/
// TODO FIXME Remove
@Override @Override
public List<Taxonomy> findAll() { public List<Taxonomy> findAll() {
return taxonomyRepository.findAll(); return taxonomyRepository.findAll();
} }
/**
* @deprecated Will be removed when {@link Taxonomy} is removed.
*/
// TODO FIXME Remove
@Override @Override
@Transactional @Transactional
public long upgradeTaxonomy(Taxonomy t1, Taxonomy2 t2) { public long upgradeTaxonomy(Taxonomy t1, Taxonomy2 t2) {
......
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