2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.introspection;
24 import com.google.common.base.CaseFormat;
25 import com.google.common.base.Joiner;
26 import org.eclipse.persistence.descriptors.ClassDescriptor;
27 import org.eclipse.persistence.dynamic.DynamicEntity;
28 import org.eclipse.persistence.dynamic.DynamicType;
29 import org.eclipse.persistence.exceptions.DynamicException;
30 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
31 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
32 import org.eclipse.persistence.mappings.DatabaseMapping;
33 import org.eclipse.persistence.oxm.XMLField;
34 import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping;
35 import org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping;
36 import org.onap.aai.restcore.MediaType;
37 import org.onap.aai.schema.enums.ObjectMetadata;
38 import org.onap.aai.schema.enums.PropertyMetadata;
39 import org.springframework.web.util.UriUtils;
41 import javax.xml.bind.JAXBException;
42 import javax.xml.bind.Marshaller;
43 import javax.xml.bind.Unmarshaller;
44 import javax.xml.transform.stream.StreamSource;
45 import java.io.StringReader;
46 import java.io.StringWriter;
47 import java.io.UnsupportedEncodingException;
49 import java.util.Map.Entry;
51 public class MoxyStrategy extends Introspector {
53 private DynamicEntity internalObject = null;
54 private DynamicType internalType = null;
55 private DynamicJAXBContext jaxbContext = null;
56 private ClassDescriptor cd = null;
57 private Marshaller marshaller = null;
58 private Unmarshaller unmarshaller = null;
59 private Version version = null;
60 private Set<String> properties = null;
61 private Set<String> keys = null;
62 private Set<String> requiredProperties = null;
64 private boolean isInitialized = false;
66 protected MoxyStrategy(Object obj) {
68 /* must look up the correct jaxbcontext for this object */
69 className = MoxyStrategy.class.getSimpleName();
70 internalObject = (DynamicEntity)obj;
71 ModelInjestor injestor = ModelInjestor.getInstance();
72 version = injestor.getVersionFromClassName(internalObject.getClass().getName());
73 jaxbContext = injestor.getContextForVersion(version);
74 super.loader = LoaderFactory.createLoaderForVersion(getModelType(), version);
75 String simpleName = internalObject.getClass().getName();
76 internalType = jaxbContext.getDynamicType(simpleName);
77 cd = internalType.getDescriptor();
79 marshaller = jaxbContext.createMarshaller();
80 unmarshaller = jaxbContext.createUnmarshaller();
81 } catch (JAXBException e) {
90 Set<String> props = new LinkedHashSet<>();
91 for (String s : internalType.getPropertiesNames()) {
92 props.add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, s));
95 props = Collections.unmodifiableSet(props);
96 this.properties = props;
98 Set<String> requiredProps = new LinkedHashSet<>();
99 requiredProps = new LinkedHashSet<>();
100 for (DatabaseMapping dm : cd.getMappings()) {
101 if (dm.getField() instanceof XMLField) {
102 XMLField x = (XMLField)dm.getField();
104 if (x.isRequired()) {
105 requiredProps.add(this.removeXPathDescriptor(x.getName()));
110 requiredProps = Collections.unmodifiableSet(requiredProps);
111 this.requiredProperties = requiredProps;
113 Set<String> keys = new LinkedHashSet<>();
115 for (String name : internalType.getDescriptor().getPrimaryKeyFieldNames()) {
116 keys.add(this.removeXPathDescriptor(name));
118 keys = Collections.unmodifiableSet(keys);
125 public boolean hasProperty(String name) {
126 String convertedName = convertPropertyName(name);
128 return internalType.containsProperty(convertedName);
132 public Object get(String name) {
133 return internalObject.get(name);
137 public void set(String name, Object obj) throws IllegalArgumentException {
139 internalObject.set(name, obj);
143 public Set<String> getProperties() {
149 return this.properties;
154 public Set<String> getRequiredProperties() {
160 return this.requiredProperties;
164 public Set<String> getKeys() {
174 public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
175 String propName = this.convertPropertyName(prop);
176 DatabaseMapping mapping = cd.getMappingForAttributeName(propName);
177 Map<PropertyMetadata, String> result = new HashMap<>();
178 if (mapping != null) {
179 Set<Entry> entrySet = mapping.getProperties().entrySet();
180 for (Entry<?,?> entry : entrySet) {
182 PropertyMetadata.valueOf(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, (String)entry.getKey())), (String)entry.getValue());
190 public String getJavaClassName() {
191 return internalObject.getClass().getName();
197 public Class<?> getClass(String name) {
198 name = convertPropertyName(name);
199 Class<?> resultClass = null;
201 if (internalType.getPropertyType(name) == null) {
202 if (cd.getMappingForAttributeName(name) instanceof XMLCompositeDirectCollectionMapping) {
203 resultClass = cd.getMappingForAttributeName(name).getContainerPolicy().getContainerClass();
205 } else if (cd.getMappingForAttributeName(name) instanceof XMLCompositeCollectionMapping) {
206 resultClass = cd.getMappingForAttributeName(name).getContainerPolicy().getContainerClass();
208 ClassDescriptor referenceDiscriptor = cd.getMappingForAttributeName(name).getReferenceDescriptor();
209 if (referenceDiscriptor != null) {
210 resultClass = referenceDiscriptor.getJavaClass();
212 resultClass = Object.class;
216 resultClass = internalType.getPropertyType(name);
218 } catch (DynamicException e) {
219 //property doesn't exist
225 public Class<?> getGenericTypeClass(String name) {
226 name = convertPropertyName(name);
227 Class<?> resultClass = null;
228 if (internalType.getPropertyType(name) == null) {
229 if (cd.getMappingForAttributeName(name) instanceof XMLCompositeDirectCollectionMapping) {
230 resultClass = cd.getMappingForAttributeName(name).getFields().get(0).getType();
232 } else if (cd.getMappingForAttributeName(name) instanceof XMLCompositeCollectionMapping) {
233 resultClass = cd.getMappingForAttributeName(name).getReferenceDescriptor().getJavaClass();
241 public Object getUnderlyingObject() {
242 return this.internalObject;
246 public String getChildName() {
248 String className = internalObject.getClass().getSimpleName();
249 String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
251 if (this.isContainer()) {
252 lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName());
259 public String getName() {
260 String className = internalObject.getClass().getSimpleName();
261 String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
263 if (this.isContainer()) {
264 lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().get(0)).getSimpleName());
272 public String getObjectId() throws UnsupportedEncodingException {
274 String container = this.getMetadata(ObjectMetadata.CONTAINER);
275 if (this.isContainer()) {
276 result += "/" + this.getName();
279 if (container != null) {
280 result += "/" + container;
282 result += "/" + this.getDbName() + "/" + this.findKey();
290 protected String findKey() throws UnsupportedEncodingException {
291 Set<String> keys = null;
292 keys = this.getKeys();
293 List<String> results = new ArrayList<>();
294 for (String key : keys) {
295 if (this.getType(key).toLowerCase().contains("long")) {
296 key = ((Long)this.getValue(key)).toString();
298 key = (String)this.getValue(key);
300 key = UriUtils.encode(key, "UTF-8");
305 return Joiner.on("/").join(results);
309 public String preProcessKey (String key) {
311 //String trimmedRestURI = restURI.replaceAll("/[\\w\\-]+?/[\\w\\-]+?$", "");
312 String[] split = key.split("/");
314 for (i = split.length-1; i >= 0; i--) {
316 if (jaxbContext.getDynamicType(split[i]) != null) {
322 result = Joiner.on("/").join(Arrays.copyOfRange(split, 0, i));
329 public String marshal(MarshallerProperties properties) {
330 StringWriter result = new StringWriter();
332 if (properties.getMediaType().equals(MediaType.APPLICATION_JSON_TYPE)) {
333 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
334 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, properties.getIncludeRoot());
335 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, properties.getWrapperAsArrayName());
336 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, false);
338 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted());
339 marshaller.marshal(this.internalObject, result);
340 } catch (JAXBException e) {
341 //e.printStackTrace();
344 return result.toString();
348 public Object clone() {
349 Object result = null;
351 unmarshaller = jaxbContext.createUnmarshaller();
353 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
354 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
355 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
357 result = unmarshaller.unmarshal(new StreamSource(new StringReader(this.marshal(true))), this.internalObject.getClass()).getValue();
358 } catch (JAXBException e) {
359 // TODO Auto-generated catch block
360 //e.printStackTrace();
362 result = IntrospectorFactory.newInstance(getModelType(), result);
366 public ModelType getModelType() {
367 return ModelType.MOXY;
370 private String removeXPathDescriptor(String name) {
372 return name.replaceAll("/text\\(\\)", "");
376 public String getMetadata(ObjectMetadata name) {
378 return (String)cd.getProperty(name.toString());
382 public Version getVersion() {