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=========================================================
20 package org.onap.aai.introspection;
22 import org.json.simple.JSONObject;
23 import org.onap.aai.schema.enums.ObjectMetadata;
24 import org.onap.aai.schema.enums.PropertyMetadata;
25 import org.onap.aai.setup.SchemaVersion;
27 import java.io.UnsupportedEncodingException;
28 import java.util.List;
31 import java.util.UUID;
33 public class JSONStrategy extends Introspector {
35 private JSONObject json = null;
36 private String namedType = "";
37 protected JSONStrategy(Object o) {
40 //Assumes you provide a wrapper
41 Set<String> keySet = json.keySet();
42 if (keySet.size() == 1) {
43 namedType = keySet.iterator().next();
44 json = (JSONObject)json.get(namedType);
46 throw new IllegalArgumentException("This object has no named type.");
50 protected JSONStrategy(Object o, String namedType) {
53 this.namedType = namedType;
58 public boolean hasProperty(String name) {
63 public Object getValue(String name) {
65 result = json.get(name);
71 public void setValue(String name, Object obj) {
76 public Object getUnderlyingObject() {
81 public Set<String> getProperties() {
82 Set<String> result = json.keySet();
87 public Set<String> getRequiredProperties() {
90 return this.getProperties();
94 public Set<String> getKeys() {
96 return this.getProperties();
100 public Set<String> getAllKeys() {
102 return this.getProperties();
106 public String getType(String name) {
108 Class<?> resultClass = this.getClass(name);
109 if (resultClass != null) {
110 result = resultClass.getName();
113 if (result.equals("org.json.simple.JSONArray")) {
114 result = "java.util.List";
121 public String getGenericType(String name) {
123 Class<?> resultClass = this.getGenericTypeClass(name);
124 if (resultClass != null) {
125 result = resultClass.getName();
131 public String getJavaClassName() {
132 return json.getClass().getName();
136 public Class<?> getClass(String name) {
137 Class<?> result = null;
138 result = json.get(name).getClass();
144 public Class<?> getGenericTypeClass(String name) {
145 Object resultObject = null;
146 Class<?> resultClass = null;
147 resultObject = this.getValue(name);
148 if (resultObject.getClass().getName().equals("org.json.simple.JSONArray")) {
149 resultClass = ((List)resultObject).get(0).getClass();
156 public Object newInstanceOfProperty(String name) {
158 return this.getClass(name).newInstance();
159 } catch (InstantiationException | IllegalAccessException e) {
165 public Object newInstanceOfNestedProperty(String name) {
167 return this.getGenericTypeClass(name).newInstance();
168 } catch (InstantiationException | IllegalAccessException e) {
174 public boolean isComplexType(String name) {
175 String result = this.getType(name);
177 if (result.contains("JSONObject")) {
186 public boolean isComplexGenericType(String name) {
187 String result = this.getGenericType(name);
189 if (result.contains("JSONObject")) {
198 public boolean isListType(String name) {
199 String result = this.getType(name);
201 if (result.contains("java.util.List")) {
210 public boolean isContainer() {
211 Set<String> props = this.getProperties();
212 boolean result = false;
213 if (props.size() == 1 && this.isListType(props.iterator().next())) {
220 protected String findKey() {
225 public String getName() {
226 return this.namedType;
230 public String getDbName() {
231 return this.getName();
235 public String getURI() {
237 // use a UUID for now
238 return UUID.randomUUID().toString();
242 public String getGenericURI() {
244 //there is none defined for this
249 public String preProcessKey (String key) {
251 // don't do anything with it
257 public String marshal(MarshallerProperties properties) {
263 public Object clone() {
269 public String findEdgeName(String parent, String child) {
271 // Always has for now
277 public ModelType getModelType() {
278 return ModelType.JSON;
282 public Set<String> getIndexedProperties() {
283 // TODO Auto-generated method stub
288 public String getChildName() {
289 // TODO Auto-generated method stub
294 public boolean hasChild(Introspector child) {
295 // TODO Auto-generated method stub
300 public boolean isSimpleType(String name) {
301 // TODO Auto-generated method stub
306 public boolean isSimpleGenericType(String name) {
307 // TODO Auto-generated method stub
312 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
313 // TODO Auto-generated method stub
318 public String getMetadata(ObjectMetadata metadataName) {
319 // TODO Auto-generated method stub
324 public String getChildDBName() {
325 // TODO Auto-generated method stub
330 public String getFullGenericURI() {
331 // TODO Auto-generated method stub
336 protected Object get(String name) {
337 // TODO Auto-generated method stub
342 protected void set(String name, Object value) {
343 // TODO Auto-generated method stub
348 public String getObjectId() throws UnsupportedEncodingException {
349 // TODO Auto-generated method stub
354 public SchemaVersion getVersion() {
355 // TODO Auto-generated method stub