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