X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=aai-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fquery%2Fbuilder%2FQueryBuilder.java;h=f4ffac0ecc263396400504b2a3a3b2de309a0da9;hb=a86d6a6644d6de3f3f814cd6e25cfe2213d5dd05;hp=0350aba2fad45c4b17b76bef04e01c40af5b1dd6;hpb=a395fa69a28a04d0a667fe458b4f10497a1d6794;p=aai%2Faai-common.git diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java b/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java index 0350aba2..f4ffac0e 100644 --- a/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java +++ b/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java @@ -19,27 +19,31 @@ */ package org.onap.aai.query.builder; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.ws.rs.core.MultivaluedMap; - import org.apache.tinkerpop.gremlin.process.traversal.Path; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.db.props.AAIProperties; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.parsers.query.QueryParser; import org.onap.aai.parsers.query.QueryParserStrategy; -import org.onap.aai.serialization.db.AAIDirection; -import org.onap.aai.serialization.db.EdgeProperty; -import org.onap.aai.serialization.db.EdgeType; +import org.springframework.context.ApplicationContext; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.edges.enums.EdgeProperty; +import org.onap.aai.edges.enums.EdgeType; + +import javax.ws.rs.core.MultivaluedMap; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.util.Iterator; +import java.util.List; +import java.util.Map; /** * The Class QueryBuilder. @@ -49,9 +53,14 @@ public abstract class QueryBuilder implements Iterator { protected final GraphTraversalSource source; protected QueryParserStrategy factory = null; protected Loader loader = null; + protected EdgeIngestor edgeRules; protected boolean optimize = false; protected Vertex start = null; - + + protected int parentStepIndex = 0; + protected int containerStepIndex = 0; + protected int stepIndex = 0; + /** * Instantiates a new query builder. * @@ -60,6 +69,7 @@ public abstract class QueryBuilder implements Iterator { public QueryBuilder(Loader loader, GraphTraversalSource source) { this.loader = loader; this.source = source; + initEdgeIngestor(); } /** @@ -72,8 +82,15 @@ public abstract class QueryBuilder implements Iterator { this.loader = loader; this.start = start; this.source = source; + initEdgeIngestor(); } - + + public void changeLoader(Loader loader) { + this.loader = loader; + } + + protected abstract QueryBuilder cloneQueryAtStep(int index); + /** * Gets the vertices by indexed property. * @@ -111,6 +128,32 @@ public abstract class QueryBuilder implements Iterator { * @return vertices that match these values */ public abstract QueryBuilder getVerticesByProperty(String key, List values); + + /** + * Gets the vertices that have this property key. + * + * @param key the key + * @param value the value + * @return the vertices by property + */ + public abstract QueryBuilder getVerticesByProperty(String key); + + /** + * Gets the vertices that do not have this property key. + * + * @param key the key + * @param value the value + * @return the vertices by property + */ + public abstract QueryBuilder getVerticesExcludeByProperty(String key); + + /** + * filters by elements that start with the value for this property + * @param key + * @param value + * @return vertices that match these values + */ + public abstract QueryBuilder getVerticesStartsWithProperty(String key, Object value); /** * Gets the vertices that are excluded by property. @@ -140,6 +183,23 @@ public abstract class QueryBuilder implements Iterator { public abstract QueryBuilder getVerticesExcludeByProperty(String key, List values); /** + * filters by all the values greater than for this property + * @param key + * @param values + * @return vertices that match these values + */ + public abstract QueryBuilder getVerticesGreaterThanProperty(String key, Object value) ; + + /** + * filters by all the values less than for this property + * @param key + * @param values + * @return vertices that match these values + */ + + public abstract QueryBuilder getVerticesLessThanProperty(String key, Object value) ; + + /** * Gets the child vertices from parent. * * @param parentKey the parent key @@ -194,7 +254,17 @@ public abstract class QueryBuilder implements Iterator { * @return the query builder */ public abstract QueryBuilder createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException; - + + public abstract QueryBuilder getVerticesByBooleanProperty(String key, Object value); + /** + * Creates the private edge traversal. + * + * @param parent the parent + * @param child the child + * @return the query builder + */ + public abstract QueryBuilder createPrivateEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException; + /** * Creates the edge traversal. * @@ -206,7 +276,6 @@ public abstract class QueryBuilder implements Iterator { String nodeType = parent.property(AAIProperties.NODE_TYPE).orElse(null); this.createEdgeTraversal(type, nodeType, child.getDbName()); return (QueryBuilder) this; - } /** @@ -224,6 +293,45 @@ public abstract class QueryBuilder implements Iterator { return createEdgeTraversal(type, out, in); } + public QueryBuilder createEdgeTraversal(String outNodeType, String inNodeType) throws AAIException { + + Introspector out = loader.introspectorFromName(outNodeType); + Introspector in = loader.introspectorFromName(inNodeType); + + QueryBuilder cousinBuilder = null; + QueryBuilder treeBuilder = null; + QueryBuilder queryBuilder = null; + + try { + cousinBuilder = this.newInstance().createEdgeTraversal(EdgeType.COUSIN, out, in); + } catch (AAIException e) { + } + + if(cousinBuilder != null){ + try { + treeBuilder = this.newInstance().createEdgeTraversal(EdgeType.TREE, out, in); + } catch (AAIException e) { + } + if(treeBuilder != null){ + queryBuilder = this.union(new QueryBuilder[]{cousinBuilder, treeBuilder}); + } else { + queryBuilder = this.union(new QueryBuilder[]{cousinBuilder}); + } + } else { + treeBuilder = this.newInstance().createEdgeTraversal(EdgeType.TREE, out, in); + queryBuilder = this.union(new QueryBuilder[]{treeBuilder}); + } + + + return queryBuilder; + } + + public QueryBuilder createPrivateEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws AAIException { + Introspector out = loader.introspectorFromName(outNodeType); + Introspector in = loader.introspectorFromName(inNodeType); + return createPrivateEdgeTraversal(type, out, in); + } + /** * * @param type @@ -383,6 +491,8 @@ public abstract class QueryBuilder implements Iterator { public abstract QueryBuilder where(QueryBuilder... builder); + public abstract QueryBuilder or(QueryBuilder... builder); + public abstract QueryBuilder store(String name); public abstract QueryBuilder cap(String name); public abstract QueryBuilder unfold(); @@ -400,6 +510,7 @@ public abstract class QueryBuilder implements Iterator { public abstract QueryBuilder groupCount(); public abstract QueryBuilder by(String name); public abstract QueryBuilder both(); + public abstract QueryBuilder tree(); /** * Used to prevent the traversal from repeating its path through the graph. @@ -452,4 +563,15 @@ public abstract class QueryBuilder implements Iterator { } protected abstract QueryBuilder has(String key, String value); + + protected void initEdgeIngestor() { + //TODO proper spring wiring, but that requires a lot of refactoring so for now we have this + ApplicationContext ctx = SpringContextAware.getApplicationContext(); + EdgeIngestor ei = ctx.getBean(EdgeIngestor.class); + setEdgeIngestor(ei); + } + + protected void setEdgeIngestor(EdgeIngestor ei) { + this.edgeRules = ei; + } }