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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations