Commit e87c421f authored by Matija Obreza's avatar Matija Obreza

Delete accessions by instCode + id

parent 06861d32
......@@ -33,4 +33,6 @@ public interface BatchRESTService {
void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch)throws RESTApiException;
int deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException;
int deleteAccessionsById(FaoInstitute institute, List<Long> batch);
}
......@@ -932,4 +932,34 @@ public class BatchRESTServiceImpl implements BatchRESTService {
return toDelete.size();
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'DELETE') or hasPermission(#institute, 'MANAGE')")
public int deleteAccessionsById(FaoInstitute institute, List<Long> batch) {
LOG.info("Batch deleting " + batch.size() + " entries for " + institute);
final List<Accession> toDelete = new ArrayList<Accession>(batch.size());
if (toDelete.size() > 0) {
genesysService.removeAccessions(institute, toDelete);
}
for (final Long accessionId : batch) {
Accession accession;
accession = genesysService.getAccession(accessionId.longValue());
if (accession != null) {
if (!institute.getCode().equals(accession.getInstituteCode())) {
LOG.warn("Accession does not belong to instCode=" + institute.getCode() + " acn=" + accession.getId());
} else {
toDelete.add(accession);
}
}
}
if (toDelete.size() > 0) {
genesysService.removeAccessions(institute, toDelete);
}
return toDelete.size();
}
}
......@@ -23,12 +23,14 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.elastic.AccessionDetails;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.json.AccessionJson;
import org.genesys2.server.service.BatchRESTService;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.ElasticService;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GenesysRESTService;
import org.genesys2.server.service.GenesysService;
......@@ -38,6 +40,7 @@ import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.genesys2.server.service.impl.RESTApiException;
import org.genesys2.server.service.impl.RESTApiValueException;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
import org.genesys2.spring.ResourceNotFoundException;
......@@ -92,15 +95,19 @@ public class AccessionController extends RestController {
@Autowired
GenesysRESTService restService;
@Autowired
ElasticService elasticService;
/**
* Check if accessions exists in the system
*
* @return
* @throws NonUniqueAccessionException
* @throws NonUniqueAccessionException
*/
@RequestMapping(value = "/exists/{instCode}/{genus:.+}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
boolean exists(@PathVariable("instCode") String instCode, @PathVariable("genus") String genus, @RequestParam("acceNumb") String acceNumb) throws NonUniqueAccessionException {
boolean exists(@PathVariable("instCode") String instCode, @PathVariable("genus") String genus, @RequestParam("acceNumb") String acceNumb)
throws NonUniqueAccessionException {
if (LOG.isDebugEnabled()) {
LOG.debug("Checking if accn exists " + instCode + "." + acceNumb + " genus=" + genus);
}
......@@ -121,11 +128,12 @@ public class AccessionController extends RestController {
* @throws IOException
* @throws JsonProcessingException
* @throws RESTApiValueException
* @throws NonUniqueAccessionException
* @throws NonUniqueAccessionException
*/
@RequestMapping(value = "/{instCode}/check", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
JsonNode check(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException, RESTApiValueException, NonUniqueAccessionException {
JsonNode check(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException, RESTApiValueException,
NonUniqueAccessionException {
final FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException("No institute " + instCode);
......@@ -222,7 +230,7 @@ public class AccessionController extends RestController {
* Update accessions in the system
*
* @return
* @throws RESTApiException
* @throws RESTApiException
* @throws IOException
* @throws JsonProcessingException
*/
......@@ -247,14 +255,14 @@ public class AccessionController extends RestController {
}
/**
* Update accessions in the system
* Delete accessions by acceNumb (and genus)?
*
* @return
* @throws RESTApiException
* @throws RESTApiException
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
@RequestMapping(value = "/{instCode}/delete-named", 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<AccessionHeaderJson> batch) throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
......@@ -269,32 +277,50 @@ public class AccessionController extends RestController {
return new JsonDeleteResult(deleted);
}
@RequestMapping(value = "/{instCode}/{id}", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
/**
* Delete accessions by acceNumb (and genus)?
*
* @return
* @throws RESTApiException
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete", method = { RequestMethod.POST }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
JsonDeleteResult deleteAccessionsById(@PathVariable("instCode") String instCode, @RequestBody List<Long> batch) throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
final FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException();
}
final int deleted = batchRESTService.deleteAccessionsById(institute, batch);
LOG.info("Deleted " + deleted + " accessions from " + instCode);
return new JsonDeleteResult(deleted);
}
@RequestMapping(value = "/{instCode}/list", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
AccessionJson get(@PathVariable("instCode") String instCode, @PathVariable("id") long accessionId) {
return restService.getAccessionJSON(instCode, accessionId);
Page<AccessionDetails> list(@PathVariable("instCode") String instCode, @RequestParam("page") int page, @RequestParam("query") String query)
throws SearchException {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException();
}
query = StringUtils.defaultIfBlank(query, "*");
return elasticService.search(query, new PageRequest(page - 1, 20));
}
@RequestMapping(value = "/{id}", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
AccessionDetails getDetails(@PathVariable("id") long accessionId) {
final Accession accession = genesysService.getAccession(accessionId);
if (accession == null) {
final AccessionDetails ad = genesysService.getAccessionDetails(accessionId);
if (ad == null) {
throw new ResourceNotFoundException();
}
AccessionDetails ad = AccessionDetails.from(accession);
ad.aliases(genesysService.listAccessionAliases(accession));
ad.exch(genesysService.listAccessionExchange(accession));
ad.collect(genesysService.listAccessionCollect(accession));
ad.breeding(genesysService.listAccessionBreeding(accession));
ad.geo(genesysService.listAccessionGeo(accession));
ad.svalbard(genesysService.getSvalbardData(accession));
ad.remarks(genesysService.listAccessionRemarks(accession));
//ad.traits(genesysService.listMethods(accession), genesysService.getAccessionTraitValues(accession));
ad.crops(cropService.getCrops(accession.getTaxonomy()));
return ad;
}
......
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