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;
28 import org.json.simple.JSONArray;
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 Class<?> resultClass = null;
150 Object resultObject = this.getValue(name);
151 if (resultObject instanceof JSONArray) {
152 resultClass = ((List) resultObject).get(0).getClass();
159 public Object newInstanceOfProperty(String name) {
161 return this.getClass(name).newInstance();
162 } catch (InstantiationException | IllegalAccessException e) {
168 public Object newInstanceOfNestedProperty(String name) {
170 return this.getGenericTypeClass(name).newInstance();
171 } catch (InstantiationException | IllegalAccessException e) {
177 public boolean isComplexType(String name) {
178 String result = this.getType(name);
179 return result.contains("JSONObject");
183 public boolean isComplexGenericType(String name) {
184 String result = this.getGenericType(name);
185 return result.contains("JSONObject");
189 public boolean isListType(String name) {
190 String result = this.getType(name);
191 return result.contains("java.util.List");
195 public boolean isContainer() {
196 Set<String> props = this.getProperties();
197 return props.size() == 1 && this.isListType(props.iterator().next());
201 protected String findKey() {
206 public String getName() {
207 return this.namedType;
211 public String getDbName() {
212 return this.getName();
216 public String getURI() {
218 // use a UUID for now
219 return UUID.randomUUID().toString();
223 public String getGenericURI() {
225 // there is none defined for this
230 public String preProcessKey(String key) {
232 // don't do anything with it
238 public String marshal(MarshallerProperties properties) {
245 * public String findEdgeName(String parent, String child) {
247 * // Always has for now
254 public ModelType getModelType() {
255 return ModelType.JSON;
259 public Set<String> getIndexedProperties() {
260 // TODO Auto-generated method stub
265 public String getChildName() {
266 // TODO Auto-generated method stub
271 public boolean hasChild(Introspector child) {
272 // TODO Auto-generated method stub
277 public boolean isSimpleType(String name) {
278 // TODO Auto-generated method stub
283 public boolean isSimpleGenericType(String name) {
284 // TODO Auto-generated method stub
289 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
290 // TODO Auto-generated method stub
295 public String getMetadata(ObjectMetadata metadataName) {
296 // TODO Auto-generated method stub
301 public String getChildDBName() {
302 // TODO Auto-generated method stub
307 public String getFullGenericURI() {
308 // TODO Auto-generated method stub
313 protected Object get(String name) {
314 // TODO Auto-generated method stub
319 protected void set(String name, Object value) {
320 // TODO Auto-generated method stub
325 public String getObjectId() throws UnsupportedEncodingException {
326 // TODO Auto-generated method stub
331 public SchemaVersion getVersion() {
332 // TODO Auto-generated method stub