2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.aai.introspection;
23 import java.io.UnsupportedEncodingException;
24 import java.util.List;
27 import java.util.UUID;
29 import org.json.simple.JSONObject;
31 import org.openecomp.aai.schema.enums.ObjectMetadata;
32 import org.openecomp.aai.schema.enums.PropertyMetadata;
34 public class JSONStrategy extends Introspector {
36 private JSONObject json = null;
37 private String namedType = "";
38 protected JSONStrategy(Object o) {
41 //Assumes you provide a wrapper
42 Set<String> keySet = json.keySet();
43 if (keySet.size() == 1) {
44 namedType = keySet.iterator().next();
45 json = (JSONObject)json.get(namedType);
47 throw new IllegalArgumentException("This object has no named type.");
51 protected JSONStrategy(Object o, String namedType) {
54 this.namedType = namedType;
59 public boolean hasProperty(String name) {
64 public Object getValue(String name) {
66 result = json.get(name);
72 public void setValue(String name, Object obj) {
77 public Object getUnderlyingObject() {
82 public Set<String> getProperties() {
83 Set<String> result = json.keySet();
88 public Set<String> getRequiredProperties() {
91 return this.getProperties();
95 public Set<String> getKeys() {
97 return this.getProperties();
101 public Set<String> getAllKeys() {
103 return this.getProperties();
107 public String getType(String name) {
109 Class<?> resultClass = this.getClass(name);
110 if (resultClass != null) {
111 result = resultClass.getName();
114 if (result.equals("org.json.simple.JSONArray")) {
115 result = "java.util.List";
122 public String getGenericType(String name) {
124 Class<?> resultClass = this.getGenericTypeClass(name);
125 if (resultClass != null) {
126 result = resultClass.getName();
132 public String getJavaClassName() {
133 return json.getClass().getName();
137 public Class<?> getClass(String name) {
138 Class<?> result = null;
139 result = json.get(name).getClass();
145 public Class<?> getGenericTypeClass(String name) {
146 Object resultObject = null;
147 Class<?> resultClass = null;
148 resultObject = this.getValue(name);
149 if (resultObject.getClass().getName().equals("org.json.simple.JSONArray")) {
150 resultClass = ((List)resultObject).get(0).getClass();
157 public Object newInstanceOfProperty(String name) {
159 return this.getClass(name).newInstance();
160 } catch (InstantiationException | IllegalAccessException e) {
166 public Object newInstanceOfNestedProperty(String name) {
168 return this.getGenericTypeClass(name).newInstance();
169 } catch (InstantiationException | IllegalAccessException e) {
175 public boolean isComplexType(String name) {
176 String result = this.getType(name);
178 if (result.contains("JSONObject")) {
187 public boolean isComplexGenericType(String name) {
188 String result = this.getGenericType(name);
190 if (result.contains("JSONObject")) {
199 public boolean isListType(String name) {
200 String result = this.getType(name);
202 if (result.contains("java.util.List")) {
211 public boolean isContainer() {
212 Set<String> props = this.getProperties();
213 boolean result = false;
214 if (props.size() == 1 && this.isListType(props.iterator().next())) {
221 protected String findKey() {
226 public String getName() {
227 return this.namedType;
231 public String getDbName() {
232 return this.getName();
236 public String getURI() {
238 // use a UUID for now
239 return UUID.randomUUID().toString();
243 public String getGenericURI() {
245 //there is none defined for this
250 public String preProcessKey (String key) {
252 // don't do anything with it
258 public String marshal(MarshallerProperties properties) {
264 public Object clone() {
270 public String findEdgeName(String parent, String child) {
272 // Always has for now
278 public ModelType getModelType() {
279 return ModelType.JSON;
283 public Set<String> getIndexedProperties() {
284 // TODO Auto-generated method stub
289 public String getChildName() {
290 // TODO Auto-generated method stub
295 public boolean hasChild(Introspector child) {
296 // TODO Auto-generated method stub
301 public boolean isSimpleType(String name) {
302 // TODO Auto-generated method stub
307 public boolean isSimpleGenericType(String name) {
308 // TODO Auto-generated method stub
313 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
314 // TODO Auto-generated method stub
319 public String getMetadata(ObjectMetadata metadataName) {
320 // TODO Auto-generated method stub
325 public String getChildDBName() {
326 // TODO Auto-generated method stub
331 public String getFullGenericURI() {
332 // TODO Auto-generated method stub
337 protected Object get(String name) {
338 // TODO Auto-generated method stub
343 protected void set(String name, Object value) {
344 // TODO Auto-generated method stub
349 public String getObjectId() throws UnsupportedEncodingException {
350 // TODO Auto-generated method stub
355 public Version getVersion() {
356 // TODO Auto-generated method stub