Commit 50d6485b authored by Aleksandr Sharaban's avatar Aleksandr Sharaban Committed by Matija Obreza

Feature #32756 - Taxonomy2: Cleanup.

Created service method which removes all obsolete Taxonomy2 records.
parent abec9e92
......@@ -16,12 +16,15 @@
package org.genesys2.server.persistence.domain;
import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
......@@ -68,4 +71,19 @@ public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
List<Taxonomy2> findByGenusAndSpeciesAndSubtaxa(String genus, String species, String subtaxa);
@Query(nativeQuery = true, value = "SELECT ct.taxonomyId FROM croptaxonomy ct " +
"UNION ALL SELECT t2.taxGenus FROM taxonomy2 t2 " +
"UNION ALL SELECT t3.taxSpecies FROM taxonomy2 t3 " +
"UNION ALL SELECT a.taxonomyId2 FROM accession a " +
"UNION ALL SELECT a2.taxGenus FROM accession a2 " +
"UNION ALL SELECT ah.taxGenus FROM accessionhistoric ah " +
"UNION ALL SELECT ah2.taxonomyId2 FROM accessionhistoric ah2;")
Set<BigInteger> findTaxonomyReferencedIds();
@Query("select t.id from Taxonomy2 t")
Set<Long> findTaxonomyIds();
@Query("delete from Taxonomy2 t where t.id in (?1)")
@Modifying
void removeUnusedIds(Set<Long> ids);
}
......@@ -47,4 +47,5 @@ public interface TaxonomyService {
List<String> autocompleteSubtaxa(String ac, Crop crop, List<String> genus, List<String> species);
void cleanupTaxonomies();
}
......@@ -16,7 +16,9 @@
package org.genesys2.server.service.impl;
import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
......@@ -199,4 +201,18 @@ public class TaxonomyServiceImpl implements TaxonomyService {
public Taxonomy2 get(String genus) {
return find(genus, "sp.", "", "", "");
}
@Override
@Transactional
public void cleanupTaxonomies() {
Set<BigInteger> referencedIds = taxonomy2Repository.findTaxonomyReferencedIds();
Set<Long> allIds = taxonomy2Repository.findTaxonomyIds();
for (BigInteger integer: referencedIds) {
allIds.remove(integer.longValue());
}
if (allIds.size() > 0) {
taxonomy2Repository.removeUnusedIds(allIds);
}
}
}
......@@ -31,12 +31,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.service.CountryNamesUpdater;
import org.genesys2.server.service.ElasticService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoRegionService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.*;
import org.genesys2.server.service.impl.ContentSanitizer;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.worker.ElasticUpdater;
......@@ -100,6 +95,9 @@ public class AdminController {
@Autowired
private GeoRegionService geoRegionService;
@Autowired
private TaxonomyService taxonomyService;
ObjectMapper mapper = new ObjectMapper();
@RequestMapping("/")
......@@ -357,4 +355,11 @@ public class AdminController {
elasticService.regenerateAccessionSequentialNumber();
return "redirect:/admin/";
}
@RequestMapping(value = "/cleanup-taxonomies", method = RequestMethod.POST)
public String cleanupTaxonomies() {
taxonomyService.cleanupTaxonomies();
return "redirect:/admin/";
}
}
......@@ -121,5 +121,12 @@
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<h3>Taxonomy</h3>
<form method="post" action="<c:url value="/admin/cleanup-taxonomies" />">
<input type="submit" class="btn btn-default" value="Cleanup taxonomies" />
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</body>
</html>
\ No newline at end of file
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