Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / parsers / query / UniqueURIQueryParser.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.parsers.query;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25
26 import javax.ws.rs.core.MultivaluedMap;
27 import javax.ws.rs.core.UriBuilder;
28
29 import org.openecomp.aai.exceptions.AAIException;
30 import org.openecomp.aai.introspection.Introspector;
31 import org.openecomp.aai.introspection.Loader;
32 import org.openecomp.aai.parsers.uri.Parsable;
33 import org.openecomp.aai.parsers.uri.URIParser;
34 import org.openecomp.aai.parsers.uri.URIToDBKey;
35 import org.openecomp.aai.query.builder.QueryBuilder;
36 import org.openecomp.aai.serialization.db.EdgeType;
37
38
39 /**
40  * The Class UniqueURIQueryParser.
41  */
42 public class UniqueURIQueryParser extends QueryParser implements Parsable {
43
44         
45         private URIToDBKey dbKeyParser = null;
46         
47         private Introspector previous = null;
48         
49         private boolean endsInContainer = false;
50         
51         private Introspector finalContainer = null;
52         
53         private String parentName = "";
54         
55         /**
56          * Instantiates a new unique URI query parser.
57          *
58          * @param loader the loader
59          * @param queryBuilder the query builder
60          * @param uri the uri
61          * @throws UnsupportedEncodingException the unsupported encoding exception
62          * @throws IllegalArgumentException the illegal argument exception
63          * @throws AAIException the AAI exception
64          */
65         public UniqueURIQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri) throws UnsupportedEncodingException, IllegalArgumentException, AAIException {
66                 super(loader, queryBuilder, uri);
67                 URIParser parser = new URIParser(loader, uri);
68                 parser.parse(this);
69                 
70                 if (!endsInContainer) {
71                         this.dbKeyParser = new URIToDBKey(loader, uri);
72                         String dbKey = (String)dbKeyParser.getResult();
73                         queryBuilder.getVerticesByIndexedProperty("aai-unique-key", dbKey);
74                         queryBuilder.markParentBoundary();
75                         
76                         if (!(parentName.equals("") || parentName.equals(this.resultResource))) {
77                                 URI parentUri = UriBuilder.fromPath(uri.getRawPath().substring(0, uri.getRawPath().indexOf(containerResource))).build();
78                                 this.dbKeyParser = new URIToDBKey(loader, parentUri);
79                                 this.parentQueryBuilder = queryBuilder.newInstance().getVerticesByIndexedProperty("aai-unique-key", (String)dbKeyParser.getResult());
80                                 this.parentResourceType = parentName;
81                         } 
82                         this.containerResource = "";
83                 } else {
84                         URI parentUri = UriBuilder.fromPath(uri.getRawPath().substring(0, uri.getRawPath().indexOf(this.finalContainer.getDbName()))).build();
85                         this.dbKeyParser = new URIToDBKey(loader, parentUri);
86                         String dbKey = (String)dbKeyParser.getResult();
87                         this.parentResourceType = parentName;
88
89                         if (!dbKey.equals("")) {
90                                 queryBuilder.getVerticesByIndexedProperty("aai-unique-key", dbKey);
91                                 queryBuilder.markParentBoundary();
92                                 queryBuilder.createEdgeTraversal(EdgeType.TREE, previous, finalContainer);
93
94                         } 
95                         
96                         queryBuilder.createContainerQuery(finalContainer);
97                                 
98                         
99                 }
100         }
101         
102         
103         /**
104          * Instantiates a new unique URI query parser.
105          *
106          * @param loader the loader
107          * @param queryBuilder the query builder
108          */
109         public UniqueURIQueryParser(Loader loader, QueryBuilder queryBuilder) {
110                 super(loader, queryBuilder);
111         }
112
113
114         /**
115          * @{inheritDoc}
116          */
117         @Override
118         public void processObject(Introspector obj, MultivaluedMap<String, String> uriKeys) {
119                 this.resultResource = obj.getDbName();
120                 if (previous != null) {
121                         this.parentName = previous.getDbName();
122                 }
123                 this.previous  = obj;
124                 
125                 
126         }
127
128
129         /**
130          * @{inheritDoc}
131          */
132         @Override
133         public void processContainer(Introspector obj, MultivaluedMap<String, String> uriKeys, boolean isFinalContainer) {
134                 this.containerResource = obj.getName();
135                 if (previous != null) {
136                         this.parentName = previous.getDbName();
137                 }
138                 if (isFinalContainer) {
139                         this.endsInContainer = true;
140                         this.resultResource = obj.getChildDBName();
141                         
142                         this.finalContainer = obj;
143                 }
144                 
145         }
146         
147         /**
148          * @{inheritDoc}
149          */
150         @Override
151         public void processNamespace(Introspector obj) {
152         
153         }
154
155         /**
156          * @{inheritDoc}
157          */
158         @Override
159         public String getCloudRegionTransform() {
160                 return "add";
161         }
162
163         /**
164          * @{inheritDoc}
165          */
166         @Override
167         public boolean useOriginalLoader() {
168                 return false;
169         }
170         
171 }