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