You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ar...@apache.org on 2018/05/30 08:15:28 UTC
[1/7] metamodel git commit: Neo4j: column types support.
Repository: metamodel
Updated Branches:
refs/heads/master fe02a59b8 -> 71b57403b
Neo4j: column types support.
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/fdaaed05
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/fdaaed05
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/fdaaed05
Branch: refs/heads/master
Commit: fdaaed05d7ed3aadfe79b3ce71dd49bc45b6b9f4
Parents: fe02a59
Author: jakub <j....@quadient.com>
Authored: Fri May 18 09:49:48 2018 +0200
Committer: jakub <j....@quadient.com>
Committed: Fri May 18 09:49:48 2018 +0200
----------------------------------------------------------------------
.../metamodel/neo4j/Neo4jDataContext.java | 210 +++++++++++++++----
.../apache/metamodel/neo4j/Neo4jDataSet.java | 26 ++-
2 files changed, 181 insertions(+), 55 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/metamodel/blob/fdaaed05/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
index 00d0b5a..70686ef 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
@@ -19,6 +19,8 @@
package org.apache.metamodel.neo4j;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -36,6 +38,7 @@ import org.apache.metamodel.data.DocumentSource;
import org.apache.metamodel.query.FilterItem;
import org.apache.metamodel.query.SelectItem;
import org.apache.metamodel.schema.Column;
+import org.apache.metamodel.schema.ColumnType;
import org.apache.metamodel.schema.MutableSchema;
import org.apache.metamodel.schema.MutableTable;
import org.apache.metamodel.schema.Schema;
@@ -158,59 +161,178 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
}
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 Long nodeId = (Long) 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 ColumnType[] columnTypes = guessColumnTypesFromValues(nodesPerLabel.get(0),
+ new ArrayList<>(Arrays.asList(columnNames)));
+ final SimpleTableDef tableDef = new SimpleTableDef(label, columnNames, columnTypes);
+ tableDefs.add(tableDef);
+ }
+ }
- List<JSONObject> nodesPerLabel = getAllNodesPerLabel(label);
+ private void fillRelationshipPropertiesPerLabel(final Long nodeId, final Set<String> relationshipPropertiesPerLabel)
+ throws JSONException {
+ final List<JSONObject> relationshipsPerNode = getOutgoingRelationshipsPerNode(nodeId);
+
+ for (final JSONObject relationship : relationshipsPerNode) {
+ // Add the relationship as a column in the table
+ final String relationshipName = relationship.getString("type");
+ final String relationshipNameProperty = RELATIONSHIP_PREFIX + relationshipName;
+
+ if (!relationshipPropertiesPerLabel.contains(relationshipNameProperty)) {
+ relationshipPropertiesPerLabel.add(relationshipNameProperty);
+ }
- 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);
- }
- }
+ // Add all the relationship properties as table columns
+ final List<String> propertiesPerRelationship = getAllPropertiesPerRelationship(relationship);
+ relationshipPropertiesPerLabel.addAll(propertiesPerRelationship);
+ }
+ }
+
+ private List<String> getPropertiesFromLabelNodes(final List<JSONObject> nodesPerLabel) {
+ final List<String> propertiesPerLabel = new ArrayList<>();
+
+ for (final JSONObject node : nodesPerLabel) {
+ final List<String> propertiesPerNode = getAllPropertiesPerNode(node);
+
+ for (final String property : propertiesPerNode) {
+ if (!propertiesPerLabel.contains(property)) {
+ propertiesPerLabel.add(property);
}
+ }
+ }
+
+ return propertiesPerLabel;
+ }
- Set<String> relationshipPropertiesPerLabel = new LinkedHashSet<String>();
- for (JSONObject node : nodesPerLabel) {
- Integer nodeId = (Integer) node.getJSONObject("metadata").get("id");
- List<JSONObject> relationshipsPerNode = getOutgoingRelationshipsPerNode(nodeId);
- for (JSONObject relationship : relationshipsPerNode) {
- // Add the relationship as a column in the table
- String relationshipName = relationship.getString("type");
- String relationshipNameProperty = RELATIONSHIP_PREFIX + relationshipName;
- if (!relationshipPropertiesPerLabel.contains(relationshipNameProperty)) {
- relationshipPropertiesPerLabel.add(relationshipNameProperty);
- }
+ private ColumnType[] guessColumnTypesFromValues(final JSONObject jsonObject, final List<String> columnNames) {
+ final List<ColumnType> columnTypes = new ArrayList<>();
+
+ try {
+ fillColumnTypesFromMetadata(jsonObject, columnNames, columnTypes);
+ fillColumnTypesFromData(jsonObject, columnNames, columnTypes);
+ } catch (final JSONException e) {
+ // ignore missing data
+ }
+
+ fillColumnTypesFromRemainingColumns(columnNames, columnTypes);
+ return columnTypes.toArray(new ColumnType[columnTypes.size()]);
+ }
- // Add all the relationship properties as table columns
- List<String> propertiesPerRelationship = getAllPropertiesPerRelationship(relationship);
- relationshipPropertiesPerLabel.addAll(propertiesPerRelationship);
- }
+ private void fillColumnTypesFromData(final JSONObject jsonObject, final List<String> columnNames,
+ final List<ColumnType> columnTypes) throws JSONException {
+ final String dataKey = "data";
+
+ if (jsonObject.has(dataKey)) {
+ final JSONObject data = jsonObject.getJSONObject(dataKey);
+ final Iterator keysIterator = data.keys();
+
+ while (keysIterator.hasNext()) {
+ final String key = (String) keysIterator.next();
+ final ColumnType type = getTypeFromValue(data, key);
+ columnTypes.add(type);
+ removeIfAvailable(columnNames, key);
+ }
+ }
+ }
+
+ private void fillColumnTypesFromMetadata(final JSONObject jsonObject, final List<String> columnNames,
+ final List<ColumnType> columnTypes) throws JSONException {
+ final String metadataKey = "metadata";
+
+ if (jsonObject.has(metadataKey)) {
+ final JSONObject metadata = jsonObject.getJSONObject(metadataKey);
+
+ if (metadata.has("id")) {
+ columnTypes.add(ColumnType.BIGINT);
+ removeIfAvailable(columnNames, "_id");
+ }
+ }
+ }
+
+ private void fillColumnTypesFromRemainingColumns(final List<String> columnNames,
+ final List<ColumnType> columnTypes) {
+ for (final String remainingColumnName : columnNames) {
+ if (remainingColumnName.contains("rel_")) {
+ if (remainingColumnName.contains("#")) {
+ columnTypes.add(ColumnType.ARRAY);
+ } else {
+ columnTypes.add(ColumnType.BIGINT);
}
- propertiesPerLabel.addAll(relationshipPropertiesPerLabel);
-
- // Do not add a table if label has no nodes (empty tables are
- // considered non-existent)
- if (!nodesPerLabel.isEmpty()) {
- SimpleTableDef tableDef = new SimpleTableDef(label,
- propertiesPerLabel.toArray(new String[propertiesPerLabel.size()]));
- tableDefs.add(tableDef);
+ } else {
+ columnTypes.add(ColumnType.STRING);
+ }
+ }
+ }
+
+ private void removeIfAvailable(final List<String> list, final String key) {
+ if (list.contains(key)) {
+ list.remove(key);
+ }
+ }
+
+ private ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ data.getBoolean(key);
+ return ColumnType.BOOLEAN;
+ } catch (final JSONException e1) {
+ try {
+ data.getInt(key);
+ return ColumnType.INTEGER;
+ } catch (final JSONException e2) {
+ try {
+ data.getLong(key);
+ return ColumnType.BIGINT;
+ } catch (final JSONException e3) {
+ try {
+ data.getDouble(key);
+ return ColumnType.DOUBLE;
+ } catch (final JSONException e4) {
+ try {
+ data.getJSONArray(key);
+ return ColumnType.ARRAY;
+ } catch (final JSONException e5) {
+ try {
+ data.getJSONObject(key);
+ return ColumnType.MAP;
+ } catch (final JSONException e6) {
+ return ColumnType.STRING;
+ }
+ }
+ }
}
}
- return tableDefs.toArray(new SimpleTableDef[tableDefs.size()]);
- } catch (JSONException e) {
- logger.error("Error occured in parsing JSON while detecting the schema: ", e);
- throw new IllegalStateException(e);
}
}
@@ -236,8 +358,8 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
}
}
- private List<JSONObject> getOutgoingRelationshipsPerNode(Integer nodeId) {
- List<JSONObject> outgoingRelationshipsPerNode = new ArrayList<JSONObject>();
+ private List<JSONObject> getOutgoingRelationshipsPerNode(final Long nodeId) {
+ List<JSONObject> outgoingRelationshipsPerNode = new ArrayList<>();
String outgoingRelationshipsPerNodeJsonString = _requestWrapper.executeRestRequest(new HttpGet(_serviceRoot
+ "/node/" + nodeId + "/relationships/out"));
http://git-wip-us.apache.org/repos/asf/metamodel/blob/fdaaed05/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java
index 537a068..6c1af50 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java
@@ -44,28 +44,32 @@ final class Neo4jDataSet extends AbstractDataSet {
@Override
public boolean next() {
try {
- JSONArray resultsArray = _resultJSONObject.getJSONArray("results");
+ final JSONArray resultsArray = _resultJSONObject.getJSONArray("results");
+
if (resultsArray.length() > 0) {
- JSONObject results = resultsArray.getJSONObject(0);
- JSONArray data = results.getJSONArray("data");
+ final JSONObject results = resultsArray.getJSONObject(0);
+ final JSONArray data = results.getJSONArray("data");
+
if (_currentRowIndex < data.length()) {
- JSONObject row = data.getJSONObject(_currentRowIndex);
- JSONArray jsonValues = row.getJSONArray("row");
-
- Object[] objectValues = new Object[jsonValues.length()];
+ final JSONObject row = data.getJSONObject(_currentRowIndex);
+ final JSONArray jsonValues = row.getJSONArray("row");
+ final Object[] objectValues = new Object[jsonValues.length()];
+
for (int i = 0; i < jsonValues.length(); i++) {
- objectValues[i] = jsonValues.getString(i);
+ objectValues[i] = jsonValues.get(i);
}
+
_row = new DefaultRow(new SimpleDataSetHeader(getSelectItems()), objectValues);
_currentRowIndex++;
+
return true;
}
} else {
- JSONArray errorArray = _resultJSONObject.getJSONArray("errors");
- JSONObject error = errorArray.getJSONObject(0);
+ final JSONArray errorArray = _resultJSONObject.getJSONArray("errors");
+ final JSONObject error = errorArray.getJSONObject(0);
throw new IllegalStateException(error.toString());
}
- } catch (JSONException e) {
+ } catch (final JSONException e) {
throw new IllegalStateException(e);
}
return false;
[2/7] metamodel git commit: Refactoring.
Posted by ar...@apache.org.
Refactoring.
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/b60601e7
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/b60601e7
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/b60601e7
Branch: refs/heads/master
Commit: b60601e76e0218ae314f1dbf3e50c6a27a0877cf
Parents: fdaaed0
Author: jakub <j....@quadient.com>
Authored: Tue May 22 12:22:26 2018 +0200
Committer: jakub <j....@quadient.com>
Committed: Tue May 22 12:22:26 2018 +0200
----------------------------------------------------------------------
.../metamodel/neo4j/Neo4jDataContext.java | 110 +----------------
.../neo4j/utils/AbstractColumnTypeHandler.java | 33 ++++++
.../neo4j/utils/ArrayColumnTypeHandler.java | 39 +++++++
.../neo4j/utils/BooleanColumnTypeHandler.java | 39 +++++++
.../neo4j/utils/ColumnTypeHandler.java | 38 ++++++
.../neo4j/utils/ColumnTypeResolver.java | 117 +++++++++++++++++++
.../neo4j/utils/DoubleColumnTypeHandler.java | 39 +++++++
.../neo4j/utils/IntegerColumnTypeHandler.java | 39 +++++++
.../neo4j/utils/LongColumnTypeHandler.java | 39 +++++++
.../neo4j/utils/MapColumnTypeHandler.java | 39 +++++++
.../neo4j/utils/StringColumnTypeHandler.java | 39 +++++++
11 files changed, 465 insertions(+), 106 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
index 70686ef..83f01e7 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
@@ -19,8 +19,6 @@
package org.apache.metamodel.neo4j;
import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -35,10 +33,10 @@ import org.apache.metamodel.MetaModelException;
import org.apache.metamodel.QueryPostprocessDataContext;
import org.apache.metamodel.data.DataSet;
import org.apache.metamodel.data.DocumentSource;
+import org.apache.metamodel.neo4j.utils.ColumnTypeResolver;
import org.apache.metamodel.query.FilterItem;
import org.apache.metamodel.query.SelectItem;
import org.apache.metamodel.schema.Column;
-import org.apache.metamodel.schema.ColumnType;
import org.apache.metamodel.schema.MutableSchema;
import org.apache.metamodel.schema.MutableTable;
import org.apache.metamodel.schema.Schema;
@@ -194,9 +192,9 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
// 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 ColumnType[] columnTypes = guessColumnTypesFromValues(nodesPerLabel.get(0),
- new ArrayList<>(Arrays.asList(columnNames)));
- final SimpleTableDef tableDef = new SimpleTableDef(label, columnNames, columnTypes);
+ final ColumnTypeResolver columnTypeResolver = new ColumnTypeResolver();
+ final SimpleTableDef tableDef = new SimpleTableDef(label, columnNames,
+ columnTypeResolver.getColumnTypes(nodesPerLabel.get(0), columnNames));
tableDefs.add(tableDef);
}
}
@@ -236,106 +234,6 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
return propertiesPerLabel;
}
- private ColumnType[] guessColumnTypesFromValues(final JSONObject jsonObject, final List<String> columnNames) {
- final List<ColumnType> columnTypes = new ArrayList<>();
-
- try {
- fillColumnTypesFromMetadata(jsonObject, columnNames, columnTypes);
- fillColumnTypesFromData(jsonObject, columnNames, columnTypes);
- } catch (final JSONException e) {
- // ignore missing data
- }
-
- fillColumnTypesFromRemainingColumns(columnNames, columnTypes);
- return columnTypes.toArray(new ColumnType[columnTypes.size()]);
- }
-
- private void fillColumnTypesFromData(final JSONObject jsonObject, final List<String> columnNames,
- final List<ColumnType> columnTypes) throws JSONException {
- final String dataKey = "data";
-
- if (jsonObject.has(dataKey)) {
- final JSONObject data = jsonObject.getJSONObject(dataKey);
- final Iterator keysIterator = data.keys();
-
- while (keysIterator.hasNext()) {
- final String key = (String) keysIterator.next();
- final ColumnType type = getTypeFromValue(data, key);
- columnTypes.add(type);
- removeIfAvailable(columnNames, key);
- }
- }
- }
-
- private void fillColumnTypesFromMetadata(final JSONObject jsonObject, final List<String> columnNames,
- final List<ColumnType> columnTypes) throws JSONException {
- final String metadataKey = "metadata";
-
- if (jsonObject.has(metadataKey)) {
- final JSONObject metadata = jsonObject.getJSONObject(metadataKey);
-
- if (metadata.has("id")) {
- columnTypes.add(ColumnType.BIGINT);
- removeIfAvailable(columnNames, "_id");
- }
- }
- }
-
- private void fillColumnTypesFromRemainingColumns(final List<String> columnNames,
- final List<ColumnType> columnTypes) {
- for (final String remainingColumnName : columnNames) {
- if (remainingColumnName.contains("rel_")) {
- if (remainingColumnName.contains("#")) {
- columnTypes.add(ColumnType.ARRAY);
- } else {
- columnTypes.add(ColumnType.BIGINT);
- }
- } else {
- columnTypes.add(ColumnType.STRING);
- }
- }
- }
-
- private void removeIfAvailable(final List<String> list, final String key) {
- if (list.contains(key)) {
- list.remove(key);
- }
- }
-
- private ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- data.getBoolean(key);
- return ColumnType.BOOLEAN;
- } catch (final JSONException e1) {
- try {
- data.getInt(key);
- return ColumnType.INTEGER;
- } catch (final JSONException e2) {
- try {
- data.getLong(key);
- return ColumnType.BIGINT;
- } catch (final JSONException e3) {
- try {
- data.getDouble(key);
- return ColumnType.DOUBLE;
- } catch (final JSONException e4) {
- try {
- data.getJSONArray(key);
- return ColumnType.ARRAY;
- } catch (final JSONException e5) {
- try {
- data.getJSONObject(key);
- return ColumnType.MAP;
- } catch (final JSONException e6) {
- return ColumnType.STRING;
- }
- }
- }
- }
- }
- }
- }
-
private List<String> getAllPropertiesPerRelationship(JSONObject relationship) {
List<String> propertyNames = new ArrayList<String>();
try {
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java
new file mode 100644
index 0000000..1198fe0
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java
@@ -0,0 +1,33 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONObject;
+
+public abstract class AbstractColumnTypeHandler implements ColumnTypeHandler {
+ protected ColumnTypeHandler _successor;
+
+ @Override
+ public void setSuccessor(final ColumnTypeHandler successor) {
+ _successor = successor;
+ }
+
+ public abstract ColumnType getTypeFromValue(final JSONObject data, final String key);
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java
new file mode 100644
index 0000000..3d491f4
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class ArrayColumnTypeHandler extends AbstractColumnTypeHandler {
+ @Override
+ public ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ data.getJSONArray(key);
+ return ColumnType.ARRAY;
+ } catch (final JSONException e) {
+ if (_successor != null) {
+ return _successor.getTypeFromValue(data, key);
+ } else {
+ return ColumnType.STRING;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java
new file mode 100644
index 0000000..5aa4194
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class BooleanColumnTypeHandler extends AbstractColumnTypeHandler {
+ @Override
+ public ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ data.getBoolean(key);
+ return ColumnType.BOOLEAN;
+ } catch (final JSONException e) {
+ if (_successor != null) {
+ return _successor.getTypeFromValue(data, key);
+ } else {
+ return ColumnType.STRING;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java
new file mode 100644
index 0000000..f673f4d
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONObject;
+
+public interface ColumnTypeHandler {
+ /**
+ * Sets the following node in the chain.
+ * @param successor
+ */
+ void setSuccessor(final ColumnTypeHandler successor);
+
+ /**
+ * Returns a column type based on the given value.
+ * @param data
+ * @param key
+ * @return ColumnType
+ */
+ ColumnType getTypeFromValue(final JSONObject data, final String key);
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
new file mode 100644
index 0000000..75677a2
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
@@ -0,0 +1,117 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class ColumnTypeResolver {
+ public ColumnType[] getColumnTypes(final JSONObject jsonObject, final String[] columnNamesArray) {
+ final List<String> columnNames = new ArrayList<>(Arrays.asList(columnNamesArray));
+ final List<ColumnType> columnTypes = new ArrayList<>();
+
+ try {
+ fillColumnTypesFromMetadata(jsonObject, columnNames, columnTypes);
+ fillColumnTypesFromData(jsonObject, columnNames, columnTypes);
+ } catch (final JSONException e) {
+ // ignore missing data
+ }
+
+ fillColumnTypesFromRemainingColumns(columnNames, columnTypes);
+ return columnTypes.toArray(new ColumnType[columnTypes.size()]);
+ }
+
+ private void fillColumnTypesFromData(final JSONObject jsonObject, final List<String> columnNames,
+ final List<ColumnType> columnTypes) throws JSONException {
+ final String dataKey = "data";
+
+ if (jsonObject.has(dataKey)) {
+ final JSONObject data = jsonObject.getJSONObject(dataKey);
+ final Iterator keysIterator = data.keys();
+
+ while (keysIterator.hasNext()) {
+ final String key = (String) keysIterator.next();
+ final ColumnType type = getTypeFromValue(data, key);
+ columnTypes.add(type);
+ removeIfAvailable(columnNames, key);
+ }
+ }
+ }
+
+ private void fillColumnTypesFromMetadata(final JSONObject jsonObject, final List<String> columnNames,
+ final List<ColumnType> columnTypes) throws JSONException {
+ final String metadataKey = "metadata";
+
+ if (jsonObject.has(metadataKey)) {
+ final JSONObject metadata = jsonObject.getJSONObject(metadataKey);
+
+ if (metadata.has("id")) {
+ columnTypes.add(ColumnType.BIGINT);
+ removeIfAvailable(columnNames, "_id");
+ }
+ }
+ }
+
+ private void fillColumnTypesFromRemainingColumns(final List<String> columnNames,
+ final List<ColumnType> columnTypes) {
+ for (final String remainingColumnName : columnNames) {
+ if (remainingColumnName.contains("rel_")) {
+ if (remainingColumnName.contains("#")) {
+ columnTypes.add(ColumnType.ARRAY);
+ } else {
+ columnTypes.add(ColumnType.BIGINT);
+ }
+ } else {
+ columnTypes.add(ColumnType.STRING);
+ }
+ }
+ }
+
+ private void removeIfAvailable(final List<String> list, final String key) {
+ if (list.contains(key)) {
+ list.remove(key);
+ }
+ }
+
+ private ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ final BooleanColumnTypeHandler booleanHandler = new BooleanColumnTypeHandler();
+ final IntegerColumnTypeHandler integerHandler = new IntegerColumnTypeHandler();
+ final LongColumnTypeHandler longHandler = new LongColumnTypeHandler();
+ final DoubleColumnTypeHandler doubleHandler = new DoubleColumnTypeHandler();
+ final ArrayColumnTypeHandler arrayHandler = new ArrayColumnTypeHandler();
+ final MapColumnTypeHandler mapHandler = new MapColumnTypeHandler();
+ final StringColumnTypeHandler stringHandler = new StringColumnTypeHandler();
+
+ // chain of responsibility
+ booleanHandler.setSuccessor(integerHandler);
+ integerHandler.setSuccessor(longHandler);
+ longHandler.setSuccessor(doubleHandler);
+ doubleHandler.setSuccessor(arrayHandler);
+ arrayHandler.setSuccessor(mapHandler);
+ mapHandler.setSuccessor(stringHandler);
+
+ return booleanHandler.getTypeFromValue(data, key);
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java
new file mode 100644
index 0000000..35e9ef8
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class DoubleColumnTypeHandler extends AbstractColumnTypeHandler {
+ @Override
+ public ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ data.getDouble(key);
+ return ColumnType.DOUBLE;
+ } catch (final JSONException e) {
+ if (_successor != null) {
+ return _successor.getTypeFromValue(data, key);
+ } else {
+ return ColumnType.STRING;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java
new file mode 100644
index 0000000..9a48742
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class IntegerColumnTypeHandler extends AbstractColumnTypeHandler {
+ @Override
+ public ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ data.getInt(key);
+ return ColumnType.INTEGER;
+ } catch (final JSONException e) {
+ if (_successor != null) {
+ return _successor.getTypeFromValue(data, key);
+ } else {
+ return ColumnType.STRING;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java
new file mode 100644
index 0000000..939e355
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class LongColumnTypeHandler extends AbstractColumnTypeHandler {
+ @Override
+ public ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ data.getLong(key);
+ return ColumnType.BIGINT;
+ } catch (final JSONException e) {
+ if (_successor != null) {
+ return _successor.getTypeFromValue(data, key);
+ } else {
+ return ColumnType.STRING;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java
new file mode 100644
index 0000000..8151490
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class MapColumnTypeHandler extends AbstractColumnTypeHandler {
+ @Override
+ public ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ data.getJSONObject(key);
+ return ColumnType.MAP;
+ } catch (final JSONException e) {
+ if (_successor != null) {
+ return _successor.getTypeFromValue(data, key);
+ } else {
+ return ColumnType.STRING;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/b60601e7/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java
new file mode 100644
index 0000000..06afe36
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class StringColumnTypeHandler extends AbstractColumnTypeHandler {
+ @Override
+ public ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ data.getString(key);
+ return ColumnType.STRING;
+ } catch (final JSONException e) {
+ if (_successor != null) {
+ return _successor.getTypeFromValue(data, key);
+ } else {
+ return ColumnType.STRING;
+ }
+ }
+ }
+}
[4/7] metamodel git commit: Refactoring -- chain or responsibility
pattern was replaced by if-then block. JUnit for new functionality.
Posted by ar...@apache.org.
Refactoring -- chain or responsibility pattern was replaced by if-then block.
JUnit for new functionality.
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/e0a821c3
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/e0a821c3
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/e0a821c3
Branch: refs/heads/master
Commit: e0a821c3c55c61245097aa22e4fdb6cdc8d1e7b0
Parents: 59ec9cc
Author: jakub <j....@quadient.com>
Authored: Wed May 23 16:28:43 2018 +0200
Committer: jakub <j....@quadient.com>
Committed: Wed May 23 16:28:43 2018 +0200
----------------------------------------------------------------------
.../metamodel/neo4j/Neo4jDataContext.java | 77 +++++++++---------
.../neo4j/utils/AbstractColumnTypeHandler.java | 33 --------
.../neo4j/utils/ArrayColumnTypeHandler.java | 39 ----------
.../neo4j/utils/BooleanColumnTypeHandler.java | 39 ----------
.../neo4j/utils/ColumnTypeHandler.java | 38 ---------
.../neo4j/utils/ColumnTypeResolver.java | 43 ++++++----
.../neo4j/utils/DoubleColumnTypeHandler.java | 39 ----------
.../neo4j/utils/IntegerColumnTypeHandler.java | 39 ----------
.../neo4j/utils/LongColumnTypeHandler.java | 39 ----------
.../neo4j/utils/MapColumnTypeHandler.java | 39 ----------
.../neo4j/utils/StringColumnTypeHandler.java | 39 ----------
.../neo4j/utils/ColumnTypeResolverTest.java | 82 ++++++++++++++++++++
12 files changed, 150 insertions(+), 396 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
index b23f1c9..524d29c 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
@@ -167,55 +167,59 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
labelsJsonArray = new JSONArray(labelsJsonString);
for (int i = 0; i < labelsJsonArray.length(); i++) {
- fillTableDefsFromLabel(labelsJsonArray.getString(i), tableDefs);
+ 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 occured in parsing JSON while detecting the schema: ", e);
+ logger.error("Error occurred in parsing JSON while detecting the schema: ", e);
throw new IllegalStateException(e);
}
}
- private void fillTableDefsFromLabel(final String label, final List<SimpleTableDef> tableDefs) throws JSONException {
+ 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");
- fillRelationshipPropertiesPerLabel(nodeId, relationshipPropertiesPerLabel);
+ final Set<String> relationshipPropertiesForNode = createRelationshipPropertiesForNode(nodeId);
+ relationshipPropertiesPerLabel.addAll(relationshipPropertiesForNode);
}
propertiesPerLabel.addAll(relationshipPropertiesPerLabel);
- // Do not add a table if label has no nodes (empty tables are considered non-existent)
- if (!nodesPerLabel.isEmpty()) {
+ 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();
- final SimpleTableDef tableDef = new SimpleTableDef(label, columnNames,
+ return new SimpleTableDef(label, columnNames,
columnTypeResolver.getColumnTypes(nodesPerLabel.get(0), columnNames));
- tableDefs.add(tableDef);
}
}
- private void fillRelationshipPropertiesPerLabel(final Integer nodeId, final Set<String> relationshipPropertiesPerLabel)
- throws JSONException {
+ 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");
final String relationshipNameProperty = RELATIONSHIP_PREFIX + relationshipName;
+ relationshipProperties.add(relationshipNameProperty);
- if (!relationshipPropertiesPerLabel.contains(relationshipNameProperty)) {
- relationshipPropertiesPerLabel.add(relationshipNameProperty);
- }
-
// Add all the relationship properties as table columns
final List<String> propertiesPerRelationship = getAllPropertiesPerRelationship(relationship);
- relationshipPropertiesPerLabel.addAll(propertiesPerRelationship);
+ relationshipProperties.addAll(propertiesPerRelationship);
}
+
+ return relationshipProperties;
}
private List<String> getPropertiesFromLabelNodes(final List<JSONObject> nodesPerLabel) {
@@ -235,14 +239,17 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
}
private List<String> getAllPropertiesPerRelationship(JSONObject relationship) {
- List<String> propertyNames = new ArrayList<String>();
+ final List<String> propertyNames = new ArrayList<>();
try {
- String relationshipName = RELATIONSHIP_PREFIX + relationship.getJSONObject("metadata").getString("type");
- JSONObject relationshipPropertiesJSONObject = relationship.getJSONObject("data");
+ final String relationshipName =
+ RELATIONSHIP_PREFIX + relationship.getJSONObject("metadata").getString("type");
+ final JSONObject relationshipPropertiesJSONObject = relationship.getJSONObject("data");
+
if (relationshipPropertiesJSONObject.length() > 0) {
- JSONArray relationshipPropertiesNamesJSONArray = relationshipPropertiesJSONObject.names();
+ final JSONArray relationshipPropertiesNamesJSONArray = relationshipPropertiesJSONObject.names();
+
for (int i = 0; i < relationshipPropertiesNamesJSONArray.length(); i++) {
- String propertyName = relationshipName + RELATIONSHIP_COLUMN_SEPARATOR
+ final String propertyName = relationshipName + RELATIONSHIP_COLUMN_SEPARATOR
+ relationshipPropertiesNamesJSONArray.getString(i);
if (!propertyNames.contains(propertyName)) {
propertyNames.add(propertyName);
@@ -250,30 +257,30 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
}
}
return propertyNames;
- } catch (JSONException e) {
- logger.error("Error occured in parsing JSON while getting relationship properties: ", e);
+ } catch (final JSONException e) {
+ logger.error("Error occurred in parsing JSON while getting relationship properties: ", e);
throw new IllegalStateException(e);
}
}
private List<JSONObject> getOutgoingRelationshipsPerNode(final Integer nodeId) {
- List<JSONObject> outgoingRelationshipsPerNode = new ArrayList<>();
-
- String outgoingRelationshipsPerNodeJsonString = _requestWrapper.executeRestRequest(new HttpGet(_serviceRoot
- + "/node/" + nodeId + "/relationships/out"));
-
- JSONArray outgoingRelationshipsPerNodeJsonArray;
+ final List<JSONObject> outgoingRelationshipsPerNode = new ArrayList<>();
+ final String outgoingRelationshipsPerNodeJsonString = _requestWrapper.executeRestRequest(
+ new HttpGet(_serviceRoot + "/node/" + nodeId + "/relationships/out"));
+ final JSONArray outgoingRelationshipsPerNodeJsonArray;
+
try {
outgoingRelationshipsPerNodeJsonArray = new JSONArray(outgoingRelationshipsPerNodeJsonString);
for (int i = 0; i < outgoingRelationshipsPerNodeJsonArray.length(); i++) {
- JSONObject relationship = outgoingRelationshipsPerNodeJsonArray.getJSONObject(i);
+ final JSONObject relationship = outgoingRelationshipsPerNodeJsonArray.getJSONObject(i);
if (!outgoingRelationshipsPerNode.contains(relationship)) {
outgoingRelationshipsPerNode.add(relationship);
}
}
return outgoingRelationshipsPerNode;
- } catch (JSONException e) {
- logger.error("Error occured in parsing JSON while detecting outgoing relationships for node: " + nodeId, e);
+ } catch (final JSONException e) {
+ logger.error("Error occurred in parsing JSON while detecting outgoing relationships for node: " + nodeId,
+ e);
throw new IllegalStateException(e);
}
}
@@ -293,7 +300,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
}
return allNodesPerLabel;
} catch (JSONException e) {
- logger.error("Error occured in parsing JSON while detecting the nodes for a label: " + label, e);
+ logger.error("Error occurred in parsing JSON while detecting the nodes for a label: " + label, e);
throw new IllegalStateException(e);
}
}
@@ -318,7 +325,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
}
return properties;
} catch (JSONException e) {
- logger.error("Error occured in parsing JSON while detecting the properties of a node: " + node, e);
+ logger.error("Error occurred in parsing JSON while detecting the properties of a node: " + node, e);
throw new IllegalStateException(e);
}
}
@@ -334,7 +341,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
final List<SelectItem> selectItems = columns.stream().map(SelectItem::new).collect(Collectors.toList());
dataSet = new Neo4jDataSet(selectItems, resultJSONObject);
} catch (JSONException e) {
- logger.error("Error occured in parsing JSON while materializing the schema: ", e);
+ logger.error("Error occurred in parsing JSON while materializing the schema: ", e);
throw new IllegalStateException(e);
}
@@ -366,7 +373,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
Number value = (Number) valueJSONArray.get(0);
return value;
} catch (JSONException e) {
- logger.error("Error occured in parsing JSON response: ", e);
+ logger.error("Error occurred in parsing JSON response: ", e);
// Do not throw an exception here. Returning null here will make
// MetaModel attempt to count records manually and therefore recover
// from the error.
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java
deleted file mode 100644
index 1198fe0..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONObject;
-
-public abstract class AbstractColumnTypeHandler implements ColumnTypeHandler {
- protected ColumnTypeHandler _successor;
-
- @Override
- public void setSuccessor(final ColumnTypeHandler successor) {
- _successor = successor;
- }
-
- public abstract ColumnType getTypeFromValue(final JSONObject data, final String key);
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java
deleted file mode 100644
index 3d491f4..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-public class ArrayColumnTypeHandler extends AbstractColumnTypeHandler {
- @Override
- public ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- data.getJSONArray(key);
- return ColumnType.ARRAY;
- } catch (final JSONException e) {
- if (_successor != null) {
- return _successor.getTypeFromValue(data, key);
- } else {
- return ColumnType.STRING;
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java
deleted file mode 100644
index 5aa4194..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-public class BooleanColumnTypeHandler extends AbstractColumnTypeHandler {
- @Override
- public ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- data.getBoolean(key);
- return ColumnType.BOOLEAN;
- } catch (final JSONException e) {
- if (_successor != null) {
- return _successor.getTypeFromValue(data, key);
- } else {
- return ColumnType.STRING;
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java
deleted file mode 100644
index f673f4d..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONObject;
-
-public interface ColumnTypeHandler {
- /**
- * Sets the following node in the chain.
- * @param successor
- */
- void setSuccessor(final ColumnTypeHandler successor);
-
- /**
- * Returns a column type based on the given value.
- * @param data
- * @param key
- * @return ColumnType
- */
- ColumnType getTypeFromValue(final JSONObject data, final String key);
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
index 75677a2..f5a817b 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
@@ -24,10 +24,15 @@ import java.util.Iterator;
import java.util.List;
import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class ColumnTypeResolver {
+ private static final Logger logger = LoggerFactory.getLogger(ColumnTypeResolver.class);
+
public ColumnType[] getColumnTypes(final JSONObject jsonObject, final String[] columnNamesArray) {
final List<String> columnNames = new ArrayList<>(Arrays.asList(columnNamesArray));
final List<ColumnType> columnTypes = new ArrayList<>();
@@ -49,7 +54,7 @@ public class ColumnTypeResolver {
if (jsonObject.has(dataKey)) {
final JSONObject data = jsonObject.getJSONObject(dataKey);
- final Iterator keysIterator = data.keys();
+ final Iterator<?> keysIterator = data.keys();
while (keysIterator.hasNext()) {
final String key = (String) keysIterator.next();
@@ -96,22 +101,26 @@ public class ColumnTypeResolver {
}
private ColumnType getTypeFromValue(final JSONObject data, final String key) {
- final BooleanColumnTypeHandler booleanHandler = new BooleanColumnTypeHandler();
- final IntegerColumnTypeHandler integerHandler = new IntegerColumnTypeHandler();
- final LongColumnTypeHandler longHandler = new LongColumnTypeHandler();
- final DoubleColumnTypeHandler doubleHandler = new DoubleColumnTypeHandler();
- final ArrayColumnTypeHandler arrayHandler = new ArrayColumnTypeHandler();
- final MapColumnTypeHandler mapHandler = new MapColumnTypeHandler();
- final StringColumnTypeHandler stringHandler = new StringColumnTypeHandler();
-
- // chain of responsibility
- booleanHandler.setSuccessor(integerHandler);
- integerHandler.setSuccessor(longHandler);
- longHandler.setSuccessor(doubleHandler);
- doubleHandler.setSuccessor(arrayHandler);
- arrayHandler.setSuccessor(mapHandler);
- mapHandler.setSuccessor(stringHandler);
+ try {
+ final Class<? extends Object> keyClass = data.get(key).getClass();
+
+ if (keyClass.equals(Boolean.class)) {
+ return ColumnType.BOOLEAN;
+ } else if (keyClass.equals(Integer.class)) {
+ return ColumnType.INTEGER;
+ } else if (keyClass.equals(Long.class)) {
+ return ColumnType.BIGINT;
+ } else if (keyClass.equals(Double.class)) {
+ return ColumnType.DOUBLE;
+ } else if (keyClass.equals(JSONArray.class)) {
+ return ColumnType.ARRAY;
+ } else if (keyClass.equals(JSONObject.class)) {
+ return ColumnType.MAP;
+ }
+ } catch (final JSONException e) {
+ logger.error("JSON object does not contain required key '{}'. {}", key, e.getMessage());
+ }
- return booleanHandler.getTypeFromValue(data, key);
+ return ColumnType.STRING;
}
}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java
deleted file mode 100644
index 35e9ef8..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-public class DoubleColumnTypeHandler extends AbstractColumnTypeHandler {
- @Override
- public ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- data.getDouble(key);
- return ColumnType.DOUBLE;
- } catch (final JSONException e) {
- if (_successor != null) {
- return _successor.getTypeFromValue(data, key);
- } else {
- return ColumnType.STRING;
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java
deleted file mode 100644
index 9a48742..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-public class IntegerColumnTypeHandler extends AbstractColumnTypeHandler {
- @Override
- public ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- data.getInt(key);
- return ColumnType.INTEGER;
- } catch (final JSONException e) {
- if (_successor != null) {
- return _successor.getTypeFromValue(data, key);
- } else {
- return ColumnType.STRING;
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java
deleted file mode 100644
index 939e355..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-public class LongColumnTypeHandler extends AbstractColumnTypeHandler {
- @Override
- public ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- data.getLong(key);
- return ColumnType.BIGINT;
- } catch (final JSONException e) {
- if (_successor != null) {
- return _successor.getTypeFromValue(data, key);
- } else {
- return ColumnType.STRING;
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java
deleted file mode 100644
index 8151490..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-public class MapColumnTypeHandler extends AbstractColumnTypeHandler {
- @Override
- public ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- data.getJSONObject(key);
- return ColumnType.MAP;
- } catch (final JSONException e) {
- if (_successor != null) {
- return _successor.getTypeFromValue(data, key);
- } else {
- return ColumnType.STRING;
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java
deleted file mode 100644
index 06afe36..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-public class StringColumnTypeHandler extends AbstractColumnTypeHandler {
- @Override
- public ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- data.getString(key);
- return ColumnType.STRING;
- } catch (final JSONException e) {
- if (_successor != null) {
- return _successor.getTypeFromValue(data, key);
- } else {
- return ColumnType.STRING;
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
----------------------------------------------------------------------
diff --git a/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java b/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
new file mode 100644
index 0000000..8e56b66
--- /dev/null
+++ b/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j.utils;
+
+import static junit.framework.TestCase.assertEquals;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Test;
+
+public class ColumnTypeResolverTest {
+ private static final String COLUMN_ID = "id";
+ private static final String COLUMN_ID_NEO4J = "_id";
+ private static final String COLUMN_METADATA = "metadata";
+ private static final String COLUMN_DATA = "data";
+ private static final String COLUMN_BOOLEAN = "boolean";
+ private static final String COLUMN_INTEGER = "integer";
+ private static final String COLUMN_LONG = "long";
+ private static final String COLUMN_DOUBLE = "double";
+ private static final String COLUMN_ARRAY = "array";
+ private static final String COLUMN_MAP = "map";
+ private static final String COLUMN_STRING = "string";
+
+ @Test
+ public void testGetColumnTypes() throws Exception {
+ final ColumnTypeResolver resolver = new ColumnTypeResolver();
+ final JSONObject jsonObject = createJSONObject();
+ final String[] columnNames =
+ new String[] { COLUMN_ID_NEO4J, COLUMN_BOOLEAN, COLUMN_INTEGER, COLUMN_LONG, COLUMN_DOUBLE,
+ COLUMN_ARRAY, COLUMN_MAP, COLUMN_STRING };
+ final ColumnType[] columnTypes = resolver.getColumnTypes(jsonObject, columnNames);
+ assertEquals(columnTypes.length, columnNames.length);
+ assertEquals(columnTypes[0], ColumnType.BIGINT); // ID
+ assertEquals(columnTypes[1], ColumnType.BOOLEAN);
+ assertEquals(columnTypes[2], ColumnType.STRING);
+ assertEquals(columnTypes[3], ColumnType.ARRAY);
+ assertEquals(columnTypes[4], ColumnType.DOUBLE);
+ assertEquals(columnTypes[5], ColumnType.INTEGER);
+ assertEquals(columnTypes[6], ColumnType.MAP);
+ assertEquals(columnTypes[7], ColumnType.BIGINT);
+ }
+
+ private JSONObject createJSONObject() throws JSONException {
+ final JSONObject json = new JSONObject();
+ final JSONObject metadata = new JSONObject();
+ metadata.put(COLUMN_ID, 42L);
+ json.put(COLUMN_METADATA, metadata);
+ final JSONObject data = new JSONObject();
+ data.put(COLUMN_BOOLEAN, true);
+ data.put(COLUMN_STRING, "forty-two");
+ final JSONArray array = new JSONArray();
+ array.put(1).put(2).put(3);
+ data.put(COLUMN_ARRAY, array);
+ data.put(COLUMN_DOUBLE, 3.141592);
+ data.put(COLUMN_INTEGER, 42);
+ final JSONObject map = new JSONObject();
+ map.put("1", "one").put("2", "two").put("3", "three");
+ data.put(COLUMN_MAP, map);
+ data.put(COLUMN_LONG, 12345678910L);
+ json.put(COLUMN_DATA, data);
+
+ return json;
+ }
+}
[6/7] metamodel git commit: Refactoring: * Removal of
ColumnTypeResolver._columnTypes field. * String constants in
Neo4jDataContext. * JSONArray => List conversion in Neo4jDataSet.
Posted by ar...@apache.org.
Refactoring:
* Removal of ColumnTypeResolver._columnTypes field.
* String constants in Neo4jDataContext.
* JSONArray => List<String> conversion in Neo4jDataSet.
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/0ec8610c
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/0ec8610c
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/0ec8610c
Branch: refs/heads/master
Commit: 0ec8610c409e62866bedaa5bd67e82d33155892e
Parents: f1457a1
Author: jakub <j....@quadient.com>
Authored: Mon May 28 15:33:55 2018 +0200
Committer: jakub <j....@quadient.com>
Committed: Mon May 28 15:33:55 2018 +0200
----------------------------------------------------------------------
.../metamodel/neo4j/ColumnTypeResolver.java | 137 +++++++++++++++++++
.../neo4j/Neo4jCypherQueryBuilder.java | 14 +-
.../metamodel/neo4j/Neo4jDataContext.java | 52 +++----
.../apache/metamodel/neo4j/Neo4jDataSet.java | 36 ++++-
.../neo4j/utils/ColumnTypeResolver.java | 126 -----------------
.../metamodel/neo4j/ColumnTypeResolverTest.java | 76 ++++++++++
.../neo4j/utils/ColumnTypeResolverTest.java | 79 -----------
7 files changed, 276 insertions(+), 244 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/metamodel/blob/0ec8610c/neo4j/src/main/java/org/apache/metamodel/neo4j/ColumnTypeResolver.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/ColumnTypeResolver.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/ColumnTypeResolver.java
new file mode 100644
index 0000000..84be8d6
--- /dev/null
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/ColumnTypeResolver.java
@@ -0,0 +1,137 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j;
+
+import static org.apache.metamodel.neo4j.Neo4jDataContext.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class ColumnTypeResolver {
+ private static final Logger logger = LoggerFactory.getLogger(ColumnTypeResolver.class);
+ private final JSONObject _jsonObject;
+ private final List<String> _columnNames = new ArrayList<>();
+
+ public ColumnTypeResolver(final JSONObject jsonObject, final String[] columnNamesArray) {
+ _jsonObject = jsonObject;
+ _columnNames.addAll(Arrays.asList(columnNamesArray));
+ }
+
+ public ColumnType[] getColumnTypes() {
+ final List<ColumnType> columnTypes = new ArrayList<>();
+
+ try {
+ columnTypes.addAll(getColumnTypesFromMetadata());
+ columnTypes.addAll(getColumnTypesFromData());
+ } catch (final JSONException e) {
+ // ignore missing data
+ }
+
+ columnTypes.addAll(getColumnTypesFromRemainingColumns());
+ return columnTypes.toArray(new ColumnType[columnTypes.size()]);
+ }
+
+ private List<ColumnType> getColumnTypesFromData() throws JSONException {
+ final List<ColumnType> columnTypes = new ArrayList<>();
+
+ if (_jsonObject.has(NEO4J_KEY_DATA)) {
+ final JSONObject data = _jsonObject.getJSONObject(NEO4J_KEY_DATA);
+ final Iterator<?> keysIterator = data.keys();
+
+ while (keysIterator.hasNext()) {
+ final String key = (String) keysIterator.next();
+ final ColumnType type = getTypeFromValue(data, key);
+ columnTypes.add(type);
+ removeIfAvailable(_columnNames, key);
+ }
+ }
+
+ return columnTypes;
+ }
+
+ private List<ColumnType> getColumnTypesFromMetadata() throws JSONException {
+ final List<ColumnType> columnTypes = new ArrayList<>();
+
+ if (_jsonObject.has(NEO4J_KEY_METADATA)) {
+ final JSONObject metadata = _jsonObject.getJSONObject(NEO4J_KEY_METADATA);
+
+ if (metadata.has(NEO4J_KEY_ID)) {
+ columnTypes.add(ColumnType.BIGINT);
+ removeIfAvailable(_columnNames, NEO4J_COLUMN_NAME_ID);
+ }
+ }
+
+ return columnTypes;
+ }
+
+ private List<ColumnType> getColumnTypesFromRemainingColumns() {
+ final List<ColumnType> columnTypes = new ArrayList<>();
+
+ for (final String remainingColumnName : _columnNames) {
+ if (remainingColumnName.contains(NEO4J_COLUMN_NAME_RELATION_PREFIX)) {
+ if (remainingColumnName.contains(NEO4J_COLUMN_NAME_RELATION_LIST_INDICATOR)) {
+ columnTypes.add(ColumnType.LIST);
+ } else {
+ columnTypes.add(ColumnType.BIGINT);
+ }
+ } else {
+ columnTypes.add(ColumnType.STRING);
+ }
+ }
+
+ return columnTypes;
+ }
+
+ private void removeIfAvailable(final List<String> list, final String key) {
+ if (list.contains(key)) {
+ list.remove(key);
+ }
+ }
+
+ private ColumnType getTypeFromValue(final JSONObject data, final String key) {
+ try {
+ final Class<? extends Object> keyClass = data.get(key).getClass();
+
+ if (keyClass.equals(Boolean.class)) {
+ return ColumnType.BOOLEAN;
+ } else if (keyClass.equals(Integer.class)) {
+ return ColumnType.INTEGER;
+ } else if (keyClass.equals(Long.class)) {
+ return ColumnType.BIGINT;
+ } else if (keyClass.equals(Double.class)) {
+ return ColumnType.DOUBLE;
+ } else if (keyClass.equals(JSONArray.class)) {
+ return ColumnType.LIST;
+ }
+ } catch (final JSONException e) {
+ logger.error("JSON object does not contain required key '{}'. {}", key, e.getMessage());
+ }
+
+ return ColumnType.STRING;
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/0ec8610c/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jCypherQueryBuilder.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jCypherQueryBuilder.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jCypherQueryBuilder.java
index 38079ce..c880c92 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jCypherQueryBuilder.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jCypherQueryBuilder.java
@@ -18,6 +18,8 @@
*/
package org.apache.metamodel.neo4j;
+import static org.apache.metamodel.neo4j.Neo4jDataContext.NEO4J_COLUMN_NAME_ID;
+
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
@@ -41,15 +43,15 @@ public class Neo4jCypherQueryBuilder {
Map<String, String> returnClauseMap = new LinkedHashMap<>();
Map<String, Integer> relationshipIndexMap = new LinkedHashMap<>();
for (String columnName : columnNames) {
- if (columnName.startsWith(Neo4jDataContext.RELATIONSHIP_PREFIX)) {
- columnName = columnName.replace(Neo4jDataContext.RELATIONSHIP_PREFIX, "");
+ if (columnName.startsWith(Neo4jDataContext.NEO4J_COLUMN_NAME_RELATION_PREFIX)) {
+ columnName = columnName.replace(Neo4jDataContext.NEO4J_COLUMN_NAME_RELATION_PREFIX, "");
String relationshipName;
String relationshipPropertyName;
- if (columnName.contains(Neo4jDataContext.RELATIONSHIP_COLUMN_SEPARATOR)) {
- String[] parsedColumnNameArray = columnName.split(Neo4jDataContext.RELATIONSHIP_COLUMN_SEPARATOR);
-
+ if (columnName.contains(Neo4jDataContext.NEO4J_COLUMN_NAME_RELATION_LIST_INDICATOR)) {
+ String[] parsedColumnNameArray =
+ columnName.split(Neo4jDataContext.NEO4J_COLUMN_NAME_RELATION_LIST_INDICATOR);
relationshipName = parsedColumnNameArray[0];
relationshipPropertyName = parsedColumnNameArray[1];
} else {
@@ -77,7 +79,7 @@ public class Neo4jCypherQueryBuilder {
returnClauseMap.put(columnName, relationshipAlias + "." + relationshipPropertyName);
}
} else {
- if (columnName.equals("_id")) {
+ if (columnName.equals(NEO4J_COLUMN_NAME_ID)) {
returnClauseMap.put(columnName, "id(n)");
} else {
returnClauseMap.put(columnName, "n." + columnName);
http://git-wip-us.apache.org/repos/asf/metamodel/blob/0ec8610c/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
index 662807d..2f8340a 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
@@ -33,7 +33,6 @@ import org.apache.metamodel.MetaModelException;
import org.apache.metamodel.QueryPostprocessDataContext;
import org.apache.metamodel.data.DataSet;
import org.apache.metamodel.data.DocumentSource;
-import org.apache.metamodel.neo4j.utils.ColumnTypeResolver;
import org.apache.metamodel.query.FilterItem;
import org.apache.metamodel.query.SelectItem;
import org.apache.metamodel.schema.Column;
@@ -53,23 +52,24 @@ import org.slf4j.LoggerFactory;
* DataContext implementation for Neo4j
*/
public class Neo4jDataContext extends QueryPostprocessDataContext implements DataContext, DocumentSourceProvider {
-
- public static final Logger logger = LoggerFactory.getLogger(Neo4jDataContext.class);
-
public static final String SCHEMA_NAME = "neo4j";
-
public static final int DEFAULT_PORT = 7474;
-
- public static final String RELATIONSHIP_PREFIX = "rel_";
-
- public static final String RELATIONSHIP_COLUMN_SEPARATOR = "#";
+ public static final String NEO4J_KEY_METADATA = "metadata";
+ public static final String NEO4J_KEY_METADATA_TYPE = "type";
+ public static final String NEO4J_KEY_PROPERTIES = "properties";
+ public static final String NEO4J_KEY_DATA = "data";
+ public static final String NEO4J_KEY_ID = "id";
+ public static final String NEO4J_KEY_RESPONSE_RESULTS = "results";
+ public static final String NEO4J_KEY_RESPONSE_ROW = "row";
+ public static final String NEO4J_COLUMN_NAME_ID = "_id";
+ public static final String NEO4J_COLUMN_NAME_RELATION_PREFIX = "rel_";
+ public static final String NEO4J_COLUMN_NAME_RELATION_LIST_INDICATOR = "#";
+
+ private static final Logger logger = LoggerFactory.getLogger(Neo4jDataContext.class);
private final SimpleTableDef[] _tableDefs;
-
private final Neo4jRequestWrapper _requestWrapper;
-
private final HttpHost _httpHost;
-
private String _serviceRoot = "/db/data";
public Neo4jDataContext(String hostname, int port, String username, String password, SimpleTableDef... tableDefs) {
@@ -187,7 +187,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
final Set<String> relationshipPropertiesPerLabel = new LinkedHashSet<>();
for (final JSONObject node : nodesPerLabel) {
- final Integer nodeId = (Integer) node.getJSONObject("metadata").get("id");
+ final Integer nodeId = (Integer) node.getJSONObject(NEO4J_KEY_METADATA).get(NEO4J_KEY_ID);
final Set<String> relationshipPropertiesForNode = createRelationshipPropertiesForNode(nodeId);
relationshipPropertiesPerLabel.addAll(relationshipPropertiesForNode);
}
@@ -209,8 +209,8 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
for (final JSONObject relationship : relationshipsPerNode) {
// Add the relationship as a column in the table
- final String relationshipName = relationship.getString("type");
- final String relationshipNameProperty = RELATIONSHIP_PREFIX + relationshipName;
+ final String relationshipName = relationship.getString(NEO4J_KEY_METADATA_TYPE);
+ final String relationshipNameProperty = NEO4J_COLUMN_NAME_RELATION_PREFIX + relationshipName;
relationshipProperties.add(relationshipNameProperty);
// Add all the relationship properties as table columns
@@ -240,15 +240,16 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
private List<String> getAllPropertiesPerRelationship(JSONObject relationship) {
final List<String> propertyNames = new ArrayList<>();
try {
- final String relationshipName =
- RELATIONSHIP_PREFIX + relationship.getJSONObject("metadata").getString("type");
- final JSONObject relationshipPropertiesJSONObject = relationship.getJSONObject("data");
+ final String relationshipName = NEO4J_COLUMN_NAME_RELATION_PREFIX + relationship
+ .getJSONObject(NEO4J_KEY_METADATA)
+ .getString(NEO4J_KEY_METADATA_TYPE);
+ final JSONObject relationshipPropertiesJSONObject = relationship.getJSONObject(NEO4J_KEY_DATA);
if (relationshipPropertiesJSONObject.length() > 0) {
final JSONArray relationshipPropertiesNamesJSONArray = relationshipPropertiesJSONObject.names();
-
+
for (int i = 0; i < relationshipPropertiesNamesJSONArray.length(); i++) {
- final String propertyName = relationshipName + RELATIONSHIP_COLUMN_SEPARATOR
+ final String propertyName = relationshipName + NEO4J_COLUMN_NAME_RELATION_LIST_INDICATOR
+ relationshipPropertiesNamesJSONArray.getString(i);
if (!propertyNames.contains(propertyName)) {
propertyNames.add(propertyName);
@@ -306,12 +307,11 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
private List<String> getAllPropertiesPerNode(JSONObject node) {
List<String> properties = new ArrayList<String>();
- properties.add("_id");
+ properties.add(NEO4J_COLUMN_NAME_ID);
String propertiesEndpoint;
try {
- propertiesEndpoint = node.getString("properties");
-
+ propertiesEndpoint = node.getString(NEO4J_KEY_PROPERTIES);
String allPropertiesPerNodeJsonString = _requestWrapper.executeRestRequest(new HttpGet(propertiesEndpoint));
JSONObject allPropertiesPerNodeJsonObject = new JSONObject(allPropertiesPerNodeJsonString);
@@ -364,11 +364,11 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
JSONObject jsonResponseObject;
try {
jsonResponseObject = new JSONObject(jsonResponse);
- JSONArray resultsJSONArray = jsonResponseObject.getJSONArray("results");
+ JSONArray resultsJSONArray = jsonResponseObject.getJSONArray(NEO4J_KEY_RESPONSE_RESULTS);
JSONObject resultJSONObject = (JSONObject) resultsJSONArray.get(0);
- JSONArray dataJSONArray = resultJSONObject.getJSONArray("data");
+ JSONArray dataJSONArray = resultJSONObject.getJSONArray(NEO4J_KEY_DATA);
JSONObject rowJSONObject = (JSONObject) dataJSONArray.get(0);
- JSONArray valueJSONArray = rowJSONObject.getJSONArray("row");
+ JSONArray valueJSONArray = rowJSONObject.getJSONArray(NEO4J_KEY_RESPONSE_ROW);
Number value = (Number) valueJSONArray.get(0);
return value;
} catch (JSONException e) {
http://git-wip-us.apache.org/repos/asf/metamodel/blob/0ec8610c/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java
index 6c1af50..9674f43 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java
@@ -18,6 +18,11 @@
*/
package org.apache.metamodel.neo4j;
+import static org.apache.metamodel.neo4j.Neo4jDataContext.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
import org.apache.metamodel.data.AbstractDataSet;
import org.apache.metamodel.data.DefaultRow;
import org.apache.metamodel.data.Row;
@@ -27,8 +32,6 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import java.util.List;
-
final class Neo4jDataSet extends AbstractDataSet {
private JSONObject _resultJSONObject;
@@ -44,19 +47,25 @@ final class Neo4jDataSet extends AbstractDataSet {
@Override
public boolean next() {
try {
- final JSONArray resultsArray = _resultJSONObject.getJSONArray("results");
+ final JSONArray resultsArray = _resultJSONObject.getJSONArray(NEO4J_KEY_RESPONSE_RESULTS);
if (resultsArray.length() > 0) {
final JSONObject results = resultsArray.getJSONObject(0);
- final JSONArray data = results.getJSONArray("data");
+ final JSONArray data = results.getJSONArray(NEO4J_KEY_DATA);
if (_currentRowIndex < data.length()) {
final JSONObject row = data.getJSONObject(_currentRowIndex);
- final JSONArray jsonValues = row.getJSONArray("row");
+ final JSONArray jsonValues = row.getJSONArray(NEO4J_KEY_RESPONSE_ROW);
final Object[] objectValues = new Object[jsonValues.length()];
for (int i = 0; i < jsonValues.length(); i++) {
- objectValues[i] = jsonValues.get(i);
+ final Object value = jsonValues.get(i);
+
+ if (value instanceof JSONArray) {
+ objectValues[i] = convertJSONArrayToList((JSONArray) value);
+ } else {
+ objectValues[i] = value;
+ }
}
_row = new DefaultRow(new SimpleDataSetHeader(getSelectItems()), objectValues);
@@ -75,9 +84,22 @@ final class Neo4jDataSet extends AbstractDataSet {
return false;
}
+ private List<String> convertJSONArrayToList(final JSONArray jsonArray) throws JSONException {
+ final List<String> list = new ArrayList<>();
+
+ for (int i = 0; i < jsonArray.length(); i++) {
+ final Object item = jsonArray.get(i);
+
+ if (item != null) {
+ list.add(item.toString());
+ }
+ }
+
+ return list;
+ }
+
@Override
public Row getRow() {
return _row;
}
-
}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/0ec8610c/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
deleted file mode 100644
index aac8249..0000000
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
+++ /dev/null
@@ -1,126 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class ColumnTypeResolver {
- private static final Logger logger = LoggerFactory.getLogger(ColumnTypeResolver.class);
- private final JSONObject _jsonObject;
- private final List<String> _columnNames = new ArrayList<>();
- private final List<ColumnType> _columnTypes = new ArrayList<>();
-
- public ColumnTypeResolver(final JSONObject jsonObject, final String[] columnNamesArray) {
- _jsonObject = jsonObject;
- _columnNames.addAll(Arrays.asList(columnNamesArray));
- }
-
- public ColumnType[] getColumnTypes() {
- try {
- fillColumnTypesFromMetadata();
- fillColumnTypesFromData();
- } catch (final JSONException e) {
- // ignore missing data
- }
-
- fillColumnTypesFromRemainingColumns();
- return _columnTypes.toArray(new ColumnType[_columnTypes.size()]);
- }
-
- private void fillColumnTypesFromData() throws JSONException {
- final String dataKey = "data";
-
- if (_jsonObject.has(dataKey)) {
- final JSONObject data = _jsonObject.getJSONObject(dataKey);
- final Iterator<?> keysIterator = data.keys();
-
- while (keysIterator.hasNext()) {
- final String key = (String) keysIterator.next();
- final ColumnType type = getTypeFromValue(data, key);
- _columnTypes.add(type);
- removeIfAvailable(_columnNames, key);
- }
- }
- }
-
- private void fillColumnTypesFromMetadata() throws JSONException {
- final String metadataKey = "metadata";
-
- if (_jsonObject.has(metadataKey)) {
- final JSONObject metadata = _jsonObject.getJSONObject(metadataKey);
-
- if (metadata.has("id")) {
- _columnTypes.add(ColumnType.BIGINT);
- removeIfAvailable(_columnNames, "_id");
- }
- }
- }
-
- private void fillColumnTypesFromRemainingColumns() {
- for (final String remainingColumnName : _columnNames) {
- if (remainingColumnName.contains("rel_")) {
- if (remainingColumnName.contains("#")) {
- _columnTypes.add(ColumnType.LIST);
- } else {
- _columnTypes.add(ColumnType.BIGINT);
- }
- } else {
- _columnTypes.add(ColumnType.STRING);
- }
- }
- }
-
- private void removeIfAvailable(final List<String> list, final String key) {
- if (list.contains(key)) {
- list.remove(key);
- }
- }
-
- private ColumnType getTypeFromValue(final JSONObject data, final String key) {
- try {
- final Class<? extends Object> keyClass = data.get(key).getClass();
-
- if (keyClass.equals(Boolean.class)) {
- return ColumnType.BOOLEAN;
- } else if (keyClass.equals(Integer.class)) {
- return ColumnType.INTEGER;
- } else if (keyClass.equals(Long.class)) {
- return ColumnType.BIGINT;
- } else if (keyClass.equals(Double.class)) {
- return ColumnType.DOUBLE;
- } else if (keyClass.equals(JSONArray.class)) {
- return ColumnType.LIST;
- }
- } catch (final JSONException e) {
- logger.error("JSON object does not contain required key '{}'. {}", key, e.getMessage());
- }
-
- return ColumnType.STRING;
- }
-}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/0ec8610c/neo4j/src/test/java/org/apache/metamodel/neo4j/ColumnTypeResolverTest.java
----------------------------------------------------------------------
diff --git a/neo4j/src/test/java/org/apache/metamodel/neo4j/ColumnTypeResolverTest.java b/neo4j/src/test/java/org/apache/metamodel/neo4j/ColumnTypeResolverTest.java
new file mode 100644
index 0000000..6615acf
--- /dev/null
+++ b/neo4j/src/test/java/org/apache/metamodel/neo4j/ColumnTypeResolverTest.java
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.metamodel.neo4j;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.apache.metamodel.neo4j.Neo4jDataContext.*;
+
+import org.apache.metamodel.schema.ColumnType;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Test;
+
+public class ColumnTypeResolverTest {
+ private static final String COLUMN_BOOLEAN = "boolean";
+ private static final String COLUMN_INTEGER = "integer";
+ private static final String COLUMN_LONG = "long";
+ private static final String COLUMN_DOUBLE = "double";
+ private static final String COLUMN_ARRAY = "array";
+ private static final String COLUMN_STRING = "string";
+
+ @Test
+ public void testGetColumnTypes() throws Exception {
+ final JSONObject jsonObject = createJSONObject();
+ final String[] columnNames =
+ new String[] { NEO4J_COLUMN_NAME_ID, COLUMN_BOOLEAN, COLUMN_INTEGER, COLUMN_LONG, COLUMN_DOUBLE,
+ COLUMN_ARRAY, COLUMN_STRING };
+ final ColumnTypeResolver resolver = new ColumnTypeResolver(jsonObject, columnNames);
+ final ColumnType[] columnTypes = resolver.getColumnTypes();
+ assertEquals(columnTypes.length, columnNames.length);
+ assertEquals(columnTypes[0], ColumnType.BIGINT); // ID
+ assertEquals(columnTypes[1], ColumnType.BOOLEAN);
+ assertEquals(columnTypes[2], ColumnType.STRING);
+ assertEquals(columnTypes[3], ColumnType.LIST);
+ assertEquals(columnTypes[4], ColumnType.DOUBLE);
+ assertEquals(columnTypes[5], ColumnType.INTEGER);
+ assertEquals(columnTypes[6], ColumnType.BIGINT);
+ }
+
+ private JSONObject createJSONObject() throws JSONException {
+ final JSONObject json = new JSONObject();
+ final JSONObject metadata = new JSONObject();
+ metadata.put(NEO4J_KEY_ID, 42L);
+ json.put(NEO4J_KEY_METADATA, metadata);
+ final JSONObject data = new JSONObject();
+ data.put(COLUMN_BOOLEAN, true);
+ data.put(COLUMN_STRING, "forty-two");
+ final JSONArray array = new JSONArray();
+ array.put(1).put(2).put(3);
+ data.put(COLUMN_ARRAY, array);
+ data.put(COLUMN_DOUBLE, 3.141592);
+ data.put(COLUMN_INTEGER, 42);
+ final JSONObject map = new JSONObject();
+ map.put("1", "one").put("2", "two").put("3", "three");
+ data.put(COLUMN_LONG, 12345678910L);
+ json.put(NEO4J_KEY_DATA, data);
+
+ return json;
+ }
+}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/0ec8610c/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
----------------------------------------------------------------------
diff --git a/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java b/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
deleted file mode 100644
index 3dce4b2..0000000
--- a/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.metamodel.neo4j.utils;
-
-import static junit.framework.TestCase.assertEquals;
-
-import org.apache.metamodel.schema.ColumnType;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.junit.Test;
-
-public class ColumnTypeResolverTest {
- private static final String COLUMN_ID = "id";
- private static final String COLUMN_ID_NEO4J = "_id";
- private static final String COLUMN_METADATA = "metadata";
- private static final String COLUMN_DATA = "data";
- private static final String COLUMN_BOOLEAN = "boolean";
- private static final String COLUMN_INTEGER = "integer";
- private static final String COLUMN_LONG = "long";
- private static final String COLUMN_DOUBLE = "double";
- private static final String COLUMN_ARRAY = "array";
- private static final String COLUMN_STRING = "string";
-
- @Test
- public void testGetColumnTypes() throws Exception {
- final JSONObject jsonObject = createJSONObject();
- final String[] columnNames =
- new String[] { COLUMN_ID_NEO4J, COLUMN_BOOLEAN, COLUMN_INTEGER, COLUMN_LONG, COLUMN_DOUBLE,
- COLUMN_ARRAY, COLUMN_STRING };
- final ColumnTypeResolver resolver = new ColumnTypeResolver(jsonObject, columnNames);
- final ColumnType[] columnTypes = resolver.getColumnTypes();
- assertEquals(columnTypes.length, columnNames.length);
- assertEquals(columnTypes[0], ColumnType.BIGINT); // ID
- assertEquals(columnTypes[1], ColumnType.BOOLEAN);
- assertEquals(columnTypes[2], ColumnType.STRING);
- assertEquals(columnTypes[3], ColumnType.LIST);
- assertEquals(columnTypes[4], ColumnType.DOUBLE);
- assertEquals(columnTypes[5], ColumnType.INTEGER);
- assertEquals(columnTypes[6], ColumnType.BIGINT);
- }
-
- private JSONObject createJSONObject() throws JSONException {
- final JSONObject json = new JSONObject();
- final JSONObject metadata = new JSONObject();
- metadata.put(COLUMN_ID, 42L);
- json.put(COLUMN_METADATA, metadata);
- final JSONObject data = new JSONObject();
- data.put(COLUMN_BOOLEAN, true);
- data.put(COLUMN_STRING, "forty-two");
- final JSONArray array = new JSONArray();
- array.put(1).put(2).put(3);
- data.put(COLUMN_ARRAY, array);
- data.put(COLUMN_DOUBLE, 3.141592);
- data.put(COLUMN_INTEGER, 42);
- final JSONObject map = new JSONObject();
- map.put("1", "one").put("2", "two").put("3", "three");
- data.put(COLUMN_LONG, 12345678910L);
- json.put(COLUMN_DATA, data);
-
- return json;
- }
-}
[7/7] metamodel git commit: Refactoring: New Neo4jDataContext.NEO4J_*
constants are not public anymore (package private).
Posted by ar...@apache.org.
Refactoring: New Neo4jDataContext.NEO4J_* constants are not public anymore (package private).
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/71b57403
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/71b57403
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/71b57403
Branch: refs/heads/master
Commit: 71b57403b9f7e61182a99f7ac24ccff4e00bb134
Parents: 0ec8610
Author: jakub <j....@quadient.com>
Authored: Tue May 29 10:48:36 2018 +0200
Committer: jakub <j....@quadient.com>
Committed: Tue May 29 10:48:36 2018 +0200
----------------------------------------------------------------------
.../metamodel/neo4j/Neo4jDataContext.java | 21 ++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/metamodel/blob/71b57403/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
index 2f8340a..10a5244 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
@@ -54,16 +54,17 @@ import org.slf4j.LoggerFactory;
public class Neo4jDataContext extends QueryPostprocessDataContext implements DataContext, DocumentSourceProvider {
public static final String SCHEMA_NAME = "neo4j";
public static final int DEFAULT_PORT = 7474;
- public static final String NEO4J_KEY_METADATA = "metadata";
- public static final String NEO4J_KEY_METADATA_TYPE = "type";
- public static final String NEO4J_KEY_PROPERTIES = "properties";
- public static final String NEO4J_KEY_DATA = "data";
- public static final String NEO4J_KEY_ID = "id";
- public static final String NEO4J_KEY_RESPONSE_RESULTS = "results";
- public static final String NEO4J_KEY_RESPONSE_ROW = "row";
- public static final String NEO4J_COLUMN_NAME_ID = "_id";
- public static final String NEO4J_COLUMN_NAME_RELATION_PREFIX = "rel_";
- public static final String NEO4J_COLUMN_NAME_RELATION_LIST_INDICATOR = "#";
+
+ static final String NEO4J_KEY_METADATA = "metadata";
+ static final String NEO4J_KEY_METADATA_TYPE = "type";
+ static final String NEO4J_KEY_PROPERTIES = "properties";
+ static final String NEO4J_KEY_DATA = "data";
+ static final String NEO4J_KEY_ID = "id";
+ static final String NEO4J_KEY_RESPONSE_RESULTS = "results";
+ static final String NEO4J_KEY_RESPONSE_ROW = "row";
+ static final String NEO4J_COLUMN_NAME_ID = "_id";
+ static final String NEO4J_COLUMN_NAME_RELATION_PREFIX = "rel_";
+ static final String NEO4J_COLUMN_NAME_RELATION_LIST_INDICATOR = "#";
private static final Logger logger = LoggerFactory.getLogger(Neo4jDataContext.class);
[5/7] metamodel git commit: Refactoring. Column type MAP was removed
and ARRAY was replaced by LIST.
Posted by ar...@apache.org.
Refactoring.
Column type MAP was removed and ARRAY was replaced by LIST.
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/f1457a15
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/f1457a15
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/f1457a15
Branch: refs/heads/master
Commit: f1457a15c3f1c32ff9a382ba81ec3ddafd76f326
Parents: e0a821c
Author: jakub <j....@quadient.com>
Authored: Fri May 25 08:01:21 2018 +0200
Committer: jakub <j....@quadient.com>
Committed: Fri May 25 08:01:21 2018 +0200
----------------------------------------------------------------------
.../metamodel/neo4j/Neo4jDataContext.java | 5 +-
.../neo4j/utils/ColumnTypeResolver.java | 58 ++++++++++----------
.../neo4j/utils/ColumnTypeResolverTest.java | 13 ++---
3 files changed, 36 insertions(+), 40 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/metamodel/blob/f1457a15/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
index 524d29c..662807d 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
@@ -198,9 +198,8 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
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();
- return new SimpleTableDef(label, columnNames,
- columnTypeResolver.getColumnTypes(nodesPerLabel.get(0), columnNames));
+ final ColumnTypeResolver columnTypeResolver = new ColumnTypeResolver(nodesPerLabel.get(0), columnNames);
+ return new SimpleTableDef(label, columnNames, columnTypeResolver.getColumnTypes());
}
}
http://git-wip-us.apache.org/repos/asf/metamodel/blob/f1457a15/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
index f5a817b..aac8249 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java
@@ -32,64 +32,66 @@ import org.slf4j.LoggerFactory;
public class ColumnTypeResolver {
private static final Logger logger = LoggerFactory.getLogger(ColumnTypeResolver.class);
-
- public ColumnType[] getColumnTypes(final JSONObject jsonObject, final String[] columnNamesArray) {
- final List<String> columnNames = new ArrayList<>(Arrays.asList(columnNamesArray));
- final List<ColumnType> columnTypes = new ArrayList<>();
+ private final JSONObject _jsonObject;
+ private final List<String> _columnNames = new ArrayList<>();
+ private final List<ColumnType> _columnTypes = new ArrayList<>();
+ public ColumnTypeResolver(final JSONObject jsonObject, final String[] columnNamesArray) {
+ _jsonObject = jsonObject;
+ _columnNames.addAll(Arrays.asList(columnNamesArray));
+ }
+
+ public ColumnType[] getColumnTypes() {
try {
- fillColumnTypesFromMetadata(jsonObject, columnNames, columnTypes);
- fillColumnTypesFromData(jsonObject, columnNames, columnTypes);
+ fillColumnTypesFromMetadata();
+ fillColumnTypesFromData();
} catch (final JSONException e) {
// ignore missing data
}
- fillColumnTypesFromRemainingColumns(columnNames, columnTypes);
- return columnTypes.toArray(new ColumnType[columnTypes.size()]);
+ fillColumnTypesFromRemainingColumns();
+ return _columnTypes.toArray(new ColumnType[_columnTypes.size()]);
}
- private void fillColumnTypesFromData(final JSONObject jsonObject, final List<String> columnNames,
- final List<ColumnType> columnTypes) throws JSONException {
+ private void fillColumnTypesFromData() throws JSONException {
final String dataKey = "data";
- if (jsonObject.has(dataKey)) {
- final JSONObject data = jsonObject.getJSONObject(dataKey);
+ if (_jsonObject.has(dataKey)) {
+ final JSONObject data = _jsonObject.getJSONObject(dataKey);
final Iterator<?> keysIterator = data.keys();
while (keysIterator.hasNext()) {
final String key = (String) keysIterator.next();
final ColumnType type = getTypeFromValue(data, key);
- columnTypes.add(type);
- removeIfAvailable(columnNames, key);
+ _columnTypes.add(type);
+ removeIfAvailable(_columnNames, key);
}
}
}
- private void fillColumnTypesFromMetadata(final JSONObject jsonObject, final List<String> columnNames,
- final List<ColumnType> columnTypes) throws JSONException {
+ private void fillColumnTypesFromMetadata() throws JSONException {
final String metadataKey = "metadata";
- if (jsonObject.has(metadataKey)) {
- final JSONObject metadata = jsonObject.getJSONObject(metadataKey);
+ if (_jsonObject.has(metadataKey)) {
+ final JSONObject metadata = _jsonObject.getJSONObject(metadataKey);
if (metadata.has("id")) {
- columnTypes.add(ColumnType.BIGINT);
- removeIfAvailable(columnNames, "_id");
+ _columnTypes.add(ColumnType.BIGINT);
+ removeIfAvailable(_columnNames, "_id");
}
}
}
- private void fillColumnTypesFromRemainingColumns(final List<String> columnNames,
- final List<ColumnType> columnTypes) {
- for (final String remainingColumnName : columnNames) {
+ private void fillColumnTypesFromRemainingColumns() {
+ for (final String remainingColumnName : _columnNames) {
if (remainingColumnName.contains("rel_")) {
if (remainingColumnName.contains("#")) {
- columnTypes.add(ColumnType.ARRAY);
+ _columnTypes.add(ColumnType.LIST);
} else {
- columnTypes.add(ColumnType.BIGINT);
+ _columnTypes.add(ColumnType.BIGINT);
}
} else {
- columnTypes.add(ColumnType.STRING);
+ _columnTypes.add(ColumnType.STRING);
}
}
}
@@ -113,9 +115,7 @@ public class ColumnTypeResolver {
} else if (keyClass.equals(Double.class)) {
return ColumnType.DOUBLE;
} else if (keyClass.equals(JSONArray.class)) {
- return ColumnType.ARRAY;
- } else if (keyClass.equals(JSONObject.class)) {
- return ColumnType.MAP;
+ return ColumnType.LIST;
}
} catch (final JSONException e) {
logger.error("JSON object does not contain required key '{}'. {}", key, e.getMessage());
http://git-wip-us.apache.org/repos/asf/metamodel/blob/f1457a15/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
----------------------------------------------------------------------
diff --git a/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java b/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
index 8e56b66..3dce4b2 100644
--- a/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
+++ b/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java
@@ -36,26 +36,24 @@ public class ColumnTypeResolverTest {
private static final String COLUMN_LONG = "long";
private static final String COLUMN_DOUBLE = "double";
private static final String COLUMN_ARRAY = "array";
- private static final String COLUMN_MAP = "map";
private static final String COLUMN_STRING = "string";
@Test
public void testGetColumnTypes() throws Exception {
- final ColumnTypeResolver resolver = new ColumnTypeResolver();
final JSONObject jsonObject = createJSONObject();
final String[] columnNames =
new String[] { COLUMN_ID_NEO4J, COLUMN_BOOLEAN, COLUMN_INTEGER, COLUMN_LONG, COLUMN_DOUBLE,
- COLUMN_ARRAY, COLUMN_MAP, COLUMN_STRING };
- final ColumnType[] columnTypes = resolver.getColumnTypes(jsonObject, columnNames);
+ COLUMN_ARRAY, COLUMN_STRING };
+ final ColumnTypeResolver resolver = new ColumnTypeResolver(jsonObject, columnNames);
+ final ColumnType[] columnTypes = resolver.getColumnTypes();
assertEquals(columnTypes.length, columnNames.length);
assertEquals(columnTypes[0], ColumnType.BIGINT); // ID
assertEquals(columnTypes[1], ColumnType.BOOLEAN);
assertEquals(columnTypes[2], ColumnType.STRING);
- assertEquals(columnTypes[3], ColumnType.ARRAY);
+ assertEquals(columnTypes[3], ColumnType.LIST);
assertEquals(columnTypes[4], ColumnType.DOUBLE);
assertEquals(columnTypes[5], ColumnType.INTEGER);
- assertEquals(columnTypes[6], ColumnType.MAP);
- assertEquals(columnTypes[7], ColumnType.BIGINT);
+ assertEquals(columnTypes[6], ColumnType.BIGINT);
}
private JSONObject createJSONObject() throws JSONException {
@@ -73,7 +71,6 @@ public class ColumnTypeResolverTest {
data.put(COLUMN_INTEGER, 42);
final JSONObject map = new JSONObject();
map.put("1", "one").put("2", "two").put("3", "three");
- data.put(COLUMN_MAP, map);
data.put(COLUMN_LONG, 12345678910L);
json.put(COLUMN_DATA, data);
[3/7] metamodel git commit: Node ID from metadata is Integer,
not Long.
Posted by ar...@apache.org.
Node ID from metadata is Integer, not Long.
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/59ec9ccb
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/59ec9ccb
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/59ec9ccb
Branch: refs/heads/master
Commit: 59ec9ccbd569b33a3281a7adc2153e244e4f8a5d
Parents: b60601e
Author: jakub <j....@quadient.com>
Authored: Tue May 22 13:22:20 2018 +0200
Committer: jakub <j....@quadient.com>
Committed: Tue May 22 13:22:20 2018 +0200
----------------------------------------------------------------------
.../main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/metamodel/blob/59ec9ccb/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
----------------------------------------------------------------------
diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
index 83f01e7..b23f1c9 100644
--- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
+++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java
@@ -183,7 +183,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
final Set<String> relationshipPropertiesPerLabel = new LinkedHashSet<>();
for (final JSONObject node : nodesPerLabel) {
- final Long nodeId = (Long) node.getJSONObject("metadata").get("id");
+ final Integer nodeId = (Integer) node.getJSONObject("metadata").get("id");
fillRelationshipPropertiesPerLabel(nodeId, relationshipPropertiesPerLabel);
}
@@ -199,7 +199,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
}
}
- private void fillRelationshipPropertiesPerLabel(final Long nodeId, final Set<String> relationshipPropertiesPerLabel)
+ private void fillRelationshipPropertiesPerLabel(final Integer nodeId, final Set<String> relationshipPropertiesPerLabel)
throws JSONException {
final List<JSONObject> relationshipsPerNode = getOutgoingRelationshipsPerNode(nodeId);
@@ -256,7 +256,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat
}
}
- private List<JSONObject> getOutgoingRelationshipsPerNode(final Long nodeId) {
+ private List<JSONObject> getOutgoingRelationshipsPerNode(final Integer nodeId) {
List<JSONObject> outgoingRelationshipsPerNode = new ArrayList<>();
String outgoingRelationshipsPerNodeJsonString = _requestWrapper.executeRestRequest(new HttpGet(_serviceRoot