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