Commit 86ad0d17 authored by Matija Obreza's avatar Matija Obreza
Browse files

REST delete accession

parent 24dc61ba
......@@ -16,11 +16,14 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import java.util.List;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AccessionAliasRepository extends JpaRepository<AccessionAlias, Long> {
......@@ -28,4 +31,7 @@ public interface AccessionAliasRepository extends JpaRepository<AccessionAlias,
List<AccessionAlias> findByAccessionAndAliasType(Accession accession, int aliasType);
@Modifying
@Query("delete from AccessionAlias aa where aa.accession in (from Accession a where a.id in ( ?1 ))")
void deleteForAccessions(Collection<Long> accessionIds);
}
......@@ -16,12 +16,19 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionBreeding;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AccessionBreedingRepository extends JpaRepository<AccessionBreeding, Long> {
AccessionBreeding findByAccession(Accession accession);
@Modifying
@Query("delete from AccessionBreeding ab where ab.accession.id in ?1")
void deleteForAccessions(Collection<Long> accessionIds);
}
......@@ -16,12 +16,19 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionCollect;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AccessionCollectRepository extends JpaRepository<AccessionCollect, Long> {
AccessionCollect findByAccession(Accession accession);
@Modifying
@Query("delete from AccessionCollect ac where ac.accession.id in ?1")
void deleteForAccessions(Collection<Long> accessionIds);
}
......@@ -16,12 +16,20 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionExchange;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AccessionExchangeRepository extends JpaRepository<AccessionExchange, Long> {
AccessionExchange findByAccession(Accession accession);
@Modifying
@Query("delete from AccessionExchange ae where ae.accession.id in ?1")
void deleteForAccessions(Collection<Long> accessionIds);
}
......@@ -16,12 +16,14 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AccessionGeoRepository extends JpaRepository<AccessionGeo, Long> {
......@@ -31,4 +33,8 @@ public interface AccessionGeoRepository extends JpaRepository<AccessionGeo, Long
@Query("select ag from AccessionGeo ag join fetch ag.accession a where a.id in ?1")
List<AccessionGeo> findForAccessions(Set<Long> accessionIds);
@Modifying
@Query("delete from AccessionGeo ag where ag.accession.id in ?1")
void deleteForAccessions(Collection<Long> accessionIds);
}
......@@ -16,12 +16,20 @@
package org.genesys2.server.persistence.domain;
import java.util.Collection;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AllAccnames;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AccessionNameRepository extends JpaRepository<AllAccnames, Long> {
AllAccnames findByAccession(Accession accession);
@Modifying
@Query("delete from AllAccnames an where an.accession.id in ?1")
void deleteForAccessions(Collection<Long> accessionIds);
}
......@@ -27,6 +27,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface AccessionRepository extends JpaRepository<Accession, Long> {
......@@ -103,4 +104,8 @@ public interface AccessionRepository extends JpaRepository<Accession, Long> {
@Query("select count(a.id) from Accession a where a.id in ( ?1 ) and a.availability = true")
long countAvailable(Set<Long> accessionIds);
@Modifying
@Query("update Accession a set a.inSvalbard = true where a in ?1")
void setInSvalbard(List<Accession> matching);
}
......@@ -16,9 +16,17 @@
package org.genesys2.server.persistence.domain;
import java.util.Set;
import org.genesys2.server.model.genesys.SvalbardData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface SvalbardRepository extends JpaRepository<SvalbardData, Long> {
@Modifying
@Query("delete from SvalbardData sd where sd.id in ?1")
void deleteForAccessions(Set<Long> accessionIds);
}
......@@ -30,4 +30,6 @@ public interface BatchRESTService {
boolean upsertAccessionData(FaoInstitute institute, Map<AccessionJson, ObjectNode> batch);
void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch);
int deleteAccessions(FaoInstitute institute, List<AccessionJson> batch);
}
......@@ -119,7 +119,7 @@ public interface GenesysService {
void updateAccessionInstitueRefs();
void saveAccessions(List<Accession> matching);
void saveAccessions(FaoInstitute institute, List<Accession> matching);
void saveSvalbards(List<SvalbardData> svalbards);
......@@ -155,4 +155,10 @@ public interface GenesysService {
List<AccessionGeo> listAccessionsGeo(Set<Long> copy);
void removeAccessions(FaoInstitute institute, List<Accession> toDelete);
void setInSvalbard(List<Accession> matching);
void addAccessions(List<Accession> accessions);
}
......@@ -390,8 +390,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (toSave.size() > 0) {
LOG.info("Storing " + toSave.size() + " accessions.");
genesysService.saveAccessions(toSave);
genesysService.updateAccessionCount(institute);
genesysService.saveAccessions(institute, toSave);
}
if (toSaveColl.size() > 0) {
......@@ -584,4 +583,32 @@ public class BatchRESTServiceImpl implements BatchRESTService {
toSave.add(accessionAlias);
}
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'DELETE') or hasPermission(#institute, 'MANAGE')")
public int deleteAccessions(FaoInstitute institute, List<AccessionJson> batch) {
LOG.info("Batch deleting " + batch.size() + " entries for " + institute);
List<Accession> toDelete = new ArrayList<Accession>(batch.size());
for (AccessionJson dataJson : batch) {
if (LOG.isDebugEnabled())
LOG.debug("Loading accession " + dataJson);
if (!institute.getCode().equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + institute.getCode() + " acn=" + dataJson);
}
Accession accession = genesysService.getAccession(dataJson.instCode, dataJson.genus, dataJson.acceNumb);
if (accession != null) {
toDelete.add(accession);
}
}
if (toDelete.size() > 0) {
genesysService.removeAccessions(institute, toDelete);
}
return toDelete.size();
}
}
......@@ -453,11 +453,28 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override
@Transactional(readOnly = false)
public void saveAccessions(List<Accession> accessions) {
public void setInSvalbard(List<Accession> matching) {
if (matching.size() > 0)
accessionRepository.setInSvalbard(matching);
}
/**
* @deprecated Should be removed
*/
@Override
@Transactional(readOnly = false)
public void addAccessions(List<Accession> accessions) {
accessionRepository.save(accessions);
}
@Override
@Transactional(readOnly = false)
public void saveAccessions(FaoInstitute institute, List<Accession> accessions) {
if (LOG.isDebugEnabled()) {
LOG.debug("Saving " + accessions.size() + " accessions");
}
accessionRepository.save(accessions);
updateAccessionCount(institute);
}
@Override
......@@ -469,6 +486,31 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
}
}
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'DELETE') or hasPermission(#institute, 'MANAGE')")
public void removeAccessions(FaoInstitute institute, List<Accession> toDelete) {
if (toDelete.size() > 0) {
Set<Long> accessionIds = new HashSet<Long>();
for (Accession accn : toDelete) {
accessionIds.add(accn.getId());
}
accessionAliasRepository.deleteForAccessions(accessionIds);
accessionBreedingRepository.deleteForAccessions(accessionIds);
accessionCollectRepository.deleteForAccessions(accessionIds);
accessionExchangeRepository.deleteForAccessions(accessionIds);
accessionGeoRepository.deleteForAccessions(accessionIds);
accessionNamesRepository.deleteForAccessions(accessionIds);
svalbardRepository.deleteForAccessions(accessionIds);
accessionRepository.delete(toDelete);
updateAccessionCount(institute);
}
}
@Override
// Worker threads don't carry this information
// @PreAuthorize("hasRole('ADMINISTRATOR')")
......@@ -1265,4 +1307,5 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return paramMethods;
}
}
......@@ -287,7 +287,7 @@ public class SGSVInsertMissing {
// Save data
if (accessions.size() > 0) {
genesysService.saveAccessions(accessions);
genesysService.addAccessions(accessions);
LOG.info("Added " + accessions.size() + " new entires");
}
}
......
......@@ -275,17 +275,17 @@ public class SGSVUpdate {
updateAccession = true;
}
// Doesn't have taxonomy due to no species
if (accn.getTaxonomy() == null && StringUtils.isNotBlank(entry.species)) {
accn.setTaxonomy(taxonomyService.ensureTaxonomy(entry.genus, entry.species));
updateAccession = true;
}
if (entry.origCty != null && StringUtils.isBlank(accn.getOrigin()) && !StringUtils.equals(accn.getOrigin(), entry.origCty)) {
accn.setOrigin(entry.origCty);
accn.setCountryOfOrigin(geoService.getCountry(entry.origCty));
updateAccession = true;
}
// // Doesn't have taxonomy due to no species
// if (accn.getTaxonomy() == null && StringUtils.isNotBlank(entry.species)) {
// accn.setTaxonomy(taxonomyService.ensureTaxonomy(entry.genus, entry.species));
// updateAccession = true;
// }
//
// if (entry.origCty != null && StringUtils.isBlank(accn.getOrigin()) && !StringUtils.equals(accn.getOrigin(), entry.origCty)) {
// accn.setOrigin(entry.origCty);
// accn.setCountryOfOrigin(geoService.getCountry(entry.origCty));
// updateAccession = true;
// }
SvalbardData svalbardData = svalbardRepository.findOne(accn.getId());
if (svalbardData == null) {
......@@ -309,7 +309,7 @@ public class SGSVUpdate {
// Save data
if (matching.size() > 0) {
LOG.info("Setting inSGSV=true for accessions size=" + matching.size());
genesysService.saveAccessions(matching);
genesysService.setInSvalbard(matching);
}
if (svalbards.size() > 0) {
try {
......
......@@ -209,6 +209,28 @@ public class AccessionController extends RestController {
return JSON_OK;
}
/**
* Update accessions in the system
*
* @return
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
JsonDeleteResult deleteAccessions(@PathVariable("instCode") String instCode, @RequestBody List<AccessionJson> batch) {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException();
}
int deleted = batchRESTService.deleteAccessions(institute, batch);
LOG.info("Deleted " + deleted + " accessions from " + instCode);
return new JsonDeleteResult(deleted);
}
private AccessionJson readAid3(JsonNode json) {
AccessionJson dataJson = new AccessionJson();
......@@ -218,4 +240,20 @@ public class AccessionController extends RestController {
return dataJson;
}
public static final class JsonDeleteResult {
private int deleted;
public JsonDeleteResult(int deleted) {
this.deleted = deleted;
}
public int getDeleted() {
return deleted;
}
public boolean getResult() {
return true;
}
}
}
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