Commit d2399237 authored by Matija Obreza's avatar Matija Obreza

Excel: using common date format

parent 96a9b671
......@@ -24,8 +24,6 @@ import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
......@@ -34,7 +32,9 @@ import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.POIXMLProperties;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.openxml4j.util.Nullable;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
......@@ -42,6 +42,7 @@ import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SuperSXSSFSheet;
......@@ -153,17 +154,37 @@ public class DownloadServiceImpl implements DownloadService {
private String genesysCatalogUrl;
private HashMap<String, Method> pdciMethods;
private static class WorkbookStyles {
CellStyle dateStyle;
public static WorkbookStyles create(Workbook workbook) {
WorkbookStyles wbStyles = new WorkbookStyles();
wbStyles.dateStyle = workbook.createCellStyle();
wbStyles.dateStyle.setDataFormat(workbook.createDataFormat().getFormat("dd-mmm-yyyy"));
wbStyles.dateStyle.setAlignment(HorizontalAlignment.RIGHT);
return wbStyles;
}
}
@Override
public void writeXlsxMCPD(final AccessionFilter filter, final OutputStream outputStream) throws IOException {
XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/MCPD.xlsx"));
POIXMLProperties props = template.getProperties();
POIXMLProperties.CoreProperties coreProp = props.getCoreProperties();
coreProp.setCreated(new Nullable<Date>(new Date()));
POIXMLProperties.CustomProperties custProp = props.getCustomProperties();
custProp.addProperty("Filter", filter.toString());
custProp.addProperty("Genesys URL", baseUrl);
// keep 50 rows in memory, exceeding rows will be flushed to disk
SuperSXSSFWorkbook wb = new SuperSXSSFWorkbook(template, 50);
CellStyle dateStyle = wb.createCellStyle();
dateStyle.setDataFormat(wb.createDataFormat().getFormat("dd/mm/yyyy"));
dateStyle.setAlignment(HorizontalAlignment.LEFT);
WorkbookStyles wbStyles = WorkbookStyles.create(wb);
CellStyle dateStyle = wbStyles.dateStyle;
Sheet legal = wb.getXSSFSheet("Legal information");
Row r;
......@@ -196,7 +217,7 @@ public class DownloadServiceImpl implements DownloadService {
final Writer writer = new Writer();
try {
accessionProcessor.process(filter, (accession) -> {
writer.processMCPD(sheet, accession);
writer.processMCPD(sheet, wbStyles, accession);
return accession;
});
LOG.warn("Done streaming MCPD rows");
......@@ -219,9 +240,9 @@ public class DownloadServiceImpl implements DownloadService {
private class Writer {
int i = 0;
private void processMCPD(final Sheet sheet, final Accession accession) {
private void processMCPD(final Sheet sheet, WorkbookStyles wbStyles, final Accession accession) {
Row row = sheet.createRow(++i);
writeMCPDRow(row, accession);
writeMCPDRow(row, wbStyles, accession);
if (i % 1000 == 0) {
LOG.info("Writing MCPD row={}", i);
......@@ -251,7 +272,7 @@ public class DownloadServiceImpl implements DownloadService {
}
}
private void writeMCPDRow(final Row row, final Accession accession) {
private void writeMCPDRow(final Row row, WorkbookStyles wbStyles, final Accession accession) {
// Process and write result
row.createCell(COL_INSTCODE).setCellValue(accession.getInstitute().getCode());
......@@ -375,9 +396,7 @@ public class DownloadServiceImpl implements DownloadService {
}
{
Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String lastModifiedDate = accession.getLastModifiedDate() != null ? formatter.format(accession.getLastModifiedDate()) : "";
updateCell(row, COL_LASTMODIFIED, lastModifiedDate);
updateCell(row, COL_LASTMODIFIED, accession.getLastModifiedDate(), wbStyles.dateStyle);
}
}
......@@ -386,8 +405,10 @@ public class DownloadServiceImpl implements DownloadService {
final XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/CATALOG-DATASET.xlsx"));
final SuperSXSSFWorkbook workbook = new SuperSXSSFWorkbook(template, 50);
WorkbookStyles wbStyles = WorkbookStyles.create(workbook);
final Sheet datasetSheet = workbook.getXSSFSheet("metadata");
datasetWriteMetadata(dataset, datasetSheet);
datasetWriteMetadata(dataset, datasetSheet, wbStyles);
final SuperSXSSFSheet accessionsSheet = workbook.getStreamingSheet("accessions");
accessionsSheet.setRowGenerator((sheet) -> {
......@@ -415,7 +436,7 @@ public class DownloadServiceImpl implements DownloadService {
outputStream.flush();
}
private void datasetWriteMetadata(Dataset dataset, Sheet sheet) {
private void datasetWriteMetadata(Dataset dataset, Sheet sheet, WorkbookStyles wbStyles) {
int row = 1, col = 5;
// 1 uuid
updateCell(sheet.getRow(row++), col, dataset.getUuid().toString());
......@@ -432,7 +453,7 @@ public class DownloadServiceImpl implements DownloadService {
// 7 Dataset URL
updateCellUrl(sheet.getRow(row++), col, genesysCatalogUrl + "/datasets/" + dataset.getUuid());
// 8 Date of registration of the dataset
updateCell(sheet.getRow(row++), col, dataset.getCreatedDate());
updateCell(sheet.getRow(row++), col, dataset.getCreatedDate(), wbStyles.dateStyle);
// 9 License
updateCell(sheet.getRow(row++), col, dataset.getRights());
// 10 Language of the dataset
......@@ -767,12 +788,14 @@ public class DownloadServiceImpl implements DownloadService {
final XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/CATALOG-DATASET.xlsx"));
final SuperSXSSFWorkbook workbook = new SuperSXSSFWorkbook(template, 50);
WorkbookStyles wbStyles = WorkbookStyles.create(workbook);
final Sheet datasetSheet = workbook.getXSSFSheet("metadata");
final List<org.genesys2.server.model.genesys.Method> metadataMethods = genesysService.listMethods(metadata);
String crops = metadataMethods.stream().map(method -> method.getParameter().getCrop().getShortName()).distinct().collect(Collectors.joining(";"));
datasetWriteMetadata(metadata, crops, datasetSheet);
datasetWriteMetadata(metadata, crops, datasetSheet, wbStyles);
final SuperSXSSFSheet accessionsSheet = workbook.getStreamingSheet("accessions");
accessionsSheet.setRowGenerator((sheet) -> {
......@@ -980,7 +1003,7 @@ public class DownloadServiceImpl implements DownloadService {
}
}
private void datasetWriteMetadata(Metadata metadata, String crops, Sheet sheet) {
private void datasetWriteMetadata(Metadata metadata, String crops, Sheet sheet, WorkbookStyles wbStyles) {
int row = 1, col = 5;
// 1 uuid
updateCell(sheet.getRow(row++), col, metadata.getUuid().toString());
......@@ -997,7 +1020,7 @@ public class DownloadServiceImpl implements DownloadService {
// 7 Dataset URL
updateCellUrl(sheet.getRow(row++), col, "");
// 8 Date of registration of the dataset
updateCell(sheet.getRow(row++), col, metadata.getCreatedDate());
updateCell(sheet.getRow(row++), col, metadata.getCreatedDate(), wbStyles.dateStyle);
// 9 License
updateCell(sheet.getRow(row++), col, "");
// 10 Language of the dataset
......@@ -1094,12 +1117,15 @@ public class DownloadServiceImpl implements DownloadService {
return c;
}
private Cell updateCell(Row row, int column, Date date) {
private Cell updateCell(Row row, int column, Date date, CellStyle dateStyle) {
if (date == null)
return null;
Cell c = row.getCell(column);
if (c == null)
c = row.createCell(column);
if (dateStyle != null) {
c.setCellStyle(dateStyle);
}
c.setCellValue(date);
return c;
}
......
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