Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / RawFormat.java
index 8636ebf..8dcd3a8 100644 (file)
@@ -25,9 +25,8 @@ import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 
-import java.util.Iterator;
-import java.util.List;
-import java.util.Optional;
+import java.util.*;
+import java.util.stream.Collectors;
 
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
@@ -38,6 +37,7 @@ import org.onap.aai.introspection.Loader;
 import org.onap.aai.serialization.db.DBSerializer;
 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
 import org.onap.aai.serialization.queryformats.params.Depth;
+import org.onap.aai.serialization.queryformats.params.AsTree;
 import org.onap.aai.serialization.queryformats.params.NodesOnly;
 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
 
@@ -55,6 +55,25 @@ public class RawFormat extends MultiFormatMapper {
         this.serializer = builder.getSerializer();
         this.depth = builder.getDepth();
         this.nodesOnly = builder.isNodesOnly();
+        this.isTree = builder.isTree();
+    }
+
+    @Override
+    public Optional<JsonObject> getJsonFromVertex(Vertex v, Map<String, List<String>> selectedProps) throws AAIFormatVertexException {
+        JsonObject json = new JsonObject();
+        json.addProperty("id", v.id().toString());
+        json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
+        json.addProperty("url", this.urlBuilder.pathed(v));
+        Optional<JsonObject> properties = this.createSelectedPropertiesObject(v, selectedProps);
+        if (properties.isPresent()) {
+            json.add("properties", properties.get());
+        } else {
+            return Optional.empty();
+        }
+        if (!nodesOnly) {
+            json.add("related-to", this.createRelationshipObject(v));
+        }
+        return Optional.of(json);
     }
 
     @Override
@@ -88,6 +107,48 @@ public class RawFormat extends MultiFormatMapper {
         return Optional.of(json);
     }
 
+    public Optional<JsonObject> createSelectedPropertiesObject(Vertex v, Map<String, List<String>> selectedProps) throws AAIFormatVertexException {
+        JsonObject json = new JsonObject();
+        String nodeType = v.<String>value(AAIProperties.NODE_TYPE);
+        Set<String> propList = removeSingleQuotesForProperties(selectedProps.get(nodeType));
+        Iterator<VertexProperty<Object>> iter = v.properties();
+
+        Gson gson = new Gson();
+        while (iter.hasNext()) {
+            VertexProperty<Object> prop = iter.next();
+            if (propList != null && !propList.isEmpty()) {
+                if (propList.contains(prop.label())) {
+                    if (prop.value() instanceof String) {
+                        json.addProperty(prop.key(), (String) prop.value());
+                    } else if (prop.value() instanceof Boolean) {
+                        json.addProperty(prop.key(), (Boolean) prop.value());
+                    } else if (prop.value() instanceof Number) {
+                        json.addProperty(prop.key(), (Number) prop.value());
+                    } else if (prop.value() instanceof List) {
+                        json.addProperty(prop.key(), gson.toJson(prop.value()));
+                    } else {
+                        // throw exception?
+                        return null;
+                    }
+                }
+            } else {
+                return this.createPropertiesObject(v);
+            }
+        }
+
+        return Optional.of(json);
+    }
+
+    private Set<String> removeSingleQuotesForProperties(List<String> props){
+        if (props != null && !props.isEmpty()) {
+            return props.stream().map(
+                e -> e.substring(1, e.length()-1)).collect(Collectors.toSet());
+        } else {
+            return Collections.emptySet();
+        }
+
+    }
+
     protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
         JsonArray jarray = new JsonArray();
         Iterator<Edge> inIter = v.edges(Direction.IN);
@@ -122,7 +183,7 @@ public class RawFormat extends MultiFormatMapper {
         return json;
     }
 
-    public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
+    public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
 
         protected final Loader loader;
         protected final DBSerializer serializer;
@@ -131,6 +192,7 @@ public class RawFormat extends MultiFormatMapper {
         protected boolean nodesOnly = false;
         protected int depth = 1;
         protected boolean modelDriven = false;
+        protected boolean tree = false;
 
         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
             this.loader = loader;
@@ -150,6 +212,13 @@ public class RawFormat extends MultiFormatMapper {
             return this.urlBuilder;
         }
 
+        protected boolean isTree() { return this.tree; }
+
+        public Builder isTree(Boolean tree) {
+            this.tree = tree;
+            return this;
+        }
+
         public Builder includeUrl() {
             this.includeUrl = true;
             return this;