Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / introspection / JSONStrategy.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.introspection;
22
23 import java.io.UnsupportedEncodingException;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.UUID;
28
29 import org.json.simple.JSONObject;
30
31 import org.openecomp.aai.schema.enums.ObjectMetadata;
32 import org.openecomp.aai.schema.enums.PropertyMetadata;
33
34 public class JSONStrategy extends Introspector {
35
36         private JSONObject json = null;
37         private String namedType = "";
38         protected JSONStrategy(Object o) {
39                 super(o);
40                 json = (JSONObject)o;
41                 //Assumes you provide a wrapper
42                 Set<String> keySet = json.keySet();
43                 if (keySet.size() == 1) {
44                         namedType = keySet.iterator().next();
45                         json = (JSONObject)json.get(namedType);
46                 } else {
47                         throw new IllegalArgumentException("This object has no named type.");
48                 }
49         }
50         
51         protected JSONStrategy(Object o, String namedType) {
52                 super(o);
53                 json = (JSONObject)o;
54                 this.namedType = namedType;
55                 
56         }
57         
58         @Override
59         public boolean hasProperty(String name) {
60                 //TODO 
61                 return true;
62         }
63         @Override
64         public Object getValue(String name) {
65                 Object result = "";
66                 result = json.get(name);
67                 
68                 return result;
69         }
70
71         @Override
72         public void setValue(String name, Object obj) {
73                 json.put(name, obj);
74                 
75         }
76         @Override
77         public Object getUnderlyingObject() {
78                 return this.json;
79         }
80         
81         @Override
82         public Set<String> getProperties() {
83                 Set<String> result = json.keySet();
84                 return result;
85         }
86         
87         @Override
88         public Set<String> getRequiredProperties() {
89                 //unknowable
90                 
91                 return this.getProperties();
92         }
93
94         @Override
95         public Set<String> getKeys() {
96                 //unknowable
97                 return this.getProperties();
98         }
99
100         @Override
101         public Set<String> getAllKeys() {
102                 //unknowable
103                 return this.getProperties();
104         }
105
106         @Override
107         public String getType(String name) {
108                 String result = "";
109                 Class<?> resultClass = this.getClass(name);
110                 if (resultClass != null) {
111                         result = resultClass.getName();
112                 }
113                 
114                 if (result.equals("org.json.simple.JSONArray")) {
115                         result = "java.util.List";
116                 }
117                 
118                 return result;
119         }
120
121         @Override
122         public String getGenericType(String name) {
123                 String result = "";
124                 Class<?> resultClass = this.getGenericTypeClass(name);
125                 if (resultClass != null) {
126                         result = resultClass.getName();
127                 }
128                 return result;
129         }
130
131         @Override
132         public String getJavaClassName() {
133                 return json.getClass().getName();
134         }
135
136         @Override
137         public Class<?> getClass(String name) {
138                 Class<?> result = null;
139                 result = json.get(name).getClass();
140                 
141                 return result;
142         }
143
144         @Override
145         public Class<?> getGenericTypeClass(String name) {
146                 Object resultObject = null;
147                 Class<?> resultClass = null;
148                 resultObject = this.getValue(name);
149                 if (resultObject.getClass().getName().equals("org.json.simple.JSONArray")) {
150                         resultClass = ((List)resultObject).get(0).getClass();
151                 }
152                 
153                 return resultClass;
154         }
155
156         @Override
157         public Object newInstanceOfProperty(String name) {
158                 try {
159                         return this.getClass(name).newInstance();
160                 } catch (InstantiationException | IllegalAccessException e) {
161                         return null;
162                 }
163         }
164
165         @Override
166         public Object newInstanceOfNestedProperty(String name) {
167                 try {
168                         return this.getGenericTypeClass(name).newInstance();
169                 } catch (InstantiationException | IllegalAccessException e) {
170                         return null;
171                 }
172         }
173         
174         @Override
175         public boolean isComplexType(String name) {
176                 String result = this.getType(name);
177                 
178                 if (result.contains("JSONObject")) {
179                         return true;
180                 } else {
181                         return false;
182                 }
183                 
184         }
185         
186         @Override
187         public boolean isComplexGenericType(String name) {
188                 String result = this.getGenericType(name);
189                 
190                 if (result.contains("JSONObject")) {
191                         return true;
192                 } else {
193                         return false;
194                 }
195                 
196         }
197         
198         @Override
199         public boolean isListType(String name) {
200                 String result = this.getType(name);
201                 
202                 if (result.contains("java.util.List")) {
203                         return true;
204                 } else {
205                         return false;
206                 }
207                 
208         }
209         
210         @Override
211         public boolean isContainer() {
212                 Set<String> props = this.getProperties();
213                 boolean result = false;
214                 if (props.size() == 1 && this.isListType(props.iterator().next())) {
215                         result = true;
216                 }
217                 
218                 return result;
219         }
220         @Override
221         protected String findKey() {
222                 return "";
223         }
224         
225         @Override
226         public String getName() {
227                 return this.namedType;
228         }
229         
230         @Override
231         public String getDbName() {
232                 return this.getName();
233         }
234         
235         @Override
236         public String getURI() {
237                 
238                 // use a UUID for now 
239                 return UUID.randomUUID().toString();
240         }
241         
242         @Override
243         public String getGenericURI() {
244                 
245                 //there is none defined for this
246                 return "";
247         }
248         
249         @Override
250         public String preProcessKey (String key) {
251                 
252                 // don't do anything with it
253                 return key;
254                 
255         }
256         
257         @Override
258         public String marshal(MarshallerProperties properties) {
259                 //TODO
260                 return null;
261         }
262         
263         @Override
264         public Object clone() {
265                 //TODO
266                 return null;
267         }
268         
269         /*@Override
270         public String findEdgeName(String parent, String child) {
271                 
272                 // Always has for now
273                 return "has";
274                 
275         }*/
276         
277         @Override
278         public ModelType getModelType() {
279                 return ModelType.JSON;
280         }
281
282         @Override
283         public Set<String> getIndexedProperties() {
284                 // TODO Auto-generated method stub
285                 return null;
286         }
287
288         @Override
289         public String getChildName() {
290                 // TODO Auto-generated method stub
291                 return null;
292         }
293
294         @Override
295         public boolean hasChild(Introspector child) {
296                 // TODO Auto-generated method stub
297                 return false;
298         }
299
300         @Override
301         public boolean isSimpleType(String name) {
302                 // TODO Auto-generated method stub
303                 return false;
304         }
305
306         @Override
307         public boolean isSimpleGenericType(String name) {
308                 // TODO Auto-generated method stub
309                 return false;
310         }
311
312         @Override
313         public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
314                 // TODO Auto-generated method stub
315                 return null;
316         }
317
318         @Override
319         public String getMetadata(ObjectMetadata metadataName) {
320                 // TODO Auto-generated method stub
321                 return null;
322         }
323
324         @Override
325         public String getChildDBName() {
326                 // TODO Auto-generated method stub
327                 return null;
328         }
329
330         @Override
331         public String getFullGenericURI() {
332                 // TODO Auto-generated method stub
333                 return null;
334         }
335
336         @Override
337         protected Object get(String name) {
338                 // TODO Auto-generated method stub
339                 return null;
340         }
341
342         @Override
343         protected void set(String name, Object value) {
344                 // TODO Auto-generated method stub
345                 
346         }
347
348         @Override
349         public String getObjectId() throws UnsupportedEncodingException {
350                 // TODO Auto-generated method stub
351                 return null;
352         }
353
354         @Override
355         public Version getVersion() {
356                 // TODO Auto-generated method stub
357                 return null;
358         }
359
360 }