Search in sources :

Example 6 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class XLSInstanceReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    boolean skipFirst = getParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE).as(Boolean.class);
    // first sheet as default
    sheetNum = getParameter(InstanceTableIOConstants.SHEET_INDEX).as(int.class, 0);
    instances = new DefaultInstanceCollection(new ArrayList<Instance>());
    try {
        // analyze the excel sheet to get all information
        analyser = new AnalyseXLSSchemaTable(getSource().getLocation(), sheetNum);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Reading the excel sheet has failed", e));
        return reporter;
    }
    // get type definition of the schema
    type = getSourceSchema().getType(QName.valueOf(getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class)));
    // get property definition
    propAr = type.getChildren().toArray(new PropertyDefinition[type.getChildren().size()]);
    Collection<List<String>> rows = analyser.getRows();
    // skip if first row is a header
    if (!skipFirst) {
        // otherwise first line is also an instance
        createInstanceCollection(analyser.getHeader(), reporter);
        line++;
    }
    // iterate over all rows to create the instances
    Iterator<List<String>> allRows = rows.iterator();
    while (allRows.hasNext()) {
        List<String> row = allRows.next();
        createInstanceCollection(row, reporter);
        line++;
    }
    reporter.setSuccess(true);
    return reporter;
}
Also used : AnalyseXLSSchemaTable(eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable) ArrayList(java.util.ArrayList) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ArrayList(java.util.ArrayList) List(java.util.List) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 7 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class XLSSchemaReader method loadFromSource.

@Override
protected Schema loadFromSource(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    sheetNum = getParameter(InstanceTableIOConstants.SHEET_INDEX).as(int.class, 0);
    progress.begin("Load XLS/XLSX schema", ProgressIndicator.UNKNOWN);
    String namespace = "http://www.esdi-humboldt.eu/hale/xls";
    DefaultSchema schema = new DefaultSchema(namespace, getSource().getLocation());
    AnalyseXLSSchemaTable analyser;
    try {
        analyser = new AnalyseXLSSchemaTable(getSource().getLocation(), sheetNum);
        header = analyser.getHeader();
        // create type definition
        String typename = getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class);
        if (typename == null || typename.isEmpty()) {
            reporter.setSuccess(false);
            reporter.error(new IOMessageImpl("No Typename was set", null));
            return null;
        }
        DefaultTypeDefinition type = new DefaultTypeDefinition(new QName(typename));
        // constraints on main type
        type.setConstraint(MappingRelevantFlag.ENABLED);
        type.setConstraint(MappableFlag.ENABLED);
        type.setConstraint(HasValueFlag.DISABLED);
        type.setConstraint(AbstractFlag.DISABLED);
        // set metadata for main type
        type.setLocation(getSource().getLocation());
        StringBuffer defaultPropertyTypeBuffer = new StringBuffer();
        String[] comboSelections;
        if (getParameter(PARAM_PROPERTYTYPE).isEmpty()) {
            for (int i = 0; i < header.size(); i++) {
                defaultPropertyTypeBuffer.append("java.lang.String");
                defaultPropertyTypeBuffer.append(",");
            }
            defaultPropertyTypeBuffer.deleteCharAt(defaultPropertyTypeBuffer.lastIndexOf(","));
            String combs = defaultPropertyTypeBuffer.toString();
            comboSelections = combs.split(",");
        } else {
            comboSelections = getParameter(PARAM_PROPERTYTYPE).as(String.class).split(",");
        }
        String[] properties;
        if (getParameter(PARAM_PROPERTY).isEmpty()) {
            properties = header.toArray(new String[0]);
        } else {
            properties = getParameter(PARAM_PROPERTY).as(String.class).split(",");
        }
        // than the entries in the first line
        if ((header.size() != properties.length && properties.length != 0) || (header.size() != comboSelections.length && comboSelections.length != 0)) {
            fail("Not the same number of entries for property names, property types and words in the first line of the file");
        }
        for (int i = 0; i < comboSelections.length; i++) {
            PropertyType propertyType = PropertyTypeExtension.getInstance().getFactory(comboSelections[i]).createExtensionObject();
            DefaultPropertyDefinition property = new DefaultPropertyDefinition(new QName(properties[i]), type, propertyType.getTypeDefinition());
            configureProperty(property);
        }
        boolean skip = Arrays.equals(properties, header.toArray(new String[0]));
        type.setConstraint(new CSVConfiguration(CSVUtil.getSep(this), CSVUtil.getQuote(this), CSVUtil.getEscape(this), skip));
        schema.addType(type);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Cannot load xls/xlsx schema", e));
        reporter.setSuccess(false);
        return null;
    }
    reporter.setSuccess(true);
    return schema;
}
Also used : DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) AnalyseXLSSchemaTable(eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PropertyType(eu.esdihumboldt.hale.io.csv.PropertyType) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) CSVConfiguration(eu.esdihumboldt.hale.io.csv.reader.internal.CSVConfiguration) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)

