use of eu.esdihumboldt.hale.io.csv.writer.CellType in project hale by halestudio.
the class XLSAlignmentMappingWriter method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
super.execute(progress, reporter);
Workbook workbook;
// write xls file
if (getContentType().getId().equals("eu.esdihumboldt.hale.io.xls.xls")) {
workbook = new HSSFWorkbook();
} else // write xlsx file
if (getContentType().getId().equals("eu.esdihumboldt.hale.io.xls.xlsx")) {
workbook = new XSSFWorkbook();
} else {
reporter.error(new IOMessageImpl("Content type is invalid!", null));
reporter.setSuccess(false);
return reporter;
}
Sheet sheet = workbook.createSheet();
workbook.setSheetName(0, "Mapping table");
Row row = null;
Cell cell = null;
// create cell style of the header
CellStyle headerStyle = XLSCellStyles.getHeaderStyle(workbook);
// create cell style
CellStyle cellStyle = XLSCellStyles.getNormalStyle(workbook, false);
// create highlight style for type cells
CellStyle highlightStyle = XLSCellStyles.getHighlightedStyle(workbook, false);
// create disabled style
CellStyle disabledStyle = XLSCellStyles.getNormalStyle(workbook, true);
// create disabled highlight style
CellStyle disabledTypeStyle = XLSCellStyles.getHighlightedStyle(workbook, true);
List<Map<CellType, CellInformation>> mapping = getMappingList();
// determine if cells are organized by type cell
boolean byTypeCell = isByTypeCell();
int rownum = 0;
// write header
row = sheet.createRow(rownum++);
for (int i = 0; i < getMappingHeader().size(); i++) {
cell = row.createCell(i);
cell.setCellValue(getMappingHeader().get(i));
cell.setCellStyle(headerStyle);
}
// write all mappings
for (Map<CellType, CellInformation> entry : mapping) {
boolean disabled = false;
if (getParameter(TRANSFORMATION_AND_DISABLED_FOR).as(Boolean.class)) {
List<String> transformationDisabled = entry.get(CellType.TRANSFORMATION_AND_DISABLED).getText();
disabled = !transformationDisabled.isEmpty() && !transformationDisabled.contains(TransformationMode.active.displayName());
}
// create a row
row = sheet.createRow(rownum);
CellStyle rowStyle = cellStyle;
String targetProp = getCellValue(entry, CellType.TARGET_PROPERTIES);
boolean isTypeCell = targetProp == null || targetProp.isEmpty();
if (isTypeCell && byTypeCell) {
if (disabled) {
// disabled type cell
rowStyle = disabledTypeStyle;
} else {
// normal type cell
rowStyle = highlightStyle;
}
} else if (disabled) {
// disabled property cell
rowStyle = disabledStyle;
}
List<CellType> celltypes = getCellTypes();
for (int i = 0; i < celltypes.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(getCellValue(entry, celltypes.get(i)));
cell.setCellStyle(rowStyle);
}
rownum++;
}
// could be integrated in configuration page
// int maxColWidth = calculateWidth(getParameter(MAX_COLUMN_WIDTH).as(Integer.class));
int maxColWidth = calculateWidth(maxWidth);
// autosize all columns
for (int i = 0; i < getMappingHeader().size(); i++) {
sheet.autoSizeColumn(i);
if (sheet.getColumnWidth(i) > maxColWidth)
sheet.setColumnWidth(i, maxColWidth);
}
// write file
FileOutputStream out = new FileOutputStream(getTarget().getLocation().getPath());
workbook.write(out);
out.close();
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.io.csv.writer.CellType in project hale by halestudio.
the class CSVAlignmentMappingWriter method execute.
/**
* @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
* eu.esdihumboldt.hale.common.core.io.report.IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
// do initialization
super.execute(progress, reporter);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(getTarget().getOutput()));
writer.writeNext(getMappingHeader().toArray(new String[] {}));
mapping = getMappingList();
for (Map<CellType, CellInformation> entry : mapping) {
// write each mapping to a row
String[] row = new String[entry.size()];
List<CellType> celltypes = getCellTypes();
for (int i = 0; i < entry.size(); i++) {
row[i] = getCellValue(entry, celltypes.get(i));
}
writer.writeNext(row);
}
writer.close();
reporter.setSuccess(true);
return reporter;
}
Aggregations