Search in sources :

Example 1 with CellInformation

use of eu.esdihumboldt.hale.io.csv.writer.CellInformation 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;
}
Also used : IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) CellInformation(eu.esdihumboldt.hale.io.csv.writer.CellInformation) CellType(eu.esdihumboldt.hale.io.csv.writer.CellType) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Map(java.util.Map)

Example 2 with CellInformation

use of eu.esdihumboldt.hale.io.csv.writer.CellInformation 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;
}
Also used : CellType(eu.esdihumboldt.hale.io.csv.writer.CellType) CSVWriter(au.com.bytecode.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter) CellInformation(eu.esdihumboldt.hale.io.csv.writer.CellInformation)

Aggregations

CellInformation (eu.esdihumboldt.hale.io.csv.writer.CellInformation)2 CellType (eu.esdihumboldt.hale.io.csv.writer.CellType)2 CSVWriter (au.com.bytecode.opencsv.CSVWriter)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Map (java.util.Map)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellStyle (org.apache.poi.ss.usermodel.CellStyle)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1