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.util.List;
27 import java.util.UUID;
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;
35 public class JSONStrategy extends Introspector {
37 private JSONObject json = null;
38 private String namedType = "";
40 protected JSONStrategy(Object 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);
49 throw new IllegalArgumentException("This object has no named type.");
53 protected JSONStrategy(Object o, String namedType) {
55 json = (JSONObject) o;
56 this.namedType = namedType;
61 public boolean hasProperty(String name) {
67 public Object getValue(String name) {
69 result = json.get(name);
75 public void setValue(String name, Object obj) {
81 public Object getUnderlyingObject() {
86 public Set<String> getProperties() {
87 Set<String> result = json.keySet();
92 public Set<String> getRequiredProperties() {
95 return this.getProperties();
99 public Set<String> getKeys() {
101 return this.getProperties();
105 public Set<String> getAllKeys() {
107 return this.getProperties();
111 public String getType(String name) {
113 Class<?> resultClass = this.getClass(name);
114 if (resultClass != null) {
115 result = resultClass.getName();
118 if (result.equals("org.json.simple.JSONArray")) {
119 result = "java.util.List";
126 public String getGenericType(String name) {
128 Class<?> resultClass = this.getGenericTypeClass(name);
129 if (resultClass != null) {
130 result = resultClass.getName();
136 public String getJavaClassName() {
137 return json.getClass().getName();
141 public Class<?> getClass(String name) {
142 Class<?> result = null;
143 result = json.get(name).getClass();
149 public Class<?> getGenericTypeClass(String name) {
150 Object resultObject = null;
151 Class<?> resultClass = null;
152 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).newInstance();
164 } catch (InstantiationException | IllegalAccessException e) {
170 public Object newInstanceOfNestedProperty(String name) {
172 return this.getGenericTypeClass(name).newInstance();
173 } catch (InstantiationException | IllegalAccessException e) {
179 public boolean isComplexType(String name) {
180 String result = this.getType(name);
182 if (result.contains("JSONObject")) {
191 public boolean isComplexGenericType(String name) {
192 String result = this.getGenericType(name);
194 if (result.contains("JSONObject")) {
203 public boolean isListType(String name) {
204 String result = this.getType(name);
206 if (result.contains("java.util.List")) {
215 public boolean isContainer() {
216 Set<String> props = this.getProperties();
217 boolean result = false;
218 if (props.size() == 1 && this.isListType(props.iterator().next())) {
226 protected String findKey() {
231 public String getName() {
232 return this.namedType;
236 public String getDbName() {
237 return this.getName();
241 public String getURI() {
243 // use a UUID for now
244 return UUID.randomUUID().toString();
248 public String getGenericURI() {
250 // there is none defined for this
255 public String preProcessKey(String key) {
257 // don't do anything with it
263 public String marshal(MarshallerProperties properties) {
269 public Object clone() {
276 * public String findEdgeName(String parent, String child) {
278 * // Always has for now
285 public ModelType getModelType() {
286 return ModelType.JSON;
290 public Set<String> getIndexedProperties() {
291 // TODO Auto-generated method stub
296 public String getChildName() {
297 // TODO Auto-generated method stub
302 public boolean hasChild(Introspector child) {
303 // TODO Auto-generated method stub
308 public boolean isSimpleType(String name) {
309 // TODO Auto-generated method stub
314 public boolean isSimpleGenericType(String name) {
315 // TODO Auto-generated method stub
320 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
321 // TODO Auto-generated method stub
326 public String getMetadata(ObjectMetadata metadataName) {
327 // TODO Auto-generated method stub
332 public String getChildDBName() {
333 // TODO Auto-generated method stub
338 public String getFullGenericURI() {
339 // TODO Auto-generated method stub
344 protected Object get(String name) {
345 // TODO Auto-generated method stub
350 protected void set(String name, Object value) {
351 // TODO Auto-generated method stub
356 public String getObjectId() throws UnsupportedEncodingException {
357 // TODO Auto-generated method stub
362 public SchemaVersion getVersion() {
363 // TODO Auto-generated method stub