Repository: metamodel Updated Branches: refs/heads/master 71b57403b -> 0f7a09296
Neo4j: code style update. Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/0f7a0929 Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/0f7a0929 Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/0f7a0929 Branch: refs/heads/master Commit: 0f7a09296e0f4cb987a2dd87d5c57dda962940a0 Parents: 71b5740 Author: jakub <[email protected]> Authored: Wed May 30 10:51:35 2018 +0200 Committer: jakub <[email protected]> Committed: Wed May 30 10:51:35 2018 +0200 ---------------------------------------------------------------------- .../metamodel/neo4j/ColumnTypeResolver.java | 14 +- .../neo4j/Neo4jCypherQueryBuilder.java | 39 +++--- .../metamodel/neo4j/Neo4jDataContext.java | 129 ++++++++++--------- .../apache/metamodel/neo4j/Neo4jDataSet.java | 22 ++-- .../metamodel/neo4j/Neo4jRequestWrapper.java | 56 ++++---- 5 files changed, 130 insertions(+), 130 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel/blob/0f7a0929/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 index 84be8d6..8ce4b68 100644 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/ColumnTypeResolver.java +++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/ColumnTypeResolver.java @@ -44,7 +44,7 @@ class ColumnTypeResolver { public ColumnType[] getColumnTypes() { final List<ColumnType> columnTypes = new ArrayList<>(); - + try { columnTypes.addAll(getColumnTypesFromMetadata()); columnTypes.addAll(getColumnTypesFromData()); @@ -58,7 +58,7 @@ class ColumnTypeResolver { 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(); @@ -70,13 +70,13 @@ class ColumnTypeResolver { 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); @@ -85,13 +85,13 @@ class ColumnTypeResolver { 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)) { @@ -103,7 +103,7 @@ class ColumnTypeResolver { columnTypes.add(ColumnType.STRING); } } - + return columnTypes; } http://git-wip-us.apache.org/repos/asf/metamodel/blob/0f7a0929/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 c880c92..6981385 100644 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jCypherQueryBuilder.java +++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jCypherQueryBuilder.java @@ -32,22 +32,22 @@ import org.apache.metamodel.schema.Table; public class Neo4jCypherQueryBuilder { - public static String buildSelectQuery(Table table, List<Column> columns, int firstRow, int maxRows) { - List<String> columnNames = columns.stream() - .map(col -> col.getName()) - .collect(Collectors.toList()); + public static String buildSelectQuery(final Table table, final List<Column> columns, final int firstRow, + final int maxRows) { + List<String> columnNames = columns.stream().map(Column::getName).collect(Collectors.toList()); return buildSelectQuery(table.getName(), columnNames, firstRow, maxRows); } - public static String buildSelectQuery(String tableName, List<String> columnNames, int firstRow, int maxRows) { - Map<String, String> returnClauseMap = new LinkedHashMap<>(); - Map<String, Integer> relationshipIndexMap = new LinkedHashMap<>(); + public static String buildSelectQuery(final String tableName, final List<String> columnNames, final int firstRow, + final int maxRows) { + final Map<String, String> returnClauseMap = new LinkedHashMap<>(); + final Map<String, Integer> relationshipIndexMap = new LinkedHashMap<>(); for (String columnName : columnNames) { if (columnName.startsWith(Neo4jDataContext.NEO4J_COLUMN_NAME_RELATION_PREFIX)) { columnName = columnName.replace(Neo4jDataContext.NEO4J_COLUMN_NAME_RELATION_PREFIX, ""); - String relationshipName; - String relationshipPropertyName; + final String relationshipName; + final String relationshipPropertyName; if (columnName.contains(Neo4jDataContext.NEO4J_COLUMN_NAME_RELATION_LIST_INDICATOR)) { String[] parsedColumnNameArray = @@ -59,7 +59,7 @@ public class Neo4jCypherQueryBuilder { relationshipPropertyName = "metamodel_neo4j_relationship_marker"; } - String relationshipAlias; + final String relationshipAlias; if (relationshipIndexMap.containsKey(relationshipName)) { relationshipAlias = "r" + relationshipIndexMap.get(relationshipName); } else { @@ -87,17 +87,17 @@ public class Neo4jCypherQueryBuilder { } } - StringBuilder cypherBuilder = new StringBuilder(); + final StringBuilder cypherBuilder = new StringBuilder(); cypherBuilder.append("MATCH (n:"); cypherBuilder.append(tableName); - for (Map.Entry<String, Integer> relationshipAliasEntry : relationshipIndexMap.entrySet()) { + for (final Map.Entry<String, Integer> relationshipAliasEntry : relationshipIndexMap.entrySet()) { cypherBuilder.append(") OPTIONAL MATCH (n)-[r" + relationshipAliasEntry.getValue() + ":" + relationshipAliasEntry.getKey() + "]->(r" + relationshipAliasEntry.getValue() + "_relationshipEndNode"); } cypherBuilder.append(") RETURN "); boolean addComma = false; - for (Map.Entry<String, String> returnClauseEntry : returnClauseMap.entrySet()) { + for (final Map.Entry<String, String> returnClauseEntry : returnClauseMap.entrySet()) { if (addComma) { cypherBuilder.append(","); } @@ -114,7 +114,7 @@ public class Neo4jCypherQueryBuilder { return cypherBuilder.toString(); } - public static String buildCountQuery(String tableName, List<FilterItem> whereItems) { + public static String buildCountQuery(final String tableName, final List<FilterItem> whereItems) { StringBuilder cypherBuilder = new StringBuilder(); cypherBuilder.append("MATCH (n:"); cypherBuilder.append(tableName); @@ -124,12 +124,12 @@ public class Neo4jCypherQueryBuilder { return cypherBuilder.toString(); } - private static String buildWhereClause(List<FilterItem> whereItems, String queryObjectHandle) { + private static String buildWhereClause(final List<FilterItem> whereItems, final String queryObjectHandle) { if ((whereItems != null) && (!whereItems.isEmpty())) { - StringBuilder whereClauseBuilder = new StringBuilder(); + final StringBuilder whereClauseBuilder = new StringBuilder(); whereClauseBuilder.append("WHERE "); - FilterItem firstWhereItem = whereItems.get(0); + final FilterItem firstWhereItem = whereItems.get(0); whereClauseBuilder.append(buildWhereClauseItem(firstWhereItem, queryObjectHandle)); for (int i = 1; i < whereItems.size(); i++) { @@ -144,8 +144,8 @@ public class Neo4jCypherQueryBuilder { } } - private static String buildWhereClauseItem(FilterItem whereItem, String queryObjectHandle) { - StringBuilder whereClauseItemBuilder = new StringBuilder(); + private static String buildWhereClauseItem(final FilterItem whereItem, final String queryObjectHandle) { + final StringBuilder whereClauseItemBuilder = new StringBuilder(); whereClauseItemBuilder.append(queryObjectHandle); whereClauseItemBuilder.append("."); whereClauseItemBuilder.append(whereItem.getSelectItem().getColumn().getName()); @@ -160,5 +160,4 @@ public class Neo4jCypherQueryBuilder { } return whereClauseItemBuilder.toString(); } - } http://git-wip-us.apache.org/repos/asf/metamodel/blob/0f7a0929/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 10a5244..db0703f 100644 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java +++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java @@ -54,7 +54,7 @@ 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; - + static final String NEO4J_KEY_METADATA = "metadata"; static final String NEO4J_KEY_METADATA_TYPE = "type"; static final String NEO4J_KEY_PROPERTIES = "properties"; @@ -73,7 +73,8 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat private final HttpHost _httpHost; private String _serviceRoot = "/db/data"; - public Neo4jDataContext(String hostname, int port, String username, String password, SimpleTableDef... tableDefs) { + public Neo4jDataContext(final String hostname, final int port, final String username, final String password, + final SimpleTableDef... tableDefs) { super(false); _httpHost = new HttpHost(hostname, port); final CloseableHttpClient httpClient = HttpClientBuilder.create().build(); @@ -81,8 +82,8 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat _tableDefs = tableDefs; } - public Neo4jDataContext(String hostname, int port, String username, String password, String serviceRoot, - SimpleTableDef... tableDefs) { + public Neo4jDataContext(final String hostname, final int port, final String username, final String password, + final String serviceRoot, final SimpleTableDef... tableDefs) { super(false); _httpHost = new HttpHost(hostname, port); final CloseableHttpClient httpClient = HttpClientBuilder.create().build(); @@ -91,7 +92,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat _serviceRoot = serviceRoot; } - public Neo4jDataContext(String hostname, int port, String username, String password) { + public Neo4jDataContext(final String hostname, final int port, final String username, final String password) { super(false); _httpHost = new HttpHost(hostname, port); final CloseableHttpClient httpClient = HttpClientBuilder.create().build(); @@ -99,7 +100,8 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat _tableDefs = detectTableDefs(); } - public Neo4jDataContext(String hostname, int port, String username, String password, String serviceRoot) { + public Neo4jDataContext(final String hostname, final int port, final String username, final String password, + final String serviceRoot) { super(false); _httpHost = new HttpHost(hostname, port); final CloseableHttpClient httpClient = HttpClientBuilder.create().build(); @@ -108,14 +110,15 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat _serviceRoot = serviceRoot; } - public Neo4jDataContext(String hostname, int port, CloseableHttpClient httpClient) { + public Neo4jDataContext(final String hostname, final int port, final CloseableHttpClient httpClient) { super(false); _httpHost = new HttpHost(hostname, port); _requestWrapper = new Neo4jRequestWrapper(httpClient, _httpHost, _serviceRoot); _tableDefs = detectTableDefs(); } - public Neo4jDataContext(String hostname, int port, CloseableHttpClient httpClient, String serviceRoot) { + public Neo4jDataContext(final String hostname, final int port, final CloseableHttpClient httpClient, + final String serviceRoot) { super(false); _httpHost = new HttpHost(hostname, port); _requestWrapper = new Neo4jRequestWrapper(httpClient, _httpHost, _serviceRoot); @@ -123,15 +126,16 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat _serviceRoot = serviceRoot; } - public Neo4jDataContext(String hostname, int port, CloseableHttpClient httpClient, SimpleTableDef... tableDefs) { + public Neo4jDataContext(final String hostname, final int port, final CloseableHttpClient httpClient, + final SimpleTableDef... tableDefs) { super(false); _httpHost = new HttpHost(hostname, port); _requestWrapper = new Neo4jRequestWrapper(httpClient, _httpHost, _serviceRoot); _tableDefs = tableDefs; } - public Neo4jDataContext(String hostname, int port, CloseableHttpClient httpClient, String serviceRoot, - SimpleTableDef... tableDefs) { + public Neo4jDataContext(final String hostname, final int port, final CloseableHttpClient httpClient, + final String serviceRoot, final SimpleTableDef... tableDefs) { super(false); _httpHost = new HttpHost(hostname, port); _requestWrapper = new Neo4jRequestWrapper(httpClient, _httpHost, _serviceRoot); @@ -146,8 +150,8 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat @Override protected Schema getMainSchema() throws MetaModelException { - MutableSchema schema = new MutableSchema(getMainSchemaName()); - for (SimpleTableDef tableDef : _tableDefs) { + final MutableSchema schema = new MutableSchema(getMainSchemaName()); + for (final SimpleTableDef tableDef : _tableDefs) { MutableTable table = tableDef.toTable().setSchema(schema); schema.addTable(table); } @@ -163,25 +167,25 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat 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++) { final SimpleTableDef tableDefFromLabel = createTableDefFromLabel(labelsJsonArray.getString(i)); - + if (tableDefFromLabel != null) { tableDefs.add(tableDefFromLabel); } } - + return tableDefs.toArray(new SimpleTableDef[tableDefs.size()]); } catch (final JSONException e) { logger.error("Error occurred in parsing JSON while detecting the schema: ", e); throw new IllegalStateException(e); } } - + private SimpleTableDef createTableDefFromLabel(final String label) throws JSONException { final List<JSONObject> nodesPerLabel = getAllNodesPerLabel(label); final List<String> propertiesPerLabel = getPropertiesFromLabelNodes(nodesPerLabel); @@ -192,16 +196,16 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat final Set<String> relationshipPropertiesForNode = createRelationshipPropertiesForNode(nodeId); relationshipPropertiesPerLabel.addAll(relationshipPropertiesForNode); } - + propertiesPerLabel.addAll(relationshipPropertiesPerLabel); - 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(nodesPerLabel.get(0), columnNames); return new SimpleTableDef(label, columnNames, columnTypeResolver.getColumnTypes()); - } + } } private Set<String> createRelationshipPropertiesForNode(final Integer nodeId) throws JSONException { @@ -213,18 +217,18 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat 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 final List<String> propertiesPerRelationship = getAllPropertiesPerRelationship(relationship); relationshipProperties.addAll(propertiesPerRelationship); } - + return relationshipProperties; } 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); @@ -234,7 +238,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat } } } - + return propertiesPerLabel; } @@ -245,7 +249,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat .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(); @@ -269,7 +273,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat 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++) { @@ -287,60 +291,60 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat } private List<JSONObject> getAllNodesPerLabel(String label) { - List<JSONObject> allNodesPerLabel = new ArrayList<JSONObject>(); - - String allNodesForLabelJsonString = _requestWrapper.executeRestRequest(new HttpGet(_serviceRoot + "/label/" - + label + "/nodes")); + final List<JSONObject> allNodesPerLabel = new ArrayList<>(); + final String allNodesForLabelJsonString = + _requestWrapper.executeRestRequest(new HttpGet(_serviceRoot + "/label/" + label + "/nodes")); + final JSONArray allNodesForLabelJsonArray; - JSONArray allNodesForLabelJsonArray; try { allNodesForLabelJsonArray = new JSONArray(allNodesForLabelJsonString); for (int i = 0; i < allNodesForLabelJsonArray.length(); i++) { - JSONObject node = allNodesForLabelJsonArray.getJSONObject(i); + final JSONObject node = allNodesForLabelJsonArray.getJSONObject(i); allNodesPerLabel.add(node); } return allNodesPerLabel; - } catch (JSONException e) { + } catch (final JSONException e) { logger.error("Error occurred in parsing JSON while detecting the nodes for a label: " + label, e); throw new IllegalStateException(e); } } - private List<String> getAllPropertiesPerNode(JSONObject node) { - List<String> properties = new ArrayList<String>(); + private List<String> getAllPropertiesPerNode(final JSONObject node) { + final List<String> properties = new ArrayList<>(); properties.add(NEO4J_COLUMN_NAME_ID); - - String propertiesEndpoint; + final String propertiesEndpoint; try { propertiesEndpoint = node.getString(NEO4J_KEY_PROPERTIES); - String allPropertiesPerNodeJsonString = _requestWrapper.executeRestRequest(new HttpGet(propertiesEndpoint)); + final String allPropertiesPerNodeJsonString = + _requestWrapper.executeRestRequest(new HttpGet(propertiesEndpoint)); + final JSONObject allPropertiesPerNodeJsonObject = new JSONObject(allPropertiesPerNodeJsonString); - JSONObject allPropertiesPerNodeJsonObject = new JSONObject(allPropertiesPerNodeJsonString); for (int j = 0; j < allPropertiesPerNodeJsonObject.length(); j++) { - JSONArray propertiesJsonArray = allPropertiesPerNodeJsonObject.names(); + final JSONArray propertiesJsonArray = allPropertiesPerNodeJsonObject.names(); for (int k = 0; k < propertiesJsonArray.length(); k++) { - String property = propertiesJsonArray.getString(k); + final String property = propertiesJsonArray.getString(k); properties.add(property); } } return properties; - } catch (JSONException e) { + } catch (final JSONException e) { logger.error("Error occurred in parsing JSON while detecting the properties of a node: " + node, e); throw new IllegalStateException(e); } } @Override - protected DataSet materializeMainSchemaTable(Table table, List<Column> columns, int firstRow, int maxRows) { + protected DataSet materializeMainSchemaTable(final Table table, final List<Column> columns, final int firstRow, + final int maxRows) { if ((columns != null) && (columns.size() > 0)) { - Neo4jDataSet dataSet = null; + final Neo4jDataSet dataSet; try { - String selectQuery = Neo4jCypherQueryBuilder.buildSelectQuery(table, columns, firstRow, maxRows); - String responseJSONString = _requestWrapper.executeCypherQuery(selectQuery); - JSONObject resultJSONObject = new JSONObject(responseJSONString); + final String selectQuery = Neo4jCypherQueryBuilder.buildSelectQuery(table, columns, firstRow, maxRows); + final String responseJSONString = _requestWrapper.executeCypherQuery(selectQuery); + final JSONObject resultJSONObject = new JSONObject(responseJSONString); final List<SelectItem> selectItems = columns.stream().map(SelectItem::new).collect(Collectors.toList()); dataSet = new Neo4jDataSet(selectItems, resultJSONObject); - } catch (JSONException e) { + } catch (final JSONException e) { logger.error("Error occurred in parsing JSON while materializing the schema: ", e); throw new IllegalStateException(e); } @@ -353,26 +357,27 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat } @Override - protected DataSet materializeMainSchemaTable(Table table, List<Column> columns, int maxRows) { + protected DataSet materializeMainSchemaTable(final Table table, final List<Column> columns, final int maxRows) { return materializeMainSchemaTable(table, columns, 1, maxRows); } @Override - protected Number executeCountQuery(Table table, List<FilterItem> whereItems, boolean functionApproximationAllowed) { - String countQuery = Neo4jCypherQueryBuilder.buildCountQuery(table.getName(), whereItems); - String jsonResponse = _requestWrapper.executeCypherQuery(countQuery); + protected Number executeCountQuery(final Table table, final List<FilterItem> whereItems, + final boolean functionApproximationAllowed) { + final String countQuery = Neo4jCypherQueryBuilder.buildCountQuery(table.getName(), whereItems); + final String jsonResponse = _requestWrapper.executeCypherQuery(countQuery); + final JSONObject jsonResponseObject; - JSONObject jsonResponseObject; try { jsonResponseObject = new JSONObject(jsonResponse); - JSONArray resultsJSONArray = jsonResponseObject.getJSONArray(NEO4J_KEY_RESPONSE_RESULTS); - JSONObject resultJSONObject = (JSONObject) resultsJSONArray.get(0); - JSONArray dataJSONArray = resultJSONObject.getJSONArray(NEO4J_KEY_DATA); - JSONObject rowJSONObject = (JSONObject) dataJSONArray.get(0); - JSONArray valueJSONArray = rowJSONObject.getJSONArray(NEO4J_KEY_RESPONSE_ROW); - Number value = (Number) valueJSONArray.get(0); + final JSONArray resultsJSONArray = jsonResponseObject.getJSONArray(NEO4J_KEY_RESPONSE_RESULTS); + final JSONObject resultJSONObject = (JSONObject) resultsJSONArray.get(0); + final JSONArray dataJSONArray = resultJSONObject.getJSONArray(NEO4J_KEY_DATA); + final JSONObject rowJSONObject = (JSONObject) dataJSONArray.get(0); + final JSONArray valueJSONArray = rowJSONObject.getJSONArray(NEO4J_KEY_RESPONSE_ROW); + final Number value = (Number) valueJSONArray.get(0); return value; - } catch (JSONException e) { + } catch (final JSONException 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 @@ -387,7 +392,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat } @Override - public DocumentSource getDocumentSourceForTable(String sourceCollectionName) { + public DocumentSource getDocumentSourceForTable(final String sourceCollectionName) { return null; } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/0f7a0929/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 9674f43..9f23917 100644 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java +++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataSet.java @@ -34,11 +34,11 @@ import org.json.JSONObject; final class Neo4jDataSet extends AbstractDataSet { - private JSONObject _resultJSONObject; + private final JSONObject _resultJSONObject; private int _currentRowIndex; private Row _row; - public Neo4jDataSet(List<SelectItem> selectItems, JSONObject resultJSONObject) { + public Neo4jDataSet(final List<SelectItem> selectItems, final JSONObject resultJSONObject) { super(selectItems); _resultJSONObject = resultJSONObject; _currentRowIndex = 0; @@ -48,29 +48,29 @@ final class Neo4jDataSet extends AbstractDataSet { public boolean next() { try { final JSONArray resultsArray = _resultJSONObject.getJSONArray(NEO4J_KEY_RESPONSE_RESULTS); - + if (resultsArray.length() > 0) { final JSONObject results = resultsArray.getJSONObject(0); final JSONArray data = results.getJSONArray(NEO4J_KEY_DATA); - + if (_currentRowIndex < data.length()) { final JSONObject row = data.getJSONObject(_currentRowIndex); final JSONArray jsonValues = row.getJSONArray(NEO4J_KEY_RESPONSE_ROW); final Object[] objectValues = new Object[jsonValues.length()]; - + for (int i = 0; i < jsonValues.length(); 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); _currentRowIndex++; - + return true; } } else { @@ -86,15 +86,15 @@ final class Neo4jDataSet extends AbstractDataSet { 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; } http://git-wip-us.apache.org/repos/asf/metamodel/blob/0f7a0929/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jRequestWrapper.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jRequestWrapper.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jRequestWrapper.java index 864a2b9..1661882 100644 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jRequestWrapper.java +++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jRequestWrapper.java @@ -24,7 +24,6 @@ import java.util.HashMap; import java.util.List; import org.apache.http.HttpHost; -import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; @@ -43,7 +42,7 @@ import com.google.common.io.BaseEncoding; /** * The class takes care of sending an {@link HttpRequestBase} or a Cypher query * to the specified Neo4j instance. Also takes care of the authentication. - * + * */ public class Neo4jRequestWrapper { @@ -55,8 +54,8 @@ public class Neo4jRequestWrapper { private final String _username; private final String _password; - public Neo4jRequestWrapper(CloseableHttpClient httpClient, HttpHost httpHost, String username, String password, - String serviceRoot) { + public Neo4jRequestWrapper(final CloseableHttpClient httpClient, final HttpHost httpHost, final String username, + final String password, final String serviceRoot) { _httpClient = httpClient; _httpHost = httpHost; _username = username; @@ -64,52 +63,50 @@ public class Neo4jRequestWrapper { _cypherQueryHttpPost = new HttpPost(serviceRoot + "/transaction/commit"); } - public Neo4jRequestWrapper(CloseableHttpClient httpClient, HttpHost httpHost, String serviceRoot) { + public Neo4jRequestWrapper(final CloseableHttpClient httpClient, final HttpHost httpHost, + final String serviceRoot) { this(httpClient, httpHost, null, null, serviceRoot); } - public String executeRestRequest(HttpRequestBase httpRequest) { + public String executeRestRequest(final HttpRequestBase httpRequest) { return executeRestRequest(httpRequest, _username, _password); } - public String executeRestRequest(HttpRequestBase httpRequest, String username, String password) { + public String executeRestRequest(final HttpRequestBase httpRequest, final String username, final String password) { if ((username != null) && (password != null)) { - String base64credentials = BaseEncoding.base64().encode( - (username + ":" + password).getBytes(StandardCharsets.UTF_8)); + String base64credentials = + BaseEncoding.base64().encode((username + ":" + password).getBytes(StandardCharsets.UTF_8)); httpRequest.addHeader("Authorization", "Basic " + base64credentials); } try { - CloseableHttpResponse response = _httpClient.execute(_httpHost, httpRequest); + final CloseableHttpResponse response = _httpClient.execute(_httpHost, httpRequest); if (response.getEntity() != null) { return EntityUtils.toString(response.getEntity()); } return null; - } catch (ClientProtocolException e) { - logger.error("An error occured while executing " + httpRequest, e); - throw new IllegalStateException(e); - } catch (IOException e) { - logger.error("An error occured while executing " + httpRequest, e); + } catch (final IOException e) { + logger.error("An error occurred while executing " + httpRequest, e); throw new IllegalStateException(e); } } - public String executeCypherQuery(String cypherQuery) { - JSONObject cypherQueryRequest = new JSONObject(); - HashMap<String, String> statement = new HashMap<String, String>(); + public String executeCypherQuery(final String cypherQuery) { + final JSONObject cypherQueryRequest = new JSONObject(); + final HashMap<String, String> statement = new HashMap<>(); statement.put("statement", cypherQuery); - JSONArray statementsArray = new JSONArray(); + final JSONArray statementsArray = new JSONArray(); statementsArray.put(statement); return executeRequest(cypherQueryRequest, statementsArray); } - public String executeCypherQueries(List<String> cypherQueries) { - JSONObject cypherQueryRequest = new JSONObject(); - JSONArray statementsArray = new JSONArray(); - for (String cypherQuery : cypherQueries) { - HashMap<String, String> statement = new HashMap<String, String>(); + public String executeCypherQueries(final List<String> cypherQueries) { + final JSONObject cypherQueryRequest = new JSONObject(); + final JSONArray statementsArray = new JSONArray(); + for (final String cypherQuery : cypherQueries) { + final HashMap<String, String> statement = new HashMap<>(); statement.put("statement", cypherQuery); statementsArray.put(statement); @@ -118,19 +115,18 @@ public class Neo4jRequestWrapper { return executeRequest(cypherQueryRequest, statementsArray); } - private String executeRequest(JSONObject cypherQueryRequest, JSONArray statementsArray) { + private String executeRequest(final JSONObject cypherQueryRequest, final JSONArray statementsArray) { try { cypherQueryRequest.put("statements", statementsArray); - String requestBody = cypherQueryRequest.toString(); + final String requestBody = cypherQueryRequest.toString(); _cypherQueryHttpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON)); - String responseJSONString = executeRestRequest(_cypherQueryHttpPost); + final String responseJSONString = executeRestRequest(_cypherQueryHttpPost); return responseJSONString; - } catch (JSONException e) { - logger.error("Error occured while constructing JSON request body for " + _cypherQueryHttpPost, e); + } catch (final JSONException e) { + logger.error("Error occurred while constructing JSON request body for " + _cypherQueryHttpPost, e); throw new IllegalStateException(e); } } - }
