From 58c89ea1f45ffd4a9f812ae1c18e93b636737f55 Mon Sep 17 00:00:00 2001 From: Gurjeet Bedi Date: Wed, 14 Feb 2018 13:56:58 -0500 Subject: For supporting t_k query Passing queryparameters from GET Issue-ID: AAI-482 Change-Id: I7db51c6549fe5f7e4d61ad96121e7da3a57ff325 Signed-off-by: Gurjeet Bedi --- src/main/java/org/onap/crud/dao/GraphDao.java | 12 ++++-- .../java/org/onap/crud/dao/champ/ChampDao.java | 50 ++++++++++++++++------ 2 files changed, 45 insertions(+), 17 deletions(-) (limited to 'src/main/java/org/onap/crud/dao') diff --git a/src/main/java/org/onap/crud/dao/GraphDao.java b/src/main/java/org/onap/crud/dao/GraphDao.java index 283e1a1..3ae8266 100644 --- a/src/main/java/org/onap/crud/dao/GraphDao.java +++ b/src/main/java/org/onap/crud/dao/GraphDao.java @@ -36,7 +36,7 @@ public interface GraphDao { public Vertex getVertex(String id, String version) throws CrudException; - public Vertex getVertex(String id, String type, String version) throws CrudException; + public Vertex getVertex(String id, String type, String version, Map queryParams) throws CrudException; /** * Retrieve all of the edges which are incident to the vertex with the @@ -44,10 +44,12 @@ public interface GraphDao { * * @param id * - The unique identifier of the vertex to retrieve the edges for. + * @param queryParams + * - query parameters to be passed * @return - A collection of edges. * @throws CrudException */ - public List getVertexEdges(String id) throws CrudException; + public List getVertexEdges(String id, Map queryParams) throws CrudException; /** * Retrieve a collection of {@link Vertex} objects which match the supplied @@ -83,10 +85,14 @@ public interface GraphDao { * * @param id * - The unique identifier for the Edge to be retrieved. + * @param type + * - The type that we want to retrieve. + * @param queryParams + * - query parameters to be passed * @return - The Edge corresponding to the specified identifier. * @throws CrudException */ - public Edge getEdge(String id, String type) throws CrudException; + public Edge getEdge(String id, String type, Map queryParams) throws CrudException; /** * Retrieve a collection of {@link Edge} objects with a given type and which diff --git a/src/main/java/org/onap/crud/dao/champ/ChampDao.java b/src/main/java/org/onap/crud/dao/champ/ChampDao.java index 7174bfc..5c6dbbe 100644 --- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java @@ -120,9 +120,17 @@ public class ChampDao implements GraphDao { } @Override - public Vertex getVertex(String id, String type, String version) throws CrudException { - String url = baseObjectUrl + "/" + id; - OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); + public Vertex getVertex(String id, String type, String version, Map queryParams) throws CrudException { + StringBuilder strBuild = new StringBuilder(baseObjectUrl); + strBuild.append("/"); + strBuild.append(id); + if(queryParams != null && !queryParams.isEmpty()) + { + strBuild.append("?"); + strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset())); + } + + OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { Vertex vert = Vertex.fromJson(getResult.getResult(), version); @@ -143,11 +151,18 @@ public class ChampDao implements GraphDao { } @Override - public List getVertexEdges(String id) throws CrudException { - String url = baseObjectUrl + "/relationships/" + id; - - OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); + public List getVertexEdges(String id, Map queryParams) throws CrudException { + StringBuilder strBuild = new StringBuilder(baseObjectUrl); + strBuild.append("/relationships/"); + strBuild.append(id); + if(queryParams != null && !queryParams.isEmpty()) + { + strBuild.append("?"); + strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset())); + } + OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); + if (getResult.getResultCode() == 200) { return champGson.fromJson(getResult.getResult(), new TypeToken>() { }.getType()); @@ -186,10 +201,17 @@ public class ChampDao implements GraphDao { } @Override - public Edge getEdge(String id, String type) throws CrudException { - String url = baseRelationshipUrl + "/" + id; - OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); - + public Edge getEdge(String id, String type, Map queryParams) throws CrudException { + StringBuilder strBuild = new StringBuilder(baseRelationshipUrl); + strBuild.append("/"); + strBuild.append(id); + if(queryParams != null && !queryParams.isEmpty()) + { + strBuild.append("?"); + strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset())); + } + OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); + if (getResult.getResultCode() == 200) { Edge edge = Edge.fromJson(getResult.getResult()); @@ -291,8 +313,8 @@ public class ChampDao implements GraphDao { String url = baseRelationshipUrl; // Try requests to ensure source and target exist in Champ - Vertex dbSource = getVertex(source.getId().get(), source.getType(), version); - Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version); + Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, new HashMap()); + Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, new HashMap()); Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget); properties.forEach(insertEdgeBuilder::property); @@ -557,7 +579,7 @@ public class ChampDao implements GraphDao { } // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient - private List convertToNameValuePair(Map pairs) { + private List convertToNameValuePair(Map pairs) { List nvpList = new ArrayList<>(pairs.size()); pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString()))); -- cgit 1.2.3-korg