Initial commit with all the necessary files
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / query / builder / GremlinUnique.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.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
33 import org.openecomp.aai.exceptions.AAIException;
34 import org.openecomp.aai.introspection.Introspector;
35 import org.openecomp.aai.introspection.Loader;
36 import org.openecomp.aai.parsers.query.QueryParser;
37 import org.openecomp.aai.parsers.query.TraversalStrategy;
38 import org.openecomp.aai.parsers.query.UniqueStrategy;
39
40 /**
41  * The Class GremlinUnique.
42  */
43 public class GremlinUnique extends GremlinQueryBuilder {
44         
45         /**
46          * Instantiates a new gremlin unique.
47          *
48          * @param loader the loader
49          */
50         public GremlinUnique(Loader loader, GraphTraversalSource source) {
51                 super(loader, source);
52                 this.factory = new UniqueStrategy(this.loader, this);
53         }
54         
55         /**
56          * Instantiates a new gremlin unique.
57          *
58          * @param loader the loader
59          * @param start the start
60          */
61         public GremlinUnique(Loader loader, GraphTraversalSource source, Vertex start) {
62                 super(loader, source, start);
63                 this.factory = new UniqueStrategy(this.loader, this);
64         }
65         
66         protected GremlinUnique(List<String> traversal, Loader loader, GraphTraversalSource source, GremlinQueryBuilder gtb) {
67                 super(loader, source);
68                 this.list = traversal;
69                 this.stepIndex = gtb.getStepIndex();
70                 this.parentStepIndex = gtb.getParentStepIndex();
71                 this.containerStepIndex = gtb.getContainerStepIndex();
72                 this.factory = new TraversalStrategy(this.loader, this);
73                 this.start = gtb.getStart();
74         }
75         
76         /**
77          * @{inheritDoc}
78          */
79         @Override
80         public QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException {
81                 return factory.buildURIParser(uri);
82         }
83
84         /**
85          * @{inheritDoc}
86          */
87         @Override
88         public QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException {
89                 return factory.buildRelationshipParser(relationship);
90         }
91
92         /**
93          * @{inheritDoc}
94          */
95         @Override
96         public QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams)
97                         throws UnsupportedEncodingException, AAIException {
98                 return factory.buildURIParser(uri, queryParams);
99         }
100         
101         /**
102          * @{inheritDoc}
103          */
104         @Override
105         public QueryParser createQueryFromObjectName(String objName) {
106                 return factory.buildObjectNameParser(objName);
107         }
108         
109         /**
110          * @{inheritDoc}
111          */
112         @Override
113         public QueryBuilder newInstance() {
114                 return new GremlinUnique(loader, source);
115         }
116
117         /**
118          * @{inheritDoc}
119          */
120         @Override
121         public QueryBuilder newInstance(Vertex start) {
122                 return new GremlinUnique(loader, source, start);
123         }
124         
125         @Override
126         protected QueryBuilder cloneQueryAtStep(int index) {
127                 if (index == 0) {
128                         index = stepIndex;
129                 }
130                 List<String> newList = new ArrayList<>();
131                 for (int i = 0; i < index; i++) {
132                         newList.add(this.list.get(i));
133                 }
134                 
135                 return new GremlinUnique(newList, loader, source, this);
136         }
137 }