Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / query / builder / HistoryTraversalURIOptimizedQuery.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.P;
24 import org.apache.tinkerpop.gremlin.process.traversal.Step;
25 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
29 import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.introspection.Introspector;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.schema.enums.ObjectMetadata;
35
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
41
42 public class HistoryTraversalURIOptimizedQuery<E> extends TraversalURIOptimizedQuery {
43
44     protected Map<Integer, String> stepToAaiUri = new HashMap<>();
45
46     public HistoryTraversalURIOptimizedQuery(Loader loader, GraphTraversalSource source) {
47         super(loader, source);
48     }
49
50     public HistoryTraversalURIOptimizedQuery(Loader loader, GraphTraversalSource source, Vertex start) {
51         super(loader, source, start);
52     }
53
54     protected HistoryTraversalURIOptimizedQuery(GraphTraversal traversal, Loader loader, GraphTraversalSource source,
55                                                 GraphTraversalBuilder gtb) {
56         super(traversal, loader, source, gtb);
57     }
58
59     protected HistoryTraversalURIOptimizedQuery(GraphTraversal traversal, Loader loader, GraphTraversalSource source,
60                                                 GraphTraversalBuilder gtb, Map<Integer, String> stepToAaiUri) {
61         super(traversal, loader, source, gtb, stepToAaiUri);
62     }
63
64     @Override
65     protected void vertexHas(String key, Object value) {
66         super.vertexHas(key, value);
67         touchHistoryProperties(key, value);
68     }
69
70     @Override
71     protected void vertexHasNot(String key) {
72         super.vertexHasNot(key);
73         touchHistoryProperties(key);
74     }
75
76     @Override
77     protected void vertexHas(String key) {
78         super.vertexHas(key);
79         touchHistoryProperties(key);
80     }
81
82     private void touchHistoryProperties(String key){
83         if(key != null && !key.isEmpty() && !key.equals(AAIProperties.NODE_TYPE)) {
84             traversal.where(__.properties(key));
85         }
86     }
87
88     private void touchHistoryProperties(String key, Object value){
89         if(key != null && !key.isEmpty() && !key.equals(AAIProperties.NODE_TYPE)) {
90             traversal.where(__.properties(key).hasValue(value));
91         }
92     }
93 }