metamodel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kaspersorensen <...@git.apache.org>
Subject [GitHub] metamodel pull request #179: Feature/neo4j column types
Date Fri, 25 May 2018 13:43:24 GMT
Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/179#discussion_r190897592
  
    --- Diff: neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java ---
    @@ -158,102 +159,127 @@ 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);
    +                final SimpleTableDef tableDefFromLabel = createTableDefFromLabel(labelsJsonArray.getString(i));
    +                
    +                if (tableDefFromLabel != null) {
    +                    tableDefs.add(tableDefFromLabel);
    +                }
    +            }
    +            
    +            return tableDefs.toArray(new SimpleTableDef[tableDefs.size()]);
    +        } catch (final JSONException e) {
    +            logger.error("Error occurred in parsing JSON while detecting the schema:
", e);
    +            throw new IllegalStateException(e);
    +        }
    +    }
    +    
    +    private SimpleTableDef createTableDefFromLabel(final String label) 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");
    +            final Set<String> relationshipPropertiesForNode = createRelationshipPropertiesForNode(nodeId);
    +            relationshipPropertiesPerLabel.addAll(relationshipPropertiesForNode);
    +        }
    +        
    +        propertiesPerLabel.addAll(relationshipPropertiesPerLabel);
     
    -                List<JSONObject> nodesPerLabel = getAllNodesPerLabel(label);
    +        if (nodesPerLabel.isEmpty()) { 
    +            return null; // Do not add a table if label has no nodes (empty tables are
considered non-existent)
    +        } else {
    +            final String[] columnNames = propertiesPerLabel.toArray(new String[propertiesPerLabel.size()]);
    +            final ColumnTypeResolver columnTypeResolver = new ColumnTypeResolver(nodesPerLabel.get(0),
columnNames);
    +            return new SimpleTableDef(label, columnNames, columnTypeResolver.getColumnTypes());
    +        } 
    +    }
     
    -                List<String> propertiesPerLabel = new ArrayList<String>();
    -                for (JSONObject node : nodesPerLabel) {
    -                    List<String> propertiesPerNode = getAllPropertiesPerNode(node);
    -                    for (String property : propertiesPerNode) {
    -                        if (!propertiesPerLabel.contains(property)) {
    -                            propertiesPerLabel.add(property);
    -                        }
    -                    }
    -                }
    +    private Set<String> createRelationshipPropertiesForNode(final Integer nodeId)
throws JSONException {
    +        final List<JSONObject> relationshipsPerNode = getOutgoingRelationshipsPerNode(nodeId);
    +        final Set<String> relationshipProperties = new LinkedHashSet<>();
    +
    +        for (final JSONObject relationship : relationshipsPerNode) {
    +            // Add the relationship as a column in the table
    +            final String relationshipName = relationship.getString("type");
    --- End diff --
    
    Same here.


---

Mime
View raw message