Integrate aai-schema-ingest library into aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / MoxyStrategy.java
index 235df0c..ecf3125 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
 package org.onap.aai.introspection;
 
@@ -25,6 +23,8 @@ import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 import com.google.common.base.CaseFormat;
 import com.google.common.base.Joiner;
+
+
 import org.eclipse.persistence.descriptors.ClassDescriptor;
 import org.eclipse.persistence.dynamic.DynamicEntity;
 import org.eclipse.persistence.dynamic.DynamicType;
@@ -35,11 +35,13 @@ import org.eclipse.persistence.mappings.DatabaseMapping;
 import org.eclipse.persistence.oxm.XMLField;
 import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping;
 import org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping;
+import org.onap.aai.config.SpringContextAware;
+import org.onap.aai.nodes.NodeIngestor;
 import org.onap.aai.restcore.MediaType;
 import org.onap.aai.schema.enums.ObjectMetadata;
 import org.onap.aai.schema.enums.PropertyMetadata;
+import org.onap.aai.setup.SchemaVersion;
 import org.springframework.web.util.UriUtils;
-
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
@@ -51,43 +53,47 @@ import java.util.*;
 import java.util.Map.Entry;
 
 public class MoxyStrategy extends Introspector {
-
-       private static final EELFLogger LOGGER2 = EELFManager.getInstance().getLogger(MoxyStrategy.class);
-
+       
+       private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyStrategy.class);
        private DynamicEntity internalObject = null;
        private DynamicType internalType = null;
        private DynamicJAXBContext jaxbContext = null;
        private ClassDescriptor cd = null;
        private Marshaller marshaller = null;
        private Unmarshaller unmarshaller = null;
-       private Version version = null;
+       private SchemaVersion version = null;
        private Set<String> properties = null;
        private Set<String> keys = null;
        private Set<String> requiredProperties = null;
 
        private boolean isInitialized = false;
-
+       
+       private NodeIngestor nodeIngestor;
+       
        protected MoxyStrategy(Object obj) {
                super(obj);
                /* must look up the correct jaxbcontext for this object */
                className = MoxyStrategy.class.getSimpleName();
                internalObject = (DynamicEntity)obj;
-               ModelInjestor injestor = ModelInjestor.getInstance();
-               version = injestor.getVersionFromClassName(internalObject.getClass().getName());
-               jaxbContext = injestor.getContextForVersion(version);
-               super.loader = LoaderFactory.createLoaderForVersion(getModelType(), version);
+               nodeIngestor = SpringContextAware.getBean(NodeIngestor.class);
+               version = nodeIngestor.getVersionFromClassName(internalObject.getClass().getName());
+               super.loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(getModelType(), version);
+               jaxbContext = nodeIngestor.getContextForVersion(version);
                String simpleName = internalObject.getClass().getName();
                internalType = jaxbContext.getDynamicType(simpleName);
+               
                cd = internalType.getDescriptor();
                try {
                        marshaller = jaxbContext.createMarshaller();
+                       
                        unmarshaller = jaxbContext.createUnmarshaller();
+               
                } catch (JAXBException e) {
-                       LOGGER2.error(e.getMessage(),e);
+
                }
 
        }
