Commit b45b51d5 authored by Matija Obreza's avatar Matija Obreza

Download Genesys1 Parameters+Methods (descriptor definitions) for selected crop

parent 0fb2c0a7
......@@ -16,8 +16,12 @@
package org.genesys2.server.api.v0;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;
import org.genesys2.server.api.ApiBaseController;
......@@ -38,6 +42,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -224,5 +229,27 @@ public class CropsController extends ApiBaseController {
cropService.rebuildTaxonomies(crop);
}
@RequestMapping(value = "/{shortName}/descriptors/xlsx", method = RequestMethod.POST)
public void downloadExcel(ModelMap model, @PathVariable(value = "shortName") String shortName, HttpServletResponse response) throws IOException {
final Crop crop = cropService.getCrop(shortName);
if (crop == null) {
throw new ResourceNotFoundException();
}
LOG.debug("Downloading descriptors for crop {}", shortName);
// Write descriptors to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-descriptors-%1$s.xlsx\"", crop.getShortName()));
final OutputStream outputStream = response.getOutputStream();
try {
traitService.writeDescriptorsXlsx(crop, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted: {}", e.getMessage());
}
}
}
......@@ -16,15 +16,17 @@
package org.genesys2.server.mvc;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
......@@ -53,13 +55,15 @@ 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;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Controller
@RequestMapping("/c")
......@@ -298,4 +302,25 @@ public class CropController extends BaseController {
return "/descr/index";
}
@RequestMapping(value = "/{shortName}/descriptors/xlsx", method = RequestMethod.POST)
public void downloadExcel(ModelMap model, @PathVariable(value = "shortName") String shortName, HttpServletResponse response) throws IOException {
final Crop crop = cropService.getCrop(shortName);
if (crop == null) {
throw new ResourceNotFoundException();
}
LOG.debug("Downloading descriptors for crop {}", shortName);
// Write descriptors to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-descriptors-%1$s.xlsx\"", crop.getShortName()));
final OutputStream outputStream = response.getOutputStream();
try {
traitService.writeDescriptorsXlsx(crop, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted: {}", e.getMessage());
}
}
}
......@@ -24,6 +24,7 @@ import java.util.List;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.service.filter.AccessionFilter;
/**
......@@ -46,4 +47,6 @@ public interface DownloadService {
void writeXlsxMetadata(Metadata metadata, OutputStream outputStream) throws IOException;
void writeXlsxGenesys1Descriptors(Crop crop, OutputStream outputStream) throws IOException;
}
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,10 +12,12 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.service;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
......@@ -144,4 +146,13 @@ public interface TraitService {
Page<Method> listMethods(Pageable pageable);
/**
* Write descriptors as Excel to output stream.
*
* @param crop the crop
* @param outputStream the output stream
* @throws IOException
*/
void writeDescriptorsXlsx(Crop crop, OutputStream outputStream) throws IOException;
}
......@@ -24,6 +24,7 @@ import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
......@@ -66,15 +67,18 @@ import org.genesys2.server.model.genesys.AccessionRemark;
import org.genesys2.server.model.genesys.ExperimentAccessionTrait;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.genesys.TraitCode;
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.mvc.JspHelper;
import org.genesys2.server.persistence.GenesysLowlevelRepository;
import org.genesys2.server.persistence.TraitValueRepository;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.TraitService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.worker.AccessionProcessor;
import org.genesys2.util.MCPDUtil;
......@@ -84,6 +88,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -148,6 +153,9 @@ public class DownloadServiceImpl implements DownloadService {
@Autowired
private GenesysService genesysService;
@Autowired
private TraitService traitService;
@Value("${base.url}")
private String baseUrl;
......@@ -832,41 +840,8 @@ public class DownloadServiceImpl implements DownloadService {
for (final org.genesys2.server.model.genesys.Method method : metadataMethods) {
item++;
final Row nextRow = sheet.createRow(item);
updateCell(nextRow, 1, ""); // method.getUuid().toString());
updateCell(nextRow, 2, ""); // method.getVersion());
updateCell(nextRow, 3, method.getParameter().getCrop() == null ? "" : method.getParameter().getCrop().getShortName());
updateCell(nextRow, 4, "");
updateCell(nextRow, 5, method.getParameter().getTitle());
updateCell(nextRow, 6, catalogDescriptorCategory(method.getParameter().getCategory().getName()));
updateCell(nextRow, 7, method.getMethod());
updateCell(nextRow, 8, catalogDataType(method.getFieldType()));
updateCell(nextRow, 9, 2 == method.getFieldType() ? "TRUE" : 1 == method.getFieldType() ? "FALSE" : "");
// updateCell(nextRow, 10, method.getMinValue());
// updateCell(nextRow, 11, method.getMaxValue());
// if (method.getVocabulary() != null) {
// updateCell(nextRow, 12, method.getVocabulary().getUuid().toString());
// }
final StringBuilder codes = new StringBuilder();
final StringBuilder codeTitles = new StringBuilder();
final StringBuilder codeDescriptions = new StringBuilder();
for (final TraitCode traitCode : TraitCode.parseOptions(method.getOptions())) {
codes.append(NEW_LINE).append(traitCode.getCode());
codeTitles.append(NEW_LINE).append(traitCode.getValue());
String description = ""; // traitCode.getDescription();
codeDescriptions.append(NEW_LINE).append(description != null ? description.replace(NEW_LINE, "; ") : "");
}
updateCell(nextRow, 13, codes.toString().replaceFirst(NEW_LINE, ""));
updateCell(nextRow, 14, codeTitles.toString().replaceFirst(NEW_LINE, ""));
updateCell(nextRow, 15, codeDescriptions.toString().replaceFirst(NEW_LINE, ""));
updateCell(nextRow, 16, method.getFieldName());
updateCell(nextRow, 17, method.getUnit());
writeGenesys1Method(method, nextRow);
}
});
......@@ -961,6 +936,120 @@ public class DownloadServiceImpl implements DownloadService {
workbook.dispose();
outputStream.flush();
}
@Override
public void writeXlsxGenesys1Descriptors(Crop crop, OutputStream outputStream) throws IOException {
final XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/CATALOG-DESCRIPTORS.xlsx"));
final SuperSXSSFWorkbook workbook = new SuperSXSSFWorkbook(template, 50);
WorkbookStyles wbStyles = WorkbookStyles.create(workbook);
final Sheet datasetSheet = workbook.getXSSFSheet("metadata");
List<Parameter> parameters = traitService.listTraits(crop, new PageRequest(0, 1000)).getContent();
String crops = parameters.stream().map(parameter -> parameter.getCrop().getShortName()).distinct().collect(Collectors.joining(";"));
descriptors1WriteMetadata(crop, crops, datasetSheet, wbStyles);
final SuperSXSSFSheet descriptorsSheet = workbook.getStreamingSheet("descriptors");
descriptorsSheet.setRowGenerator((sheet) -> {
int item = 5;
for (Parameter p: parameters) {
for (final org.genesys2.server.model.genesys.Method method : traitService.getTraitMethods(p)) {
item++;
final Row nextRow = sheet.createRow(item);
writeGenesys1Method(method, nextRow);
}
}
});
workbook.write(outputStream);
workbook.close();
workbook.dispose();
outputStream.flush();
}
private void writeGenesys1Method(final org.genesys2.server.model.genesys.Method method, final Row nextRow) {
updateCell(nextRow, 1, ""); // method.getUuid().toString());
updateCell(nextRow, 2, ""); // method.getVersion());
updateCell(nextRow, 3, method.getParameter().getCrop() == null ? "" : method.getParameter().getCrop().getShortName());
updateCell(nextRow, 4, "genesys1");
updateCell(nextRow, 5, StringUtils.trimToNull(method.getParameter().getTitle()));
updateCell(nextRow, 6, catalogDescriptorCategory(method.getParameter().getCategory().getName()));
updateCell(nextRow, 7, StringUtils.trimToNull(method.getMethod()));
updateCell(nextRow, 8, catalogDataType(method.getFieldType()));
updateCell(nextRow, 9, 2 == method.getFieldType() ? "TRUE" : 1 == method.getFieldType() ? "FALSE" : "");
// updateCell(nextRow, 10, method.getMinValue());
// updateCell(nextRow, 11, method.getMaxValue());
// if (method.getVocabulary() != null) {
// updateCell(nextRow, 12, method.getVocabulary().getUuid().toString());
// }
final StringBuilder codes = new StringBuilder();
final StringBuilder codeTitles = new StringBuilder();
final StringBuilder codeDescriptions = new StringBuilder();
for (final TraitCode traitCode : TraitCode.parseOptions(method.getOptions())) {
codes.append(NEW_LINE).append(traitCode.getCode());
codeTitles.append(NEW_LINE).append(traitCode.getValue());
String description = ""; // traitCode.getDescription();
codeDescriptions.append(NEW_LINE).append(description != null ? description.replace(NEW_LINE, "; ") : "");
}
updateCell(nextRow, 13, codes.toString().replaceFirst(NEW_LINE, ""));
updateCell(nextRow, 14, codeTitles.toString().replaceFirst(NEW_LINE, ""));
updateCell(nextRow, 15, codeDescriptions.toString().replaceFirst(NEW_LINE, ""));
updateCell(nextRow, 16, StringUtils.trimToNull(method.getFieldName()));
updateCell(nextRow, 17, StringUtils.trimToNull(method.getUnit()));
}
private void descriptors1WriteMetadata(Crop crop, String crops, Sheet sheet, WorkbookStyles wbStyles) {
int row = 1, col = 5;
// 1 uuid
// updateCell(sheet.getRow(row++), col, metadata.getUuid().toString());
row++;
// 2 version
// updateCell(sheet.getRow(row++), col, metadata.getVersion());
row++;
// 3 Dataset provider
updateCell(sheet.getRow(row++), col, "Genesys");
// 4 Title
updateCell(sheet.getRow(row++), col, MessageFormat.format("Genesys 1 descriptors for {0}", crop.getName()));
// 5 Version
updateCell(sheet.getRow(row++), col, "genesys1");
// 6 Dataset description
updateCell(sheet.getRow(row++), col, MessageFormat.format("Descriptors for {0} as used by Genesys 1 trait database.", crop.getName()));
// 7 Dataset URL
updateCellUrl(sheet.getRow(row++), col, MessageFormat.format("https://www.genesys-pgr.org/c/{0}/descriptors", crop.getShortName()));
// 8 Date of registration of the dataset
updateCell(sheet.getRow(row++), col, new Date(), wbStyles.dateStyle);
// 9 License
updateCell(sheet.getRow(row++), col, "CC-BY");
// 10 Language of the dataset
updateCell(sheet.getRow(row++), col, "en");
// 11 Related resources
updateCell(sheet.getRow(row++), col, "");
// 12 Crops
if (crops != null) {
updateCell(sheet.getRow(row++), col, crops);
}
// 13 Dataset provider email
updateCellEmail(sheet.getRow(row++), col, "helpdesk@genesys-pgr.org");
// 14 Dataset provider addresss
updateCell(sheet.getRow(row++), col, "");
// 15 Recommended citation
{
// updateCell(sheet.getRow(row++), col, metadata.getCitation());
row++;
}
}
private String catalogDescriptorCategory(String name) {
switch (name) {
......@@ -1157,4 +1246,5 @@ public class DownloadServiceImpl implements DownloadService {
return name;
return otherNames + ";" + name;
}
}
......@@ -16,6 +16,8 @@
package org.genesys2.server.service.impl;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -38,6 +40,7 @@ import org.genesys2.server.persistence.MethodRepository;
import org.genesys2.server.persistence.ParameterCategoryRepository;
import org.genesys2.server.persistence.ParameterRepository;
import org.genesys2.server.persistence.TraitValueRepository;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.TraitService;
import org.slf4j.Logger;
......@@ -64,6 +67,10 @@ public class TraitServiceImpl implements TraitService {
private GenesysLowlevelRepository genesysLowlevelRepository;
@Autowired
private CustomAclService aclService;
@Autowired
private DownloadService downloadService;
@Autowired
private HtmlSanitizer htmlSanitizer;
......@@ -323,4 +330,9 @@ public class TraitServiceImpl implements TraitService {
return paramMethods;
}
@Override
public void writeDescriptorsXlsx(Crop crop, OutputStream outputStream) throws IOException {
downloadService.writeXlsxGenesys1Descriptors(crop, outputStream);
}
}
......@@ -20,6 +20,16 @@
<div class="nav-header pull-left">
<local:paginate2 page="${pagedData}" />
</div>
<div class="pull-right list-view-controls">
<security:authorize access="isAuthenticated()">
<form class="form-horizontal" method="post" action="<c:url value="/c/${crop.shortName}/descriptors/xlsx" />">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<div class="col-sm-4">
<button class="btn btn-primary" type="submit"><spring:message code="metadata.download-xlsx" /></button>
</div>
</form>
</security:authorize>
</div>
</div>
<c:if test="${crop ne null}">
......
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