Update the license for 2017-2018 license
[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-2018 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 package org.onap.aai.query.builder;
21
22 import java.io.UnsupportedEncodingException;
23 import java.net.URI;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.structure.Edge;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.onap.aai.db.props.AAIProperties;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.parsers.query.QueryParser;
38 import org.onap.aai.parsers.query.QueryParserStrategy;
39 import org.onap.aai.serialization.db.EdgeType;
40
41 /**
42  * The Class QueryBuilder.
43  */
44 public abstract class QueryBuilder<E> implements Iterator<E> {
45
46         protected final GraphTraversalSource source;
47         protected QueryParserStrategy factory = null;
48         protected Loader loader = null;
49         protected boolean optimize = false;
50         protected Vertex start = null;
51         
52         /**
53          * Instantiates a new query builder.
54          *
55          * @param loader the loader
56          */
57         public QueryBuilder(Loader loader, GraphTraversalSource source) {
58                 this.loader = loader;
59                 this.source = source;
60         }
61         
62         /**
63          * Instantiates a new query builder.
64          *
65          * @param loader the loader
66          * @param start the start
67          */
68         public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
69                 this.loader = loader;
70                 this.start = start;
71                 this.source = source;
72         }
73         
74         /**
75          * Gets the vertices by indexed property.
76          *
77          * @param key the key
78          * @param value the value
79          * @return the vertices by indexed property
80          */
81         public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value) {
82                 return this.getVerticesByProperty(key, value);
83         }
84         
85         /**
86          * Gets the vertices by property.
87          *
88          * @param key the key
89          * @param value the value
90          * @return the vertices by property
91          */
92         public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, Object value);
93         
94         /**
95          * filters by all the values for this property
96          * @param key
97          * @param values
98          * @return vertices that match these values
99          */
100         public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values) {
101                 return this.getVerticesByProperty(key, values);
102         }
103
104         /**
105          * filters by all the values for this property
106          * @param key
107          * @param values
108          * @return vertices that match these values
109          */
110         public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);
111
112         /**
113          * Gets the vertices that are excluded by property.
114          *
115          * @param key the key
116          * @param value the value
117          * @return the vertices by property
118          */
119         public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, Object value);
120
121         /**
122          * filters by all the values for this property and excludes the vertices
123          * @param key
124          * @param values
125          * @return vertices that match these values
126          */
127         public QueryBuilder<Vertex> getVerticesExcludeByIndexedProperty(String key, List<?> values) {
128                 return this.getVerticesExcludeByProperty(key, values);
129         }
130
131         /**
132          * filters by all the values for this property and excludes the vertices
133          * @param key
134          * @param values
135          * @return vertices that match these values
136          */
137         public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, List<?> values);
138
139         /**
140          * Gets the child vertices from parent.
141          *
142          * @param parentKey the parent key
143          * @param parentValue the parent value
144          * @param childType the child type
145          * @return the child vertices from parent
146          */
147         public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
148                 
149         /**
150          * Gets the typed vertices by map.
151          *
152          * @param type the type
153          * @param map the map
154          * @return the typed vertices by map
155          */
156         public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, Map<String, String> map);
157
158         /**
159          * Creates the DB query.
160          *
161          * @param obj the obj
162          * @return the query builder
163          */
164         public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
165                 this.createKeyQuery(obj);
166                 this.createContainerQuery(obj);
167                 return (QueryBuilder<Vertex>) this;
168         }
169         
170         /**
171          * Creates the key query.
172          *
173          * @param obj the obj
174          * @return the query builder
175          */
176         public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
177         
178         /**
179          * Creates the container query.
180          *
181          * @param obj the obj
182          * @return the query builder
183          */
184         public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
185         
186         /**
187          * Creates the edge traversal.
188          *
189          * @param parent the parent
190          * @param child the child
191          * @return the query builder
192          */
193         public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
194         
195         /**
196          * Creates the edge traversal.
197          *
198          * @param parent the parent
199          * @param child the child
200          * @return the query builder
201          */
202         public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException {
203                 String nodeType = parent.<String>property(AAIProperties.NODE_TYPE).orElse(null);
204                 this.createEdgeTraversal(type, nodeType, child.getDbName());
205                 return (QueryBuilder<Vertex>) this;
206
207         }
208
209         /**
210          *
211          * @param type
212          * @param outNodeType
213          * @param inNodeType
214          * @return
215          * @throws AAIException
216          */
217         public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
218                 Introspector out = loader.introspectorFromName(outNodeType);
219                 Introspector in = loader.introspectorFromName(inNodeType);
220
221                 return createEdgeTraversal(type, out, in);
222         }
223
224         /**
225          *
226          * @param type
227          * @param outNodeType
228          * @param inNodeType
229          * @param labels
230          * @return
231          * @throws AAIException
232          */
233         public QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException {
234                 Introspector out = loader.introspectorFromName(outNodeType);
235                 Introspector in = loader.introspectorFromName(inNodeType);
236
237                 return createEdgeTraversalWithLabels(type, out, in, labels);
238         }
239
240         /**
241          *
242          * @param type
243          * @param out
244          * @param in
245          * @param labels
246          * @return
247          */
248         public abstract QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, Introspector out, Introspector in, List<String> labels) throws AAIException;
249
250         /**
251          *
252          * @param type
253          * @param outNodeType
254          * @param inNodeType
255          * @return
256          * @throws AAIException
257          */
258         public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
259                 this.getEdgesBetweenWithLabels(type, outNodeType, inNodeType, null);
260
261                 return (QueryBuilder<Edge>)this;
262
263         }
264         /**
265          *
266          * @param type
267          * @param outNodeType
268          * @param inNodeType
269          * @param labels
270          * @return
271          * @throws AAIException
272          */
273         public abstract QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException;
274
275         /**
276          * Creates the query from URI.
277          *
278          * @param uri the uri
279          * @return the query parser
280          * @throws UnsupportedEncodingException the unsupported encoding exception
281          * @throws AAIException the AAI exception
282          */
283         public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
284         
285         /**
286          * Creates the query from URI.
287          *
288          * @param uri the uri
289          * @param queryParams the query params
290          * @return the query parser
291          * @throws UnsupportedEncodingException the unsupported encoding exception
292          * @throws AAIException the AAI exception
293          */
294         public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
295
296         /**
297          * Creates a queryparser from a given object name.
298          * 
299          * @param objName - name of the object type as it appears in the database
300          * @return
301          */
302         public abstract QueryParser createQueryFromObjectName(String objName);
303         
304         /**
305          * Creates the query from relationship.
306          *
307          * @param relationship the relationship
308          * @return the query parser
309          * @throws UnsupportedEncodingException the unsupported encoding exception
310          * @throws AAIException the AAI exception
311          */
312         public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
313
314         /**
315          * Gets the parent query.
316          *
317          * @return the parent query
318          */
319         public abstract QueryBuilder<E> getParentQuery();
320         
321         /**
322          * Gets the query.
323          *
324          * @return the query
325          */
326         public abstract <E2> E2 getQuery();
327         
328         /**
329          * Form boundary.
330          */
331         public abstract void markParentBoundary();
332         
333         public abstract QueryBuilder<E> limit(long amount);
334
335         /**
336          * New instance.
337          *
338          * @param start the start
339          * @return the query builder
340          */
341         public abstract QueryBuilder<E> newInstance(Vertex start);
342         
343         /**
344          * New instance.
345          *
346          * @return the query builder
347          */
348         public abstract QueryBuilder<E> newInstance();
349         
350         /**
351          * Gets the start.
352          *
353          * @return the start
354          */
355         public abstract Vertex getStart();
356
357         protected Object correctObjectType(Object obj) {
358                 
359                 if (obj != null && obj.getClass().equals(Long.class)) {
360                         return new Integer(obj.toString());
361                 }
362                 
363                 return obj;
364         }
365         /**
366          * uses all fields in the introspector to create a query
367          * 
368          * @param obj
369          * @return
370          */
371         public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
372
373         /**
374          * lets you join any number of QueryBuilders
375          * <b>be careful about starting with a union it will not use indexes</b>
376          * @param builder
377          * @return
378          */
379         public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
380         
381         public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
382         
383         public abstract QueryBuilder<E> store(String name);
384         public abstract QueryBuilder<E> cap(String name);
385         public abstract QueryBuilder<E> unfold();
386         public abstract QueryBuilder<E> dedup();
387         public abstract QueryBuilder<E> emit();
388         public abstract QueryBuilder<E> repeat(QueryBuilder<E> builder);
389         public abstract QueryBuilder<Edge> outE();
390         public abstract QueryBuilder<Edge> inE();
391         public abstract QueryBuilder<Vertex> inV();
392         public abstract QueryBuilder<Vertex> outV();
393         public abstract QueryBuilder<E> not(QueryBuilder<E> builder);
394         public abstract QueryBuilder<E> as(String name);
395         public abstract QueryBuilder<E> select(String name);
396         public abstract QueryBuilder<E> until(QueryBuilder<E> builder);
397         public abstract QueryBuilder<E> groupCount();
398         public abstract QueryBuilder<E> by(String name);
399         public abstract QueryBuilder<E> both();
400         
401         /**
402          * Used to prevent the traversal from repeating its path through the graph.
403          * See http://tinkerpop.apache.org/docs/3.0.1-incubating/#simplepath-step for more info.
404          * 
405          * @return a QueryBuilder with the simplePath step appended to its traversal
406          */
407         public abstract QueryBuilder<E> simplePath();
408         
409         public abstract void markContainer();
410
411         public abstract QueryBuilder<E> getContainerQuery();
412
413         public abstract List<E> toList();
414         
415         public QueryBuilder<Vertex> getVerticesByProperty(String key, MissingOptionalParameter value) {
416                 return (QueryBuilder<Vertex>) this;
417         }
418
419                 
420 }