Commit 5c835cca authored by Matija Obreza's avatar Matija Obreza
Browse files

DwCA download of selected accessions

parent d504d2f1
......@@ -143,10 +143,8 @@ public interface GenesysService {
long countDatasets(FaoInstitute faoInstitute);
void writeAccessions(FaoInstitute faoInstitute, OutputStream outputStream) throws IOException;
void writeAccessions(String jsonFilter, OutputStream outputStream) throws IOException;
void saveAliases(List<AccessionAlias> aliases);
void removeAliases(List<AccessionAlias> aliases);
......@@ -179,5 +177,4 @@ public interface GenesysService {
Set<Long> listAccessions(FaoInstitute holdingInstitute, Set<Long> accessionIds);
int countAccessions(String jsonFilter);
}
......@@ -94,6 +94,7 @@ public class DirectMysqlQuery {
}
protected DirectMysqlQuery filter(ObjectNode jsonTree, MethodResolver methodResolver) {
createQuery(whereBuffer, "a.id", jsonTree.get("id"), params);
createQuery(whereBuffer, "a.taxGenus", jsonTree.get("genusId"), params);
createQuery(whereBuffer, "a.taxSpecies", jsonTree.get("speciesId"), params);
createQuery(whereBuffer, "a.acceNumb", jsonTree.get("acceNumb"), params);
......
......@@ -849,11 +849,6 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
}
}
@Override
public void writeAccessions(final FaoInstitute faoInstitute, OutputStream outputStream) throws IOException {
writeAccessions("{\"instCode\":[\"" + faoInstitute.getCode() + "\"]}", outputStream);
}
@Override
// TODO FIXME Need proper term URLs
public void writeAccessions(String jsonFilter, OutputStream outputStream) throws IOException {
......
......@@ -347,7 +347,7 @@ public class ExplorerController extends BaseController {
return "/accession/map";
}
@RequestMapping(value = "/explore/dwca", method = RequestMethod.GET)
@RequestMapping(value = "/explore/dwca", method = RequestMethod.POST)
public void dwca(ModelMap model, @RequestParam(value = "crop", required = false, defaultValue = "") String cropName,
@RequestParam(value = "filter", required = false, defaultValue = "{}") String jsonFilter, HttpServletResponse response) throws IOException {
......
......@@ -16,10 +16,14 @@
package org.genesys2.server.servlet.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.service.GenesysService;
......@@ -37,11 +41,17 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Controller
@Scope("request")
@RequestMapping("/sel")
public class SelectionController extends BaseController {
private ObjectMapper mapper = new ObjectMapper();
@Autowired
private SelectionBean selectionBean;
......@@ -104,6 +114,40 @@ public class SelectionController extends BaseController {
return "redirect:/sel/";
}
/**
* Download DwCA of selected accessions
*
* @param model
* @param cropName
* @param jsonFilter
* @param response
* @throws IOException
*/
@RequestMapping(value = "/dwca", method = RequestMethod.POST)
public void dwca(ModelMap model, HttpServletResponse response) throws IOException {
// Create JSON filter
ObjectNode jsonTree = mapper.createObjectNode();
ArrayNode arr = jsonTree.putArray("id");
for (long id : selectionBean.copy()) {
arr.add(id);
}
int countFiltered = selectionBean.size();
_logger.info("Attempting to download DwCA for " + countFiltered + " accessions");
if (countFiltered > 100000) {
throw new RuntimeException("Refusing to export more than 100,000 entries");
}
response.setContentType("application/zip");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-selected.zip\""));
// Write Darwin Core Archive to the stream.
OutputStream outputStream = response.getOutputStream();
genesysService.writeAccessions(jsonTree.toString(), outputStream);
response.flushBuffer();
}
// TODO REMOVE
@RequestMapping(value = "/json/count", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
......
......@@ -43,11 +43,17 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Controller
@Scope("request")
@RequestMapping("/wiews")
public class WiewsController extends BaseController {
private ObjectMapper mapper = new ObjectMapper();
@Autowired
private InstituteService instituteService;
......@@ -198,22 +204,25 @@ public class WiewsController extends BaseController {
return "/accession/data";
}
@RequestMapping(value="/{wiewsCode}/dwca", method=RequestMethod.POST)
@RequestMapping(value = "/{wiewsCode}/dwca", method = RequestMethod.POST)
public void viewData(ModelMap model, @PathVariable(value = "wiewsCode") String wiewsCode, HttpServletResponse response) throws IOException {
_logger.debug("Viewing country " + wiewsCode);
FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
if (faoInstitute == null) {
throw new ResourceNotFoundException();
}
_logger.warn("Searching accessions of: " + faoInstitute);
// Create JSON filter
ObjectNode jsonTree = mapper.createObjectNode();
ArrayNode arr = jsonTree.putArray("instCode");
arr.add(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()));
// Write Darwin Core Archive to the stream.
OutputStream outputStream = response.getOutputStream();
genesysService.writeAccessions(faoInstitute, outputStream);
genesysService.writeAccessions(jsonTree.toString(), outputStream);
response.flushBuffer();
}
}
......@@ -12,12 +12,22 @@
</h1>
<div class="main-col-header clearfix">
<div class="nav-header pull-left">
<div class="results"><spring:message code="accessions.number" arguments="${pagedData.totalElements}" />
<a href="<c:url value="/explore/map"><c:param name="crop" value="${crop.shortName}" /><c:param name="filter">${jsonFilter}</c:param></c:url>">Map</a>
<div class="nav-header">
<c:if test="${pagedData.totalElements le 100000}">
<a href="<c:url value="/explore/dwca"><c:param name="crop" value="${crop.shortName}" /><c:param name="filter">${jsonFilter}</c:param></c:url>"><spring:message code="filter.download-dwca" /></a>
<form class="pull-right form-horizontal" method="post" action="/explore/dwca">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<input type="hidden" name="crop" value="${crop.shortName}" />
<input type="hidden" name="filter" value="<c:out value="${jsonFilter}" />" />
<div class="row" style="margin-top: 2em;">
<div class="col-sm-4">
<button class="btn btn-default" type="submit"><spring:message code="filter.download-dwca" /></button>
</div>
</div>
</form>
</c:if>
<div class="results"><spring:message code="accessions.number" arguments="${pagedData.totalElements}" />
<a href="<c:url value="/explore/map"><c:param name="crop" value="${crop.shortName}" /><c:param name="filter">${jsonFilter}</c:param></c:url>">Map</a>
</div>
<div class="pagination">
<spring:message code="paged.pageOfPages" arguments="${pagedData.number+1},${pagedData.totalPages}" />
......
......@@ -20,8 +20,18 @@
<c:if test="${pagedData != null}">
<div class="main-col-header clearfix">
<div class="nav-header pull-left">
<div class="nav-header">
<form class="pull-right form-horizontal" method="post" action="/sel/dwca">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<div class="row" style="margin-top: 2em;">
<div class="col-sm-4">
<button class="btn btn-default" type="submit"><spring:message code="filter.download-dwca" /></button>
</div>
</div>
</form>
<div class="results"><spring:message code="accessions.number" arguments="${pagedData.totalElements}" /></div>
<div class="pagination">
<spring:message code="paged.pageOfPages" arguments="${pagedData.number+1},${pagedData.totalPages}" />
<a href="<spring:url value=""><spring:param name="page" value="${pagedData.number eq 0 ? 1 : pagedData.number}" /><spring:param name="filter" value="${jsonFilter}" /></spring:url>"><spring:message code="pagination.previous-page" /></a>
......
Supports Markdown
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