Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / query / builder / HistoryGremlinTraversal.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.query.builder;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.onap.aai.db.props.AAIProperties;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.parsers.query.QueryParser;
37
38 /**
39  * The Class GremlinTraversal.
40  */
41 public class HistoryGremlinTraversal<E> extends GremlinTraversal<E> {
42
43     /**
44      * Instantiates a new gremlin traversal.
45      *
46      * @param loader the loader
47      */
48     public HistoryGremlinTraversal(Loader loader, GraphTraversalSource source) {
49         super(loader, source);
50     }
51
52     /**
53      * Instantiates a new gremlin traversal.
54      *
55      * @param loader the loader
56      * @param start the start
57      */
58     public HistoryGremlinTraversal(Loader loader, GraphTraversalSource source, Vertex start) {
59         super(loader, source, start);
60     }
61
62     protected HistoryGremlinTraversal(List<String> traversal, Loader loader, GraphTraversalSource source,
63             GremlinQueryBuilder<E> gtb) {
64         super(traversal, loader, source, gtb);
65     }
66
67     /**
68      * @{inheritDoc}
69      */
70     @Override
71     public QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException {
72         return factory.buildURIParser(uri);
73     }
74
75     /**
76      * @{inheritDoc}
77      */
78     @Override
79     public QueryParser createQueryFromRelationship(Introspector relationship)
80             throws UnsupportedEncodingException, AAIException {
81         return factory.buildRelationshipParser(relationship);
82     }
83
84     /**
85      * @{inheritDoc}
86      */
87     @Override
88     public QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams)
89             throws UnsupportedEncodingException, AAIException {
90         return factory.buildURIParser(uri, queryParams);
91     }
92
93     /**
94      * @{inheritDoc}
95      */
96     @Override
97     public QueryParser createQueryFromObjectName(String objName) {
98         return factory.buildObjectNameParser(objName);
99     }
100
101     /**
102      * @{inheritDoc}
103      */
104     @Override
105     public QueryBuilder<E> newInstance(Vertex start) {
106         return new HistoryGremlinTraversal<>(loader, source, start);
107     }
108
109     /**
110      * @{inheritDoc}
111      */
112     @Override
113     public QueryBuilder<E> newInstance() {
114         return new HistoryGremlinTraversal<>(loader, source);
115     }
116
117     @Override
118     protected QueryBuilder<E> cloneQueryAtStep(int index) {
119
120         int idx = index;
121
122         if (idx == 0) {
123             idx = stepIndex;
124         }
125
126         List<String> newList = new ArrayList<>();
127         for (int i = 0; i < idx; i++) {
128             newList.add(this.list.get(i));
129         }
130
131         return new HistoryGremlinTraversal<>(newList, loader, source, this);
132     }
133
134     @Override
135     protected void vertexHas(String key, Object value) {
136         super.vertexHas(key, value);
137         touchHistoryProperties(key, value);
138     }
139
140     @Override
141     protected void vertexHasNot(String key) {
142         super.vertexHasNot(key);
143         touchHistoryProperties(key);
144
145     }
146
147     @Override
148     protected void vertexHas(String key) {
149         super.vertexHas(key);
150         touchHistoryProperties(key);
151     }
152
153     /*
154      * This is required for the subgraphstrategies to work
155      */
156     private void touchHistoryProperties(String key) {
157         if (key != null && !key.isEmpty() && !key.equals(AAIProperties.NODE_TYPE)) {
158             list.add(".where(__.properties('" + key + "'))");
159         }
160
161     }
162
163     private void touchHistoryProperties(String key, Object value) {
164         if (key != null && !key.isEmpty() && !key.equals(AAIProperties.NODE_TYPE)) {
165             list.add(".where(__.properties('" + key + "').hasValue(" + value + "))");
166         }
167     }
168 }