Commit 266aa26a authored by Matija Obreza's avatar Matija Obreza

Merge branch 'genesys-ui-132-download-accession-data' into 'master'

Download accession data - server side

See merge request genesys-pgr/genesys-server!293
parents 38b6cb07 f8f1a27e
......@@ -16,12 +16,16 @@
package org.genesys2.server.api.v1;
import static org.springframework.context.i18n.LocaleContextHolder.*;
import static org.springframework.context.i18n.LocaleContextHolder.getLocale;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.ShortFilterService.FilterInfo;
......@@ -33,6 +37,7 @@ import org.genesys2.server.model.genesys.PDCIStatistics;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.InstituteService;
......@@ -51,6 +56,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
......@@ -89,6 +95,9 @@ public class InstituteController {
@Autowired
protected ShortFilterService shortFilterService;
@Autowired
private DownloadService downloadService;
/**
* List institutes by filterCode or filter
*
......@@ -127,21 +136,93 @@ public class InstituteController {
throw new ResourceNotFoundException();
}
InstituteFilter instituteFilter = new InstituteFilter();
instituteFilter.code=Sets.newHashSet(faoInstitute.getCode());
AccessionFilter byInstituteFilter=new AccessionFilter();
byInstituteFilter.holder=instituteFilter;
InstituteDetails details = new InstituteDetails();
details.details = faoInstitute;
details.blurb = contentService.getArticle(faoInstitute, "blurp", getLocale());
details.pdciStats = faoInstitute.getStatisticsPDCI();
details.lastUpdates = genesysService.getLastUpdatedStatistics(faoInstitute);
details.overview= getOverviewData(byInstituteFilter);
AccessionFilter byInstituteFilter = new AccessionFilter();
byInstituteFilter.holder().code = Sets.newHashSet(faoInstitute.getCode());
InstituteDetails details = new InstituteDetails();
details.details = faoInstitute;
details.blurb = contentService.getArticle(faoInstitute, "blurp", getLocale());
details.pdciStats = faoInstitute.getStatisticsPDCI();
details.lastUpdates = genesysService.getLastUpdatedStatistics(faoInstitute);
details.overview = getOverviewData(byInstituteFilter);
return details;
}
@RequestMapping(value = "/{wiewsCode}/download", method = RequestMethod.POST, params = { "dwca" })
public void downloadDwca(@PathVariable(value = "wiewsCode") String wiewsCode, HttpServletResponse response) throws IOException {
final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
if (faoInstitute == null) {
throw new ResourceNotFoundException();
}
LOG.warn("Searching accessions of: {}", faoInstitute);
// Create JSON filter
final AccessionFilter filter = new AccessionFilter();
filter.holder().code = Sets.newHashSet(faoInstitute.getCode());
// Write Darwin Core Archive to the stream.
response.setContentType("application/zip");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.zip\"", faoInstitute.getCode()));
final OutputStream outputStream = response.getOutputStream();
genesysService.writeAccessions(filter, outputStream);
response.flushBuffer();
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{wiewsCode}/download", method = RequestMethod.POST, params = { "pdci" })
public void downloadPdci(@PathVariable(value = "wiewsCode") String wiewsCode, HttpServletResponse response) throws IOException {
final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
if (faoInstitute == null) {
throw new ResourceNotFoundException();
}
LOG.warn("Searching accessions of: {}", faoInstitute);
// Create JSON filter
final AccessionFilter filter = new AccessionFilter();
filter.holder().code = Sets.newHashSet(faoInstitute.getCode());
// Write MCPD to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-pdci-%1$s.xlsx\"", faoInstitute.getCode()));
response.flushBuffer();
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxPDCI(filter, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
}
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{wiewsCode}/download", method = RequestMethod.POST, params = { "mcpd" })
public void downloadMcpd(@PathVariable(value = "wiewsCode") String wiewsCode, HttpServletResponse response) throws IOException {
final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
if (faoInstitute == null) {
throw new ResourceNotFoundException();
}
LOG.warn("Searching accessions of: {}", faoInstitute);
// Create JSON filter
final AccessionFilter filter = new AccessionFilter();
filter.holder().code = Sets.newHashSet(faoInstitute.getCode());
// Write MCPD to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.xlsx\"", faoInstitute.getCode()));
response.flushBuffer();
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxMCPD(filter, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
}
}
@JsonView({JsonViews.Protected.class})
@PostMapping(value = "/{code:[A-Z]+[0-9]+}/update")
......
......@@ -682,7 +682,7 @@ public class ExplorerController extends BaseController implements InitializingBe
final OutputStream outputStream = response.getOutputStream();
try {
genesysService.writeAccessions(appliedFilters, outputStream);
genesysService.writeAccessions(AccessionFilter.convert(appliedFilters), outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted");
......@@ -710,7 +710,7 @@ public class ExplorerController extends BaseController implements InitializingBe
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxMCPD(appliedFilters, outputStream);
downloadService.writeXlsxMCPD(AccessionFilter.convert(appliedFilters), outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
......
......@@ -260,7 +260,7 @@ public class ProjectController extends BaseController {
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.zip\"", project.getCode()));
final OutputStream outputStream = response.getOutputStream();
genesysService.writeAccessions(filters, outputStream);
genesysService.writeAccessions(AccessionFilter.convert(filters), outputStream);
response.flushBuffer();
}
......@@ -283,7 +283,7 @@ public class ProjectController extends BaseController {
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxMCPD(filters, outputStream);
downloadService.writeXlsxMCPD(AccessionFilter.convert(filters), outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
......@@ -309,7 +309,7 @@ public class ProjectController extends BaseController {
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxPDCI(filters, outputStream);
downloadService.writeXlsxPDCI(AccessionFilter.convert(filters), outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
......
......@@ -19,6 +19,7 @@ package org.genesys2.server.mvc;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
......@@ -32,14 +33,10 @@ import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionList;
import org.genesys2.server.model.json.UserAccessionList;
import org.genesys2.server.service.AccessionListService;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
......@@ -81,8 +78,11 @@ public class SelectionController extends BaseController {
@Autowired
private PermissionEvaluator permissionEvaluator;
// @Autowired
// private GenesysFilterService filterService;
@Autowired
private GenesysFilterService filterService;
private AccessionService accessionService;
@RequestMapping(value = "/")
public String view(ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
......@@ -190,12 +190,11 @@ public class SelectionController extends BaseController {
@RequestMapping(value = "/dwca", method = RequestMethod.POST)
public void dwca(ModelMap model, HttpServletResponse response) throws IOException {
// Create JSON filter
final AppliedFilters appliedFilters = new AppliedFilters();
AppliedFilter arr = new FilterHandler.AppliedFilter().setFilterName(FilterConstants.ID);
AccessionFilter filter=new AccessionFilter();
filter.id = new HashSet<>();
for (final long id : selectionBean.copy()) {
arr.addFilterValue(new FilterHandler.LiteralValueFilter(id));
filter.id.add(id);
}
appliedFilters.add(arr);
final int countFiltered = selectionBean.size();
LOG.info("Attempting to download DwCA for {} accessions", countFiltered);
......@@ -209,21 +208,20 @@ public class SelectionController extends BaseController {
// Write Darwin Core Archive to the stream.
final OutputStream outputStream = response.getOutputStream();
genesysService.writeAccessions(appliedFilters, outputStream);
genesysService.writeAccessions(filter, outputStream);
response.flushBuffer();
}
@RequestMapping(value = "/download/mcpd", method = RequestMethod.POST)
public void downloadXlsxMCPD(ModelMap model, HttpServletResponse response) throws IOException {
// Create JSON filter
final AppliedFilters appliedFilters = new AppliedFilters();
AppliedFilter arr = new FilterHandler.AppliedFilter().setFilterName(FilterConstants.ID);
AccessionFilter filter=new AccessionFilter();
filter.id = new HashSet<>();
for (final long id : selectionBean.copy()) {
arr.addFilterValue(new FilterHandler.LiteralValueFilter(id));
filter.id.add(id);
}
appliedFilters.add(arr);
final int countFiltered = genesysService.countAccessions(appliedFilters);
final long countFiltered = accessionService.countAccessions(filter);
LOG.info("Attempting to download XLSX MCPD for {} accessions", countFiltered);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
......@@ -232,7 +230,7 @@ public class SelectionController extends BaseController {
// Write Darwin Core Archive to the stream.
final OutputStream outputStream = response.getOutputStream();
downloadService.writeXlsxMCPD(appliedFilters, outputStream);
downloadService.writeXlsxMCPD(filter, outputStream);
response.flushBuffer();
}
......@@ -373,10 +371,10 @@ public class SelectionController extends BaseController {
return "redirect:/sel/";
}
@RequestMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<LabelValue<String>> autocomplete(@RequestParam(value = "term", required = true) String ac) {
return filterService.autocomplete(FilterConstants.INSTCODE, ac, new AppliedFilters());
}
//
// @RequestMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
// @ResponseBody
// public List<LabelValue<String>> autocomplete(@RequestParam(value = "term", required = true) String ac) {
// return filterService.autocomplete(FilterConstants.INSTCODE, ac, new AppliedFilters());
// }
}
......@@ -58,7 +58,6 @@ import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.OrganizationService;
import org.genesys2.server.service.StatisticsService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.AppliedFiltersConverter;
......@@ -127,9 +126,6 @@ public class WiewsController extends BaseController {
@Autowired
private DownloadService downloadService;
@Autowired
private StatisticsService statisticsService;
@Autowired
private ElasticsearchService elasticService;
......@@ -459,17 +455,17 @@ public class WiewsController extends BaseController {
LOG.warn("Searching accessions of: {}", faoInstitute);
// Create JSON filter
final AppliedFilters appliedFilters = new AppliedFilters();
final AppliedFilter arr = new AppliedFilter().setFilterName(FilterConstants.INSTCODE);
arr.addFilterValue(new FilterHandler.LiteralValueFilter(faoInstitute.getCode()));
appliedFilters.add(arr);
AccessionFilter instituteFilter=new AccessionFilter();
instituteFilter.holder().code = Sets.newHashSet(faoInstitute.getCode());
// Write Darwin Core Archive to the stream.
response.setContentType("application/zip");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.zip\"", faoInstitute.getCode()));
response.addHeader("Content-Length", "-1");
response.flushBuffer();
final OutputStream outputStream = response.getOutputStream();
genesysService.writeAccessions(appliedFilters, outputStream);
genesysService.writeAccessions(instituteFilter, outputStream);
response.flushBuffer();
}
......@@ -483,19 +479,18 @@ public class WiewsController extends BaseController {
LOG.warn("Searching accessions of: {}", faoInstitute);
// Create JSON filter
final AppliedFilters appliedFilters = new AppliedFilters();
final AppliedFilter arr = new AppliedFilter().setFilterName(FilterConstants.INSTCODE);
arr.addFilterValue(new FilterHandler.LiteralValueFilter(faoInstitute.getCode()));
appliedFilters.add(arr);
AccessionFilter instituteFilter=new AccessionFilter();
instituteFilter.holder().code = Sets.newHashSet(faoInstitute.getCode());
// Write MCPD to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.xlsx\"", faoInstitute.getCode()));
response.addHeader("Content-Length", "-1");
response.flushBuffer();
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxMCPD(appliedFilters, outputStream);
downloadService.writeXlsxMCPD(instituteFilter, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
......@@ -512,19 +507,18 @@ public class WiewsController extends BaseController {
LOG.warn("Searching accessions of: {}", faoInstitute);
// Create JSON filter
final AppliedFilters appliedFilters = new AppliedFilters();
final AppliedFilter arr = new AppliedFilter().setFilterName(FilterConstants.INSTCODE);
arr.addFilterValue(new FilterHandler.LiteralValueFilter(faoInstitute.getCode()));
appliedFilters.add(arr);
AccessionFilter instituteFilter=new AccessionFilter();
instituteFilter.holder().code = Sets.newHashSet(faoInstitute.getCode());
// Write MCPD to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-pdci-%1$s.xlsx\"", faoInstitute.getCode()));
response.addHeader("Content-Length", "-1");
response.flushBuffer();
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxPDCI(appliedFilters, outputStream);
downloadService.writeXlsxPDCI(instituteFilter, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
......
......@@ -23,7 +23,7 @@ import java.util.List;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.filter.AccessionFilter;
/**
* Defines available downloads
......@@ -33,9 +33,9 @@ import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
*/
public interface DownloadService {
void writeXlsxMCPD(AppliedFilters filters, OutputStream outputStream) throws IOException;
void writeXlsxMCPD(AccessionFilter accessionFilter, OutputStream outputStream) throws IOException;
void writeXlsxPDCI(AppliedFilters appliedFilters, OutputStream outputStream) throws IOException;
void writeXlsxPDCI(AccessionFilter accessionFilter, OutputStream outputStream) throws IOException;
void writeXlsxDescriptor(List<Descriptor> descriptors, OutputStream outputStream) throws IOException;
......
......@@ -47,6 +47,7 @@ import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Organization;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.impl.NoSuchAccessionException;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
......@@ -153,7 +154,7 @@ public interface GenesysService {
long countDatasets(FaoInstitute faoInstitute);
void writeAccessions(AppliedFilters filters, OutputStream outputStream) throws IOException;
void writeAccessions(AccessionFilter filter, OutputStream outputStream) throws IOException;
List<AccessionGeo> listAccessionsGeo(Set<Long> copy);
......
......@@ -50,7 +50,7 @@ public class AccessionListenersConfig {
ArrayList<AccessionModifiedListener> listeners = Lists.newArrayList(glisUpdater(glisGenesysApi));
System.err.println("Declared AccessionModifiedListeners " + listeners.size());
List<AccessionModifiedListener> active = listeners.stream().filter(listener -> listener != null).collect(Collectors.toList());
System.err.println("Active AccessionModifiedListeners " + listeners.size());
System.err.println("Active AccessionModifiedListeners " + active.size());
return active;
}
......
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