Merge "Fix all blocker sonar issues and some checkstyle"
[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 import org.json.simple.JSONArray;
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         Class<?> resultClass = null;
150         Object resultObject = this.getValue(name);
151         if (resultObject instanceof JSONArray) {
152             resultClass = ((List) resultObject).get(0).getClass();
153         }
154
155         return resultClass;
156     }
157
158     @Override
159     public Object newInstanceOfProperty(String name) {
160         try {
161             return this.getClass(name).newInstance();
162         } catch (InstantiationException | IllegalAccessException e) {
163             return null;
164         }
165     }
166
167     @Override
168     public Object newInstanceOfNestedProperty(String name) {
169         try {
170             return this.getGenericTypeClass(name).newInstance();
171         } catch (InstantiationException | IllegalAccessException e) {
172             return null;
173         }
174     }
175
176     @Override
177     public boolean isComplexType(String name) {
178         String result = this.getType(name);
179         return result.contains("JSONObject");
180     }
181
182     @Override
183     public boolean isComplexGenericType(String name) {
184         String result = this.getGenericType(name);
185         return result.contains("JSONObject");
186     }
187
188     @Override
189     public boolean isListType(String name) {
190         String result = this.getType(name);
191         return result.contains("java.util.List");
192     }
193
194     @Override
195     public boolean isContainer() {
196         Set<String> props = this.getProperties();
197         return props.size() == 1 && this.isListType(props.iterator().next());
198     }
199
200     @Override
201     protected String findKey() {
202         return "";
203     }
204
205     @Override
206     public String getName() {
207         return this.namedType;
208     }
209
210     @Override
211     public String getDbName() {
212         return this.getName();
213     }
214
215     @Override
216     public String getURI() {
217
218         // use a UUID for now
219         return UUID.randomUUID().toString();
220     }
221
222     @Override
223     public String getGenericURI() {
224
225         // there is none defined for this
226         return "";
227     }
228
229     @Override
230     public String preProcessKey(String key) {
231
232         // don't do anything with it
233         return key;
234
235     }
236
237     @Override
238     public String marshal(MarshallerProperties properties) {
239         // TODO
240         return null;
241     }
242
243     /*
244      * @Override
245      * public String findEdgeName(String parent, String child) {
246      *
247      * // Always has for now
248      * return "has";
249      *
250      * }
251      */
252
253     @Override
254     public ModelType getModelType() {
255         return ModelType.JSON;
256     }
257
258     @Override
259     public Set<String> getIndexedProperties() {
260         // TODO Auto-generated method stub
261         return null;
262     }
263
264     @Override
265     public String getChildName() {
266         // TODO Auto-generated method stub
267         return null;
268     }
269
270     @Override
271     public boolean hasChild(Introspector child) {
272         // TODO Auto-generated method stub
273         return false;
274     }
275
276     @Override
277     public boolean isSimpleType(String name) {
278         // TODO Auto-generated method stub
279         return false;
280     }
281
282     @Override
283     public boolean isSimpleGenericType(String name) {
284         // TODO Auto-generated method stub
285         return false;
286     }
287
288     @Override
289     public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
290         // TODO Auto-generated method stub
291         return null;
292     }
293
294     @Override
295     public String getMetadata(ObjectMetadata metadataName) {
296         // TODO Auto-generated method stub
297         return null;
298     }
299
300     @Override
301     public String getChildDBName() {
302         // TODO Auto-generated method stub
303         return null;
304     }
305
306     @Override
307     public String getFullGenericURI() {
308         // TODO Auto-generated method stub
309         return null;
310     }
311
312     @Override
313     protected Object get(String name) {
314         // TODO Auto-generated method stub
315         return null;
316     }
317
318     @Override
319     protected void set(String name, Object value) {
320         // TODO Auto-generated method stub
321
322     }
323
324     @Override
325     public String getObjectId() throws UnsupportedEncodingException {
326         // TODO Auto-generated method stub
327         return null;
328     }
329
330     @Override
331     public SchemaVersion getVersion() {
332         // TODO Auto-generated method stub
333         return null;
334     }
335
336 }