Commit 4543c36b authored by Matija Obreza's avatar Matija Obreza
Browse files

Admin: update tileIndex, pdci without modifying Accession @LastModifiedDate

parent ebb86823
...@@ -28,11 +28,15 @@ import javax.persistence.PersistenceContext; ...@@ -28,11 +28,15 @@ import javax.persistence.PersistenceContext;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.lang.time.StopWatch; import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.lang3.ObjectUtils;
import org.genesys.catalog.service.DatasetService; import org.genesys.catalog.service.DatasetService;
import org.genesys.worldclim.WorldClimUtil; import org.genesys.worldclim.WorldClimUtil;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo; import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionId; import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.PDCI; import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.QAccessionId;
import org.genesys2.server.model.impl.FaoInstitute; import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionRepository; import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.PDCIRepository; import org.genesys2.server.persistence.PDCIRepository;
...@@ -45,6 +49,7 @@ import org.genesys2.server.service.GeoService; ...@@ -45,6 +49,7 @@ import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService; import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService; import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter; import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.worker.AccessionCounter;
import org.genesys2.server.service.worker.AccessionProcessor; import org.genesys2.server.service.worker.AccessionProcessor;
import org.genesys2.server.service.worker.ITPGRFAStatusUpdater; import org.genesys2.server.service.worker.ITPGRFAStatusUpdater;
import org.genesys2.server.service.worker.InstituteUpdater; import org.genesys2.server.service.worker.InstituteUpdater;
...@@ -67,12 +72,13 @@ import org.xml.sax.SAXException; ...@@ -67,12 +72,13 @@ import org.xml.sax.SAXException;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.jpa.impl.JPAQueryFactory;
@Controller @Controller
@RequestMapping("/admin") @RequestMapping("/admin")
@PreAuthorize("hasRole('ADMINISTRATOR')") @PreAuthorize("hasRole('ADMINISTRATOR')")
public class AdminController { public class AdminController {
public static final Logger LOG = LoggerFactory.getLogger(AdminController.class); public static final Logger LOG = LoggerFactory.getLogger(AdminController.class);
@Autowired @Autowired
...@@ -114,6 +120,12 @@ public class AdminController { ...@@ -114,6 +120,12 @@ public class AdminController {
@PersistenceContext @PersistenceContext
private EntityManager entityManager; private EntityManager entityManager;
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired
private AccessionCounter accessionCounter;
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
@Autowired @Autowired
...@@ -124,10 +136,10 @@ public class AdminController { ...@@ -124,10 +136,10 @@ public class AdminController {
@Autowired @Autowired
private AccessionProcessor accessionProcessor; private AccessionProcessor accessionProcessor;
@Autowired @Autowired
private PDCIRepository pdciRepository; private PDCIRepository pdciRepository;
@RequestMapping("/") @RequestMapping("/")
public String root(Model model) { public String root(Model model) {
return "/admin/index"; return "/admin/index";
...@@ -231,9 +243,9 @@ public class AdminController { ...@@ -231,9 +243,9 @@ public class AdminController {
@RequestMapping(value = "/pdci", method = RequestMethod.POST, params = "action=institute-pdci") @RequestMapping(value = "/pdci", method = RequestMethod.POST, params = "action=institute-pdci")
public String updatePDCI() { public String updatePDCI() {
for (FaoInstitute institute: instituteService.listActive(new PageRequest(0, Integer.MAX_VALUE))) { for (FaoInstitute institute : instituteService.listActive(new PageRequest(0, Integer.MAX_VALUE))) {
LOG.info("Updating PDCI for {}", institute.getCode()); LOG.info("Updating PDCI for {}", institute.getCode());
genesysService.updatePDCI(institute); accessionCounter.recountInstitute(institute);
} }
return "redirect:/admin/"; return "redirect:/admin/";
...@@ -241,12 +253,12 @@ public class AdminController { ...@@ -241,12 +253,12 @@ public class AdminController {
@RequestMapping(value = "/pdci", method = RequestMethod.POST, params = "action=filtered-pdci") @RequestMapping(value = "/pdci", method = RequestMethod.POST, params = "action=filtered-pdci")
public String updateFilteredPDCI(@RequestParam(name = "filter") String filters) throws JsonParseException, JsonMappingException, IOException { public String updateFilteredPDCI(@RequestParam(name = "filter") String filters) throws JsonParseException, JsonMappingException, IOException {
AccessionFilter filter = mapper.readValue(filters, AccessionFilter.class); AccessionFilter filter = mapper.readValue(filters, AccessionFilter.class);
LOG.warn("Recalculating PDCI for accessions matching filter: {}", filter); LOG.warn("Recalculating PDCI for accessions matching filter: {}", filter);
accessionProcessor.apply(filter, (accessions) -> { accessionProcessor.apply(filter, (accessions) -> {
// Everything here is executed within a @Transaction(readOnly = false) context // Everything here is executed within a @Transaction(readOnly = false) context
if (accessions == null) { if (accessions == null) {
return accessions; return accessions;
...@@ -257,28 +269,37 @@ public class AdminController { ...@@ -257,28 +269,37 @@ public class AdminController {
PDCI pdci = accessionId.getPdci(); PDCI pdci = accessionId.getPdci();
// create new PDCI if missing // create new PDCI if missing
PDCI resultingPdci = PDCICalculator.updatePdci(pdci == null ? new PDCI() : pdci, accession); PDCI resultingPdci = PDCICalculator.updatePdci(pdci == null ? new PDCI() : pdci, accession);
// if PDCI was missing, link it with accession
if (pdci == null) { updateAccessionPDCI(accession, resultingPdci);
LOG.trace("Assigning new PDCI for {}", accession); accessionCounter.recountInstitute(accession.getInstitute());
resultingPdci.setAccession(accessionId);
accessionId.setPdci(resultingPdci);
} else {
LOG.trace("Updating PDCI for {}", accession);
accessionId.setPdci(pdciRepository.save(resultingPdci));
}
}); });
LOG.debug("Updated {} PDCI entries", accessions.size()); LOG.debug("Updated {} PDCI entries", accessions.size());
return accessionRepository.save(accessions); return accessions;
}); });
return "redirect:/admin/"; return "redirect:/admin/";
} }
private void updateAccessionPDCI(Accession accession, PDCI pdci) {
AccessionId accessionId = accession.getAccessionId();
pdci.setAccession(accessionId);
pdciRepository.save(pdci);
if (accessionId.getPdci() == null) {
accessionId.setPdci(pdci);
LOG.trace("Assigning new PDCI for {}", accession);
jpaQueryFactory.update(QAccessionId.accessionId).where(QAccessionId.accessionId.eq(accessionId)).set(QAccessionId.accessionId.pdci, pdci).execute();
// jpaQueryFactory.update(QAccession.accession).where(QAccession.accession.eq(accession)).set(QAccession.accession.pdci,
// pdci).execute();
}
}
@RequestMapping(value = "/admin-action", method = RequestMethod.POST, params = "action=tile-index") @RequestMapping(value = "/admin-action", method = RequestMethod.POST, params = "action=tile-index")
public String updateTileIndex(@RequestParam(name = "filter") String filters) throws JsonParseException, JsonMappingException, IOException { public String updateTileIndex(@RequestParam(name = "filter") String filters) throws JsonParseException, JsonMappingException, IOException {
AccessionFilter filter = mapper.readValue(filters, AccessionFilter.class); AccessionFilter filter = mapper.readValue(filters, AccessionFilter.class);
LOG.warn("Recalculating tileIndex for accessions matching filter: {}", filter); LOG.warn("Recalculating tileIndex for accessions matching filter: {}", filter);
...@@ -288,19 +309,35 @@ public class AdminController { ...@@ -288,19 +309,35 @@ public class AdminController {
} }
accessions.forEach(accession -> { accessions.forEach(accession -> {
AccessionGeo geo = accession.getAccessionId().getGeo(); AccessionGeo geo = accession.getAccessionId().getGeo();
if (geo != null) { if (geo != null) {
accession.setTileIndex(WorldClimUtil.getTileIndex(5, geo.getLongitude(), geo.getLatitude())); Long index = WorldClimUtil.getTileIndex(5, geo.getLongitude(), geo.getLatitude());
if (ObjectUtils.compare(index, accession.getTileIndex()) != 0) {
updateAccessionTileIndex(accession, index);
}
} else {
if (accession.getTileIndex() != null) {
updateAccessionTileIndex(accession, null);
}
} }
}); });
LOG.debug("Regenerated {} tileIndexes", accessions.size()); LOG.debug("Regenerated {} tileIndexes", accessions.size());
return accessionRepository.save(accessions); return accessions;
}); });
return "redirect:/admin/"; return "redirect:/admin/";
} }
private void updateAccessionTileIndex(Accession accession, Long index) {
if (index == null) {
jpaQueryFactory.update(QAccession.accession).where(QAccession.accession.eq(accession)).setNull(QAccession.accession.tileIndex).execute();
} else {
jpaQueryFactory.update(QAccession.accession).where(QAccession.accession.eq(accession)).set(QAccession.accession.tileIndex, index).execute();
}
}
@RequestMapping(value = "/admin-action", method = RequestMethod.POST, params = "georegion") @RequestMapping(value = "/admin-action", method = RequestMethod.POST, params = "georegion")
public String updateGeoReg() throws IOException, ParserConfigurationException, SAXException { public String updateGeoReg() throws IOException, ParserConfigurationException, SAXException {
geoRegionService.updateGeoRegionData(); geoRegionService.updateGeoRegionData();
...@@ -308,7 +345,8 @@ public class AdminController { ...@@ -308,7 +345,8 @@ public class AdminController {
} }
/** /**
* Scan AccessionData table and convert ACCENUMB to ACCENUMBNUMB (extract the number from the ACCNUMB) * Scan AccessionData table and convert ACCENUMB to ACCENUMBNUMB (extract the
* number from the ACCNUMB)
* *
* @return * @return
*/ */
...@@ -364,7 +402,7 @@ public class AdminController { ...@@ -364,7 +402,7 @@ public class AdminController {
} }
@PostMapping(value = "/clear-dois") @PostMapping(value = "/clear-dois")
public String clearDois(){ public String clearDois() {
LOG.info("Clear DOIs"); LOG.info("Clear DOIs");
genesysService.removeDOIs(); genesysService.removeDOIs();
return "redirect:/admin/"; return "redirect:/admin/";
......
...@@ -15,9 +15,16 @@ ...@@ -15,9 +15,16 @@
*/ */
package org.genesys2.server.service.impl; package org.genesys2.server.service.impl;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import com.querydsl.core.BooleanBuilder;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData; import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionId; import org.genesys2.server.model.genesys.AccessionId;
...@@ -27,7 +34,6 @@ import org.genesys2.server.persistence.AccessionRepository; ...@@ -27,7 +34,6 @@ import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.AccessionService; import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.ElasticsearchService; import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.filter.AccessionFilter; import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.worker.AccessionCounter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -39,6 +45,7 @@ import org.springframework.stereotype.Service; ...@@ -39,6 +45,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple; import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory; import com.querydsl.jpa.impl.JPAQueryFactory;
...@@ -59,9 +66,6 @@ public class AccessionServiceImpl implements AccessionService { ...@@ -59,9 +66,6 @@ public class AccessionServiceImpl implements AccessionService {
@Autowired @Autowired
private ElasticsearchService elasticsearchService; private ElasticsearchService elasticsearchService;
@Autowired
private AccessionCounter accessionCounter;
@Autowired @Autowired
private JPAQueryFactory jpaQueryFactory; private JPAQueryFactory jpaQueryFactory;
...@@ -212,7 +216,6 @@ public class AccessionServiceImpl implements AccessionService { ...@@ -212,7 +216,6 @@ public class AccessionServiceImpl implements AccessionService {
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public List<Accession> processAccessions(List<Long> accessionIds, IAccessionAction action) { public List<Accession> processAccessions(List<Long> accessionIds, IAccessionAction action) {
List<Accession> accessions = accessionRepository.findAll(accessionIds); List<Accession> accessions = accessionRepository.findAll(accessionIds);
accessions.stream().map(a -> a.getInstitute()).distinct().forEach(institute -> accessionCounter.recountInstitute(institute));
LOG.debug("Processing {} accessions of {} IDs provided", accessions.size(), accessionIds.size()); LOG.debug("Processing {} accessions of {} IDs provided", accessions.size(), accessionIds.size());
accessions.forEach(accession -> { accessions.forEach(accession -> {
try { try {
...@@ -228,7 +231,6 @@ public class AccessionServiceImpl implements AccessionService { ...@@ -228,7 +231,6 @@ public class AccessionServiceImpl implements AccessionService {
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public List<Accession> processAccessions(List<Long> accessionIds, IAccessionBatchAction action) { public List<Accession> processAccessions(List<Long> accessionIds, IAccessionBatchAction action) {
List<Accession> accessions = accessionRepository.findAll(accessionIds); List<Accession> accessions = accessionRepository.findAll(accessionIds);
accessions.stream().map(a -> a.getInstitute()).distinct().forEach(institute -> accessionCounter.recountInstitute(institute));
LOG.debug("Processing {} accessions of {} IDs provided", accessions.size(), accessionIds.size()); LOG.debug("Processing {} accessions of {} IDs provided", accessions.size(), accessionIds.size());
try { try {
return action.apply(accessions); return action.apply(accessions);
......
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