[AAI-178 Amsterdam] Make Edge Properties to be
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / parsers / uri / URIToObject.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.uri;
22
23 import org.openecomp.aai.exceptions.AAIException;
24 import org.openecomp.aai.introspection.Introspector;
25 import org.openecomp.aai.introspection.Loader;
26 import org.openecomp.aai.introspection.Version;
27 import org.openecomp.aai.schema.enums.ObjectMetadata;
28 import org.openecomp.aai.serialization.db.EdgeType;
29
30 import javax.ws.rs.core.MultivaluedMap;
31 import java.io.UnsupportedEncodingException;
32 import java.net.URI;
33 import java.util.HashMap;
34 import java.util.List;
35
36 /**
37  * Given a URI this class returns an object, or series of nested objects
38  * with their keys populated based off the values in the URI.
39  * 
40  * It populates the keys in the order they are listed in the model.
41  * 
42  
43  *
44  */
45 public class URIToObject implements Parsable {
46
47         
48         private Introspector topEntity = null;
49         
50         private String topEntityName = null;
51         
52         private String entityName = null;
53         
54         private Introspector entity = null;
55         
56         private Introspector previous = null;
57         
58         private List<Object> parentList = null;
59         
60         private Version version = null;
61         private Loader loader = null;
62         private final HashMap<String, Introspector> relatedObjects;
63         
64         /**
65          * Instantiates a new URI to object.
66          *
67          * @param loader the loader
68          * @param uri the uri
69          * @throws IllegalArgumentException the illegal argument exception
70          * @throws AAIException the AAI exception
71          * @throws UnsupportedEncodingException the unsupported encoding exception
72          */
73         public URIToObject(Loader loader, URI uri) throws AAIException, UnsupportedEncodingException {
74                 
75                 URIParser parser = new URIParser(loader, uri);
76                 this.relatedObjects = new HashMap<>();
77                 
78                 parser.parse(this);
79                 this.loader = parser.getLoader();
80                 this.version = loader.getVersion();
81         }
82         public URIToObject(Loader loader, URI uri, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
83                 
84                 URIParser parser = new URIParser(loader, uri);
85                 this.relatedObjects = relatedObjects;
86
87                 parser.parse(this);
88                 this.loader = parser.getLoader();
89                 this.version = loader.getVersion();
90
91         }
92         
93         /**
94          * @{inheritDoc}
95          */
96         @Override
97         public void processNamespace(Introspector obj) {
98         
99         }
100         
101         /**
102          * @{inheritDoc}
103          */
104         @Override
105         public String getCloudRegionTransform() {
106                 return "add";
107         }
108         
109         /**
110          * @{inheritDoc}
111          */
112         @Override
113         public boolean useOriginalLoader() {
114                 // TODO Auto-generated method stub
115                 return false;
116         }
117
118         /**
119          * Gets the top entity.
120          *
121          * @return the top entity
122          */
123         public Introspector getTopEntity() {
124                 return this.topEntity;
125         }
126         
127         /**
128          * Gets the entity.
129          *
130          * @return the entity
131          */
132         public Introspector getEntity() {
133                 return this.entity;
134         }
135         
136         /**
137          * Gets the parent list.
138          *
139          * @return the parent list
140          */
141         public List<Object> getParentList() {
142                 return this.parentList;
143         }
144         
145         /**
146          * Gets the entity name.
147          *
148          * @return the entity name
149          */
150         public String getEntityName() {
151                 return this.entityName;
152         }
153         
154         /**
155          * Gets the top entity name.
156          *
157          * @return the top entity name
158          */
159         public String getTopEntityName() {
160                 return this.topEntityName;
161         }
162         
163         /**
164          * Gets the object version.
165          *
166          * @return the object version
167          */
168         public Version getObjectVersion() {
169                 return this.loader.getVersion();
170         }
171         public Loader getLoader() {
172                 return this.loader;
173         }
174         @Override
175         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
176                         throws AAIException {
177
178                 if (this.entityName == null) {
179                         this.topEntityName = obj.getDbName();
180                         this.topEntity = obj;
181                 }
182                 this.entityName = obj.getDbName();
183                 this.entity = obj;
184                 this.parentList = (List<Object>)this.previous.getValue(obj.getName());
185                 this.parentList.add(entity.getUnderlyingObject());
186                 
187                 for (String key : uriKeys.keySet()) {
188                         entity.setValue(key, uriKeys.getFirst(key));
189                 }
190                 try {
191                         if (relatedObjects.containsKey(entity.getObjectId())) {
192                                 Introspector relatedObject = relatedObjects.get(entity.getObjectId());
193                                 String nameProp = relatedObject.getMetadata(ObjectMetadata.NAME_PROPS);
194                                 if (nameProp == null) {
195                                         nameProp = "";
196                                 }
197                                 if (nameProp != null && !nameProp.equals("")) {
198                                         String[] nameProps = nameProp.split(",");
199                                         for (String prop : nameProps) {
200                                                 entity.setValue(prop, relatedObject.getValue(prop));
201                                         }
202                                 }
203                         }
204                 } catch (UnsupportedEncodingException e) {
205                 }
206                 this.previous = entity;
207         }
208         @Override
209         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
210                         boolean isFinalContainer) throws AAIException {
211                 this.previous = obj;
212
213                 if (this.entity != null) {
214                         this.entity.setValue(obj.getName(), obj.getUnderlyingObject());
215                 } else {
216                         this.entity = obj;
217                         this.topEntity = obj;
218                 }
219         }
220 }