a9fa7cab57a5407cab3d789d0cc597ffe705e1c1
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / query / builder / QueryBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.query.builder;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28
29 import javax.ws.rs.core.MultivaluedMap;
30
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33
34 import org.openecomp.aai.exceptions.AAIException;
35 import org.openecomp.aai.introspection.Introspector;
36 import org.openecomp.aai.introspection.Loader;
37 import org.openecomp.aai.parsers.query.QueryParser;
38 import org.openecomp.aai.parsers.query.QueryParserStrategy;
39 import org.openecomp.aai.serialization.db.EdgeType;
40 import org.openecomp.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
41
42 /**
43  * The Class QueryBuilder.
44  */
45 public abstract class QueryBuilder implements Iterator<Vertex> {
46
47         protected QueryParserStrategy factory = null;
48         
49         protected Loader loader = null;
50         
51         protected boolean optimize = false;
52         
53         protected Vertex start = null;
54         protected final GraphTraversalSource source;
55         
56         /**
57          * Instantiates a new query builder.
58          *
59          * @param loader the loader
60          */
61         public QueryBuilder(Loader loader, GraphTraversalSource source) {
62                 this.loader = loader;
63                 this.source = source;
64         }
65         
66         /**
67          * Instantiates a new query builder.
68          *
69          * @param loader the loader
70          * @param start the start
71          */
72         public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
73                 this.loader = loader;
74                 this.start = start;
75                 this.source = source;
76         }
77         
78         /**
79          * Gets the vertices by indexed property.
80          *
81          * @param key the key
82          * @param value the value
83          * @return the vertices by indexed property
84          */
85         public abstract QueryBuilder getVerticesByIndexedProperty(String key, Object value);
86         
87         /**
88          * Gets the vertices by property.
89          *
90          * @param key the key
91          * @param value the value
92          * @return the vertices by property
93          */
94         public abstract QueryBuilder getVerticesByProperty(String key, Object value);
95         
96         /**
97          * filters by all the values for this property
98          * @param key
99          * @param values
100          * @return vertices that match these values
101          */
102         public abstract QueryBuilder getVerticesByIndexedProperty(String key, List<?> values);
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 getVerticesByProperty(String key, List<?> values);
111
112         /**
113          * Gets the child vertices from parent.
114          *
115          * @param parentKey the parent key
116          * @param parentValue the parent value
117          * @param childType the child type
118          * @return the child vertices from parent
119          */
120         public abstract QueryBuilder getChildVerticesFromParent(String parentKey, String parentValue, String childType);
121                 
122         /**
123          * Gets the typed vertices by map.
124          *
125          * @param type the type
126          * @param map the map
127          * @return the typed vertices by map
128          */
129         public abstract QueryBuilder getTypedVerticesByMap(String type, LinkedHashMap<String, String> map);
130
131         /**
132          * Creates the DB query.
133          *
134          * @param obj the obj
135          * @return the query builder
136          */
137         public abstract QueryBuilder createDBQuery(Introspector obj);
138         
139         /**
140          * Creates the key query.
141          *
142          * @param obj the obj
143          * @return the query builder
144          */
145         public abstract QueryBuilder createKeyQuery(Introspector obj);
146         
147         /**
148          * Creates the container query.
149          *
150          * @param obj the obj
151          * @return the query builder
152          */
153         public abstract QueryBuilder createContainerQuery(Introspector obj);
154         
155         /**
156          * Creates the edge traversal.
157          *
158          * @param parent the parent
159          * @param child the child
160          * @return the query builder
161          */
162         public abstract QueryBuilder createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
163         
164         /**
165          * Creates the edge traversal.
166          *
167          * @param parent the parent
168          * @param child the child
169          * @return the query builder
170          */
171         public abstract QueryBuilder createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException;
172
173         public QueryBuilder createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws NoEdgeRuleFoundException, AAIException {
174                 Introspector out = loader.introspectorFromName(outNodeType);
175                 Introspector in = loader.introspectorFromName(inNodeType);
176                 
177                 return createEdgeTraversal(type, out, in);
178         }
179
180         /**
181          * Creates the query from URI.
182          *
183          * @param uri the uri
184          * @return the query parser
185          * @throws UnsupportedEncodingException the unsupported encoding exception
186          * @throws AAIException the AAI exception
187          */
188         public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
189         
190         /**
191          * Creates the query from URI.
192          *
193          * @param uri the uri
194          * @param queryParams the query params
195          * @return the query parser
196          * @throws UnsupportedEncodingException the unsupported encoding exception
197          * @throws AAIException the AAI exception
198          */
199         public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
200
201         /**
202          * Creates a queryparser from a given object name.
203          * 
204          * @param objName - name of the object type as it appears in the database
205          * @return
206          */
207         public abstract QueryParser createQueryFromObjectName(String objName);
208         
209         /**
210          * Creates the query from relationship.
211          *
212          * @param relationship the relationship
213          * @return the query parser
214          * @throws UnsupportedEncodingException the unsupported encoding exception
215          * @throws AAIException the AAI exception
216          */
217         public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
218
219         /**
220          * Gets the parent query.
221          *
222          * @return the parent query
223          */
224         public abstract QueryBuilder getParentQuery();
225         
226         /**
227          * Gets the query.
228          *
229          * @return the query
230          */
231         public abstract <T> T getQuery();
232         
233         /**
234          * Form boundary.
235          */
236         public abstract void markParentBoundary();
237         
238         public abstract QueryBuilder limit(long amount);
239         /**
240          * New instance.
241          *
242          * @param start the start
243          * @return the query builder
244          */
245         public abstract QueryBuilder newInstance(Vertex start);
246         
247         /**
248          * New instance.
249          *
250          * @return the query builder
251          */
252         public abstract QueryBuilder newInstance();
253         
254         /**
255          * Gets the start.
256          *
257          * @return the start
258          */
259         public abstract Vertex getStart();
260
261         protected Object correctObjectType(Object obj) {
262                 
263                 if (obj != null && obj.getClass().equals(Long.class)) {
264                         return new Integer(obj.toString());
265                 }
266                 
267                 return obj;
268         }
269         /**
270          * uses all fields in the introspector to create a query
271          * 
272          * @param obj
273          * @return
274          */
275         public abstract QueryBuilder exactMatchQuery(Introspector obj);
276
277         /**
278          * lets you join any number of QueryBuilders
279          * <b>be careful about starting with a union it will not use indexes</b>
280          * @param builder
281          * @return
282          */
283         public abstract QueryBuilder union(QueryBuilder[] builder);
284         
285         public abstract QueryBuilder where(QueryBuilder[] builder);
286         public abstract void markContainer();
287
288         public abstract QueryBuilder getContainerQuery();
289
290         public abstract List<Vertex> toList();
291         
292                 
293 }