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 Class<?> resultClass = null;
151 Object resultObject = this.getValue(name);
152 if (resultObject instanceof 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);
180 return result.contains("JSONObject");
184 public boolean isComplexGenericType(String name) {
185 String result = this.getGenericType(name);
186 return result.contains("JSONObject");
190 public boolean isListType(String name) {
191 String result = this.getType(name);
192 return result.contains("java.util.List");
196 public boolean isContainer() {
197 Set<String> props = this.getProperties();
198 return props.size() == 1 && this.isListType(props.iterator().next());
202 protected String findKey() {
207 public String getName() {
208 return this.namedType;
212 public String getDbName() {
213 return this.getName();
217 public String getURI() {
219 // use a UUID for now
220 return UUID.randomUUID().toString();
224 public String getGenericURI() {
226 // there is none defined for this
231 public String preProcessKey(String key) {
233 // don't do anything with it
239 public String marshal(MarshallerProperties properties) {
246 * public String findEdgeName(String parent, String child) {
248 * // Always has for now
255 public ModelType getModelType() {
256 return ModelType.JSON;
260 public Set<String> getIndexedProperties() {
261 // TODO Auto-generated method stub
266 public String getChildName() {
267 // TODO Auto-generated method stub
272 public boolean hasChild(Introspector child) {
273 // TODO Auto-generated method stub
278 public boolean isSimpleType(String name) {
279 // TODO Auto-generated method stub
284 public boolean isSimpleGenericType(String name) {
285 // TODO Auto-generated method stub
290 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
291 // TODO Auto-generated method stub
296 public String getMetadata(ObjectMetadata metadataName) {
297 // TODO Auto-generated method stub
302 public String getChildDBName() {
303 // TODO Auto-generated method stub
308 public String getFullGenericURI() {
309 // TODO Auto-generated method stub
314 protected Object get(String name) {
315 // TODO Auto-generated method stub
320 protected void set(String name, Object value) {
321 // TODO Auto-generated method stub
326 public String getObjectId() throws UnsupportedEncodingException {
327 // TODO Auto-generated method stub
332 public SchemaVersion getVersion() {
333 // TODO Auto-generated method stub