[AAI-168 Amsterdam] Check in source
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / queryformats / SimpleFormat.java
index 649164c..c14f9b7 100644 (file)
 
 package org.openecomp.aai.serialization.queryformats;
 
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
 
-import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
-import org.apache.tinkerpop.gremlin.structure.VertexProperty;
-
 import org.openecomp.aai.db.props.AAIProperties;
+import org.openecomp.aai.exceptions.AAIException;
+import org.openecomp.aai.introspection.Introspector;
+import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
-import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
-import com.google.gson.Gson;
-import com.google.gson.JsonArray;
+
 import com.google.gson.JsonObject;
 
-public class SimpleFormat implements FormatMapper {
+public class SimpleFormat extends RawFormat {
 
-       private final UrlBuilder urlBuilder;
-       private final Set<String> blacklist;
-       private final Collection<String> props = Arrays.asList(AAIProperties.AAI_URI, AAIProperties.NODE_TYPE);
-       public SimpleFormat(UrlBuilder urlBuilder) {
-               this.urlBuilder = urlBuilder;
-               this.blacklist = new HashSet<>();
-               blacklist.addAll(props);
-       }
+
+       protected SimpleFormat(Builder builder) {
+               super(builder);
        
-       @Override
-       public JsonObject formatObject(Object input) throws AAIFormatVertexException {
-               Vertex v = (Vertex)input;
-               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));
-               json.add("properties", this.createPropertiesObject(v));
-               json.add("related-to", this.createRelationshipObject(v));
-               
-               return json;
        }
-
+       
        @Override
        public int parallelThreshold() {
-               return 100;
+               return 20;
        }
        
-       private JsonObject createPropertiesObject(Vertex v) {
-               JsonObject json = new JsonObject();
-               Iterator<VertexProperty<Object>> iter = v.properties();
-               
-               while (iter.hasNext()) {
-                       VertexProperty<Object> prop = iter.next();
-                       if (!blacklist.contains(prop.key())) {
-                               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) {
-                                       Gson gson = new Gson();
-                                       String list = gson.toJson(prop.value());
-                                       
-                                       json.addProperty(prop.key(), list);
-                               } else {
-                                       //throw exception?
-                                       return null;
-                               }
+       @Override
+       public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
+               try {
+                       final Introspector obj = loader.introspectorFromName(
+                                                                               v.<String>property(AAIProperties.NODE_TYPE)
+                                                                                       .orElse(null)
+                                                                        );
+
+                       final List<Vertex> wrapper = new ArrayList<>();
+
+                       wrapper.add(v);
+
+                       try {
+                               serializer.dbToObject(wrapper, obj, this.depth, false, "false");
+                       } catch (AAIException | UnsupportedEncodingException  e) {
+                               throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
                        }
+
+                       final String json = obj.marshal(false);
+                       return parser.parse(json).getAsJsonObject();
+               } catch (AAIUnknownObjectException e) {
+                       throw new AAIFormatVertexException("Failed to format vertex - unknown object", e);
                }
                
-               return json;
-       }
-       
-       private JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
-               JsonArray jarray = new JsonArray();
-               Iterator<Vertex> iter = v.vertices(Direction.BOTH);
-               
-               while (iter.hasNext()) {
-                       Vertex related = iter.next();
-                       
-                       JsonObject json = new JsonObject();
-                       json.addProperty("id", related.id().toString());
-                       json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
-                       json.addProperty("url", this.urlBuilder.pathed(related));
-                       jarray.add(json);
-               }
-               
-               return jarray;
+
        }
 }