Github user arjansh commented on a diff in the pull request:
https://github.com/apache/metamodel/pull/179#discussion_r189892629
--- Diff:
neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java ---
@@ -158,60 +159,79 @@ protected String getMainSchemaName() throws
MetaModelException {
}
public SimpleTableDef[] detectTableDefs() {
- List<SimpleTableDef> tableDefs = new ArrayList<SimpleTableDef>();
-
- String labelsJsonString = _requestWrapper.executeRestRequest(new
HttpGet(_serviceRoot + "/labels"));
-
- JSONArray labelsJsonArray;
+ final List<SimpleTableDef> tableDefs = new ArrayList<>();
+ final String labelsJsonString =
_requestWrapper.executeRestRequest(new HttpGet(_serviceRoot + "/labels"));
+ final JSONArray labelsJsonArray;
+
try {
labelsJsonArray = new JSONArray(labelsJsonString);
+
for (int i = 0; i < labelsJsonArray.length(); i++) {
- String label = labelsJsonArray.getString(i);
+ fillTableDefsFromLabel(labelsJsonArray.getString(i),
tableDefs);
+ }
+
+ return tableDefs.toArray(new SimpleTableDef[tableDefs.size()]);
+ } catch (final JSONException e) {
+ logger.error("Error occured in parsing JSON while detecting
the schema: ", e);
+ throw new IllegalStateException(e);
+ }
+ }
+
+ private void fillTableDefsFromLabel(final String label, final
List<SimpleTableDef> tableDefs) throws JSONException {
+ final List<JSONObject> nodesPerLabel = getAllNodesPerLabel(label);
+ final List<String> propertiesPerLabel =
getPropertiesFromLabelNodes(nodesPerLabel);
+ final Set<String> relationshipPropertiesPerLabel = new
LinkedHashSet<>();
+
+ for (final JSONObject node : nodesPerLabel) {
+ final Integer nodeId = (Integer)
node.getJSONObject("metadata").get("id");
+ fillRelationshipPropertiesPerLabel(nodeId,
relationshipPropertiesPerLabel);
+ }
+
+ propertiesPerLabel.addAll(relationshipPropertiesPerLabel);
+
+ // Do not add a table if label has no nodes (empty tables are
considered non-existent)
+ if (!nodesPerLabel.isEmpty()) {
+ final String[] columnNames = propertiesPerLabel.toArray(new
String[propertiesPerLabel.size()]);
+ final ColumnTypeResolver columnTypeResolver = new
ColumnTypeResolver();
+ final SimpleTableDef tableDef = new SimpleTableDef(label,
columnNames,
+
columnTypeResolver.getColumnTypes(nodesPerLabel.get(0), columnNames));
+ tableDefs.add(tableDef);
+ }
+ }
+
+ private void fillRelationshipPropertiesPerLabel(final Integer nodeId,
final Set<String> relationshipPropertiesPerLabel)
--- End diff --
Essentially this method has nothing to do with labels, it gets relationship
properties for a node and adds them to the given set, so I would call it
`addRelationshipPropertiesForNode` instead and rename the
`relationshipPropertiesPerLabel` parameter to `relationshipProperties`.
Personally, again, but this is just my personal preference, I would
refactor the method so it only takes `nodeId` as an argument and returns the
set of of relationship properties for that `nodeId` and then have the method
which invokes it add all of these to its own set using the Set#addAll(Set)
method.
---