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

REST delete accession

parent 24dc61ba
...@@ -16,11 +16,14 @@ ...@@ -16,11 +16,14 @@
package org.genesys2.server.persistence.domain; package org.genesys2.server.persistence.domain;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias; import org.genesys2.server.model.genesys.AccessionAlias;
import org.springframework.data.jpa.repository.JpaRepository; 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> { public interface AccessionAliasRepository extends JpaRepository<AccessionAlias, Long> {
...@@ -28,4 +31,7 @@ public interface AccessionAliasRepository extends JpaRepository<AccessionAlias, ...@@ -28,4 +31,7 @@ public interface AccessionAliasRepository extends JpaRepository<AccessionAlias,
List<AccessionAlias> findByAccessionAndAliasType(Accession accession, int aliasType); 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 @@ ...@@ -16,12 +16,19 @@
package org.genesys2.server.persistence.domain; package org.genesys2.server.persistence.domain;
import java.util.Collection;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionBreeding; import org.genesys2.server.model.genesys.AccessionBreeding;
import org.springframework.data.jpa.repository.JpaRepository; 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> { public interface AccessionBreedingRepository extends JpaRepository<AccessionBreeding, Long> {
AccessionBreeding findByAccession(Accession accession); 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 @@ ...@@ -16,12 +16,19 @@
package org.genesys2.server.persistence.domain; package org.genesys2.server.persistence.domain;
import java.util.Collection;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionCollect; import org.genesys2.server.model.genesys.AccessionCollect;
import org.springframework.data.jpa.repository.JpaRepository; 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> { public interface AccessionCollectRepository extends JpaRepository<AccessionCollect, Long> {
AccessionCollect findByAccession(Accession accession); 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 @@ ...@@ -16,12 +16,20 @@
package org.genesys2.server.persistence.domain; package org.genesys2.server.persistence.domain;
import java.util.Collection;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionExchange; import org.genesys2.server.model.genesys.AccessionExchange;
import org.springframework.data.jpa.repository.JpaRepository; 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> { public interface AccessionExchangeRepository extends JpaRepository<AccessionExchange, Long> {
AccessionExchange findByAccession(Accession accession); 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 @@ ...@@ -16,12 +16,14 @@
package org.genesys2.server.persistence.domain; package org.genesys2.server.persistence.domain;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo; import org.genesys2.server.model.genesys.AccessionGeo;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
public interface AccessionGeoRepository extends JpaRepository<AccessionGeo, Long> { public interface AccessionGeoRepository extends JpaRepository<AccessionGeo, Long> {
...@@ -31,4 +33,8 @@ 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") @Query("select ag from AccessionGeo ag join fetch ag.accession a where a.id in ?1")
List<AccessionGeo> findForAccessions(Set<Long> accessionIds); 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 @@ ...@@ -16,12 +16,20 @@
package org.genesys2.server.persistence.domain; package org.genesys2.server.persistence.domain;
import java.util.Collection;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AllAccnames; import org.genesys2.server.model.genesys.AllAccnames;
import org.springframework.data.jpa.repository.JpaRepository; 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> { public interface AccessionNameRepository extends JpaRepository<AllAccnames, Long> {
AllAccnames findByAccession(Accession accession); 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; ...@@ -27,6 +27,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
public interface AccessionRepository extends JpaRepository<Accession, Long> { public interface AccessionRepository extends JpaRepository<Accession, Long> {
...@@ -103,4 +104,8 @@ 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") @Query("select count(a.id) from Accession a where a.id in ( ?1 ) and a.availability = true")
long countAvailable(Set<Long> accessionIds); 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 @@ ...@@ -16,9 +16,17 @@
package org.genesys2.server.persistence.domain; package org.genesys2.server.persistence.domain;
import java.util.Set;
import org.genesys2.server.model.genesys.SvalbardData; import org.genesys2.server.model.genesys.SvalbardData;
import org.springframework.data.jpa.repository.JpaRepository; 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> { 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 { ...@@ -30,4 +30,6 @@ public interface BatchRESTService {
boolean upsertAccessionData(FaoInstitute institute, Map<AccessionJson, ObjectNode> batch); boolean upsertAccessionData(FaoInstitute institute, Map<AccessionJson, ObjectNode> batch);
void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch); void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch);
int deleteAccessions(FaoInstitute institute, List<AccessionJson> batch);
} }
...@@ -119,7 +119,7 @@ public interface GenesysService { ...@@ -119,7 +119,7 @@ public interface GenesysService {
void updateAccessionInstitueRefs(); void updateAccessionInstitueRefs();
void saveAccessions(List<Accession> matching); void saveAccessions(FaoInstitute institute, List<Accession> matching);
void saveSvalbards(List<SvalbardData> svalbards); void saveSvalbards(List<SvalbardData> svalbards);
...@@ -155,4 +155,10 @@ public interface GenesysService { ...@@ -155,4 +155,10 @@ public interface GenesysService {
List<AccessionGeo> listAccessionsGeo(Set<Long> copy); 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 { ...@@ -390,8 +390,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (toSave.size() > 0) { if (toSave.size() > 0) {
LOG.info("Storing " + toSave.size() + " accessions."); LOG.info("Storing " + toSave.size() + " accessions.");
genesysService.saveAccessions(toSave); genesysService.saveAccessions(institute, toSave);
genesysService.updateAccessionCount(institute);
} }
if (toSaveColl.size() > 0) { if (toSaveColl.size() > 0) {
...@@ -584,4 +583,32 @@ public class BatchRESTServiceImpl implements BatchRESTService { ...@@ -584,4 +583,32 @@ public class BatchRESTServiceImpl implements BatchRESTService {
toSave.add(accessionAlias); 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 ...@@ -453,11 +453,28 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override @Override
@Transactional(readOnly = false) @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()) { if (LOG.isDebugEnabled()) {
LOG.debug("Saving " + accessions.size() + " accessions"); LOG.debug("Saving " + accessions.size() + " accessions");
} }
accessionRepository.save(accessions); accessionRepository.save(accessions);
updateAccessionCount(institute);
} }
@Override @Override
...@@ -469,6 +486,31 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -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 @Override
// Worker threads don't carry this information // Worker threads don't carry this information
// @PreAuthorize("hasRole('ADMINISTRATOR')") // @PreAuthorize("hasRole('ADMINISTRATOR')")
...@@ -1265,4 +1307,5 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -1265,4 +1307,5 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return paramMethods; return paramMethods;
} }
} }
...@@ -287,7 +287,7 @@ public class SGSVInsertMissing { ...@@ -287,7 +287,7 @@ public class SGSVInsertMissing {
// Save data // Save data
if (accessions.size() > 0) { if (accessions.size() > 0) {
genesysService.saveAccessions(accessions); genesysService.addAccessions(accessions);
LOG.info("Added " + accessions.size() + " new entires"); LOG.info("Added " + accessions.size() + " new entires");
} }
} }
......
...@@ -275,17 +275,17 @@ public class SGSVUpdate { ...@@ -275,17 +275,17 @@ public class SGSVUpdate {
updateAccession = true; updateAccession = true;
} }
// Doesn't have taxonomy due to no species // // Doesn't have taxonomy due to no species
if (accn.getTaxonomy() == null && StringUtils.isNotBlank(entry.species)) { // if (accn.getTaxonomy() == null && StringUtils.isNotBlank(entry.species)) {
accn.setTaxonomy(taxonomyService.ensureTaxonomy(entry.genus, entry.species)); // accn.setTaxonomy(taxonomyService.ensureTaxonomy(entry.genus, entry.species));
updateAccession = true; // updateAccession = true;
} // }
//
if (entry.origCty != null && StringUtils.isBlank(accn.getOrigin()) && !StringUtils.equals(accn.getOrigin(), entry.origCty)) { // if (entry.origCty != null && StringUtils.isBlank(accn.getOrigin()) && !StringUtils.equals(accn.getOrigin(), entry.origCty)) {
accn.setOrigin(entry.origCty); // accn.setOrigin(entry.origCty);
accn.setCountryOfOrigin(geoService.getCountry(entry.origCty)); // accn.setCountryOfOrigin(geoService.getCountry(entry.origCty));
updateAccession = true; // updateAccession = true;
} // }
SvalbardData svalbardData = svalbardRepository.findOne(accn.getId()); SvalbardData svalbardData = svalbardRepository.findOne(accn.getId());
if (svalbardData == null) { if (svalbardData == null) {
...@@ -309,7 +309,7 @@ public class SGSVUpdate { ...@@ -309,7 +309,7 @@ public class SGSVUpdate {
// Save data // Save data
if (matching.size() > 0) { if (matching.size() > 0) {
LOG.info("Setting inSGSV=true for accessions size=" + matching.size()); LOG.info("Setting inSGSV=true for accessions size=" + matching.size());
genesysService.saveAccessions(matching); genesysService.setInSvalbard(matching);
} }
if (svalbards.size() > 0) { if (svalbards.size() > 0) {
try { try {
......
...@@ -209,6 +209,28 @@ public class AccessionController extends RestController { ...@@ -209,6 +209,28 @@ public class AccessionController extends RestController {
return JSON_OK; 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) { private AccessionJson readAid3(JsonNode json) {
AccessionJson dataJson = new AccessionJson(); AccessionJson dataJson = new AccessionJson();
...@@ -218,4 +240,20 @@ public class AccessionController extends RestController { ...@@ -218,4 +240,20 @@ public class AccessionController extends RestController {
return dataJson; 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