Commit 49951e9f authored by Alexander Prendetskiy's avatar Alexander Prendetskiy Committed by Matija Obreza
Browse files

export descriptors to excel

parent d49e9972
......@@ -15,7 +15,9 @@
*/
package org.genesys.catalog.api.v0;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
......@@ -28,6 +30,8 @@ import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
......@@ -46,6 +50,8 @@ import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import javax.servlet.http.HttpServletResponse;
/**
* The Class DescriptorController.
*
......@@ -57,6 +63,8 @@ import io.swagger.annotations.Api;
@Api(tags = { "descriptor" })
public class DescriptorController {
private Logger LOG = LoggerFactory.getLogger(DescriptorController.class);
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/descriptor";
......@@ -293,4 +301,30 @@ public class DescriptorController {
return descriptorService.rejectDescriptor(descriptor);
}
@PostMapping(value = "/export", produces = { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" })
public void exportDescriptors(
@RequestParam(name = "f", required = false) String filterCode,
@RequestBody(required = false) DescriptorFilter filter, HttpServletResponse response) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DescriptorFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-descriptors-" + filterCode + ".xlsx\""));
response.flushBuffer();
// Write XLSX to the stream.
final OutputStream outputStream = response.getOutputStream();
try {
descriptorService.exportDescriptors(filter, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
}
}
}
......@@ -24,6 +24,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.method.P;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
......@@ -169,4 +171,5 @@ public interface DescriptorService {
*/
Descriptor nextVersion(@P("descriptor") Descriptor descriptor, boolean major);
void exportDescriptors(DescriptorFilter filter, OutputStream outputStream) throws IOException;
}
......@@ -17,6 +17,8 @@ package org.genesys.catalog.service.impl;
import static org.genesys.catalog.model.traits.QDescriptor.descriptor;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
......@@ -41,6 +43,7 @@ import org.genesys.catalog.service.VersionManager;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.genesys2.server.service.DownloadService;
import org.genesys2.util.JPAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -77,6 +80,9 @@ public class DescriptorServiceImpl implements DescriptorService {
@Autowired
private VocabularyTermRepository termRepository;
@Autowired
private DownloadService downloadService;
/** The securityUtils. */
@Autowired
private SecurityUtils securityUtils;
......@@ -443,4 +449,12 @@ public class DescriptorServiceImpl implements DescriptorService {
return lazyLoad(descriptorRepository.save(copy));
}
@Override
public void exportDescriptors(DescriptorFilter filter, OutputStream outputStream) throws IOException {
List<Descriptor> descriptors = (List<Descriptor>) descriptorRepository
.findAll(filter.buildQuery());
downloadService.writeXlsxDescriptor(descriptors, outputStream);
}
}
......@@ -21,6 +21,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
/**
......@@ -35,6 +36,8 @@ public interface DownloadService {
void writeXlsxPDCI(AppliedFilters appliedFilters, OutputStream outputStream) throws IOException;
void writeXlsxDescriptor(List<Descriptor> descriptors, OutputStream outputStream) throws IOException;
void writeCsvLastUpdated(List<Object[]> lastUpdatedStatistics, OutputStreamWriter outputStream);
}
......@@ -39,6 +39,7 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook;
// NOTE Excel 2016 has a limit of 66,530 hyperlinks
// import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys2.server.model.genesys.AccessionAlias;
import org.genesys2.server.model.genesys.AccessionCollect;
import org.genesys2.server.model.genesys.AccessionData;
......@@ -375,6 +376,52 @@ public class DownloadServiceImpl implements DownloadService {
}
}
@Override
@Transactional(readOnly = true)
public void writeXlsxDescriptor(final List<Descriptor> descriptors, final OutputStream outputStream) throws IOException {
XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/DESCRIPTORS.xlsx"));
SXSSFWorkbook workbook = new SXSSFWorkbook(template, -1);
Sheet descriptorSheet = workbook.getSheet("Descriptors");
writeDescriptorInformation(descriptors, descriptorSheet);
((SXSSFSheet) descriptorSheet).flushRows();
workbook.write(outputStream);
workbook.dispose();
outputStream.flush();
}
private void writeDescriptorInformation(List<Descriptor> descriptors, Sheet sheet) {
int item = 0;
for(Descriptor descriptor: descriptors) {
item++;
Row nextRow = sheet.createRow(item);
createCell(nextRow, 0, descriptor.getUuid().toString());
createCell(nextRow, 1, descriptor.getVersion());
createCell(nextRow, 2, descriptor.getCrop());
createCell(nextRow, 3, descriptor.getVersionTag());
createCell(nextRow, 4, descriptor.getTitle());
createCell(nextRow, 5, descriptor.getCategory().name());
createCell(nextRow, 6, descriptor.isKey());
createCell(nextRow, 7, descriptor.getDescription());
createCell(nextRow, 8, descriptor.getDataType().name());
createCell(nextRow, 9, descriptor.getIntegerOnly());
createCell(nextRow, 10, descriptor.getMinValue());
createCell(nextRow, 11, descriptor.getMaxValue());
String vocabulary = descriptor.getVocabulary() == null ? "" : descriptor.getVocabulary().getTitle();
createCell(nextRow, 12, vocabulary);
createCell(nextRow, 13, descriptor.isPublished());
createCell(nextRow, 14, descriptor.getColumnName());
createCell(nextRow, 15, descriptor.getUom());
}
}
@Override
@Transactional(readOnly = true)
......
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