Skip to content

Commit

Permalink
[Feature #103] Tabular module - refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew-Kulich committed Sep 13, 2022
1 parent 7c6463f commit 9277321
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ ExecutionContext executeSelf() {
table = onTable(null);

List<Column> outputColumns = new ArrayList<>();
List<Statement> rowStatements = new ArrayList<>();

CsvPreference csvPreference = new CsvPreference.Builder(
quoteCharacter,
Expand Down Expand Up @@ -165,8 +166,8 @@ ExecutionContext executeSelf() {
: new Column(columnName, columnTitle);
outputColumns.add(schemaColumn);

tableSchema.setAboutUrl(schemaColumn, sourceResource);
schemaColumn.setProperty(dataPrefix, sourceResource);
tableSchema.setAboutUrl(schemaColumn, sourceResource.getUri());
schemaColumn.setProperty(dataPrefix, sourceResource.getUri());
schemaColumn.setTitle(columnTitle);
if(isDuplicate) throwNotUniqueException(schemaColumn,columnTitle, columnName);
}
Expand Down Expand Up @@ -201,9 +202,9 @@ ExecutionContext executeSelf() {
for (int i = 0; i < header.length; i++) {
// 4.6.8.1
Column column = outputColumns.get(i);
column.setValueUrl(outputModel, row.get(i), tableSchema.getAboutUrl(), rowNumber);
rowStatements.add(createRowResource(row.get(i), rowNumber, column));
// 4.6.8.2
r.setDescribes(column.createAboutUrl(tableSchema.getAboutUrl(), rowNumber));
r.setDescribes(tableSchema.createAboutUrl(rowNumber));
//TODO: URITemplate

// 4.6.8.5 - else, if value is list and cellOrdering == true
Expand All @@ -216,19 +217,28 @@ ExecutionContext executeSelf() {
LOG.error("Error while reading file from resource uri {}", sourceResource, e);
}

tableSchema.adjustProperties(hasInputSchema, outputColumns, sourceResource);
tableSchema.adjustProperties(hasInputSchema, outputColumns, sourceResource.getUri());
em.persist(tableGroup);
em.getTransaction().commit();
tableSchema.addColumnsList(em, outputColumns);

outputModel.add(rowStatements);
outputModel.add(
bNodesTransformer.transferJOPAEntitiesToBNodes
(JopaPersistenceUtils.getDataset(em).getDefaultModel()));

em.close();
return getExecutionContext(inputModel, outputModel);
}

private Statement createRowResource(String cellValue, int rowNumber, Column column) {
Resource rowResource = ResourceFactory.createResource(tableSchema.createAboutUrl(rowNumber));

return ResourceFactory.createStatement(
rowResource,
ResourceFactory.createProperty(column.getPropertyUrl()),
ResourceFactory.createPlainLiteral(cellValue));
}

private boolean hasInputSchema(TableSchema inputTableSchema) {
if (inputTableSchema != null){
tableSchema = inputTableSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import cz.cvut.spipes.constants.CSVW;
import cz.cvut.spipes.constants.KBSS_CSVW;
import cz.cvut.spipes.modules.util.TabularModuleUtils;
import cz.cvut.spipes.registry.StreamResource;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand Down Expand Up @@ -127,13 +123,13 @@ public String getProperty() {
return property;
}

public void setProperty(String dataPrefix, StreamResource sourceResource) throws UnsupportedEncodingException {
String propertyValue = getPropertyUrl(dataPrefix, sourceResource);
public void setProperty(String dataPrefix, String sourceResourceUri) throws UnsupportedEncodingException {
String propertyValue = getPropertyUrl(dataPrefix, sourceResourceUri);
tabularModuleUtils.setVariable(this.property, propertyValue, value -> this.property = value, "property");
tabularModuleUtils.setVariable(this.propertyUrl, propertyValue, value -> this.propertyUrl = value, "propertyUrl");
}

private String getPropertyUrl(String dataPrefix, StreamResource sourceResource)
private String getPropertyUrl(String dataPrefix, String sourceResourceUri)
throws UnsupportedEncodingException {
if (getPropertyUrl() != null) {
return getPropertyUrl();
Expand All @@ -144,35 +140,6 @@ private String getPropertyUrl(String dataPrefix, StreamResource sourceResource)
if (dataPrefix != null && !dataPrefix.isEmpty()) {
return dataPrefix + URLEncoder.encode(name, "UTF-8");
}
return sourceResource.getUri() + "#" + URLEncoder.encode(name, "UTF-8");
}

public void setValueUrl(Model outputModel, String cellValue, String tableSchemaAboutUrl, int rowNumber) {
String valueURL = null;
if(getValueUrl() != null) valueURL = getValueUrl();

if (valueURL != null && !valueURL.isEmpty()) {
setValueUrl(valueURL);
} else {
if (cellValue != null) {
Resource columnResource = ResourceFactory
.createResource(createAboutUrl(tableSchemaAboutUrl, rowNumber));

outputModel.add(ResourceFactory.createStatement(
columnResource,
outputModel.createProperty(getPropertyUrl()),
ResourceFactory.createPlainLiteral(cellValue)));
}
}
}

public String createAboutUrl(String tableSchemaAboutUrl, int rowNumber) {
String columnAboutUrlStr = tableSchemaAboutUrl;
if (columnAboutUrlStr == null) columnAboutUrlStr = getAboutUrl();
columnAboutUrlStr = columnAboutUrlStr.replace(
"{_row}",
Integer.toString(rowNumber + 1)
);
return columnAboutUrlStr;
return sourceResourceUri + "#" + URLEncoder.encode(name, "UTF-8");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import cz.cvut.spipes.modules.exception.TableSchemaException;
import cz.cvut.spipes.modules.util.JopaPersistenceUtils;
import cz.cvut.spipes.modules.util.TabularModuleUtils;
import cz.cvut.spipes.registry.StreamResource;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFList;
import org.apache.jena.rdf.model.RDFNode;
Expand Down Expand Up @@ -90,12 +89,12 @@ public List<Column> sortColumns(List<String> orderList){
return columnList;
}

public void adjustProperties(boolean hasInputSchema, List<Column> outputColumns, StreamResource sourceResource) {
public void adjustProperties(boolean hasInputSchema, List<Column> outputColumns, String sourceResourceUri) {
if (hasInputSchema){
if(getColumnsSet().size() > outputColumns.size()) {
throwExtraColumnsError(outputColumns);
}
setAboutUrl(sourceResource.getUri() + "#row-{_row}");
setAboutUrl(sourceResourceUri + "#row-{_row}");
getColumnsSet().forEach(column -> column.setUri(null));
setUri(null);
}else{
Expand Down Expand Up @@ -136,7 +135,7 @@ public void addColumnsList(EntityManager em, List<Column> outputColumns) {
columnList);
}

public void setAboutUrl(Column column, StreamResource sourceResource) {
public void setAboutUrl(Column column, String sourceResourceUri) {
String columnAboutUrl = null;
if(column.getAboutUrl() != null){
columnAboutUrl = column.getAboutUrl();
Expand All @@ -145,7 +144,7 @@ public void setAboutUrl(Column column, StreamResource sourceResource) {
if (columnAboutUrl != null && !columnAboutUrl.isEmpty()) {
column.setAboutUrl(columnAboutUrl);
} else {
String tableSchemaAboutUrl = sourceResource.getUri() + "#row-{_row}";
String tableSchemaAboutUrl = sourceResourceUri + "#row-{_row}";
tabularModuleUtils.setVariable(aboutUrl, tableSchemaAboutUrl, value -> this.aboutUrl = value, "aboutUrl");
}
}
Expand All @@ -168,4 +167,14 @@ private void logError(String msg) {
throw new TableSchemaException(msg);
}else LOG.error(msg);
}

public String createAboutUrl(int rowNumber) {
String columnAboutUrlStr = aboutUrl;
if (columnAboutUrlStr == null) columnAboutUrlStr = getAboutUrl();
columnAboutUrlStr = columnAboutUrlStr.replace(
"{_row}",
Integer.toString(rowNumber + 1)
);
return columnAboutUrlStr;
}
}

0 comments on commit 9277321

Please sign in to comment.