Map<String, ColumnNode> hNodeIdTocolumnNodeMap = createColumnNodeMap();
Map<String, InternalNode> hNodeIdToDomainNodeMap = createDomainNodeMap();
JSONStringer jsonStr = new JSONStringer();
try {
JSONWriter writer = jsonStr.object();
writer.key("worksheetId").value(worksheetId).key("updateType")
.value("SemanticTypesUpdate");
writer.key(JsonKeys.Types.name());
writer.array();
// Iterate through all the columns
for (HNodePath path : worksheet.getHeaders().getAllPaths()) {
HNode node = path.getLeaf();
String nodeId = node.getId();
writer.object();
// Check if a semantic type exists for the HNode
SemanticType type = types.getSemanticTypeForHNodeId(nodeId);
if (type != null && type.getConfidenceLevel() != SemanticType.ConfidenceLevel.Low) {
writer.key(JsonKeys.HNodeId.name())
.value(type.getHNodeId())
.key(JsonKeys.SemanticTypesArray.name()).array();
ColumnNode alignmentColumnNode = hNodeIdTocolumnNodeMap.get(type.getHNodeId());
InternalNode domainNode = hNodeIdToDomainNodeMap.get(type.getHNodeId());
if (alignmentColumnNode == null || domainNode == null) {
logger.error("Column node or domain node not found in alignment." +
" (This should not happen conceptually!):" + type);
continue;
}
// Add the primary semantic type
writer.object()
.key(JsonKeys.Origin.name())
.value(type.getOrigin().name())
.key(JsonKeys.ConfidenceLevel.name())
.value(type.getConfidenceLevel().name())
.key(JsonKeys.isPrimary.name())
.value(true);
// Add the RDF literal type to show in the text box
String rdfLiteralType = alignmentColumnNode.getRdfLiteralType() == null? "" :
alignmentColumnNode.getRdfLiteralType().getDisplayName();
writer.key(JsonKeys.rdfLiteralType.name())
.value(rdfLiteralType);
// String domainDisplayLabel = (domainNode.getLabel().getPrefix() != null && (!domainNode.getLabel().getPrefix().equals(""))) ?
// (domainNode.getLabel().getPrefix() + ":" + domainNode.getLocalId()) : domainNode.getLocalId();
if (!type.isClass()) {
writer
.key(JsonKeys.FullType.name()).value(type.getType().getUri())
.key(JsonKeys.DisplayLabel.name()).value(type.getType().getDisplayName())
.key(JsonKeys.DomainId.name()).value(domainNode.getId())
.key(JsonKeys.DomainUri.name()).value(domainNode.getUri())
.key(JsonKeys.DisplayDomainLabel.name()).value(domainNode.getDisplayId());
} else {
writer
.key(JsonKeys.FullType.name()).value(domainNode.getId())
.key(JsonKeys.DisplayLabel.name()).value(domainNode.getDisplayId())
.key(JsonKeys.DomainId.name()).value("")
.key(JsonKeys.DomainUri.name()).value("")
.key(JsonKeys.DisplayDomainLabel.name())
.value("");
}
// Mark the special properties
writer
.key(JsonKeys.isMetaProperty.name())
.value(isMetaProperty(type.getType(), alignmentColumnNode));
writer.endObject();
// Iterate through the synonym semantic types
SynonymSemanticTypes synTypes = types
.getSynonymTypesForHNodeId(nodeId);
if (synTypes != null) {
for (SemanticType synType : synTypes.getSynonyms()) {
writer.object()
.key(JsonKeys.HNodeId.name())
.value(synType.getHNodeId())
.key(JsonKeys.FullType.name())
.value(synType.getType().getUri())
.key(JsonKeys.Origin.name())
.value(synType.getOrigin().name())
.key(JsonKeys.ConfidenceLevel.name())
.value(synType.getConfidenceLevel().name())
.key(JsonKeys.DisplayLabel.name())
.value(synType.getType().getDisplayName())
.key(JsonKeys.isPrimary.name())
.value(false);
if (!synType.isClass()) {
writer.key(JsonKeys.DomainUri.name()).value(synType.getDomain().getUri())
.key(JsonKeys.DomainId.name()).value("")
.key(JsonKeys.DisplayDomainLabel.name())
.value(synType.getDomain().getDisplayName());
} else {
writer.key(JsonKeys.DomainId.name()).value("")
.key(JsonKeys.DomainUri.name()).value("")
.key(JsonKeys.DisplayDomainLabel.name())
.value("");
}
writer.endObject();
}
}
writer.endArray();
} else {
writer.key(JsonKeys.HNodeId.name()).value(nodeId);
writer.key(JsonKeys.SemanticTypesArray.name()).array()
.endArray();
}
writer.endObject();
}
writer.endArray();
writer.endObject();
pw.print(writer.toString());
} catch (JSONException e) {
logger.error("Error occured while writing to JSON!", e);
}
}