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