Commit 93290f7e authored by Matija Obreza's avatar Matija Obreza

Download project data

parent cabaec62
......@@ -16,6 +16,9 @@
package org.genesys2.server.servlet.controller;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
......@@ -25,13 +28,17 @@ import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.impl.AccessionList;
import org.genesys2.server.model.impl.Project;
import org.genesys2.server.service.AccessionListService;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.ProjectService;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
......@@ -43,11 +50,13 @@ import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseBody;
......@@ -67,6 +76,13 @@ public class ProjectController extends BaseController {
@Autowired
ContentService contentService;
@Autowired
private GenesysService genesysService;
@Autowired
private DownloadService downloadService;
@RequestMapping({ "/", "" })
public String projects() {
return "redirect:/project/list";
......@@ -120,7 +136,7 @@ public class ProjectController extends BaseController {
AppliedFilter listsFilter = new AppliedFilter().setFilterName(FilterConstants.LISTS);
for (UUID uuid : project.getAccessionLists()) {
listsFilter.addFilterValue(new FilterHandler.LiteralValueFilter(uuid.toString()));
listsFilter.addFilterValue(new FilterHandler.LiteralValueFilter(uuid));
}
filters.add(listsFilter);
return filters;
......@@ -214,7 +230,7 @@ public class ProjectController extends BaseController {
@RequestMapping("/{code}/overview")
public String overview(ModelMap model, @PathVariable(value = "code") String code) throws UnsupportedEncodingException {
Project project = projectService.getProjectByCode(code);
final Project project = projectService.getProjectByCode(code);
if (project != null && project.getAccessionLists().size() > 0) {
AppliedFilters filters = projectFilters(project);
return "forward:/explore/overview?filter=" + URLEncoder.encode(filters.toString(), "UTF8");
......@@ -223,6 +239,79 @@ public class ProjectController extends BaseController {
}
}
@RequestMapping(value = "/{code}/download", method = RequestMethod.POST, params = { "dwca" })
public void downloadDwca(ModelMap model, @PathVariable(value = "code") String code, HttpServletResponse response) throws IOException {
final Project project = projectService.getProjectByCode(code);
if (project == null || project.getAccessionLists().size() == 0) {
throw new ResourceNotFoundException();
}
_logger.warn("Downloading DWCA of: " + project.getCode());
// Create JSON filter
AppliedFilters filters = projectFilters(project);
// Write Darwin Core Archive to the stream.
response.setContentType("application/zip");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.zip\"", project.getCode()));
final OutputStream outputStream = response.getOutputStream();
genesysService.writeAccessions(filters, outputStream);
response.flushBuffer();
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{code}/download", method = RequestMethod.POST, params = { "mcpd" })
public void downloadMcpd(ModelMap model, @PathVariable(value = "code") String code, HttpServletResponse response) throws IOException {
final Project project = projectService.getProjectByCode(code);
if (project == null || project.getAccessionLists().size() == 0) {
throw new ResourceNotFoundException();
}
_logger.warn("Downloading MCPD of: " + project.getCode());
// Create JSON filter
AppliedFilters filters = projectFilters(project);
// 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\"", project.getCode()));
response.flushBuffer();
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxMCPD(filters, outputStream);
response.flushBuffer();
} catch (EOFException e) {
_logger.warn("Download was aborted", e);
}
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{code}/download", method = RequestMethod.POST, params = { "pdci" })
public void downloadPdci(ModelMap model, @PathVariable(value = "code") String code, HttpServletResponse response) throws IOException {
final Project project = projectService.getProjectByCode(code);
if (project == null || project.getAccessionLists().size() == 0) {
throw new ResourceNotFoundException();
}
_logger.warn("Downloading PDCI of: " + project.getCode());
// Create JSON filter
AppliedFilters filters = projectFilters(project);
// 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\"", project.getCode()));
response.flushBuffer();
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxPDCI(filters, outputStream);
response.flushBuffer();
} catch (EOFException e) {
_logger.warn("Download was aborted", e);
}
}
protected Locale getLocale() {
return LocaleContextHolder.getLocale();
}
......
......@@ -56,11 +56,39 @@
<span class="glyphicon glyphicon-eye-open"></span>
<spring:message code="data-overview.short" />
</a>
<div class="btn-group">
<form class="form-horizontal" method="post" action="<c:url value="/download/project/${project.code}/download" />">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-download"></span>
<span>
<spring:message code="download" />
</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<security:authorize access="isAuthenticated()">
<li>
<button name="mcpd" class="btn btn-inline" type="submit">
<spring:message code="filter.download-mcpd" />
</button>
<button name="pdci" class="btn btn-inline" type="submit">
<spring:message code="filter.download-pdci" />
</button>
</li>
</security:authorize>
<li>
<button name="dwca" class="btn btn-inline" type="submit">
<spring:message code="metadata.download-dwca" />
</button>
</li>
</ul>
</form>
</div>
</c:if>
</div>
</div>
<c:if test="${statisticsCrop ne null and statisticsCrop.totalCount gt 0}">
<h4>
<span>
......@@ -73,7 +101,7 @@
</div>
</div>
</c:if>
<c:if test="${statisticsGenus ne null and statisticsGenus.totalCount gt 0}">
<h4>
<span>
......@@ -86,7 +114,7 @@
</div>
</div>
</c:if>
<c:if test="${statisticsTaxonomy ne null and statisticsTaxonomy.totalCount gt 0}">
<h4>
<span>
......@@ -99,7 +127,7 @@
</div>
</div>
</c:if>
<c:if test="${statisticsOrigCty ne null and statisticsOrigCty.totalCount gt 0}">
<h4>
<span>
......@@ -112,56 +140,56 @@
</div>
</div>
</c:if>
<c:if test="${fn:length(accessionLists) gt 0}">
<div class="content-section-2015">
<h3>
<span>
<spring:message code="project.accessionLists" />
</span>
</h3>
<div class="row">
<div class="col-md-offset-2 col-md-10">
<ul>
<c:forEach items="${accessionLists}" var="accessionList">
<li>
<a href="<c:url value="/explore"><c:param name="filter" value='{"lists":["${accessionList.uuid}"]}' /></c:url>">
<c:out value="${accessionList.title}" />
</a>
</li>
</c:forEach>
</ul>
</div>
</div>
</div>
</c:if>
</div>
<c:if test="${countByProject gt 0}">
<div class="content-section-2015">
<h3>
<span>
<spring:message code="heading.see-also" />
</span>
</h3>
<div class="row">
<div class="col-md-offset-2 col-md-10">
<ul class="see-also">
<li>
<a href="<c:url value="/project/${project.code}/data/map" />">
<spring:message code="see-also.map" />
</a>
</li>
<c:if test="${fn:length(accessionLists) gt 0}">
<div class="content-section-2015">
<h3>
<span>
<spring:message code="project.accessionLists" />
</span>
</h3>
<div class="row">
<div class="col-md-offset-2 col-md-10">
<ul>
<c:forEach items="${accessionLists}" var="accessionList">
<li>
<a href="<c:url value="/project/${project.code}/overview" />">
<spring:message code="see-also.overview" />
<a href="<c:url value="/explore"><c:param name="filter" value='{"lists":["${accessionList.uuid}"]}' /></c:url>">
<c:out value="${accessionList.title}" />
</a>
</li>
</ul>
</div>
</c:forEach>
</ul>
</div>
</div>
</c:if>
</div>
</c:if>
<c:if test="${countByProject gt 0}">
<div class="content-section-2015">
<h3>
<span>
<spring:message code="heading.see-also" />
</span>
</h3>
<div class="row">
<div class="col-md-offset-2 col-md-10">
<ul class="see-also">
<li>
<a href="<c:url value="/project/${project.code}/data/map" />">
<spring:message code="see-also.map" />
</a>
</li>
<li>
<a href="<c:url value="/project/${project.code}/overview" />">
<spring:message code="see-also.overview" />
</a>
</li>
</ul>
</div>
</div>
</div>
</c:if>
</body>
</html>
......@@ -190,8 +190,6 @@
</c:if>
<c:if test="${countByInstitute gt 0}">
<form class="form-horizontal" method="post" action="/download/wiews/${faoInstitute.code}/download">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="row" style="margin-top: 2em;">
<div class="col-sm-12">
<a class="btn btn-primary" title="<spring:message code="faoInstitute.data-title" arguments="${faoInstitute.fullName}" />" href="<c:url value="/wiews/${faoInstitute.code}/data" />">
......@@ -208,32 +206,37 @@
</a>
</c:if>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-download"></span><span><spring:message
code="download"/></span> <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<security:authorize access="isAuthenticated()">
<li>
<button name="mcpd" class="btn btn-inline" type="submit">
<spring:message code="filter.download-mcpd" />
</button>
<%-- <button name="pdci" class="btn btn-default" type="submit">
<spring:message code="filter.download-pdci" />
</button> --%>
</li>
</security:authorize>
<li>
<button name="dwca" class="btn btn-inline" type="submit">
<spring:message code="metadata.download-dwca" />
</button>
</li>
</ul>
</div>
<form class="form-horizontal" method="post" action="<c:url value="/download/wiews/${faoInstitute.code}/download" />">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-download"></span>
<span>
<spring:message code="download" />
</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<security:authorize access="isAuthenticated()">
<li>
<button name="mcpd" class="btn btn-inline" type="submit">
<spring:message code="filter.download-mcpd" />
</button>
<button name="pdci" class="btn btn-inline" type="submit">
<spring:message code="filter.download-pdci" />
</button>
</li>
</security:authorize>
<li>
<button name="dwca" class="btn btn-inline" type="submit">
<spring:message code="metadata.download-dwca" />
</button>
</li>
</ul>
</form>
</div>
</div>
</div>
</form>
</c:if>
</div>
......
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