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 com.google.common.base.CaseFormat;
24 import com.google.common.base.Joiner;
25 import com.google.common.collect.Multimap;
26 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
27 import org.openecomp.aai.annotations.Metadata;
28 import org.openecomp.aai.logging.ErrorLogHelper;
29 import org.openecomp.aai.restcore.MediaType;
30 import org.openecomp.aai.schema.enums.ObjectMetadata;
31 import org.openecomp.aai.schema.enums.PropertyMetadata;
33 import javax.xml.bind.JAXBContext;
34 import javax.xml.bind.JAXBException;
35 import javax.xml.bind.Marshaller;
36 import javax.xml.bind.Unmarshaller;
37 import javax.xml.bind.annotation.XmlElement;
38 import javax.xml.transform.stream.StreamSource;
39 import java.io.StringReader;
40 import java.io.StringWriter;
41 import java.lang.reflect.*;
44 public class PojoStrategy extends Introspector {
46 private Object internalObject = null;
47 private PojoInjestor injestor = null;
48 private Multimap<String, String> keyProps = null;
49 private Metadata classLevelMetadata = null;
50 private Version version;
51 private JAXBContext jaxbContext;
52 private Marshaller marshaller;
53 private Unmarshaller unmarshaller;
54 private Set<String> properties = null;
55 private Set<String> keys = null;
56 private Set<String> requiredProperties = null;
58 private boolean isInitialized = false;
60 protected PojoStrategy(Object obj) {
62 className = PojoStrategy.class.getSimpleName();
63 this.internalObject = obj;
64 injestor = new PojoInjestor();
65 classLevelMetadata = obj.getClass().getAnnotation(Metadata.class);
67 version = injestor.getVersion(obj.getClass().getName());
68 jaxbContext = injestor.getContextForVersion(version);
69 super.loader = LoaderFactory.createLoaderForVersion(getModelType(), version);
71 marshaller = jaxbContext.createMarshaller();
72 unmarshaller = jaxbContext.createUnmarshaller();
73 } catch (JAXBException e) {
83 Set<String> properties = new LinkedHashSet<>();
84 Set<String> keys = new LinkedHashSet<>();
85 Set<String> required = new LinkedHashSet<>();
87 Field[] fields = this.internalObject.getClass().getDeclaredFields();
89 for (Field field : fields) {
90 if (!field.getName().equals("any")) {
91 properties.add(covertFieldToOutputFormat(field.getName()));
92 Metadata annotation = field.getAnnotation(Metadata.class);
93 XmlElement xmlAnnotation = field.getAnnotation(XmlElement.class);
94 if (annotation != null) {
95 if (annotation.isKey()) {
96 keys.add(covertFieldToOutputFormat(field.getName()));
99 if (xmlAnnotation != null) {
100 if (xmlAnnotation.required()) {
101 required.add(covertFieldToOutputFormat(field.getName()));
106 properties = Collections.unmodifiableSet(properties);
107 this.properties = properties;
109 keys = Collections.unmodifiableSet(keys);
112 required = Collections.unmodifiableSet(required);
113 this.requiredProperties = required;
116 private String covertFieldToOutputFormat(String propName) {
117 return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, propName);
121 public boolean hasProperty(String name) {
128 * Gets the value of the property via reflection
130 public Object get(String name) {
131 String getMethodName = "get" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
133 return this.internalObject.getClass().getDeclaredMethod(getMethodName).invoke(this.internalObject);
134 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
140 public void set(String name, Object value) {
141 String setMethodName = "set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
143 this.internalObject.getClass().getDeclaredMethod(setMethodName, value.getClass()).invoke(this.internalObject, value);
144 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
145 ErrorLogHelper.logError("AAI_4017", "Error setting name/value pair on POJO: " + e.getMessage());
150 public Set<String> getProperties() {
155 return this.properties;
160 public Set<String> getRequiredProperties() {
165 return this.requiredProperties;
169 public Set<String> getKeys() {
177 public Class<?> getClass(String name) {
181 field = this.internalObject.getClass().getDeclaredField(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name));
182 } catch (NoSuchFieldException | SecurityException e) {
187 return field.getType();
190 public Class<?> getGenericTypeClass(String name) {
193 String getMethodName = "get" + CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name);
194 Method method = internalObject.getClass().getDeclaredMethod(getMethodName);
195 Type t = method.getGenericReturnType();
196 if(t instanceof ParameterizedType) {
197 ParameterizedType pt = (ParameterizedType)t;
198 return ((Class<?>)pt.getActualTypeArguments()[0]);
203 } catch (Exception e) {
209 public String getJavaClassName() {
210 return internalObject.getClass().getName();
214 public Object getUnderlyingObject() {
215 return this.internalObject;
219 public String getName() {
220 String className = internalObject.getClass().getSimpleName();
222 return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
226 protected String findKey() {
227 Set<String> keys = null;
228 keys = this.getKeys();
229 List<String> results = new ArrayList<>();
230 for (String key : keys) {
231 if (this.getType(key).toLowerCase().contains("long")) {
232 key = ((Long)this.getValue(key)).toString();
234 key = (String)this.getValue(key);
239 return Joiner.on("/").join(results);
243 public String marshal(MarshallerProperties properties) {
244 StringWriter result = new StringWriter();
246 if (properties.getMediaType().equals(MediaType.APPLICATION_JSON_TYPE)) {
247 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
248 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, properties.getIncludeRoot());
249 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, properties.getWrapperAsArrayName());
251 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted());
252 marshaller.marshal(this.internalObject, result);
253 } catch (JAXBException e) {
254 //e.printStackTrace();
257 return result.toString();
261 public Object clone() {
262 Object result = null;
264 unmarshaller = jaxbContext.createUnmarshaller();
266 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
267 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
268 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
270 result = unmarshaller.unmarshal(new StreamSource(new StringReader(this.marshal(true))), this.internalObject.getClass()).getValue();
271 } catch (JAXBException e) {
272 // TODO Auto-generated catch block
273 //e.printStackTrace();
275 result = IntrospectorFactory.newInstance(getModelType(), result);
280 public String preProcessKey (String key) {
282 //String trimmedRestURI = restURI.replaceAll("/[\\w\\-]+?/[\\w\\-]+?$", "");
283 String[] split = key.split("/");
285 for (i = split.length-1; i >= 0; i--) {
287 if (keyProps.containsKey(split[i])) {
293 result = Joiner.on("/").join(Arrays.copyOfRange(split, 0, i));
300 public ModelType getModelType() {
301 return ModelType.POJO;
305 public String getChildName() {
306 String className = internalObject.getClass().getSimpleName();
307 String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
309 if (this.isContainer()) {
310 lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName());
317 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
319 Map<PropertyMetadata, String> result = new HashMap<>();
321 f = internalObject.getClass().getField(prop);
322 Metadata m = f.getAnnotation(Metadata.class);
324 Field[] fields = m.getClass().getFields();
326 for (Field field : fields) {
327 fieldName = field.getName();
328 if (fieldName.equals("isAbstract")) {
329 fieldName = "abstract";
330 } else if (fieldName.equals("extendsFrom")) {
331 fieldName = "extends";
333 fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, fieldName);
334 result.put(PropertyMetadata.valueOf(fieldName), (String)field.get(m));
337 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
338 // TODO Auto-generated catch block
345 public String getObjectId() {
346 // TODO Auto-generated method stub
351 public String getMetadata(ObjectMetadata metadataName) {
354 if (ObjectMetadata.ABSTRACT.equals(metadataName)) {
355 methodName = "isAbstract";
356 } else if (ObjectMetadata.EXTENDS.equals(metadataName)) {
357 methodName = "extendsFrom";
359 methodName = metadataName.toString();
363 value = (String)this.classLevelMetadata.getClass().getMethod(methodName).invoke(classLevelMetadata);
364 } catch (IllegalArgumentException | IllegalAccessException | SecurityException | InvocationTargetException | NoSuchMethodException e) {
372 public Version getVersion() {
373 // TODO Auto-generated method stub