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