}
@Override
public UpdateContainer doIt(Workspace workspace) throws CommandException {
Worksheet worksheet = workspace.getWorksheet(worksheetId);
SuperSelection selection = getSuperSelection(worksheet);
String[] entities = entitiesToBeExt.split(",");
HashSet<String> entitiesReqd = new HashSet<String>();
entitiesReqd.addAll(Arrays.asList(entities));
JSONArray array = new JSONArray();
AddValuesCommand cmd;
RepFactory repFactory = workspace.getFactory();
HTable ht = repFactory.getHTable(repFactory.getHNode(hNodeId).getHTableId());
List<Table> tables = new ArrayList<Table>();
CloneTableUtils.getDatatable(worksheet.getDataTable(), ht, tables, selection);
for(Table table : tables) {
ArrayList<Row> rows = table.getRows(0, table.getNumRows(), selection);
for (Row row : rows) {
String id = row.getId();
row.getNode(hNodeId);
Node node = row.getNeighbor(hNodeId);
String value = node.getValue().asString();
JSONObject obj = new JSONObject();
System.out.println(value);
obj.put("rowId", id);
obj.put("text", value);
array.put(obj);
}
}
String extractions = null;
String urlParameters = array.toString();
urlParameters = new String(urlParameters.getBytes(Charset.forName("UTF-8")), Charset.forName("ISO-8859-1"));
// POST Request to ExtractEntities API.
try {
if(entityExtractor != null && entityExtractorMethod != null) {
logger.info("Using the Extract Entities JAR");
logger.info("Sending:" + urlParameters);
Object returnValue = entityExtractorMethod.invoke(entityExtractor, urlParameters);
extractions = returnValue.toString();
} else {
logger.info("Using the Extract Entities Service: " + extractionURL);
logger.info("Sending:" + urlParameters);
String url = extractionURL;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// add request header
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("charset","utf-8");
// Send POST request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
logger.info("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer extractionsBuffer = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
extractionsBuffer.append(inputLine);
}
in.close();
extractions = extractionsBuffer.toString();
}
} catch (Exception e) {
logger.error("Error in ExtractEntitiesCommand" + e.toString());
Util.logException(logger, e);
return new UpdateContainer(new ErrorUpdate(e.getMessage()));
}
// print result
logger.info("Got extractions:");
logger.info(extractions);
JSONArray result = (JSONArray) JSONUtil.createJson(extractions);
//Final Data for AddValuesCommand
JSONArray rowData = new JSONArray();
// index for result iteration
int index = 0;
for(Table table : tables) {
ArrayList<Row> rows = table.getRows(0, table.getNumRows(), selection);
for (Row row : rows) {
if (index < result.length()) {
JSONObject extraction = (JSONObject) result.getJSONObject(index++).get("extractions");
JSONObject extractionValues = new JSONObject();
//Check if the user wants People entities
if(entitiesReqd.contains("People")) {
//***Extracting People***
JSONArray peopleExtract = (JSONArray) extraction.get("people");
JSONArray peopleValues = new JSONArray();
for(int i=0; i<peopleExtract.length(); i++) {
peopleValues.put(new JSONObject().put("extraction", ((JSONObject)peopleExtract.get(i)).getString("extraction")));
}
extractionValues.put("People", peopleValues);
}
//Check if the user wants Places entities
if(entitiesReqd.contains("Places")) {
//***Extracting Places***
JSONArray placesExtract = (JSONArray) extraction.get("places");
JSONArray placesValues = new JSONArray();
for(int i=0; i<placesExtract.length(); i++) {
placesValues.put(new JSONObject().put("extraction", ((JSONObject)placesExtract.get(i)).getString("extraction")));
}
extractionValues.put("Places", placesValues);
}
//Check if the user wants Date entities
if(entitiesReqd.contains("Dates")) {
//***Extracting People***
JSONArray datesExtract = (JSONArray) extraction.get("dates");
JSONArray datesValues = new JSONArray();
for(int i=0; i<datesExtract.length(); i++) {
datesValues.put(new JSONObject().put("extraction", ((JSONObject)datesExtract.get(i)).getString("extraction")));
}
extractionValues.put("Dates", datesValues);
}
JSONObject extractionsObj = new JSONObject();
extractionsObj.put("extractions", extractionValues);
JSONObject rowDataObject = new JSONObject();
rowDataObject.put("values", extractionsObj);
rowDataObject.put("rowId", row.getId());
rowData.put(rowDataObject);
}
}
}
JSONObject addValuesObj = new JSONObject();
addValuesObj.put("name", "AddValues");
addValuesObj.put("value", rowData.toString());
addValuesObj.put("type", "other");
JSONArray addValues = new JSONArray();
addValues.put(addValuesObj);
System.out.println(JSONUtil.prettyPrintJson(addValues.toString()));
try {
AddValuesCommandFactory factory = new AddValuesCommandFactory();
cmd = (AddValuesCommand) factory.createCommand(addValues, workspace, hNodeId, worksheetId,
ht.getId(), HNodeType.Transformation, selection.getName());
HNode hnode = repFactory.getHNode(hNodeId);
cmd.setColumnName(hnode.getColumnName()+" Extractions");
cmd.doIt(workspace);