2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017-2018 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 package org.onap.aai.serialization.queryformats;
\r
22 import java.util.Iterator;
\r
23 import java.util.List;
\r
25 import org.apache.tinkerpop.gremlin.process.traversal.Path;
\r
26 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
\r
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
\r
28 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
\r
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
\r
31 import com.google.gson.JsonArray;
\r
32 import com.google.gson.JsonObject;
\r
34 public abstract class MultiFormatMapper implements FormatMapper {
\r
37 public JsonObject formatObject(Object input) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
\r
38 if (input instanceof Vertex) {
\r
39 return this.getJsonFromVertex((Vertex) input);
\r
40 } else if (input instanceof Tree) {
\r
41 return this.getJsonFomTree((Tree<?>) input);
\r
42 } else if (input instanceof Path) {
\r
43 return this.getJsonFromPath((Path) input);
\r
45 throw new AAIFormatQueryResultFormatNotSupported();
\r
49 protected abstract JsonObject getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
\r
51 protected JsonObject getJsonFromPath(Path input) throws AAIFormatVertexException {
\r
52 List<Object> path = input.objects();
\r
54 JsonObject jo = new JsonObject();
\r
55 JsonArray ja = new JsonArray();
\r
57 for (Object o : path) {
\r
58 if (o instanceof Vertex) {
\r
59 ja.add(this.getJsonFromVertex((Vertex)o));
\r
67 protected JsonObject getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
\r
69 if (tree.isEmpty()) {
\r
70 return new JsonObject();
\r
73 JsonObject t = new JsonObject();
\r
74 JsonArray ja = this.getNodesArray(tree);
\r
75 if (ja.size() > 0) {
\r
82 private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {
\r
84 JsonArray nodes = new JsonArray();
\r
85 Iterator<?> it = tree.keySet().iterator();
\r
87 while (it.hasNext()) {
\r
88 Object o = it.next();
\r
89 JsonObject me = new JsonObject();
\r
90 if (o instanceof Vertex) {
\r
91 me = this.getJsonFromVertex((Vertex) o);
\r
93 JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));
\r
94 if (ja.size() > 0) {
\r
95 me.add("nodes", ja);
\r
104 public int parallelThreshold() {
\r