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;
35 import org.openecomp.aai.exceptions.AAIException;
36 import org.openecomp.aai.introspection.Introspector;
37 import org.openecomp.aai.introspection.Loader;
38 import org.openecomp.aai.parsers.query.QueryParser;
39 import org.openecomp.aai.parsers.query.QueryParserStrategy;
40 import org.openecomp.aai.serialization.db.EdgeType;
41 import org.openecomp.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
44 * The Class QueryBuilder.
46 public abstract class QueryBuilder<E> implements Iterator<E> {
48 protected QueryParserStrategy factory = null;
50 protected Loader loader = null;
52 protected boolean optimize = false;
54 protected Vertex start = null;
55 protected final GraphTraversalSource source;
58 * Instantiates a new query builder.
60 * @param loader the loader
62 public QueryBuilder(Loader loader, GraphTraversalSource source) {
68 * Instantiates a new query builder.
70 * @param loader the loader
71 * @param start the start
73 public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
80 * Gets the vertices by indexed property.
83 * @param value the value
84 * @return the vertices by indexed property
86 public abstract QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value);
89 * Gets the vertices by property.
92 * @param value the value
93 * @return the vertices by property
95 public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, Object value);
98 * filters by all the values for this property
101 * @return vertices that match these values
103 public abstract QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values);
106 * filters by all the values for this property
109 * @return vertices that match these values
111 public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);
114 * Gets the child vertices from parent.
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
121 public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
124 * Gets the typed vertices by map.
126 * @param type the type
128 * @return the typed vertices by map
130 public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, LinkedHashMap<String, String> map);
133 * Creates the DB query.
136 * @return the query builder
138 public abstract QueryBuilder<Vertex> createDBQuery(Introspector obj);
141 * Creates the key query.
144 * @return the query builder
146 public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
149 * Creates the container query.
152 * @return the query builder
154 public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
157 * Creates the edge traversal.
159 * @param parent the parent
160 * @param child the child
161 * @return the query builder
163 public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
166 * Creates the edge traversal.
168 * @param parent the parent
169 * @param child the child
170 * @return the query builder
172 public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException;
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);
178 return createEdgeTraversal(type, out, in);
181 public abstract QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException;
183 * Creates the query from URI.
186 * @return the query parser
187 * @throws UnsupportedEncodingException the unsupported encoding exception
188 * @throws AAIException the AAI exception
190 public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
193 * Creates the query from 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
201 public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
204 * Creates a queryparser from a given object name.
206 * @param objName - name of the object type as it appears in the database
209 public abstract QueryParser createQueryFromObjectName(String objName);
212 * Creates the query from relationship.
214 * @param relationship the relationship
215 * @return the query parser
216 * @throws UnsupportedEncodingException the unsupported encoding exception
217 * @throws AAIException the AAI exception
219 public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
222 * Gets the parent query.
224 * @return the parent query
226 public abstract QueryBuilder<E> getParentQuery();
233 public abstract <E2> E2 getQuery();
238 public abstract void markParentBoundary();
240 public abstract QueryBuilder<E> limit(long amount);
244 * @param start the start
245 * @return the query builder
247 public abstract QueryBuilder<E> newInstance(Vertex start);
252 * @return the query builder
254 public abstract QueryBuilder<E> newInstance();
261 public abstract Vertex getStart();
263 protected Object correctObjectType(Object obj) {
265 if (obj != null && obj.getClass().equals(Long.class)) {
266 return new Integer(obj.toString());
272 * uses all fields in the introspector to create a query
277 public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
280 * lets you join any number of QueryBuilders
281 * <b>be careful about starting with a union it will not use indexes</b>
285 public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
287 public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
288 public abstract void markContainer();
290 public abstract QueryBuilder<E> getContainerQuery();
292 public abstract List<E> toList();