8000 fixup! feat: support reading multiple Excel sheets at once · kapil-agnihotri/hale@cb672e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
fixup! feat: support reading multiple Excel sheets at once
Browse files Browse the repository at this point in the history
Fix matching the type for an Excel sheet, refactor to use same logic in
reader and writer.

ING-4062
  • Loading branch information
stempler committed Nov 8, 2023
1 parent 16cf08c commit cb672e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
8000
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import eu.esdihumboldt.hale.common.schema.model.DefinitionUtil;
import eu.esdihumboldt.hale.common.schema.model.PropertyDefinition;
import eu.esdihumboldt.hale.common.schema.model.TypeDefinition;
import eu.esdihumboldt.hale.common.schema.model.TypeIndex;
import eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding;
import eu.esdihumboldt.hale.io.csv.reader.internal.CSVInstanceReader;
import eu.esdihumboldt.hale.io.xls.AnalyseXLSSchemaTable;
Expand Down Expand Up @@ -107,6 +108,36 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
return reporter;
}

/**
* Try to match the given type name to a schema type based on the name.
* Prefers a full match of the qualified name but also test for display name
* and local name matching the provided local name.
*
* @param typeName the type name to match
* @param schema the schema to check
* @return the matched type or <code>null</code>
*/
public static TypeDefinition matchTypeByName(QName typeName, TypeIndex schema) {
TypeDefinition type = schema.getType(typeName);

if (type == null) {
// try matching display name (since this is used when writing to
// Excel)
type = schema.getMappingRelevantTypes().stream()
.filter(t -> typeName.getLocalPart().equals(t.getDisplayName())).findFirst()
.orElse(null);
}

if (type == null) {
// try matching local name
type = schema.getMappingRelevantTypes().stream()
.filter(t -> typeName.getLocalPart().equals(t.getName().getLocalPart()))
.findFirst().orElse(null);
}

return type;
}

private void loadSheet(SheetInfo sheet, IOReporter reporter) throws Exception {
AnalyseXLSSchemaTable analyser = new AnalyseXLSSchemaTable(getSource(),
ReaderSettings.isXlsxContentType(getContentType()), sheet.getIndex());
Expand All @@ -119,33 +150,13 @@ private void loadSheet(SheetInfo sheet, IOReporter reporter) throws Exception {
}
if (type == null) {
// look for match based on sheet name
String localName;
QName qname = null;
try {
qname = QName.valueOf(sheet.getName());
localName = qname.getLocalPart();
} catch (Exception e) {
localName = sheet.getName();
}
final String flName = localName;

// exact name
if (qname != null) {
type = getSourceSchema().getType(qname);
}

if (type == null) {
// try display name (since this is used when writing to Excel)
getSourceSchema().getMappingRelevantTypes().stream()
.filter(t -> flName.equals(t.getDisplayName())).findFirst().orElse(null);
}

if (type == null) {
// try local name
getSourceSchema().getMappingRelevantTypes().stream()
.filter(t -> flName.equals(t.getName().getLocalPart())).findFirst()
.orElse(null);
qname = new QName(sheet.getName());
}
type = matchTypeByName(qname, getSourceSchema());
}

// get property definitions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import eu.esdihumboldt.hale.io.csv.InstanceTableIOConstants;
import eu.esdihumboldt.hale.io.csv.writer.AbstractTableInstanceWriter;
import eu.esdihumboldt.hale.io.xls.XLSCellStyles;
import eu.esdihumboldt.hale.io.xls.reader.XLSInstanceReader;

/**
* Instance export provider for xls files
Expand Down Expand Up @@ -137,21 +138,8 @@ private Collection<? extends TypeDefinition> getTypesToExport(SimpleLog reporter
String[] splitExportType = exportTypes.split(",");
for (String featureType : splitExportType) {
QName typeName = QName.valueOf(featureType);
TypeDefinition type = getTargetSchema().getType(typeName);

if (type == null) {
// try matching local name
type = getTargetSchema().getMappingRelevantTypes().stream()
.filter(t -> typeName.getLocalPart().equals(t.getName().getLocalPart()))
.findFirst().orElse(null);
}

if (type == null) {
// try matching display name
type = getTargetSchema().getMappingRelevantTypes().stream()
.filter(t -> typeName.getLocalPart().equals(t.getDisplayName()))
.findFirst().orElse(null);
}
TypeDefinition type = XLSInstanceReader.matchTypeByName(typeName,
getTargetSchema());

if (type != null) {
types.add(type);
Expand Down
< 3072 div id="all_commit_comments" class="js-quote-selection-container" data-quote-markdown=".js-comment-body">

0 comments on commit cb672e6

Please sign in to comment.
0