Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / MultiFormatMapper.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\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
10  *\r
11  *    http://www.apache.org/licenses/LICENSE-2.0\r
12  *\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
19  */\r
20 package org.onap.aai.serialization.queryformats;\r
21 \r
22 import java.util.Iterator;\r
23 import java.util.List;\r
24 \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
30 \r
31 import com.google.gson.JsonArray;\r
32 import com.google.gson.JsonObject;\r
33 \r
34 public abstract class MultiFormatMapper implements FormatMapper {\r
35 \r
36         @Override\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
44                 } else {\r
45                         throw new AAIFormatQueryResultFormatNotSupported();\r
46                 }\r
47         }\r
48 \r
49         protected abstract JsonObject getJsonFromVertex(Vertex input) throws AAIFormatVertexException;\r
50 \r
51         protected JsonObject getJsonFromPath(Path input) throws AAIFormatVertexException {\r
52                 List<Object> path = input.objects();\r
53 \r
54                 JsonObject jo = new JsonObject();\r
55                 JsonArray ja = new JsonArray();\r
56                 \r
57                 for (Object o : path) {\r
58                         if (o instanceof Vertex) {\r
59                                 ja.add(this.getJsonFromVertex((Vertex)o));\r
60                         }\r
61                 }\r
62                 \r
63                 jo.add("path", ja);\r
64                 return jo;\r
65         }\r
66 \r
67         protected JsonObject getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {\r
68                 \r
69                 if (tree.isEmpty()) {\r
70                         return new JsonObject();\r
71                 }\r
72                 \r
73                 JsonObject t = new JsonObject();\r
74                 JsonArray ja = this.getNodesArray(tree);\r
75                 if (ja.size() > 0) {\r
76                         t.add("nodes", ja);\r
77                 }\r
78                 \r
79                 return t;\r
80         }\r
81         \r
82         private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {\r
83                 \r
84                 JsonArray nodes = new JsonArray();\r
85                 Iterator<?> it = tree.keySet().iterator();\r
86 \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
92                         }\r
93                         JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));\r
94                         if (ja.size() > 0) {\r
95                                 me.add("nodes", ja);\r
96                         }\r
97                         nodes.add(me);\r
98                 }\r
99                 return nodes;\r
100         }\r
101         \r
102         \r
103         @Override\r
104         public int parallelThreshold() {\r
105                 return 100;\r
106         }\r
107 \r
108 }\r