Update the aai-common with the latest code
[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          * @{inheritDoc}
115          */
116         @Override
117         public void processNamespace(Introspector obj) {
118         
119         }
120
121         /**
122          * @{inheritDoc}
123          */
124         @Override
125         public String getCloudRegionTransform() {
126                 return "add";
127         }
128
129         /**
130          * @{inheritDoc}
131          */
132         @Override
133         public boolean useOriginalLoader() {
134                 return false;
135         }
136
137
138         @Override
139         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
140                         throws AAIException {
141                 this.resultResource = obj.getDbName();
142                 if (previous != null) {
143                         this.parentName = previous.getDbName();
144                 }
145                 this.previous  = obj;
146                                 
147         }
148
149
150         @Override
151         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
152                         boolean isFinalContainer) throws AAIException {
153                 this.containerResource = obj.getName();
154                 if (previous != null) {
155                         this.parentName = previous.getDbName();
156                 }
157                 if (isFinalContainer) {
158                         this.endsInContainer = true;
159                         this.resultResource = obj.getChildDBName();
160                         
161                         this.finalContainer = obj;
162                 }               
163         }
164         
165 }