private void addEmptyRow(Table nestedTable, HNode hNode) { HTable headersNestedTable = hNode.getNestedTable(); Row emptyRow = nestedTable.addRow(getFactory()); for (HNode nestedHNode : headersNestedTable.getHNodes()) { if (nestedHNode.hasNestedTable()) { addEmptyRow(emptyRow.getNode(nestedHNode.getId()).getNestedTable(), nestedHNode); } else { emptyRow.setValue(nestedHNode.getId(), "", getFactory()); } } }
private void addObjectElement(String key, Object value, HTable headers, Row row) throws JSONException { HNode hNode = addHNode(headers, key); String hNodeId = hNode.getId(); if (value instanceof String) { if (((String) value).isEmpty() && hNode.hasNestedTable()) { addEmptyRow(row.getNode(hNodeId).getNestedTable(), hNode); } row.setValue(hNodeId, (String) value, getFactory()); } else if (value instanceof Integer) { row.setValue(hNodeId, value.toString(), getFactory()); } else if (value instanceof Double) { row.setValue(hNodeId, value.toString(), getFactory()); } else if (value instanceof Long) { row.setValue(hNodeId, value.toString(), getFactory()); } else if (value instanceof Boolean) { row.setValue(hNodeId, value.toString(), getFactory()); } else if (value instanceof JSONObject) { HTable nestedHTable = addNestedHTable(hNode, key, row); Table nestedTable = row.getNode(hNodeId).getNestedTable(); addKeysAndValues((JSONObject) value, nestedHTable, nestedTable); } else if (value instanceof JSONArray) { HTable nestedHTable = addNestedHTable(hNode, key, row); Table nestedTable = row.getNode(hNodeId).getNestedTable(); JSONArray a = (JSONArray) value; for (int i = 0; i < a.length(); i++) { addListElement(a.get(i), nestedHTable, nestedTable); } } else if (value == JSONObject.NULL) { // Ignore } else { throw new Error("Cannot handle " + key + ": " + value + " yet."); } }
private void addListElement(Object listValue, HTable headers, Table dataTable) throws JSONException { if (listValue instanceof JSONObject) { Row row = dataTable.addRow(getFactory()); JSONObject o = (JSONObject) listValue; @SuppressWarnings("unchecked") Iterator<String> it = o.sortedKeys(); while (it.hasNext()) { String key = it.next(); addObjectElement(key, o.get(key), headers, row); } } else if (isPrimitiveValue(listValue)) { HNode hNode = addHNode(headers, "values"); String hNodeId = hNode.getId(); Row row = dataTable.addRow(getFactory()); // TODO, conserve the types of the primitive types. String value = ""; if (listValue instanceof String || listValue instanceof Boolean) { value = (String) listValue; } else if (listValue instanceof Double) { value = Double.toString((Double) listValue); } else if (listValue instanceof Integer) { value = Integer.toString((Integer) listValue); } else if (listValue instanceof Long) { value = Long.toString((Long) listValue); } else { // Pedro 2012/09/14 logger.error("Unexpected value in JSON array:" + listValue.toString()); } row.setValue(hNodeId, value, getFactory()); } else if (listValue instanceof JSONArray) { HNode hNode = addHNode(headers, "nested array"); String hNodeId = hNode.getId(); Row row = dataTable.addRow(getFactory()); HTable nestedHTable = addNestedHTable(hNode, "nested array values", row); Table nestedTable = row.getNode(hNodeId).getNestedTable(); JSONArray a = (JSONArray) listValue; for (int i = 0; i < a.length(); i++) { addListElement(a.get(i), nestedHTable, nestedTable); } } else { logger.error("Cannot handle whatever case is not covered by the if statements. Sorry."); } }
private void generateRDF( String wkname, String query, List<KR2RMLRDFWriter> writers, R2RMLMappingIdentifier id, String baseURI) throws IOException, JSONException, KarmaException, SQLException, ClassNotFoundException { logger.debug("Generating RDF..."); WorksheetR2RMLJenaModelParser parserTest = new WorksheetR2RMLJenaModelParser(id); KR2RMLMapping mapping = parserTest.parse(); AbstractJDBCUtil dbUtil = JDBCUtilFactory.getInstance(dbType); Connection conn = dbUtil.getConnection(hostname, portnumber, username, password, dBorSIDName); conn.setAutoCommit(false); java.sql.Statement stmt = conn.createStatement( java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(DATABASE_TABLE_FETCH_SIZE); ResultSet r = stmt.executeQuery(query); ResultSetMetaData meta = r.getMetaData(); ; // Get the column names List<String> columnNames = new ArrayList<>(); for (int i = 1; i <= meta.getColumnCount(); i++) { columnNames.add(meta.getColumnName(i)); } // Prepare required Karma objects Workspace workspace = initializeWorkspace(); RepFactory factory = workspace.getFactory(); Worksheet wk = factory.createWorksheet(wkname, workspace, encoding); List<String> headersList = addHeaders(wk, columnNames, factory); int counter = 0; ArrayList<String> rowValues = null; while ((rowValues = dbUtil.parseResultSetRow(r)) != null) { // Generate RDF and create a new worksheet for every DATABASE_TABLE_FETCH_SIZE rows if (counter % DATABASE_TABLE_FETCH_SIZE == 0 && counter != 0) { generateRDFFromWorksheet(wk, workspace, mapping, writers, baseURI); logger.debug("Done for " + counter + " rows ..."); removeWorkspace(workspace); parserTest = new WorksheetR2RMLJenaModelParser(id); mapping = parserTest.parse(); workspace = initializeWorkspace(); factory = workspace.getFactory(); wk = factory.createWorksheet(wkname, workspace, encoding); headersList = addHeaders(wk, columnNames, factory); } /** Add the data * */ Table dataTable = wk.getDataTable(); Row row = dataTable.addRow(factory); for (int i = 0; i < rowValues.size(); i++) { row.setValue(headersList.get(i), rowValues.get(i), factory); } counter++; } generateRDFFromWorksheet(wk, workspace, mapping, writers, baseURI); // Releasing all the resources r.close(); conn.close(); stmt.close(); logger.debug("done"); }