AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / parsers / query / QueryParser.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.parsers.query;
22
23 import java.net.URI;
24
25 import org.apache.tinkerpop.gremlin.structure.Vertex;
26 import org.onap.aai.config.SpringContextAware;
27 import org.onap.aai.introspection.Loader;
28 import org.onap.aai.introspection.LoaderFactory;
29 import org.onap.aai.introspection.LoaderUtil;
30 import org.onap.aai.query.builder.QueryBuilder;
31 import org.onap.aai.setup.SchemaVersion;
32 import org.onap.aai.setup.SchemaVersions;
33
34 /**
35  * The Class QueryParser.
36  */
37 public abstract class QueryParser {
38
39     protected Loader loader = null;
40     protected Loader latestLoader = null;
41     protected QueryBuilder<Vertex> queryBuilder = null;
42
43     protected QueryBuilder<Vertex> parentQueryBuilder = null;
44
45     protected URI uri = null;
46
47     protected String resultResource = "";
48
49     protected String parentResourceType = "";
50
51     protected String containerResource = "";
52
53     /**
54      * Instantiates a new query parser.
55      *
56      * @param loader the loader
57      * @param queryBuilder the query builder
58      * @param uri the uri
59      */
60     protected QueryParser(Loader loader, QueryBuilder<Vertex> queryBuilder, URI uri) {
61         this.uri = uri;
62         this.queryBuilder = queryBuilder;
63         this.loader = loader;
64         LoaderFactory loaderFactory = SpringContextAware.getBean(LoaderFactory.class);
65         SchemaVersion latest = ((SchemaVersions) SpringContextAware.getBean("schemaVersions")).getDefaultVersion();
66
67         this.latestLoader = loaderFactory.createLoaderForVersion(loader.getModelType(), latest);
68     }
69
70     /**
71      * Instantiates a new query parser.
72      *
73      * @param loader the loader
74      * @param queryBuilder the query builder
75      */
76     protected QueryParser(Loader loader, QueryBuilder<Vertex> queryBuilder) {
77         this.queryBuilder = queryBuilder;
78         this.loader = loader;
79         this.latestLoader = LoaderUtil.getLatestVersion();
80     }
81
82     /**
83      * Gets the container type.
84      *
85      * @return the container type
86      */
87     public String getContainerType() {
88
89         return this.containerResource;
90     }
91
92     /**
93      * Gets the parent result type.
94      *
95      * @return the parent result type
96      */
97     public String getParentResultType() {
98         return this.parentResourceType;
99     }
100
101     /**
102      * Gets the result type.
103      *
104      * @return the result type
105      */
106     public String getResultType() {
107         return this.resultResource;
108     }
109
110     /**
111      * Gets the query builder.
112      *
113      * @return the query builder
114      */
115     public QueryBuilder<Vertex> getQueryBuilder() {
116         return this.queryBuilder;
117     }
118
119     /**
120      * Gets the uri.
121      *
122      * @return the uri
123      */
124     public URI getUri() {
125         return this.uri;
126     }
127
128     /**
129      * Gets the parent query builder.
130      *
131      * @return the parent query builder
132      */
133     public QueryBuilder<Vertex> getParentQueryBuilder() {
134         if (this.parentQueryBuilder != null) {
135             return this.parentQueryBuilder;
136         } else {
137             return this.queryBuilder;
138         }
139     }
140
141     /**
142      * Checks if is dependent.
143      *
144      * @return true, if is dependent
145      */
146     public boolean isDependent() {
147         return !this.queryBuilder.getQuery().toString()
148                 .equals(this.queryBuilder.getParentQuery().getQuery().toString());
149     }
150
151 }