2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 package org.onap.aai.serialization.queryformats;
\r
24 import java.util.Iterator;
\r
25 import java.util.List;
\r
27 import org.apache.tinkerpop.gremlin.process.traversal.Path;
\r
28 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
\r
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
\r
30 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
\r
31 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
\r
33 import com.google.gson.JsonArray;
\r
34 import com.google.gson.JsonObject;
\r
36 public abstract class MultiFormatMapper implements FormatMapper {
\r
39 public JsonObject formatObject(Object input) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
\r
40 if (input instanceof Vertex) {
\r
41 return this.getJsonFromVertex((Vertex) input);
\r
42 } else if (input instanceof Tree) {
\r
43 return this.getJsonFomTree((Tree<?>) input);
\r
44 } else if (input instanceof Path) {
\r
45 return this.getJsonFromPath((Path) input);
\r
47 throw new AAIFormatQueryResultFormatNotSupported();
\r
51 protected abstract JsonObject getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
\r
53 protected JsonObject getJsonFromPath(Path input) throws AAIFormatVertexException {
\r
54 List<Object> path = input.objects();
\r
56 JsonObject jo = new JsonObject();
\r
57 JsonArray ja = new JsonArray();
\r
59 for (Object o : path) {
\r
60 if (o instanceof Vertex) {
\r
61 ja.add(this.getJsonFromVertex((Vertex)o));
\r
69 protected JsonObject getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
\r
71 if (tree.isEmpty()) {
\r
72 return new JsonObject();
\r
75 JsonObject t = new JsonObject();
\r
76 JsonArray ja = this.getNodesArray(tree);
\r
77 if (ja.size() > 0) {
\r
84 private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {
\r
86 JsonArray nodes = new JsonArray();
\r
87 Iterator<?> it = tree.keySet().iterator();
\r
89 while (it.hasNext()) {
\r
90 Object o = it.next();
\r
91 JsonObject me = new JsonObject();
\r
92 if (o instanceof Vertex) {
\r
93 me = this.getJsonFromVertex((Vertex) o);
\r
95 JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));
\r
96 if (ja.size() > 0) {
\r
97 me.add("nodes", ja);
\r
106 public int parallelThreshold() {
\r