Commit f8a25b78 authored by Matija Obreza's avatar Matija Obreza

Export existing trait data in Catalog Excel format

parent 42c747f2
......@@ -551,7 +551,9 @@ public class DatasetServiceImpl implements DatasetService {
throw new InvalidApiUsageException("Dataset is published, no edits allowed.");
}
LOG.warn("Matching " + accessionRefs.size() + " with Accessions");
loadedDataset.setAccessionRefs(lookupMatchingAccessions(accessionRefs));
LOG.warn("Done matching " + accessionRefs.size() + " with Accessions");
return lazyLoad(datasetRepository.save(loadedDataset));
}
......@@ -589,7 +591,7 @@ public class DatasetServiceImpl implements DatasetService {
// generate file with metadata and add it to the dataset
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
downloadService.writeXlsxDatasetMetadata(loaded, bos);
downloadService.writeXlsxDataset(loaded, bos);
final String fileName = "Metadata.xlsx";
final String contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
try {
......@@ -871,7 +873,6 @@ public class DatasetServiceImpl implements DatasetService {
List<AccessionRef> list = new ArrayList<AccessionRef>(accessionRefs);
List<Accession> foundAccessions = accessionRepository.find(list);
LOG.warn("Found {} matches for {} identifiers after {}ms", foundAccessions.size(), accessionRefs.size(), stopWatch.getTime());
LOG.info("Found {} matches for {} identifiers", foundAccessions.size(), accessionRefs.size());
accessionRefs.forEach(ref -> {
Accession foundAccession = foundAccessions.stream().filter(a -> {
......
......@@ -126,7 +126,7 @@ public class ApiExceptionHandler {
public ApiError<Exception> handleValidationError(final ConstraintViolationException e, final HttpServletRequest request) {
final StringBuilder message = new StringBuilder("Validation failed: ");
if (e.getConstraintViolations() != null) {
for (ConstraintViolation cv : e.getConstraintViolations()) {
for (ConstraintViolation<?> cv : e.getConstraintViolations()) {
final PathImpl path = (PathImpl) cv.getPropertyPath();
message.append(path.getLeafNode().getName());
message.append(" -> ");
......@@ -134,7 +134,7 @@ public class ApiExceptionHandler {
message.append("; ");
}
}
LOG.warn("{} for {} {}", message.toString(), request.getMethod(), request.getRequestURL(), e);
LOG.warn("{} for {} {}", message.toString(), request.getMethod(), request.getRequestURL());
return new ApiError<>(new Exception(message.toString(), e));
}
......
......@@ -16,7 +16,9 @@
package org.genesys2.server.api.v0;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -25,6 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;
import org.genesys2.server.api.ApiBaseController;
......@@ -139,6 +142,29 @@ public class DatasetController extends ApiBaseController {
LOG.info("Getting metadata {}", metadataId);
return OAuth2Cleanup.clean(datasetService.getDataset(metadataId));
}
@RequestMapping(value = "/{metadataId}/xlsx", method = RequestMethod.GET)
public void downloadExcel(@PathVariable(value = "metadataId") long metadataId, HttpServletResponse response) throws IOException {
LOG.debug("Downloading data for {}", metadataId);
final Metadata metadata = genesysService.getMetadata(metadataId);
if (metadata == null) {
throw new ResourceNotFoundException();
}
// Write dataset to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-dataset-%1$s.xlsx\"", metadata.getUuid()));
final OutputStream outputStream = response.getOutputStream();
try {
datasetService.writeDatasetXlsx(metadata, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted: {}", e.getMessage());
}
}
/**
* Get metadata Methods /datasets/{shortName}/methods
......
......@@ -16,6 +16,7 @@
package org.genesys2.server.mvc;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.stream.Collectors;
......@@ -104,4 +105,26 @@ public class DatasetController extends BaseController {
datasetService.writeDataset(metadata, outputStream);
response.flushBuffer();
}
@RequestMapping(value = "/view/{id}/xlsx", method = RequestMethod.POST)
public void downloadExcel(@PathVariable(value = "id") long metadataId, HttpServletResponse response) throws IOException {
LOG.debug("Downloading data for {}", metadataId);
final Metadata metadata = genesysService.getMetadata(metadataId);
if (metadata == null) {
throw new ResourceNotFoundException();
}
// Write dataset to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-dataset-%1$s.xlsx\"", metadata.getUuid()));
final OutputStream outputStream = response.getOutputStream();
try {
datasetService.writeDatasetXlsx(metadata, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted: {}", e.getMessage());
}
}
}
......@@ -98,7 +98,7 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
Set<String> uniqueDois = sublist.stream().map(aid -> aid.getDoi()).filter(doi -> doi != null).distinct().collect(Collectors.toSet());
Path<Object> theDoi = root.get("doi");
Path<Object> theInstCode = root.get("instituteCode");
Path<Object> theInstCode = root.get("institute").get("code");
Path<Object> theAcceNumb = root.get("accessionNumber");
Path<Object> theGenus = root.get("genus");
......
/**
* 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,7 +12,7 @@
* 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;
......@@ -51,4 +51,14 @@ public interface DatasetService {
*/
void writeDataset(Metadata metadata, OutputStream outputStream) throws IOException;
/**
* Write dataset as xlsx.
*
* @param metadata the metadata
* @param outputStream the output stream
* @throws IOException
*/
@PreAuthorize("isAuthenticated()")
void writeDatasetXlsx(Metadata metadata, OutputStream outputStream) throws IOException;
}
......@@ -23,12 +23,13 @@ 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.service.filter.AccessionFilter;
/**
* Defines available downloads
*
* @author matijaobreza
* @author Matija Obreza
*
*/
public interface DownloadService {
......@@ -39,8 +40,10 @@ public interface DownloadService {
void writeXlsxDescriptor(List<Descriptor> descriptors, OutputStream outputStream) throws IOException;
void writeXlsxDatasetMetadata(Dataset dataset, OutputStream outputStream) throws IOException;
void writeXlsxDataset(Dataset dataset, OutputStream outputStream) throws IOException;
void writeCsvLastUpdated(List<Object[]> lastUpdatedStatistics, OutputStreamWriter outputStream);
void writeXlsxMetadata(Metadata metadata, OutputStream outputStream) throws IOException;
}
......@@ -88,6 +88,7 @@ import org.genesys2.server.persistence.SvalbardRepository;
import org.genesys2.server.persistence.TraitValueRepository;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.DatasetService;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GenesysService;
......@@ -121,7 +122,6 @@ import com.google.common.collect.Sets;
import com.opencsv.CSVWriter;
import com.opencsv.ResultSetHelper;
import com.opencsv.ResultSetHelperService;
import com.querydsl.jpa.impl.JPAQueryFactory;
@Service
@Transactional(readOnly = true)
......@@ -132,10 +132,6 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Autowired
private EntityManager entityManager;
@Autowired
private JPAQueryFactory jpaQueryFactory;
// Services
@Autowired
private TaxonomyService taxonomyService;
......@@ -191,6 +187,9 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Autowired
private ElasticsearchService elasticService;
@Autowired
private DownloadService downloadService;
private List<String> availableDisplayColumns;
......@@ -1714,4 +1713,11 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
return accessionRepository.findActiveAndHistoric(accessionUuids);
}
// To be removed
// TODO Remove in 2.5
@Override
public void writeDatasetXlsx(Metadata metadata, OutputStream outputStream) throws IOException {
downloadService.writeXlsxMetadata(metadata, outputStream);
}
}
......@@ -38,6 +38,14 @@
<button class="btn btn-primary" type="submit"><spring:message code="metadata.download-dwca" /></button>
</div>
</form>
<security:authorize access="isAuthenticated()">
<form class="form-horizontal" method="post" action="<c:url value="/data/view/${metadata.id}/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>
......
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