-
+       
        private void init() {
                isInitialized = true;
 
@@ -98,13 +104,13 @@ public class MoxyStrategy extends Introspector {
                }
                props = Collections.unmodifiableSet(props);
                this.properties = props;
-
+               
                Set<String> requiredProps = new LinkedHashSet<>();
                requiredProps = new LinkedHashSet<>();
                for (DatabaseMapping dm : cd.getMappings()) {
-                       if (dm.getField() instanceof XMLField) {
+                       if (dm.getField() instanceof XMLField) { 
                                XMLField x = (XMLField)dm.getField();
-                               if (x != null) {
+                               if (x != null) { 
                                        if (x.isRequired()) {
                                                requiredProps.add(this.removeXPathDescriptor(x.getName()));
                                        }
@@ -113,25 +119,25 @@ public class MoxyStrategy extends Introspector {
                }
                requiredProps = Collections.unmodifiableSet(requiredProps);
                this.requiredProperties = requiredProps;
-
+       
                Set<String> keys = new LinkedHashSet<>();
-
+               
                for (String name : internalType.getDescriptor().getPrimaryKeyFieldNames()) {
                        keys.add(this.removeXPathDescriptor(name));
                }
                keys = Collections.unmodifiableSet(keys);
                this.keys = keys;
-
-
+               
+               
        }
-
+       
        @Override
        public boolean hasProperty(String name) {
                String convertedName = convertPropertyName(name);
 
-               return internalType.containsProperty(convertedName);
+               return internalType.containsProperty(convertedName);    
        }
-
+       
        @Override
        public Object get(String name) {
                return internalObject.get(name);
@@ -139,7 +145,7 @@ public class MoxyStrategy extends Introspector {
 
        @Override
        public void set(String name, Object obj) throws IllegalArgumentException {
-
+               
                internalObject.set(name, obj);
        }
 
@@ -151,7 +157,7 @@ public class MoxyStrategy extends Introspector {
                }
 
                return this.properties;
-
+               
        }
 
        @Override
@@ -173,7 +179,7 @@ public class MoxyStrategy extends Introspector {
 
                return this.keys;
        }
-
+       
        @Override
        public Map<PropertyMetadata, String> getPropertyMetadata(String prop) {
                String propName = this.convertPropertyName(prop);
@@ -186,7 +192,7 @@ public class MoxyStrategy extends Introspector {
                                                PropertyMetadata.valueOf(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, (String)entry.getKey())), (String)entry.getValue());
                        }
                }
-
+               
                return result;
        }
 
@@ -194,8 +200,8 @@ public class MoxyStrategy extends Introspector {
        public String getJavaClassName() {
                return internalObject.getClass().getName();
        }
-
-
+       
+       
 
        @Override
        public Class<?> getClass(String name) {
@@ -205,7 +211,7 @@ public class MoxyStrategy extends Introspector {
                        if (internalType.getPropertyType(name) == null) {
                                if (cd.getMappingForAttributeName(name) instanceof XMLCompositeDirectCollectionMapping) {
                                        resultClass = cd.getMappingForAttributeName(name).getContainerPolicy().getContainerClass();
-
+       
                                } else if (cd.getMappingForAttributeName(name) instanceof XMLCompositeCollectionMapping) {
                                        resultClass = cd.getMappingForAttributeName(name).getContainerPolicy().getContainerClass();
                                } else {
@@ -221,7 +227,6 @@ public class MoxyStrategy extends Introspector {
                        }
                } catch (DynamicException e) {
                        //property doesn't exist
-                       LOGGER2.error(e.getMessage(),e);
                }
                return resultClass;
        }
@@ -238,7 +243,7 @@ public class MoxyStrategy extends Introspector {
                                resultClass = cd.getMappingForAttributeName(name).getReferenceDescriptor().getJavaClass();
                        }
                }
-
+               
                return resultClass;
        }
 
@@ -246,20 +251,20 @@ public class MoxyStrategy extends Introspector {
        public Object getUnderlyingObject() {
                return this.internalObject;
        }
-
+       
        @Override
        public String getChildName() {
-
+               
                String className = internalObject.getClass().getSimpleName();
                String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
-
+               
                if (this.isContainer()) {
                        lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName());
                }
-
+               
                return lowerHyphen;
        }
-
+       
        @Override
        public String getName() {
                String className = internalObject.getClass().getSimpleName();
@@ -268,11 +273,11 @@ public class MoxyStrategy extends Introspector {
                if (this.isContainer()) {
                        lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().get(0)).getSimpleName());
                }*/
-
+               
 
                return lowerHyphen;
        }
-
+       
        @Override
        public String getObjectId() throws UnsupportedEncodingException {
                String result = "";
@@ -280,17 +285,17 @@ public class MoxyStrategy extends Introspector {
                if (this.isContainer()) {
                         result += "/" + this.getName();
                } else {
-
+                       
                        if (container != null) {
                                result += "/" + container;
                        }
                        result += "/" + this.getDbName() + "/" + this.findKey();
-
+                       
                }
-
+               
                return result;
        }
-
+       
        @Override
        protected String findKey() throws UnsupportedEncodingException {
                Set<String> keys = null;
@@ -300,10 +305,10 @@ public class MoxyStrategy extends Introspector {
                        String value = UriUtils.encode(this.getValue(key).toString(), "UTF-8");
                        results.add(value);
                }
-
+               
                return Joiner.on("/").join(results);
        }
-
+       
        @Override
        public String preProcessKey (String key) {
                String result = "";
@@ -311,19 +316,19 @@ public class MoxyStrategy extends Introspector {
                String[] split = key.split("/");
                int i = 0;
                for (i = split.length-1; i >= 0; i--) {
-
+                       
                        if (jaxbContext.getDynamicType(split[i]) != null) {
                                break;
-
+                               
                        }
-
+                       
                }
                result = Joiner.on("/").join(Arrays.copyOfRange(split, 0, i));
-
+               
                return result;
-
+               
        }
-
+       
        @Override
        public String marshal(MarshallerProperties properties) {
                StringWriter result = new StringWriter();
@@ -334,15 +339,16 @@ public class MoxyStrategy extends Introspector {
                        marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, properties.getWrapperAsArrayName());
                        marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, false);
                }
-               marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted());
+               
+               marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted());
                marshaller.marshal(this.internalObject, result);
                } catch (JAXBException e) {
-                       LOGGER2.error(e.getMessage(),e);
+                       //e.printStackTrace();
                }
 
         return result.toString();
        }
-
+       
        @Override
        public Object clone() {
                Object result = null;
@@ -352,10 +358,11 @@ public class MoxyStrategy extends Introspector {
                        unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
                        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
                                unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
-
+                               
                                result = unmarshaller.unmarshal(new StreamSource(new StringReader(this.marshal(true))), this.internalObject.getClass()).getValue();
                         } catch (JAXBException e) {
-                               LOGGER2.error(e.getMessage(),e);
+                                       // TODO Auto-generated catch block
+                                       //e.printStackTrace();
                        }
                 result = IntrospectorFactory.newInstance(getModelType(), result);
                 return result;
@@ -377,7 +384,7 @@ public class MoxyStrategy extends Introspector {
        }
 
        @Override
-       public Version getVersion() {
+       public SchemaVersion getVersion() {
                
                return this.version;
        }