Commit 22c986cc authored by Matija Obreza's avatar Matija Obreza
Browse files

Downloads

parent 8b146047
......@@ -80,7 +80,7 @@ public class DownloadServiceImpl implements DownloadService {
@Override
@Transactional(readOnly = true)
public void writeXlsxMCPD(final AppliedFilters filters, OutputStream outputStream) throws IOException {
public void writeXlsxMCPD(final AppliedFilters filters, final OutputStream outputStream) throws IOException {
XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/MCPD.xlsx"));
// keep 1000 rows in memory, exceeding rows will be flushed to disk
......@@ -113,6 +113,7 @@ public class DownloadServiceImpl implements DownloadService {
r.createCell(0).setCellValue("Attribution");
r.createCell(1).setCellValue(baseUrl + "/content/terms");
((SXSSFSheet) legal).flushRows();
outputStream.flush();
final Sheet sheet = wb.getSheet("MCPD");
......@@ -139,6 +140,7 @@ public class DownloadServiceImpl implements DownloadService {
LOG.info("Writing to output stream");
wb.write(outputStream);
wb.dispose();
outputStream.flush();
LOG.info("Done");
}
......
......@@ -951,6 +951,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
// UTF8 is used for encoding entry names
final ZipOutputStream zos = new ZipOutputStream(outputStream);
zos.setComment("Genesys Accessions filter=" + filters);
zos.flush();
// Filter information
final ZipEntry readmeEntry = new ZipEntry("README.txt");
......@@ -959,6 +960,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
zos.putNextEntry(readmeEntry);
writeREADME(filters, zos);
zos.closeEntry();
zos.flush();
// Accessions
final ZipEntry coreEntry = new ZipEntry("core.csv");
......@@ -967,6 +969,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
zos.putNextEntry(coreEntry);
writeAccessionsCore(filters, zos);
zos.closeEntry();
zos.flush();
// AccessionGeo
ZipEntry entry = new ZipEntry("names.csv");
......@@ -975,6 +978,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
zos.putNextEntry(entry);
writeAccessionsNames(filters, zos);
zos.closeEntry();
zos.flush();
// AccessionGeo
entry = new ZipEntry("geo.csv");
......@@ -983,6 +987,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
zos.putNextEntry(entry);
writeAccessionsGeo(filters, zos);
zos.closeEntry();
zos.flush();
// AccessionCollect
entry = new ZipEntry("coll.csv");
......@@ -991,6 +996,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
zos.putNextEntry(entry);
writeAccessionsColl(filters, zos);
zos.closeEntry();
zos.flush();
final ZipEntry metaEntry = new ZipEntry("meta.xml");
metaEntry.setComment("Darwin Core Archive metadata");
......
package org.genesys2.server.servlet.controller;
import java.security.Principal;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.genesys2.server.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/download")
public class DownloadController extends BaseController {
@Autowired
private ContentService contentService;
@RequestMapping(value = "/**", method = RequestMethod.POST)
public String downloadInf(ModelMap model, HttpServletRequest request, Locale locale, Principal principal, @RequestParam Map<String, String> query) {
_logger.info("Downloader " + request.getServletPath());
if (principal instanceof UsernamePasswordAuthenticationToken) {
_logger.info("Showing download screen for authenticated users");
model.addAttribute("info", contentService.getGlobalArticle("download-authenticated", locale));
} else {
_logger.info("Showing download screen for anonymous users");
model.addAttribute("info", contentService.getGlobalArticle("download-anonymous", locale));
}
model.addAttribute("path", request.getServletPath().substring("/download".length()));
query.remove("_csrf");
model.addAttribute("query", query);
_logger.info("Ready for download");
return "/download/index";
}
}
......@@ -20,6 +20,7 @@ import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
......@@ -329,8 +330,12 @@ public class ExplorerController extends BaseController {
// Write Darwin Core Archive to the stream.
final OutputStream outputStream = response.getOutputStream();
try {
genesysService.writeAccessions(appliedFilters, outputStream);
response.flushBuffer();
} catch (EOFException e) {
_logger.warn("Download was aborted");
}
}
@PreAuthorize("isAuthenticated()")
......@@ -348,12 +353,17 @@ public class ExplorerController extends BaseController {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-filtered.xlsx\""));
response.flushBuffer();
// Write Darwin Core Archive to the stream.
// Write XLSX to the stream.
final OutputStream outputStream = response.getOutputStream();
try {
downloadService.writeXlsxMCPD(appliedFilters, outputStream);
response.flushBuffer();
} catch (EOFException e) {
_logger.warn("Download was aborted", e);
}
}
private AppliedFilters updateFilterWithCrop(String cropName, String jsonFilter) throws IOException {
......
......@@ -588,3 +588,6 @@ logger.appenders=Log appenders
menu.admin.loggers=Loggers
menu.admin.caches=Caches
download.page.title=Before you download
download.download-now=Start download, I will wait.
......@@ -31,14 +31,14 @@
</div>
<div class="col-sm-12 col-md-6" style="text-align: right; padding-top: 12px">
<c:if test="${pagedData.totalElements le 100000}">
<form style="display: inline-block" method="post" action="<c:url value="/explore/dwca" />">
<form style="display: inline-block" method="post" action="<c:url value="/download/explore/dwca" />">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<input type="hidden" name="filter" value="<c:out value="${jsonFilter}" />" />
<button class="btn btn-default" type="submit"><spring:message code="filter.download-dwca" /></button>
</form>
</c:if>
<security:authorize access="isAuthenticated()">
<form style="display: inline-block" method="post" action="<c:url value="/explore/download/mcpd" />">
<form style="display: inline-block" method="post" action="<c:url value="/download/explore/download/mcpd" />">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<input type="hidden" name="filter" value="<c:out value="${jsonFilter}" />" />
<button class="btn btn-default" type="submit"><spring:message code="filter.download-mcpd" /></button>
......
<!DOCTYPE html>
<%@include file="/WEB-INF/jsp/init.jsp"%>
<html>
<head>
<title><spring:message code="download.page.title" /></title>
</head>
<body>
<h1><spring:message code="download.page.title" /></h1>
<local:blurb blurb="${info}" />
<%--
<pre>
path=${path}
query=${query}
</pre>
--%>
<c:if test="${path ne null}">
<form id="downlodform" method="post" action="${path}">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<c:forEach items="${query.keySet()}" var="name">
<input type="hidden" name="${name}" value='<c:out value="${query[name]}" />' />
</c:forEach>
<input class="btn btn-primary" type="submit" value="<spring:message code="download.download-now" />" />
</form>
</c:if>
</body>
</html>
\ No newline at end of file
<%@ tag description="Display blurb" pageEncoding="UTF-8" %>
<%@ tag body-content="tagdependent" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="blurb" required="true" type="org.genesys2.server.model.impl.Article" %>
<%@ attribute name="title" required="false" type="java.lang.Boolean" %>
<%
%>
<div class="free-text blurp" dir="${blurb.lang=='fa' || blurb.lang=='ar' ? 'rtl' : 'ltr'}">
<c:out value="${blurb.body}" escapeXml="false" />
</div>
<c:if test="${blurb!=null && blurb.lang != pageContext.response.locale.language}">
<local:not-translated />
</c:if>
<%@ tag description="Display the non-trasnslated message" pageEncoding="UTF-8" %>
<%@ tag body-content="tagdependent" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<div class="alert alert-warning translationmissing">
<spring:message code="i18n.content-not-translated" />
</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