2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.introspection;
23 import java.io.UnsupportedEncodingException;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.List;
28 import java.util.UUID;
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;
36 public class JSONStrategy extends Introspector {
38 private JSONObject json = null;
39 private String namedType = "";
41 protected JSONStrategy(Object 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);
50 throw new IllegalArgumentException("This object has no named type.");
54 protected JSONStrategy(Object o, String namedType) {
56 json = (JSONObject) o;
57 this.namedType = namedType;
62 public boolean hasProperty(String name) {
68 public Object getValue(String name) {
70 result = json.get(name);
76 public void setValue(String name, Object obj) {
82 public Object getUnderlyingObject() {
87 public Set<String> getProperties() {
88 Set<String> result = json.keySet();
93 public Set<String> getRequiredProperties() {
96 return this.getProperties();
100 public Set<String> getKeys() {
102 return this.getProperties();
106 public Set<String> getAllKeys() {
108 return this.getProperties();
112 public String getType(String name) {
114 Class<?> resultClass = this.getClass(name);
115 if (resultClass != null) {
116 result = resultClass.getName();
119 if (result.equals("org.json.simple.JSONArray")) {
120 result = "java.util.List";
127 public String getGenericType(String name) {
129 Class<?> resultClass = this.getGenericTypeClass(name);
130 if (resultClass != null) {
131 result = resultClass.getName();
137 public String getJavaClassName() {
138 return json.getClass().getName();
142 public Class<?> getClass(String name) {
143 Class<?> result = null;
144 result = json.get(name).getClass();
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();
161 public Object newInstanceOfProperty(String name) {
163 return this.getClass(name).getDeclaredConstructor().newInstance();
164 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
165 | NoSuchMethodException | SecurityException e) {
171 public Object newInstanceOfNestedProperty(String name) {
173 return this.getGenericTypeClass(name).getDeclaredConstructor().newInstance();
174 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
175 | NoSuchMethodException | SecurityException e) {
181 public boolean isComplexType(String name) {
182 String result = this.getType(name);
183 return result.contains("JSONObject");
187 public boolean isComplexGenericType(String name) {
188 String result = this.getGenericType(name);
189 return result.contains("JSONObject");
193 public boolean isListType(String name) {
194 String result = this.getType(name);
195 return result.contains("java.util.List");
199 public boolean isContainer() {
200 Set<String> props = this.getProperties();
201 return props.size() == 1 && this.isListType(props.iterator().next());
205 protected String findKey() {
210 public String getName() {
211 return this.namedType;
215 public String getDbName() {
216 return this.getName();
220 public String getURI() {
222 // use a UUID for now
223 return UUID.randomUUID().toString();
227 public String getGenericURI() {
229 // there is none defined for this
234 public String preProcessKey(String key) {
236 // don't do anything with it
242 public String marshal(MarshallerProperties properties) {
249 * public String findEdgeName(String parent, String child) {
251 * // Always has for now
258 public ModelType getModelType() {
259 return ModelType.JSON;
263 public Set<String> getIndexedProperties() {
264 // TODO Auto-generated method stub
269 public String getChildName() {
270 // TODO Auto-generated method stub
275 public boolean hasChild(Introspector child) {
276 // TODO Auto-generated method stub
281 public boolean isSimpleType(String name) {
282 // TODO Auto-generated method stub
287 public boolean isSimpleGenericType(String name) {
288 // TODO Auto-generated method stub
293 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
294 // TODO Auto-generated method stub
299 public String getMetadata(ObjectMetadata metadataName) {
300 // TODO Auto-generated method stub
305 public String getChildDBName() {
306 // TODO Auto-generated method stub
311 public String getFullGenericURI() {
312 // TODO Auto-generated method stub
317 protected Object get(String name) {
318 // TODO Auto-generated method stub
323 protected void set(String name, Object value) {
324 // TODO Auto-generated method stub
329 public String getObjectId() throws UnsupportedEncodingException {
330 // TODO Auto-generated method stub
335 public SchemaVersion getVersion() {
336 // TODO Auto-generated method stub