ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readValue(response, JsonNode.class);
Iterator<JsonNode> iter = root.getElements();
Iterator<String> iterNames = root.getFieldNames();
List<JsonNode> eventNodes = new ArrayList<JsonNode>();
BasicDBList rawEventObjects = null;
while ( iter.hasNext() )
{
String currNodeName = iterNames.next();
JsonNode currNode = iter.next();
if (!currNodeName.equals("doc")) //we can assume these are the entities/topics
{
String typeGroup = currNode.get("_typeGroup").getTextValue();
//check typegroup to see if it is an entity
if ( typeGroup.equals("entities") )
{
try
{
EntityPojo ep = new EntityPojo();
//get what fields we can
ep.setType(currNode.get("_type").getTextValue());
try {
ep.setDimension(DimensionUtility.getDimensionByType(ep.getType()));
}
catch (java.lang.IllegalArgumentException e) {
ep.setDimension(EntityPojo.Dimension.What);
}
String name = "";
JsonNode nameNode = null;
try
{
nameNode = currNode.get("name");
name = nameNode.getTextValue();
}
catch (Exception ex )
{
logger.debug("Error parsing name node: " + currNode.toString());
continue;
}
ep.setActual_name(name);
ep.setRelevance(Double.parseDouble(currNode.get("relevance").getValueAsText()));
ep.setFrequency((long)currNode.get("instances").size());
//attempt to get resolutions if they exist
JsonNode resolutionNode = currNode.get("resolutions");
if ( null != resolutionNode )
{
//resolution nodes are arrays
JsonNode resolutionFirst = resolutionNode.get(0);
ep.setSemanticLinks(new ArrayList<String>());
ep.getSemanticLinks().add(resolutionFirst.get("id").getTextValue()); //this is a link to an alchemy page
ep.setDisambiguatedName(resolutionFirst.get("name").getTextValue());
//check if we need to create a geo object
if ( null != resolutionFirst.get("latitude") )
{
GeoPojo gp = new GeoPojo();
String lat = resolutionFirst.get("latitude").getValueAsText();
String lon = resolutionFirst.get("longitude").getValueAsText();
gp.lat = Double.parseDouble(lat);
gp.lon = Double.parseDouble(lon);
ep.setGeotag(gp);
}
}
else {
ep.setDisambiguatedName(name); // use actual name)
}
entityNameMap.put(currNodeName.toLowerCase(), ep);
entities.add(ep);
}
catch (Exception ex)
{
logger.error("Error creating event pojo from OpenCalaisNode: " + ex.getMessage(), ex);
}
}
else if ( typeGroup.equals("relations") )
{
eventNodes.add(currNode);
}
}
}
//handle events
if (bAddRawEventsToMetadata) {
// For now just re-process these into DB objects since we know that works...
rawEventObjects = new BasicDBList();
}
for ( JsonNode eventNode : eventNodes )
{
AssociationPojo event = parseEvent(eventNode);
//remove useless events (an event is useless if it only has a verb (guessing currently)
if ( null != event )
{
event = removeUselessEvents(event);
if ( null != event )
{
events.add(event);
}
}
if (bAddRawEventsToMetadata) {
BasicDBObject eventDbo = (BasicDBObject) com.mongodb.util.JSON.parse(eventNode.toString());
if (null != eventDbo) {
BasicDBObject transformObj = new BasicDBObject();
for (Map.Entry<String, Object> entries: eventDbo.entrySet()) {
if (entries.getValue() instanceof String) {
String val = (String) entries.getValue();
EntityPojo transformVal = findMappedEntityName(val);
if (null != transformVal) {
transformObj.put(entries.getKey(), transformVal.getIndex());
transformObj.put(entries.getKey() + "__hash", val);
}
else {
transformObj.put(entries.getKey(), val);
}
}
else {
transformObj.put(entries.getKey(), entries.getValue());
}
}
// (add to another list, which will get written to metadata)
rawEventObjects.add(transformObj);
}
}
}
if (bAddRawEventsToMetadata) {
partialDoc.addToMetadata("OpenCalaisEvents", rawEventObjects.toArray());
}
if (null != partialDoc.getEntities()) {
partialDoc.getEntities().addAll(entities);
partialDoc.setEntities(partialDoc.getEntities());
}