Commit 8a047a50 authored by Matija Obreza's avatar Matija Obreza

Archive accession data in ES on deletion

parent e706e71b
......@@ -200,4 +200,6 @@ public interface GenesysService {
List<AccessionBreeding> removeBreeding(List<AccessionBreeding> toRemove);
Set<AccessionDetails> getAccessionDetails(Collection<Accession> accessions);
}
......@@ -248,6 +248,15 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
elasticsearchTemplate.putMapping(AccessionDetails.class);
LOG.info("Refreshing");
elasticsearchTemplate.refresh(AccessionDetails.class, true);
// genesysarchive
if (!elasticsearchTemplate.indexExists("genesysarchive")) {
LOG.info("Initializing index");
elasticsearchTemplate.createIndex("genesysarchive");
}
Map indexMapping = elasticsearchTemplate.getMapping(AccessionDetails.class);
LOG.info("Copying mapping");
elasticsearchTemplate.putMapping("genesysarchive", "mcpd", indexMapping);
}
}
......@@ -332,6 +332,15 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return ad;
}
@Override
public Set<AccessionDetails> getAccessionDetails(Collection<Accession> accessions) {
Set<AccessionDetails> set = new HashSet<AccessionDetails>(accessions.size());
for (Accession accn : accessions) {
set.add(getAccessionDetails(accn.getId()));
}
return set;
}
@Override
public List<Accession> listAccessions(FaoInstitute faoInstitute, String accessionName) {
return accessionRepository.findByInstituteAndAccessionName(faoInstitute, accessionName);
......@@ -464,7 +473,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return accessionRepository.statisticsGenusInInstitute(institute, pageable);
}
@Override
@Override
public Page<Object[]> statisticsSpeciesByInstitute(FaoInstitute institute, Pageable pageable) {
final Page<Object[]> page = accessionRepository.statisticsSpeciesInInstitute(institute, pageable);
for (final Object[] r : page.getContent()) {
......@@ -631,8 +640,12 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
public List<Accession> removeAccessions(FaoInstitute institute, List<Accession> toDelete) {
if (toDelete.size() > 0) {
final Set<Long> accessionIds = new HashSet<Long>();
for (final Accession accn : toDelete) {
accessionIds.add(accn.getId());
if (institute.getId().equals(accn.getInstitute().getId()))
accessionIds.add(accn.getId());
else
throw new RuntimeException("Accession " + accn.getAccessionName() + " does not belong to " + institute.getCode());
}
accessionAliasRepository.deleteForAccessions(accessionIds);
......
......@@ -18,6 +18,7 @@ package org.genesys2.server.service.worker;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
......@@ -248,4 +249,21 @@ public class ElasticUpdater {
return className + " id=" + id;
}
}
public Set<AccessionDetails> load(Collection<Accession> accessions) {
return genesysService.getAccessionDetails(accessions);
}
public void archive(final Set<AccessionDetails> deletedAccessions) {
final List<IndexQuery> queries = new ArrayList<>(deletedAccessions.size());
for (AccessionDetails ad : deletedAccessions) {
IndexQuery iq = new IndexQuery();
iq.setObject(ad);
iq.setIndexName("genesysarchive");
iq.setType("mcpd");
queries.add(iq);
}
elasticsearchTemplate.bulkIndex(queries);
}
}
......@@ -17,15 +17,20 @@
package org.genesys2.server.service.worker;
import java.util.Collection;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.genesys2.server.model.elastic.AccessionDetails;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRelated;
import org.genesys2.server.model.genesys.SvalbardData;
import org.genesys2.server.model.impl.FaoInstitute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -90,9 +95,10 @@ public class ElasticUpdaterAspect {
for (Object o : list) {
if (o == null)
continue;
if (o instanceof Accession) {
elasticUpdater.remove(Accession.class, ((Accession) o).getId());
}
// if (o instanceof Accession) {
// elasticUpdater.remove(Accession.class, ((Accession)
// o).getId());
// }
if (o instanceof AccessionRelated) {
elasticUpdater.update(Accession.class, ((AccessionRelated) o).getAccession().getId());
}
......@@ -105,4 +111,22 @@ public class ElasticUpdaterAspect {
return result;
}
@Around(value = "execution(* org.genesys2.server.service.GenesysService.removeAccessions(..)) && args(institute, accessions)")
public Object aroundRemove(final ProceedingJoinPoint joinPoint, final FaoInstitute institute, final Collection<Accession> accessions) throws Throwable {
if (LOG.isDebugEnabled()) {
LOG.debug("Returning from " + joinPoint.toLongString());
}
// Need to load AccessionDetails before deleting
Set<AccessionDetails> deletedAccessions = elasticUpdater.load(accessions);
try {
Object res = joinPoint.proceed();
// This will not trigger if an exception occurs
elasticUpdater.archive(deletedAccessions);
return res;
} finally {
}
}
}
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