Commit 0751e487 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Export Descriptor List as Excel

parent 894f3bea
......@@ -15,6 +15,8 @@
*/
package org.genesys.catalog.service;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
......@@ -177,4 +179,13 @@ public interface DescriptorListService {
*/
List<DescriptorList> autocompleteDescriptorLists(String text);
/**
* Write descriptorList as Excel to output stream.
*
* @param descriptorList the descriptorList
* @param outputStream the output stream
* @throws IOException
*/
void exportDescriptorList(DescriptorList descriptorList, OutputStream outputStream) throws IOException;
}
......@@ -18,6 +18,8 @@ package org.genesys.catalog.service.impl;
import static org.genesys.catalog.model.traits.QDescriptorList.descriptorList;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
......@@ -40,6 +42,7 @@ import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.service.DownloadService;
import org.genesys2.util.JPAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -86,7 +89,9 @@ public class DescriptorListServiceImpl implements DescriptorListService {
@Autowired
private DescriptorService descriptorService;
@Autowired
private DownloadService downloadService;
/* (non-Javadoc)
* @see org.genesys.catalog.service.DescriptorListService#countDescriptorLists(org.genesys.catalog.model.filters.DescriptorListFilter)
*/
......@@ -454,4 +459,12 @@ public class DescriptorListServiceImpl implements DescriptorListService {
return descriptorListRepository.findAll(predicate, markdownSortPageRequest).getContent();
}
}
/**
* {@inheritDoc}
*/
@Override
public void exportDescriptorList(final DescriptorList descriptorList, final OutputStream outputStream) throws IOException {
downloadService.writeXlsxDescriptorList(descriptorList, outputStream);
}
}
......@@ -17,7 +17,9 @@
package org.genesys2.server.api.v1;
import javax.servlet.http.HttpServletResponse;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -38,10 +40,13 @@ import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.FilteredPage;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.ElasticsearchService.TermResult;
import org.genesys2.server.service.impl.SearchException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
......@@ -69,6 +74,9 @@ import org.springframework.web.bind.annotation.RestController;
@Api(tags = { "descriptor-list" })
public class DescriptorListController {
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(DescriptorListController.class);
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/descriptorlist";
......@@ -306,6 +314,27 @@ public class DescriptorListController {
return descriptorListService.reviewDescriptorList(descriptorList);
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "{uuid}/download", method = RequestMethod.POST)
public void download(@PathVariable("uuid") final UUID uuid, HttpServletResponse response) throws IOException, NotFoundElement {
final DescriptorList descriptorList = descriptorListService.getDescriptorList(uuid);
if (descriptorList == null) {
throw new NotFoundElement();
}
// Write descriptorList metadata to the stream.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"DESCRIPTOR LIST - %1s.xlsx\"", descriptorList.getUuid()));
final OutputStream outputStream = response.getOutputStream();
try {
descriptorListService.exportDescriptorList(descriptorList, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted: {}", e.getMessage());
}
}
/**
* Loads descriptorList by uuid and version and unpublish it.
*
......
......@@ -26,6 +26,7 @@ import com.querydsl.jpa.JPQLQuery;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.service.filter.AccessionFilter;
......@@ -50,6 +51,8 @@ public interface DownloadService {
void writeXlsxDataset(Dataset dataset, OutputStream outputStream) throws IOException;
void writeXlsxDescriptorList(DescriptorList descriptorList, OutputStream outputStream) throws IOException;
void writeCsvLastUpdated(List<Object[]> lastUpdatedStatistics, OutputStreamWriter outputStream);
void writeXlsxMetadata(Metadata metadata, OutputStream outputStream) throws IOException;
......
......@@ -25,6 +25,7 @@ import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
......@@ -65,6 +66,7 @@ import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetCreator;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias;
......@@ -449,6 +451,77 @@ public class DownloadServiceImpl implements DownloadService {
}
}
@Override
public void writeXlsxDescriptorList(final DescriptorList descriptorList, final OutputStream outputStream) throws IOException {
final XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/CATALOG-DESCRIPTORLIST.xlsx"));
final SuperSXSSFWorkbook workbook = new SuperSXSSFWorkbook(template, 50);
WorkbookStyles wbStyles = WorkbookStyles.create(workbook);
final Sheet metadataSheet = workbook.getXSSFSheet("metadata");
descriptorListWriteMetadata(descriptorList, metadataSheet, wbStyles);
final SuperSXSSFSheet descriptorsSheet = workbook.getStreamingSheet("descriptors");
descriptorsSheet.setRowGenerator((sheet) -> {
writeDescriptorsSheet(descriptorList.getDescriptors(), sheet);
});
workbook.write(outputStream);
workbook.close();
workbook.dispose();
outputStream.flush();
}
private void descriptorListWriteMetadata(DescriptorList descriptorList, Sheet sheet, WorkbookStyles wbStyles) {
int row = 1, col = 5;
// 1 uuid
updateCell(sheet.getRow(row++), col, descriptorList.getUuid().toString());
// 2 version
updateCell(sheet.getRow(row++), col, descriptorList.getVersion());
// 3 Descriptors list provider
updateCell(sheet.getRow(row++), col, descriptorList.getOwner().getName());
// 4 Title
updateCell(sheet.getRow(row++), col, descriptorList.getTitle());
// 5 Descriptors list version
updateCell(sheet.getRow(row++), col, descriptorList.getVersionTag());
// 6 Descriptors list description
updateCell(sheet.getRow(row++), col, descriptorList.getDescription());
// 7 Descriptors list URL
updateCellUrl(sheet.getRow(row++), col, genesysCatalogUrl + "/descriptorlists/" + descriptorList.getUuid());
// 8 URL to definition
updateCellUrl(sheet.getRow(row++), col, descriptorList.getUrl());
// 9 Original publisher
updateCell(sheet.getRow(row++), col, descriptorList.getPublisher());
// 10 Bibliographic citation
updateCell(sheet.getRow(row++), col, descriptorList.getBibliographicCitation());
// 11 Date of registration of the descriptors list
updateCell(sheet.getRow(row++), col, descriptorList.getCreatedDate(), wbStyles.dateStyle);
// 12 Crop
updateCell(sheet.getRow(row++), col, descriptorList.getCrop());
// 13 Descriptors list email
updateCellEmail(sheet.getRow(row++), col, descriptorList.getOwner().getEmail());
// 14 Descriptors list address
updateCell(sheet.getRow(row++), col, descriptorList.getOwner().getAddress());
// 15 Recommended citation
{
StringBuilder citation = new StringBuilder();
// Genetic Resources Research Institute of the Kenya Agricultural and Livestock
// Research Organization
citation.append(descriptorList.getOwner().getName());
// (2018)
Calendar calendar = Calendar.getInstance();
calendar.setTime(descriptorList.getCreatedDate());
citation.append(" (").append(calendar.get(Calendar.YEAR)).append(")");
citation.append(". ");
// Version 1.0.
citation.append("Version ").append(descriptorList.getVersionTag()).append(". ");
citation.append(genesysCatalogUrl).append("/descriptorlists/").append(descriptorList.getUuid());
// accessed on 2018-APR-1
citation.append(" accessed on 2018-APR-1");
updateCell(sheet.getRow(row++), col, citation.toString());
}
}
@Override
public void writeXlsxDataset(final Dataset dataset, final OutputStream outputStream) throws IOException {
final XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/CATALOG-DATASET.xlsx"));
......@@ -466,7 +539,7 @@ public class DownloadServiceImpl implements DownloadService {
final SuperSXSSFSheet descriptorsSheet = workbook.getStreamingSheet("descriptors");
descriptorsSheet.setRowGenerator((sheet) -> {
datasetWriteDescriptorsSheet(dataset.getDescriptors(), sheet);
writeDescriptorsSheet(dataset.getDescriptors(), sheet);
});
final SuperSXSSFSheet locationsSheet = workbook.getStreamingSheet("location_timing");
......@@ -544,7 +617,7 @@ public class DownloadServiceImpl implements DownloadService {
}
private void datasetWriteDescriptorsSheet(final List<Descriptor> descriptors, final Sheet sheet) {
private void writeDescriptorsSheet(final List<Descriptor> descriptors, final Sheet sheet) {
int item = 5;
for (final Descriptor descriptor : descriptors) {
......
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