Update the license for 2017-2018 license
[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 package org.onap.aai.introspection;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.json.simple.JSONObject;
25 import org.onap.aai.schema.enums.ObjectMetadata;
26 import org.onap.aai.schema.enums.PropertyMetadata;
27
28 import java.io.UnsupportedEncodingException;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.UUID;
33
34 public class JSONStrategy extends Introspector {
35         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JSONStrategy.class);
36
37         private JSONObject json = null;
38         private String namedType = "";
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         @Override
65         public Object getValue(String name) {
66                 Object result = "";
67                 result = json.get(name);
68                 
69                 return result;
70         }
71
72         @Override
73         public void setValue(String name, Object obj) {
74                 json.put(name, obj);
75                 
76         }
77         @Override
78         public Object getUnderlyingObject() {
79                 return this.json;
80         }
81         
82         @Override
83         public Set<String> getProperties() {
84                 Set<String> result = json.keySet();
85                 return result;
86         }
87         
88         @Override
89         public Set<String> getRequiredProperties() {
90                 //unknowable
91                 
92                 return this.getProperties();
93         }
94
95         @Override
96         public Set<String> getKeys() {
97                 //unknowable
98                 return this.getProperties();
99         }
100
101         @Override
102         public Set<String> getAllKeys() {
103                 //unknowable
104                 return this.getProperties();
105         }
106
107         @Override
108         public String getType(String name) {
109                 String result = "";
110                 Class<?> resultClass = this.getClass(name);
111                 if (resultClass != null) {
112                         result = resultClass.getName();
113                 }
114                 
115                 if (result.equals("org.json.simple.JSONArray")) {
116                         result = "java.util.List";
117                 }
118                 
119                 return result;
120         }
121
122         @Override
123         public String getGenericType(String name) {
124                 String result = "";
125                 Class<?> resultClass = this.getGenericTypeClass(name);
126                 if (resultClass != null) {
127                         result = resultClass.getName();
128                 }
129                 return result;
130         }
131
132         @Override
133         public String getJavaClassName() {
134                 return json.getClass().getName();
135         }
136
137         @Override
138         public Class<?> getClass(String name) {
139                 Class<?> result = null;
140                 result = json.get(name).getClass();
141                 
142                 return result;
143         }
144
145         @Override
146         public Class<?> getGenericTypeClass(String name) {
147                 Object resultObject = null;
148                 Class<?> resultClass = null;
149                 resultObject = this.getValue(name);
150                 if (resultObject.getClass().getName().equals("org.json.simple.JSONArray")) {
151                         resultClass = ((List)resultObject).get(0).getClass();
152                 }
153                 
154                 return resultClass;
155         }
156
157         @Override
158         public Object newInstanceOfProperty(String name) {
159                 try {
160                         return this.getClass(name).newInstance();
161                 } catch (InstantiationException | IllegalAccessException e) {
162             LOGGER.error(e.getMessage(),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             LOGGER.error(e.getMessage(),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         @Override
224         protected String findKey() {
225                 return "";
226         }
227         
228         @Override
229         public String getName() {
230                 return this.namedType;
231         }
232         
233         @Override
234         public String getDbName() {
235                 return this.getName();
236         }
237         
238         @Override
239         public String getURI() {
240                 
241                 // use a UUID for now 
242                 return UUID.randomUUID().toString();
243         }
244         
245         @Override
246         public String getGenericURI() {
247                 
248                 //there is none defined for this
249                 return "";
250         }
251         
252         @Override
253         public String preProcessKey (String key) {
254                 
255                 // don't do anything with it
256                 return key;
257                 
258         }
259         
260         @Override
261         public String marshal(MarshallerProperties properties) {
262                 //TODO
263                 return null;
264         }
265         
266         @Override
267         public Object clone() {
268                 //TODO
269                 return null;
270         }
271         
272         /*@Override
273         public String findEdgeName(String parent, String child) {
274                 
275                 // Always has for now
276                 return "has";
277                 
278         }*/
279         
280         @Override
281         public ModelType getModelType() {
282                 return ModelType.JSON;
283         }
284
285         @Override
286         public Set<String> getIndexedProperties() {
287                 // TODO Auto-generated method stub
288                 return null;
289         }
290
291         @Override
292         public String getChildName() {
293                 // TODO Auto-generated method stub
294                 return null;
295         }
296
297         @Override
298         public boolean hasChild(Introspector child) {
299                 // TODO Auto-generated method stub
300                 return false;
301         }
302
303         @Override
304         public boolean isSimpleType(String name) {
305                 // TODO Auto-generated method stub
306                 return false;
307         }
308
309         @Override
310         public boolean isSimpleGenericType(String name) {
311                 // TODO Auto-generated method stub
312                 return false;
313         }
314
315         @Override
316         public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
317                 // TODO Auto-generated method stub
318                 return null;
319         }
320
321         @Override
322         public String getMetadata(ObjectMetadata metadataName) {
323                 // TODO Auto-generated method stub
324                 return null;
325         }
326
327         @Override
328         public String getChildDBName() {
329                 // TODO Auto-generated method stub
330                 return null;
331         }
332
333         @Override
334         public String getFullGenericURI() {
335                 // TODO Auto-generated method stub
336                 return null;
337         }
338
339         @Override
340         protected Object get(String name) {
341                 // TODO Auto-generated method stub
342                 return null;
343         }
344
345         @Override
346         protected void set(String name, Object value) {
347                 // TODO Auto-generated method stub
348                 
349         }
350
351         @Override
352         public String getObjectId() throws UnsupportedEncodingException {
353                 // TODO Auto-generated method stub
354                 return null;
355         }
356
357         @Override
358         public Version getVersion() {
359                 // TODO Auto-generated method stub
360                 return null;
361         }
362
363 }