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;
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;
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
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);
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;
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;
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;