* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
* ============LICENSE_END=========================================================
*/
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Direction;
+import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
-
import org.openecomp.aai.db.props.AAIProperties;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.introspection.Introspector;
import org.openecomp.aai.serialization.db.EdgeRules;
import org.openecomp.aai.serialization.db.EdgeType;
import org.openecomp.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
+
import com.google.common.base.Joiner;
/**
* The Class GremlinQueryBuilder.
*/
-public abstract class GremlinQueryBuilder extends QueryBuilder {
+public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> {
private EdgeRules edgeRules = EdgeRules.getInstance();
private GremlinGroovyShellSingleton gremlinGroovy = GremlinGroovyShellSingleton.getInstance();
* @{inheritDoc}
*/
@Override
- public QueryBuilder createDBQuery(Introspector obj) {
+ public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
this.createKeyQuery(obj);
this.createContainerQuery(obj);
- return this;
+ return (QueryBuilder<Vertex>) this;
}
@Override
- public QueryBuilder exactMatchQuery(Introspector obj) {
+ public QueryBuilder<Vertex> exactMatchQuery(Introspector obj) {
// TODO not implemented because this is implementation is no longer used
this.createKeyQuery(obj);
//allPropertiesQuery(obj);
this.createContainerQuery(obj);
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder getVerticesByIndexedProperty(String key, Object value) {
+ public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value) {
return this.getVerticesByProperty(key, value);
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder getVerticesByIndexedProperty(String key, List<?> values) {
+ public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values) {
return this.getVerticesByProperty(key, values);
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder getVerticesByProperty(String key, Object value) {
+ public QueryBuilder<Vertex> getVerticesByProperty(String key, Object value) {
String term = "";
if (value != null && !value.getClass().getName().equals("java.lang.String")) {
}
list.add(".has('" + key + "', " + term + ")");
stepIndex++;
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder getVerticesByProperty(String key, List<?> values) {
+ public QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values) {
String term = "";
String predicate = "P.within(#!#argument#!#)";
predicate = predicate.replace("#!#argument#!#", argument);
list.add(".has('" + key + "', " + predicate + ")");
stepIndex++;
- return this;
+ return (QueryBuilder<Vertex>) this;
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder getChildVerticesFromParent(String parentKey, String parentValue, String childType) {
+ public QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType) {
/*
String query = ".has('aai-node-type', '" + childType + "')";
return this.processGremlinQuery(parentKey, parentValue, query);
*/
//TODO
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder getTypedVerticesByMap(String type, LinkedHashMap<String, String> map) {
+ public QueryBuilder<Vertex> getTypedVerticesByMap(String type, LinkedHashMap<String, String> map) {
for (String key : map.keySet()) {
list.add(".has('" + key + "', '" + map.get(key) + "')");
}
list.add(".has('aai-node-type', '" + type + "')");
stepIndex++;
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder createKeyQuery(Introspector obj) {
+ public QueryBuilder<Vertex> createKeyQuery(Introspector obj) {
Set<String> keys = obj.getKeys();
for (String key : keys) {
this.getVerticesByProperty(key, obj.<Object>getValue(key));
}
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
if (child.isContainer()) {
childName = child.getChildDBName();
}
- this.edgeQuery(type, parentName, childName);
+ this.edgeQueryToVertex(type, parentName, childName);
return this;
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException, NoEdgeRuleFoundException {
+ public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException, NoEdgeRuleFoundException {
String nodeType = parent.<String>property(AAIProperties.NODE_TYPE).orElse(null);
- this.edgeQuery(type, nodeType, child.getDbName());
+ this.edgeQueryToVertex(type, nodeType, child.getDbName());
- return this;
+ return (QueryBuilder<Vertex>) this;
}
+ @Override
+ public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
+ this.edgeQuery(type, outNodeType, inNodeType);
+
+ return (QueryBuilder<Edge>)this;
+
+ }
/**
* Edge query.
*
* @throws NoEdgeRuleFoundException
* @throws AAIException
*/
- private void edgeQuery(EdgeType type, String outType, String inType) throws AAIException, NoEdgeRuleFoundException {
+ private void edgeQueryToVertex(EdgeType type, String outType, String inType) throws AAIException, NoEdgeRuleFoundException {
markParentBoundary();
EdgeRule rule = edgeRules.getEdgeRule(type, outType, inType);
if (rule.getDirection().equals(Direction.OUT)) {
} else {
list.add(".in('" + rule.getLabel() + "')");
}
+ stepIndex++;
list.add(".has('" + AAIProperties.NODE_TYPE + "', '" + inType + "')");
- stepIndex += 2;
+ stepIndex++;
+
+ }
+
+ /**
+ * Edge query.
+ *
+ * @param outType the out type
+ * @param inType the in type
+ * @throws NoEdgeRuleFoundException
+ * @throws AAIException
+ */
+ private void edgeQuery(EdgeType type, String outType, String inType) throws AAIException, NoEdgeRuleFoundException {
+ markParentBoundary();
+ EdgeRule rule = edgeRules.getEdgeRule(type, outType, inType);
+ if (rule.getDirection().equals(Direction.OUT)) {
+ list.add(".outE('" + rule.getLabel() + "')");
+ } else {
+ list.add(".inV('" + rule.getLabel() + "')");
+ }
+ stepIndex++;
+
}
@Override
- public QueryBuilder limit(long amount) {
+ public QueryBuilder<E> limit(long amount) {
list.add(".limit(" + amount + ")");
return this;
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder createContainerQuery(Introspector obj) {
+ public QueryBuilder<Vertex> createContainerQuery(Introspector obj) {
String type = obj.getChildDBName();
String abstractType = obj.getMetadata(ObjectMetadata.ABSTRACT);
if (abstractType != null) {
}
stepIndex++;
this.markContainer();
- return this;
+ return (QueryBuilder<Vertex>) this;
}
@Override
- public QueryBuilder union(QueryBuilder... builder) {
+ public QueryBuilder<E> union(QueryBuilder<E>... builder) {
markParentBoundary();
String[] traversals = new String[builder.length];
StringBuilder command = new StringBuilder();
}
@Override
- public QueryBuilder where(QueryBuilder... builder) {
+ public QueryBuilder<E> where(QueryBuilder<E>... builder) {
markParentBoundary();
List<String> traversals = new ArrayList<>();
for (int i = 0; i < builder.length; i++) {
return this;
}
+ @Override
+ public QueryBuilder<E> store(String name) {
+ this.list.add(".store('"+ name + "')");
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> cap(String name) {
+ this.list.add(".cap('"+ name + "')");
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> unfold() {
+ this.list.add(".unfold()");
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> dedup() {
+ this.list.add(".dedup()");
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> emit() {
+ this.list.add(".emit()");
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> repeat(QueryBuilder<E> builder) {
+ this.list.add(".repeat(__" + builder.getQuery() + ")");
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<Edge> outE() {
+ this.list.add(".outE()");
+ stepIndex++;
+
+ return (QueryBuilder<Edge>)this;
+ }
+
+ @Override
+ public QueryBuilder<Edge> inE() {
+ this.list.add(".inE()");
+ stepIndex++;
+
+ return (QueryBuilder<Edge>)this;
+ }
+
+ @Override
+ public QueryBuilder<Vertex> outV() {
+ this.list.add(".outV()");
+ stepIndex++;
+
+ return (QueryBuilder<Vertex>)this;
+ }
+
+ @Override
+ public QueryBuilder<Vertex> inV() {
+ this.list.add(".inV()");
+ stepIndex++;
+
+ return (QueryBuilder<Vertex>)this;
+ }
+
+ @Override
+ public QueryBuilder<E> not(QueryBuilder<E> builder) {
+ this.list.add(".not(" + "__" + builder.getQuery() + ")");
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> as(String name) {
+ this.list.add(".as('" + name + "')");
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> select(String name) {
+ this.list.add(".select('" + name + "')");
+ stepIndex++;
+
+ return this;
+ }
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder getParentQuery() {
+ public QueryBuilder<E> getParentQuery() {
return cloneQueryAtStep(parentStepIndex);
}
@Override
- public QueryBuilder getContainerQuery() {
+ public QueryBuilder<E> getContainerQuery() {
return cloneQueryAtStep(containerStepIndex);
}
* @{inheritDoc}
*/
@Override
- public <T> T getQuery() {
+ public <T2> T2 getQuery() {
StringBuilder sb = new StringBuilder();
for (String piece : this.list) {
sb.append(piece);
}
- return (T)sb.toString();
+ return (T2)sb.toString();
}
/**
this.containerStepIndex = stepIndex;
}
- protected abstract QueryBuilder cloneQueryAtStep(int index);
+ protected abstract QueryBuilder<E> cloneQueryAtStep(int index);
/**
* @{inheritDoc}
*/
}
@Override
- public Vertex next() {
+ public E next() {
if (this.completeTraversal == null) {
executeQuery();
}
- return (Vertex)this.completeTraversal.next();
+ return (E)this.completeTraversal.next();
}
@Override
- public List<Vertex> toList() {
+ public List<E> toList() {
if (this.completeTraversal == null) {
executeQuery();
}
- return (List<Vertex>)this.completeTraversal.toList();
+ return (List<E>)this.completeTraversal.toList();
}
}