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