2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.aai.query.builder;
23 import java.io.UnsupportedEncodingException;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import javax.ws.rs.core.MultivaluedMap;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.apache.tinkerpop.gremlin.structure.Edge;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
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;
43 * The Class QueryBuilder.
45 public abstract class QueryBuilder<E> implements Iterator<E> {
47 protected QueryParserStrategy factory = null;
49 protected Loader loader = null;
51 protected boolean optimize = false;
53 protected Vertex start = null;
54 protected final GraphTraversalSource source;
57 * Instantiates a new query builder.
59 * @param loader the loader
61 public QueryBuilder(Loader loader, GraphTraversalSource source) {
67 * Instantiates a new query builder.
69 * @param loader the loader
70 * @param start the start
72 public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
79 * Gets the vertices by indexed property.
82 * @param value the value
83 * @return the vertices by indexed property
85 public abstract QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value);
88 * Gets the vertices by property.
91 * @param value the value
92 * @return the vertices by property
94 public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, Object value);
97 * filters by all the values for this property
100 * @return vertices that match these values
102 public abstract QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values);
105 * filters by all the values for this property
108 * @return vertices that match these values
110 public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);
113 * Gets the child vertices from parent.
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
120 public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
123 * Gets the typed vertices by map.
125 * @param type the type
127 * @return the typed vertices by map
129 public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, LinkedHashMap<String, String> map);
132 * Creates the DB query.
135 * @return the query builder
137 public abstract QueryBuilder<Vertex> createDBQuery(Introspector obj);
140 * Creates the key query.
143 * @return the query builder
145 public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
148 * Creates the container query.
151 * @return the query builder
153 public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
156 * Creates the edge traversal.
158 * @param parent the parent
159 * @param child the child
160 * @return the query builder
162 public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
165 * Creates the edge traversal.
167 * @param parent the parent
168 * @param child the child
169 * @return the query builder
171 public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException;
173 public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws NoEdgeRuleFoundException, AAIException {
174 Introspector out = loader.introspectorFromName(outNodeType);
175 Introspector in = loader.introspectorFromName(inNodeType);
177 return createEdgeTraversal(type, out, in);
180 public abstract QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException;
182 * Creates the query from URI.
185 * @return the query parser
186 * @throws UnsupportedEncodingException the unsupported encoding exception
187 * @throws AAIException the AAI exception
189 public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
192 * Creates the query from URI.
195 * @param queryParams the query params
196 * @return the query parser
197 * @throws UnsupportedEncodingException the unsupported encoding exception
198 * @throws AAIException the AAI exception
200 public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
203 * Creates a queryparser from a given object name.
205 * @param objName - name of the object type as it appears in the database
208 public abstract QueryParser createQueryFromObjectName(String objName);
211 * Creates the query from relationship.
213 * @param relationship the relationship
214 * @return the query parser
215 * @throws UnsupportedEncodingException the unsupported encoding exception
216 * @throws AAIException the AAI exception
218 public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
221 * Gets the parent query.
223 * @return the parent query
225 public abstract QueryBuilder<E> getParentQuery();
232 public abstract <E2> E2 getQuery();
237 public abstract void markParentBoundary();
239 public abstract QueryBuilder<E> limit(long amount);
243 * @param start the start
244 * @return the query builder
246 public abstract QueryBuilder<E> newInstance(Vertex start);
251 * @return the query builder
253 public abstract QueryBuilder<E> newInstance();
260 public abstract Vertex getStart();
262 protected Object correctObjectType(Object obj) {
264 if (obj != null && obj.getClass().equals(Long.class)) {
265 return new Integer(obj.toString());
271 * uses all fields in the introspector to create a query
276 public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
279 * lets you join any number of QueryBuilders
280 * <b>be careful about starting with a union it will not use indexes</b>
284 public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
286 public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
288 public abstract QueryBuilder<E> store(String name);
289 public abstract QueryBuilder<E> cap(String name);
290 public abstract QueryBuilder<E> unfold();
291 public abstract QueryBuilder<E> dedup();
292 public abstract QueryBuilder<E> emit();
293 public abstract QueryBuilder<E> repeat(QueryBuilder<E> builder);
294 public abstract QueryBuilder<Edge> outE();
295 public abstract QueryBuilder<Edge> inE();
296 public abstract QueryBuilder<Vertex> inV();
297 public abstract QueryBuilder<Vertex> outV();
298 public abstract QueryBuilder<E> not(QueryBuilder<E> builder);
299 public abstract QueryBuilder<E> as(String name);
300 public abstract QueryBuilder<E> select(String name);
302 public abstract void markContainer();
304 public abstract QueryBuilder<E> getContainerQuery();
306 public abstract List<E> toList();