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;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.lang3.ObjectUtils;
import org.genesys.catalog.service.DatasetService;
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.AccessionId;
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.persistence.AccessionRepository;
import org.genesys2.server.persistence.PDCIRepository;
......@@ -45,6 +49,7 @@ import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
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.ITPGRFAStatusUpdater;
import org.genesys2.server.service.worker.InstituteUpdater;
......@@ -67,12 +72,13 @@ import org.xml.sax.SAXException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.jpa.impl.JPAQueryFactory;
@Controller
@RequestMapping("/admin")
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class AdminController {
public static final Logger LOG = LoggerFactory.getLogger(AdminController.class);
@Autowired
......@@ -114,6 +120,12 @@ public class AdminController {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired
private AccessionCounter accessionCounter;
ObjectMapper mapper = new ObjectMapper();
@Autowired
......@@ -124,10 +136,10 @@ public class AdminController {
@Autowired
private AccessionProcessor accessionProcessor;
@Autowired
private PDCIRepository pdciRepository;
@RequestMapping("/")
public String root(Model model) {
return "/admin/index";
......@@ -231,9 +243,9 @@ public class AdminController {
@RequestMapping(value = "/pdci", method = RequestMethod.POST, params = "action=institute-pdci")
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());
genesysService.updatePDCI(institute);
accessionCounter.recountInstitute(institute);
}
return "redirect:/admin/";
......@@ -241,12 +253,12 @@ public class AdminController {
@RequestMapping(value = "/pdci", method = RequestMethod.POST, params = "action=filtered-pdci")
public String updateFilteredPDCI(@RequestParam(name = "filter") String filters) throws JsonParseException, JsonMappingException, IOException {
AccessionFilter filter = mapper.readValue(filters, AccessionFilter.class);
LOG.warn("Recalculating PDCI for accessions matching filter: {}", filter);
accessionProcessor.apply(filter, (accessions) -> {
// Everything here is executed within a @Transaction(readOnly = false) context
if (accessions == null) {
return accessions;
......@@ -257,28 +269,37 @@ public class AdminController {
PDCI pdci = accessionId.getPdci();
// create new PDCI if missing
PDCI resultingPdci = PDCICalculator.updatePdci(pdci == null ? new PDCI() : pdci, accession);
// if PDCI was missing, link it with accession
if (pdci == null) {
LOG.trace("Assigning new PDCI for {}", accession);
resultingPdci.setAccession(accessionId);
accessionId.setPdci(resultingPdci);
} else {
LOG.trace("Updating PDCI for {}", accession);
accessionId.setPdci(pdciRepository.save(resultingPdci));
}
updateAccessionPDCI(accession, resultingPdci);
accessionCounter.recountInstitute(accession.getInstitute());
});
LOG.debug("Updated {} PDCI entries", accessions.size());
return accessionRepository.save(accessions);
return accessions;
});
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")
public String updateTileIndex(@RequestParam(name = "filter") String filters) throws JsonParseException, JsonMappingException, IOException {
AccessionFilter filter = mapper.readValue(filters, AccessionFilter.class);
LOG.warn("Recalculating tileIndex for accessions matching filter: {}", filter);
......@@ -288,19 +309,35 @@ public class AdminController {
}
accessions.forEach(accession -> {
AccessionGeo geo = accession.getAccessionId().getGeo();
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());
return accessionRepository.save(accessions);
return accessions;
});
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")
public String updateGeoReg() throws IOException, ParserConfigurationException, SAXException {
geoRegionService.updateGeoRegionData();
......@@ -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
*/
......@@ -364,7 +402,7 @@ public class AdminController {
}
@PostMapping(value = "/clear-dois")
public String clearDois(){
public String clearDois() {
LOG.info("Clear DOIs");
genesysService.removeDOIs();
return "redirect:/admin/";
......
......@@ -15,9 +15,16 @@
*/
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.AccessionData;
import org.genesys2.server.model.genesys.AccessionId;
......@@ -27,7 +34,6 @@ import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.worker.AccessionCounter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -39,6 +45,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
......@@ -59,9 +66,6 @@ public class AccessionServiceImpl implements AccessionService {
@Autowired
private ElasticsearchService elasticsearchService;
@Autowired
private AccessionCounter accessionCounter;
@Autowired
private JPAQueryFactory jpaQueryFactory;
......@@ -212,7 +216,6 @@ public class AccessionServiceImpl implements AccessionService {
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public List<Accession> processAccessions(List<Long> accessionIds, IAccessionAction action) {
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());
accessions.forEach(accession -> {
try {
......@@ -228,7 +231,6 @@ public class AccessionServiceImpl implements AccessionService {
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public List<Accession> processAccessions(List<Long> accessionIds, IAccessionBatchAction action) {
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());
try {
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