Example 8 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl 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 9 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class XLSInstanceWriter 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 {
    boolean solveNestedProperties = getParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES).as(Boolean.class, false);
    // get the parameter to get the type definition
    String exportType = getParameter(InstanceTableIOConstants.EXPORT_TYPE).as(String.class);
    QName selectedTypeName = null;
    if (exportType != null && !exportType.equals("") && !exportType.equals(" ")) {
        selectedTypeName = QName.valueOf(exportType);
    }
    // write xls file
    if (getContentType().getId().equals("eu.esdihumboldt.hale.io.xls.xls")) {
        wb = new HSSFWorkbook();
    } else // write xlsx file
    if (getContentType().getId().equals("eu.esdihumboldt.hale.io.xls.xlsx")) {
        wb = new XSSFWorkbook();
    } else {
        reporter.error(new IOMessageImpl("Content type is invalid!", null));
        return reporter;
    }
    cellStyle = XLSCellStyles.getNormalStyle(wb, false);
    headerStyle = XLSCellStyles.getHeaderStyle(wb);
    // get all instances of the selected Type
    InstanceCollection instances = getInstanceCollection(selectedTypeName);
    Iterator<Instance> instanceIterator = instances.iterator();
    Instance instance = null;
    try {
        instance = instanceIterator.next();
    } catch (NoSuchElementException e) {
        reporter.error(new IOMessageImpl("There are no instances for the selected type.", e));
        return reporter;
    }
    List<Instance> remainingInstances = new ArrayList<Instance>();
    headerRowStrings = new ArrayList<String>();
    // all instances with equal type definitions are stored in an extra
    // sheet
    TypeDefinition definition = instance.getDefinition();
    Sheet sheet = wb.createSheet(definition.getDisplayName());
    Row headerRow = sheet.createRow(0);
    int rowNum = 1;
    Row row = sheet.createRow(rowNum++);
    writeRow(row, super.getPropertyMap(instance, headerRowStrings, solveNestedProperties));
    while (instanceIterator.hasNext()) {
        Instance nextInst = instanceIterator.next();
        if (nextInst.getDefinition().equals(definition)) {
            row = sheet.createRow(rowNum++);
            writeRow(row, super.getPropertyMap(nextInst, headerRowStrings, solveNestedProperties));
        } else
            remainingInstances.add(nextInst);
    }
    writeHeaderRow(headerRow, headerRowStrings);
    setCellStyle(sheet, headerRowStrings.size());
    resizeSheet(sheet);
    // write file
    FileOutputStream out = new FileOutputStream(getTarget().getLocation().getPath());
    wb.write(out);
    out.close();
    reporter.setSuccess(true);
    return reporter;
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ArrayList(java.util.ArrayList) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class XLSLookupTableWriter 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 {
    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, "Lookup table");
    Row row = null;
    Cell cell = null;
    DataFormat df = workbook.createDataFormat();
    // create cell style of the header
    CellStyle headerStyle = workbook.createCellStyle();
    Font headerFont = workbook.createFont();
    // use bold font
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerStyle.setFont(headerFont);
    // set a medium border
    headerStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
    // set cell data format to text
    headerStyle.setDataFormat(df.getFormat("@"));
    // create cell style
    CellStyle rowStyle = workbook.createCellStyle();
    // set thin border around the cell
    rowStyle.setBorderBottom(CellStyle.BORDER_THIN);
    rowStyle.setBorderLeft(CellStyle.BORDER_THIN);
    rowStyle.setBorderRight(CellStyle.BORDER_THIN);
    // set cell data format to text
    rowStyle.setDataFormat(df.getFormat("@"));
    // display multiple lines
    rowStyle.setWrapText(true);
    Map<Value, Value> table = getLookupTable().getTable().asMap();
    int rownum = 0;
    // write header
    row = sheet.createRow(rownum++);
    cell = row.createCell(0);
    cell.setCellValue(getParameter(LookupTableExportConstants.PARAM_SOURCE_COLUMN).as(String.class));
    cell.setCellStyle(headerStyle);
    cell = row.createCell(1);
    cell.setCellValue(getParameter(LookupTableExportConstants.PARAM_TARGET_COLUMN).as(String.class));
    cell.setCellStyle(headerStyle);
    for (Value key : table.keySet()) {
        // create a row
        row = sheet.createRow(rownum);
        cell = row.createCell(0);
        cell.setCellValue(key.as(String.class));
        cell.setCellStyle(rowStyle);
        Value entry = table.get(key);
        cell = row.createCell(1);
        cell.setCellValue(entry.as(String.class));
        cell.setCellStyle(rowStyle);
        rownum++;
    }
    // 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) Font(org.apache.poi.ss.usermodel.Font) FileOutputStream(java.io.FileOutputStream) DataFormat(org.apache.poi.ss.usermodel.DataFormat) Value(eu.esdihumboldt.hale.common.core.io.Value) 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)

Aggregations

IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)85 IOException (java.io.IOException)43 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)33 QName (javax.xml.namespace.QName)20 URI (java.net.URI)15 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)14 InputStream (java.io.InputStream)13 File (java.io.File)12 HashMap (java.util.HashMap)11 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)9 FileOutputStream (java.io.FileOutputStream)9 ArrayList (java.util.ArrayList)9 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)8 OutputStream (java.io.OutputStream)8 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)7 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)7 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)6 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)5 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)4 Value (eu.esdihumboldt.hale.common.core.io.Value)4