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.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 implements Iterator<Vertex> {
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 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 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 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 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 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 getTypedVerticesByMap(String type, LinkedHashMap<String, String> map);
132 * Creates the DB query.
135 * @return the query builder
137 public abstract QueryBuilder createDBQuery(Introspector obj);
140 * Creates the key query.
143 * @return the query builder
145 public abstract QueryBuilder createKeyQuery(Introspector obj);
148 * Creates the container query.
151 * @return the query builder
153 public abstract QueryBuilder 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 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 createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException;
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);
177 return createEdgeTraversal(type, out, in);
181 * Creates the query from URI.
184 * @return the query parser
185 * @throws UnsupportedEncodingException the unsupported encoding exception
186 * @throws AAIException the AAI exception
188 public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
191 * Creates the query from 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
199 public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
202 * Creates a queryparser from a given object name.
204 * @param objName - name of the object type as it appears in the database
207 public abstract QueryParser createQueryFromObjectName(String objName);
210 * Creates the query from relationship.
212 * @param relationship the relationship
213 * @return the query parser
214 * @throws UnsupportedEncodingException the unsupported encoding exception
215 * @throws AAIException the AAI exception
217 public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
220 * Gets the parent query.
222 * @return the parent query
224 public abstract QueryBuilder getParentQuery();
231 public abstract <T> T getQuery();
236 public abstract void markParentBoundary();
238 public abstract QueryBuilder limit(long amount);
242 * @param start the start
243 * @return the query builder
245 public abstract QueryBuilder newInstance(Vertex start);
250 * @return the query builder
252 public abstract QueryBuilder newInstance();
259 public abstract Vertex getStart();
261 protected Object correctObjectType(Object obj) {
263 if (obj != null && obj.getClass().equals(Long.class)) {
264 return new Integer(obj.toString());
270 * uses all fields in the introspector to create a query
275 public abstract QueryBuilder exactMatchQuery(Introspector obj);
278 * lets you join any number of QueryBuilders
279 * <b>be careful about starting with a union it will not use indexes</b>
283 public abstract QueryBuilder union(QueryBuilder[] builder);
285 public abstract QueryBuilder where(QueryBuilder[] builder);
286 public abstract void markContainer();
288 public abstract QueryBuilder getContainerQuery();
290 public abstract List<Vertex> toList();