Allow filtering by reserved properties 23/30523/1
authorDaniel Silverthorn <daniel.silverthorn@amdocs.com>
Tue, 6 Feb 2018 20:19:01 +0000 (15:19 -0500)
committerDaniel Silverthorn <daniel.silverthorn@amdocs.com>
Tue, 6 Feb 2018 20:23:57 +0000 (15:23 -0500)
Allow filtering by reserved properties and return them in group and single gets

Change-Id: I897a6e807d93f6fe9f6875519dd433e52c2a1705
Issue-ID: AAI-702
Signed-off-by: Daniel Silverthorn <daniel.silverthorn@amdocs.com>
src/main/java/org/onap/crud/dao/GraphDao.java
src/main/java/org/onap/crud/dao/champ/ChampDao.java
src/main/java/org/onap/crud/entity/Vertex.java
src/main/java/org/onap/crud/service/AbstractGraphDataService.java
src/main/java/org/onap/crud/service/CrudRestService.java

index 7834bb2..283e1a1 100644 (file)
@@ -60,7 +60,7 @@ public interface GraphDao {
    * @return - A collection of vertices.
    * @throws CrudException
    */
-  public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException;
+  public List<Vertex> getVertices(String type, Map<String, Object> filter, String version) throws CrudException;
 
   /**
    * Retrieve a collection of {@link Vertex} objects which match the supplied
@@ -75,7 +75,7 @@ public interface GraphDao {
    * @return - A collection of vertices.
    * @throws CrudException
    */
-  public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties) throws CrudException;
+  public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException;
 
   /**
    * Retrieve an {@link Edge} from the graph database by specifying its unique
index cd0e66f..7174bfc 100644 (file)
@@ -160,12 +160,12 @@ public class ChampDao implements GraphDao {
   }
 
   @Override
-  public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException {
-    return getVertices(type, filter, new HashSet<String>());
+  public List<Vertex> getVertices(String type, Map<String, Object> filter, String version) throws CrudException {
+    return getVertices(type, filter, new HashSet<String>(), version);
   }
 
   @Override
-  public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties) throws CrudException {
+  public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException {
     filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
 
     List<NameValuePair> queryParams = convertToNameValuePair(filter);
@@ -176,8 +176,7 @@ public class ChampDao implements GraphDao {
     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
 
     if (getResult.getResultCode() == 200) {
-      return champGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>() {
-      }.getType());
+      return Vertex.collectionFromJson(getResult.getResult(), version);
     } else {
       // We didn't find a vertex with the supplied id, so just throw an
       // exception.
index 8fddaa3..8ef1c99 100644 (file)
@@ -30,7 +30,9 @@ import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import com.google.gson.annotations.SerializedName;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
@@ -39,6 +41,7 @@ import org.eclipse.persistence.dynamic.DynamicType;
 import org.eclipse.persistence.internal.helper.DatabaseField;
 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
 import org.eclipse.persistence.mappings.DatabaseMapping;
+import org.json.JSONArray;
 import org.json.JSONObject;
 import org.onap.aaiutils.oxm.OxmModelLoader;
 import org.onap.crud.exception.CrudException;
@@ -104,12 +107,16 @@ public class Vertex {
   }
 
   public static Vertex fromJson(String jsonString, String version) throws CrudException {
+    JSONObject doc = new JSONObject(jsonString);
+    return fromJson(doc, version);
+  }
+
+  public static Vertex fromJson(JSONObject jsonObject, String version) throws CrudException {
     Builder builder;
 
     try {
-      JSONObject doc = new JSONObject(jsonString);
-      String type = doc.getString("type");
-      builder = new Builder(type).id(doc.getString("key"));
+      String type = jsonObject.getString("type");
+      builder = new Builder(type).id(jsonObject.getString("key"));
       
       type = OxmModelValidator.resolveCollectionType(version, type);
       DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
@@ -123,8 +130,8 @@ public class Vertex {
         throw new CrudException("Unable to load oxm version", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
       }
 
-      if (doc.has("properties")) {
-        JSONObject jsonProps = doc.getJSONObject("properties");
+      if (jsonObject.has("properties")) {
+        JSONObject jsonProps = jsonObject.getJSONObject("properties");
         for (String key : (Set<String>)jsonProps.keySet()) {
           String keyJavaName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key);
           DatabaseMapping mapping = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName);
@@ -143,12 +150,23 @@ public class Vertex {
       }
     }
     catch (Exception ex) {
-      throw new CrudException("Unable to transform response: " + jsonString, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
+      throw new CrudException("Unable to transform response: " + jsonObject.toString(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
     }
     
     return builder.build(); 
   }
 
+  public static List<Vertex> collectionFromJson(String jsonString, String version) throws CrudException {
+    List<Vertex> result = new ArrayList<>();
+    JSONArray array = new JSONArray(jsonString);
+
+    for (Object jsonObject : array) {
+      result.add(Vertex.fromJson((JSONObject)jsonObject, version));
+    }
+
+    return result;
+  }
+
   @Override
   public String toString() {
     return "Vertex [id=" + id + ", type=" + type + ", properties=" + properties + "]";
index 5ad4b7d..5154308 100644 (file)
@@ -74,7 +74,7 @@ public abstract class AbstractGraphDataService {
 
   public String getVertices(String version, String type, Map<String, String> filter, HashSet<String> properties) throws CrudException {
     type = OxmModelValidator.resolveCollectionType(version, type);
-    List<Vertex> items = daoForGet.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter), properties);
+    List<Vertex> items = daoForGet.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter), properties, version);
     return CrudResponseBuilder.buildGetVerticesResponse(items, version);
   }
   
index 895b459..b3c5e7a 100644 (file)
@@ -9,19 +9,18 @@
  * Licensed 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
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
+ *
+ *    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.
  * ============LICENSE_END=========================================================
- * <p>
+ *
  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
-
 package org.onap.crud.service;
 
 import com.google.gson.JsonElement;