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.JSONObject;
30 import org.onap.aai.schema.enums.ObjectMetadata;
31 import org.onap.aai.schema.enums.PropertyMetadata;
32 import org.onap.aai.setup.SchemaVersion;
34 public class JSONStrategy extends Introspector {
36 private JSONObject json = null;
37 private String namedType = "";
39 protected JSONStrategy(Object 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);
48 throw new IllegalArgumentException("This object has no named type.");
52 protected JSONStrategy(Object o, String namedType) {
54 json = (JSONObject) o;
55 this.namedType = namedType;
60 public boolean hasProperty(String name) {
66 public Object getValue(String name) {
68 result = json.get(name);
74 public void setValue(String name, Object obj) {
80 public Object getUnderlyingObject() {
85 public Set<String> getProperties() {
86 Set<String> result = json.keySet();
91 public Set<String> getRequiredProperties() {
94 return this.getProperties();
98 public Set<String> getKeys() {
100 return this.getProperties();
104 public Set<String> getAllKeys() {
106 return this.getProperties();
110 public String getType(String name) {
112 Class<?> resultClass = this.getClass(name);
113 if (resultClass != null) {
114 result = resultClass.getName();
117 if (result.equals("org.json.simple.JSONArray")) {
118 result = "java.util.List";
125 public String getGenericType(String name) {
127 Class<?> resultClass = this.getGenericTypeClass(name);
128 if (resultClass != null) {
129 result = resultClass.getName();
135 public String getJavaClassName() {
136 return json.getClass().getName();
140 public Class<?> getClass(String name) {
141 Class<?> result = null;
142 result = json.get(name).getClass();
148 public Class<?> getGenericTypeClass(String name) {
149 Object resultObject = null;
150 Class<?> resultClass = null;
151 resultObject = this.getValue(name);
152 if (resultObject.getClass().getName().equals("org.json.simple.JSONArray")) {
153 resultClass = ((List) resultObject).get(0).getClass();
160 public Object newInstanceOfProperty(String name) {
162 return this.getClass(name).newInstance();
163 } catch (InstantiationException | IllegalAccessException e) {
169 public Object newInstanceOfNestedProperty(String name) {
171 return this.getGenericTypeClass(name).newInstance();
172 } catch (InstantiationException | IllegalAccessException e) {
178 public boolean isComplexType(String name) {
179 String result = this.getType(name);
181 if (result.contains("JSONObject")) {
190 public boolean isComplexGenericType(String name) {
191 String result = this.getGenericType(name);
193 if (result.contains("JSONObject")) {
202 public boolean isListType(String name) {
203 String result = this.getType(name);
205 if (result.contains("java.util.List")) {
214 public boolean isContainer() {
215 Set<String> props = this.getProperties();
216 boolean result = false;
217 if (props.size() == 1 && this.isListType(props.iterator().next())) {
225 protected String findKey() {
230 public String getName() {
231 return this.namedType;
235 public String getDbName() {
236 return this.getName();
240 public String getURI() {
242 // use a UUID for now
243 return UUID.randomUUID().toString();
247 public String getGenericURI() {
249 // there is none defined for this
254 public String preProcessKey(String key) {
256 // don't do anything with it
262 public String marshal(MarshallerProperties properties) {
268 public Object clone() {
275 * public String findEdgeName(String parent, String child) {
277 * // Always has for now
284 public ModelType getModelType() {
285 return ModelType.JSON;
289 public Set<String> getIndexedProperties() {
290 // TODO Auto-generated method stub
295 public String getChildName() {
296 // TODO Auto-generated method stub
301 public boolean hasChild(Introspector child) {
302 // TODO Auto-generated method stub
307 public boolean isSimpleType(String name) {
308 // TODO Auto-generated method stub
313 public boolean isSimpleGenericType(String name) {
314 // TODO Auto-generated method stub
319 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
320 // TODO Auto-generated method stub
325 public String getMetadata(ObjectMetadata metadataName) {
326 // TODO Auto-generated method stub
331 public String getChildDBName() {
332 // TODO Auto-generated method stub
337 public String getFullGenericURI() {
338 // TODO Auto-generated method stub
343 protected Object get(String name) {
344 // TODO Auto-generated method stub
349 protected void set(String name, Object value) {
350 // TODO Auto-generated method stub
355 public String getObjectId() throws UnsupportedEncodingException {
356 // TODO Auto-generated method stub
361 public SchemaVersion getVersion() {
362 // TODO Auto-generated method stub