Alignment alignment = AlignmentManager.Instance().getAlignment(
AlignmentManager.Instance().constructAlignmentId(workspace.getId(), worksheetId));
if (alignment == null) {
logger.info("Alignment is NULL for " + worksheetId);
return new UpdateContainer(new ErrorUpdate(
"Please align the worksheet before generating RDF!"));
}
Worksheet worksheet = workspace.getWorksheet(worksheetId);
SuperSelection selection = getSuperSelection(worksheet);
// Generate the KR2RML data structures for the RDF generation
final ErrorReport errorReport = new ErrorReport();
KR2RMLMappingGenerator mappingGen = null;
try{
mappingGen = new KR2RMLMappingGenerator(workspace, worksheet,
alignment, worksheet.getSemanticTypes(), "s", graphUri,
false, errorReport);
}
catch (KarmaException e)
{
logger.error("Error occured while exporting CSV!", e);
return new UpdateContainer(new ErrorUpdate("Error occured while exporting CSV: " + e.getMessage()));
}
KR2RMLMapping mapping = mappingGen.getKR2RMLMapping();
logger.debug(mapping.toString());
// Generate the RDF using KR2RML data structures
try {
KR2RMLWorksheetRDFGenerator rdfGen = new KR2RMLWorksheetRDFGenerator(worksheet,
workspace.getFactory(), workspace.getOntologyManager(),
generatedRDFFileName, false, mapping, errorReport, selection);
rdfGen.generateRDF(true);
logger.info("RDF written to file: " + generatedRDFFileName);
} catch (Exception e1) {
logger.error("Error occured while generating RDF!", e1);
return new UpdateContainer(new ErrorUpdate("Error occured while generating RDF: " + e1.getMessage()));
}
TripleStoreUtil utilObj = new TripleStoreUtil();
boolean result = false;
try{
JSONObject pref = workspace.getCommandPreferences().getCommandPreferencesJSONObject("PublishRDFCommandPreferences");
String rdfNamespace = "";
if(pref != null) {
rdfNamespace = pref.optString(PublishRDFCommandPreferencesKeys.rdfNamespace.name());
}
result = utilObj.saveToStoreFromFile(generatedRDFFileName, TripleStoreUtil.defaultDataRepoUrl, graphUri, true, rdfNamespace);
}
catch(Exception e)
{
result = false;
}
// if we the RDF is generated correctly, then we dont need to retur an UpdateContainer
// hence we return null
if(result) {
logger.info("Saved rdf to store");
this.graphUrl = graphUri;
this.tripleStoreUrl = TripleStoreUtil.defaultDataRepoUrl;
} else {
logger.error("Falied to store rdf to karma_data store");
return new UpdateContainer(new ErrorUpdate("Error: Failed to store RDF to the triple store"));
}
return null;
}