3a3feafbb351fbec650dfccac2761d37019ac8cc
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / query / builder / QueryBuilder.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.query.builder;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.util.Iterator;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29
30 import javax.ws.rs.core.MultivaluedMap;
31
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
33 import org.apache.tinkerpop.gremlin.structure.Edge;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Introspector;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.parsers.query.QueryParser;
39 import org.onap.aai.parsers.query.QueryParserStrategy;
40 import org.onap.aai.serialization.db.EdgeType;
41 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
42
43 /**
44  * The Class QueryBuilder.
45  */
46 public abstract class QueryBuilder<E> implements Iterator<E> {
47
48         protected QueryParserStrategy factory = null;
49         
50         protected Loader loader = null;
51         
52         protected boolean optimize = false;
53         
54         protected Vertex start = null;
55         protected final GraphTraversalSource source;
56         
57         /**
58          * Instantiates a new query builder.
59          *
60          * @param loader the loader
61          */
62         public QueryBuilder(Loader loader, GraphTraversalSource source) {
63                 this.loader = loader;
64                 this.source = source;
65         }
66         
67         /**
68          * Instantiates a new query builder.
69          *
70          * @param loader the loader
71          * @param start the start
72          */
73         public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
74                 this.loader = loader;
75                 this.start = start;
76                 this.source = source;
77         }
78         
79         /**
80          * Gets the vertices by indexed property.
81          *
82          * @param key the key
83          * @param value the value
84          * @return the vertices by indexed property
85          */
86         public abstract QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value);
87         
88         /**
89          * Gets the vertices by property.
90          *
91          * @param key the key
92          * @param value the value
93          * @return the vertices by property
94          */
95         public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, Object value);
96         
97         /**
98          * filters by all the values for this property
99          * @param key
100          * @param values
101          * @return vertices that match these values
102          */
103         public abstract QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values);
104
105         /**
106          * filters by all the values for this property
107          * @param key
108          * @param values
109          * @return vertices that match these values
110          */
111         public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);
112
113         /**
114          * Gets the child vertices from parent.
115          *
116          * @param parentKey the parent key
117          * @param parentValue the parent value
118          * @param childType the child type
119          * @return the child vertices from parent
120          */
121         public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
122                 
123         /**
124          * Gets the typed vertices by map.
125          *
126          * @param type the type
127          * @param map the map
128          * @return the typed vertices by map
129          */
130         public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, LinkedHashMap<String, String> map);
131
132         /**
133          * Creates the DB query.
134          *
135          * @param obj the obj
136          * @return the query builder
137          */
138         public abstract QueryBuilder<Vertex> createDBQuery(Introspector obj);
139         
140         /**
141          * Creates the key query.
142          *
143          * @param obj the obj
144          * @return the query builder
145          */
146         public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
147         
148         /**
149          * Creates the container query.
150          *
151          * @param obj the obj
152          * @return the query builder
153          */
154         public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
155         
156         /**
157          * Creates the edge traversal.
158          *
159          * @param parent the parent
160          * @param child the child
161          * @return the query builder
162          */
163         public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
164         
165         /**
166          * Creates the edge traversal.
167          *
168          * @param parent the parent
169          * @param child the child
170          * @return the query builder
171          */
172         public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException;
173
174         public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws NoEdgeRuleFoundException, AAIException {
175                 Introspector out = loader.introspectorFromName(outNodeType);
176                 Introspector in = loader.introspectorFromName(inNodeType);
177                 
178                 return createEdgeTraversal(type, out, in);
179         }
180         
181         public abstract QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException;
182         /**
183          * Creates the query from URI.
184          *
185          * @param uri the uri
186          * @return the query parser
187          * @throws UnsupportedEncodingException the unsupported encoding exception
188          * @throws AAIException the AAI exception
189          */
190         public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
191         
192         /**
193          * Creates the query from URI.
194          *
195          * @param uri the uri
196          * @param queryParams the query params
197          * @return the query parser
198          * @throws UnsupportedEncodingException the unsupported encoding exception
199          * @throws AAIException the AAI exception
200          */
201         public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
202
203         /**
204          * Creates a queryparser from a given object name.
205          * 
206          * @param objName - name of the object type as it appears in the database
207          * @return
208          */
209         public abstract QueryParser createQueryFromObjectName(String objName);
210         
211         /**
212          * Creates the query from relationship.
213          *
214          * @param relationship the relationship
215          * @return the query parser
216          * @throws UnsupportedEncodingException the unsupported encoding exception
217          * @throws AAIException the AAI exception
218          */
219         public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
220
221         /**
222          * Gets the parent query.
223          *
224          * @return the parent query
225          */
226         public abstract QueryBuilder<E> getParentQuery();
227         
228         /**
229          * Gets the query.
230          *
231          * @return the query
232          */
233         public abstract <E2> E2 getQuery();
234         
235         /**
236          * Form boundary.
237          */
238         public abstract void markParentBoundary();
239         
240         public abstract QueryBuilder<E> limit(long amount);
241         /**
242          * New instance.
243          *
244          * @param start the start
245          * @return the query builder
246          */
247         public abstract QueryBuilder<E> newInstance(Vertex start);
248         
249         /**
250          * New instance.
251          *
252          * @return the query builder
253          */
254         public abstract QueryBuilder<E> newInstance();
255         
256         /**
257          * Gets the start.
258          *
259          * @return the start
260          */
261         public abstract Vertex getStart();
262
263         protected Object correctObjectType(Object obj) {
264                 
265                 if (obj != null && obj.getClass().equals(Long.class)) {
266                         return new Integer(obj.toString());
267                 }
268                 
269                 return obj;
270         }
271         /**
272          * uses all fields in the introspector to create a query
273          * 
274          * @param obj
275          * @return
276          */
277         public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
278
279         /**
280          * lets you join any number of QueryBuilders
281          * <b>be careful about starting with a union it will not use indexes</b>
282          * @param builder
283          * @return
284          */
285         public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
286         
287         public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
288         
289         public abstract QueryBuilder<E> store(String name);
290         public abstract QueryBuilder<E> cap(String name);
291         public abstract QueryBuilder<E> unfold();
292         public abstract QueryBuilder<E> dedup();
293         public abstract QueryBuilder<E> emit();
294         public abstract QueryBuilder<E> repeat(QueryBuilder<E> builder);
295         public abstract QueryBuilder<Edge> outE();
296         public abstract QueryBuilder<Edge> inE();
297         public abstract QueryBuilder<Vertex> inV();
298         public abstract QueryBuilder<Vertex> outV();
299         public abstract QueryBuilder<E> not(QueryBuilder<E> builder);
300         public abstract QueryBuilder<E> as(String name);
301         public abstract QueryBuilder<E> select(String name);
302         public abstract QueryBuilder<E> until(QueryBuilder<E> builder);
303         
304         /**
305          * Used to prevent the traversal from repeating its path through the graph.
306          * See http://tinkerpop.apache.org/docs/3.0.1-incubating/#simplepath-step for more info.
307          * 
308          * @return a QueryBuilder with the simplePath step appended to its traversal
309          */
310         public abstract QueryBuilder<E> simplePath();
311         
312         public abstract void markContainer();
313
314         public abstract QueryBuilder<E> getContainerQuery();
315
316         public abstract List<E> toList();
317         
318                 
319 }