Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / util / PojoUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.util;
22
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.core.JsonGenerationException;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import com.fasterxml.jackson.databind.JsonMappingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.SerializationFeature;
29 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
30 import com.google.common.base.CaseFormat;
31 import com.google.common.collect.Multimap;
32 import com.thinkaurelius.titan.core.TitanVertex;
33 import org.apache.commons.io.output.ByteArrayOutputStream;
34 import org.eclipse.persistence.dynamic.DynamicEntity;
35 import org.eclipse.persistence.dynamic.DynamicType;
36 import org.eclipse.persistence.jaxb.MarshallerProperties;
37 import org.openecomp.aai.domain.model.AAIResource;
38 import org.openecomp.aai.exceptions.AAIException;
39
40 import javax.xml.bind.JAXBContext;
41 import javax.xml.bind.JAXBException;
42 import javax.xml.bind.Marshaller;
43 import java.io.IOException;
44 import java.io.StringWriter;
45 import java.lang.reflect.InvocationTargetException;
46 import java.lang.reflect.Method;
47 import java.util.*;
48 import java.util.Map.Entry;
49
50 public class PojoUtils {
51         
52         /**
53          * Gets the key value list.
54          *
55          * @param <T> the generic type
56          * @param e the e
57          * @param clazz the clazz
58          * @return the key value list
59          * @throws IllegalAccessException the illegal access exception
60          * @throws IllegalArgumentException the illegal argument exception
61          * @throws InvocationTargetException the invocation target exception
62          */
63         public <T> List<KeyValueList> getKeyValueList(Entity e, T clazz) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
64                 List<KeyValueList> kvList = e.getKeyValueList();
65                 Object value = null;
66                 Method[] methods = clazz.getClass().getDeclaredMethods();
67                 String propertyName = "";
68                 
69                 for (Method method : methods) { 
70                         if (method.getName().startsWith("get")) { 
71                                 propertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(3));
72                                 if (!(method.getReturnType().getName().contains("aai")) || method.getReturnType().getName().contains("java.util.List")) {
73                                         value = method.invoke(clazz);
74                                         KeyValueList kv = new KeyValueList();
75                                         kv.setKey(propertyName);
76                                         if (value != null) { 
77                                                 kv.setValue(value.toString());
78                                         } else { 
79                                                 kv.setValue("");
80                                         }
81                                         kvList.add(kv);
82                                 }
83                         }
84                 }
85                 return kvList;
86         }
87         
88         /**
89          * Gets the json from object.
90          *
91          * @param <T> the generic type
92          * @param clazz the clazz
93          * @return the json from object
94          * @throws JsonGenerationException the json generation exception
95          * @throws JsonMappingException the json mapping exception
96          * @throws IOException Signals that an I/O exception has occurred.
97          */
98         public <T> String getJsonFromObject(T clazz) throws JsonGenerationException, JsonMappingException, IOException {
99                 return getJsonFromObject(clazz, false, true);
100         }
101         
102         /**
103          * Gets the json from object.
104          *
105          * @param <T> the generic type
106          * @param clazz the clazz
107          * @param wrapRoot the wrap root
108          * @param indent the indent
109          * @return the json from object
110          * @throws JsonGenerationException the json generation exception
111          * @throws JsonMappingException the json mapping exception
112          * @throws IOException Signals that an I/O exception has occurred.
113          */
114         public <T> String getJsonFromObject(T clazz, boolean wrapRoot, boolean indent) throws JsonGenerationException, JsonMappingException, IOException {
115                 ObjectMapper mapper = new ObjectMapper();
116
117         mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
118         
119         mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
120         mapper.configure(SerializationFeature.INDENT_OUTPUT, indent);
121         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, wrapRoot);
122
123         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
124         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, wrapRoot);
125
126         mapper.registerModule(new JaxbAnnotationModule());
127         
128         ByteArrayOutputStream baos = new ByteArrayOutputStream();
129         
130         mapper.writeValue(baos, clazz);
131     
132         return baos.toString();
133         }
134         
135         /**
136          * Gets the json from dynamic object.
137          *
138          * @param ent the ent
139          * @param jaxbContext the jaxb context
140          * @param includeRoot the include root
141          * @return the json from dynamic object
142          * @throws JsonGenerationException the json generation exception
143          * @throws JsonMappingException the json mapping exception
144          * @throws IOException Signals that an I/O exception has occurred.
145          * @throws JAXBException the JAXB exception
146          */
147         public String getJsonFromDynamicObject(DynamicEntity ent, org.eclipse.persistence.jaxb.JAXBContext jaxbContext, boolean includeRoot) throws JsonGenerationException, JsonMappingException, IOException, JAXBException {
148                 org.eclipse.persistence.jaxb.JAXBMarshaller marshaller = jaxbContext.createMarshaller();
149                 
150                 marshaller.setProperty(org.eclipse.persistence.jaxb.JAXBMarshaller.JAXB_FORMATTED_OUTPUT, false);
151                 marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, Boolean.FALSE) ;
152                 marshaller.setProperty("eclipselink.json.include-root", includeRoot);
153                 marshaller.setProperty("eclipselink.media-type", "application/json");
154                 StringWriter writer = new StringWriter();
155                 marshaller.marshal(ent, writer);
156         
157                 return writer.toString();
158         }
159         
160         /**
161          * Gets the xml from object.
162          *
163          * @param <T> the generic type
164          * @param clazz the clazz
165          * @return the xml from object
166          * @throws JAXBException the JAXB exception
167          */
168         public <T> String getXmlFromObject(T clazz) throws JAXBException {
169                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
170                 JAXBContext jc = JAXBContext.newInstance(clazz.getClass().getPackage().getName());
171
172                 Marshaller marshaller = jc.createMarshaller();
173                 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
174                 marshaller.marshal(clazz, baos);
175         
176         return baos.toString();
177         }
178         
179         /**
180          * Gets the lookup key.
181          *
182          * @param baseKey the base key
183          * @param lookupHash the lookup hash
184          * @param keyProps the key props
185          * @return the lookup key
186          */
187         public String getLookupKey (String baseKey, HashMap<String,Object> lookupHash, Collection<String> keyProps) { 
188                 int baseKeyLen = baseKey.length();
189                 StringBuffer newKey = new StringBuffer();
190                 if (baseKeyLen > 0) { 
191                         newKey.append(baseKey);
192                 }
193                 
194                 Iterator <String> keyPropI = keyProps.iterator();
195                 while( keyPropI.hasNext() ){
196                         String keyProp = keyPropI.next();
197                         if (baseKeyLen > 0) {
198                                 newKey.append("&");
199                         }
200                         newKey.append(keyProp + "=" + lookupHash.get(keyProp));
201                 }
202                 return newKey.toString();
203         }
204         
205         /**
206          * Gets the lookup keys.
207          *
208          * @param lookupHashes the lookup hashes
209          * @param _dbRulesNodeKeyProps the db rules node key props
210          * @return the lookup keys
211          */
212         public String getLookupKeys (LinkedHashMap<String,HashMap<String,Object>> lookupHashes, Multimap<String, String> _dbRulesNodeKeyProps) { 
213                 Iterator<String> it = lookupHashes.keySet().iterator();
214                 String lookupKeys = "";
215                 while (it.hasNext()) {
216                         String objectType = (String)it.next();
217                         HashMap<String,Object> lookupHash = lookupHashes.get(objectType);
218                                                 
219                         Collection<String> keyProps = _dbRulesNodeKeyProps.get(objectType);
220                         Iterator <String> keyPropI = keyProps.iterator();
221                         while( keyPropI.hasNext() ){
222                                 lookupKeys += lookupHash.get(keyPropI.next());
223                         }
224                 }
225                 return lookupKeys;
226         }
227
228         /**
229          * Gets the example object.
230          *
231          * @param <T> the generic type
232          * @param clazz the clazz
233          * @param singleton the singleton
234          * @return the example object
235          * @throws IllegalAccessException the illegal access exception
236          * @throws IllegalArgumentException the illegal argument exception
237          * @throws InvocationTargetException the invocation target exception
238          * @throws NoSuchMethodException the no such method exception
239          * @throws SecurityException the security exception
240          * @throws AAIException the AAI exception
241          */
242         public <T> void getExampleObject(T clazz, boolean singleton) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, AAIException {
243                 Method[] methods = clazz.getClass().getDeclaredMethods();
244                 String dnHypPropertyName = "";
245                 String upCamPropertyName = "";
246                 Random rand = new Random();
247                 int randInt = rand.nextInt(10000000);
248
249                 for (Method method : methods) { 
250                         boolean go = false;
251                         if (method.getName().startsWith("get")) { 
252                                 dnHypPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(3));
253                                 upCamPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL,method.getName().substring(3));
254                                 go = true;
255                         } else if (method.getName().startsWith("is")) { 
256                                 dnHypPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(2));
257                                 upCamPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL,method.getName().substring(2));
258                                 go = true;
259                         }
260                         // don't return resource-version on a singleton
261                         if (singleton && dnHypPropertyName.equals("resource-version")) {
262                                 go = false;
263                         }
264                         if (go) { 
265                                 String retType = method.getReturnType().getName();
266                                 if (!retType.contains("aai") && !retType.contains("java.util.List")) {
267                                         // get the setter
268                                         Method meth = clazz.getClass().getMethod("set" + upCamPropertyName, method.getReturnType());
269                                         
270                                         if (retType.contains("String")) { 
271                                                 String val = "example-" + dnHypPropertyName + "-val-" +  randInt;
272                                                 if (val != null) { 
273                                                         meth.invoke(clazz, val);
274                                                 }
275                                         } else if (retType.toLowerCase().contains("long")) {
276                                                 Integer foo = rand.nextInt(100000);
277                                                 meth.invoke(clazz, foo.longValue());
278                                         } else if (retType.toLowerCase().contains("int")) { 
279                                                 meth.invoke(clazz, rand.nextInt(100000));
280                                         } else if (retType.toLowerCase().contains("short")) { 
281                                                 Integer randShort = rand.nextInt(10000);
282                                                 meth.invoke(clazz, randShort.shortValue());
283                                         } else if (retType.toLowerCase().contains("boolean")) { 
284                                                 meth.invoke(clazz, true);
285                                         }
286                                 }
287                         }
288                 }
289         }
290
291         
292         /**
293          * Gets the aai object from vertex.
294          *
295          * @param <T> the generic type
296          * @param clazz the clazz
297          * @param vert the vert
298          * @param _propertyDataTypeMap the property data type map
299          * @return the aai object from vertex
300          * @throws IllegalAccessException the illegal access exception
301          * @throws IllegalArgumentException the illegal argument exception
302          * @throws InvocationTargetException the invocation target exception
303          * @throws NoSuchMethodException the no such method exception
304          * @throws SecurityException the security exception
305          * @throws AAIException the AAI exception
306          */
307         public <T> void getAaiObjectFromVertex(T clazz, TitanVertex vert, Map<String, String> _propertyDataTypeMap) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, AAIException {
308                 Method[] methods = clazz.getClass().getDeclaredMethods();
309                 String dnHypPropertyName = "";
310                 String upCamPropertyName = "";
311                 for (Method method : methods) { 
312                         boolean go = false;
313                         if (method.getName().startsWith("get")) { 
314                                 dnHypPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(3));
315                                 upCamPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL,method.getName().substring(3));
316                                 go = true;
317                         } else if (method.getName().startsWith("is")) { 
318                                 dnHypPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(2));
319                                 upCamPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL,method.getName().substring(2));
320                                 go = true;
321                         }
322                         if (go) { 
323                                 String retType = method.getReturnType().getName();
324                                 if (!retType.contains("aai") && !retType.contains("java.util.List")) {
325                                         // get the setter
326                                         Method meth = clazz.getClass().getMethod("set" + upCamPropertyName, method.getReturnType());
327                                         
328                                         if (retType.contains("String")) { 
329                                                 String val = (String)vert.<String>property(dnHypPropertyName).orElse(null);
330                                                 if (val != null) { 
331                                                         meth.invoke(clazz, val);
332                                                 }
333                                         } else if (retType.toLowerCase().contains("long")) {
334                                                 String titanType = _propertyDataTypeMap.get(dnHypPropertyName);
335                                                 
336                                                 Long val = null;
337                                                 // we have a case where the type in titan is "Integer" but in the POJO it's Long or long
338                                                 if (titanType.toLowerCase().contains("int")) {
339                                                         Integer intVal = (Integer)vert.<Integer>property(dnHypPropertyName).orElse(null);
340                                                         if (intVal != null) { 
341                                                                 val = intVal.longValue();
342                                                         }
343                                                 } else { 
344                                                         val = (Long)vert.<Long>property(dnHypPropertyName).orElse(null);
345                                                 }
346                                                 if (val != null) { 
347                                                         meth.invoke(clazz, val);
348                                                 }
349                                         } else if (retType.toLowerCase().contains("int")) { 
350                                                 Integer val = (Integer)vert.<Integer>property(dnHypPropertyName).orElse(null);
351                                                 if (val != null) { 
352                                                         meth.invoke(clazz, val);
353                                                 }
354                                         } else if (retType.toLowerCase().contains("short")) { 
355                                                 Short val = (Short)vert.<Short>property(dnHypPropertyName).orElse(null);
356                                                 if (val != null) { 
357                                                         meth.invoke(clazz, val);
358                                                 }
359                                         } else if (retType.toLowerCase().contains("boolean")) { 
360                                                 Boolean val = (Boolean)vert.<Boolean>property(dnHypPropertyName).orElse(null);
361                                                 if (val != null) { 
362                                                         meth.invoke(clazz, val);
363                                                 }
364                                         }
365                                 }
366                         }
367                 }
368         }
369
370         /**
371          * Gets the topology object.
372          *
373          * @param <T> the generic type
374          * @param clazz the clazz
375          * @param _dbRulesNodeNameProps the db rules node name props
376          * @param _dbRulesNodeKeyProps the db rules node key props
377          * @param vert the vert
378          * @return the topology object
379          * @throws IllegalAccessException the illegal access exception
380          * @throws IllegalArgumentException the illegal argument exception
381          * @throws InvocationTargetException the invocation target exception
382          * @throws NoSuchMethodException the no such method exception
383          * @throws SecurityException the security exception
384          * @throws AAIException the AAI exception
385          */
386         public <T> void getTopologyObject(T clazz, Multimap<String, String> _dbRulesNodeNameProps, Multimap<String, String> _dbRulesNodeKeyProps, TitanVertex vert) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, AAIException {
387                 Method[] methods = clazz.getClass().getDeclaredMethods();
388                 String dnHypPropertyName = "";
389 //              Object value = null;
390                 List<String> includeProps = new ArrayList<String>();
391                                 
392                 if ("false".equals(AAIConfig.get("aai.notification.topology.allAttrs", "false"))) {
393                         for (Method method : methods) { 
394                                 if (method.getName().startsWith("is")) { 
395                                         dnHypPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(2));
396                                         String upCamPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL,method.getName().substring(2));
397                                         String retType = method.getReturnType().getName();
398                                         if (retType.equals("java.lang.Boolean")) {
399                                                 // get the setter
400                                                 Method setterMeth = clazz.getClass().getMethod("set" + upCamPropertyName, method.getReturnType());
401                                                 setterMeth.invoke(clazz, (Boolean)null);
402                                         }
403                                 }
404                         }
405                         String dnHypClassName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,clazz.getClass().getSimpleName());
406                         Collection<String> keepProps = _dbRulesNodeNameProps.get(dnHypClassName);
407                         Iterator <String> keepPropI = keepProps.iterator();
408                         while( keepPropI.hasNext() ){
409                                 includeProps.add(keepPropI.next());
410                         }
411                         Collection<String> keepProps2 = _dbRulesNodeKeyProps.get(dnHypClassName);
412                         Iterator <String> keepPropI2 = keepProps2.iterator();
413                         while( keepPropI2.hasNext() ){
414                                 includeProps.add(keepPropI2.next());
415                         }
416                 }
417                 
418                 for (Method method : methods) { 
419                         if (method.getName().startsWith("get")) { 
420                                 dnHypPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(3));
421                                 if (includeProps.size() > 0) { 
422                                         if (!includeProps.contains(dnHypPropertyName)) {
423                                                 continue;
424                                         }
425                                 }
426                                 String upCamPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL,method.getName().substring(3));
427                                 String retType = method.getReturnType().getName();
428                                 if (!retType.contains("aai") && !retType.contains("java.util.List")) {
429                                         // get the setter
430                                         Method meth = clazz.getClass().getMethod("set" + upCamPropertyName, method.getReturnType());
431                                         
432                                         if (retType.contains("String")) { 
433                                                 String val = (String)vert.<String>property(dnHypPropertyName).orElse(null);
434                                                 if (val != null) { 
435                                                         meth.invoke(clazz, val);
436                                                 }
437                                         } else if (retType.toLowerCase().contains("long")) {
438                                                 Long val = (Long)vert.<Long>property(dnHypPropertyName).orElse(null);
439                                                 if (val != null) { 
440                                                         meth.invoke(clazz, val);
441                                                 }
442                                         } else if (retType.toLowerCase().contains("int")) { 
443                                                 Integer val = (Integer)vert.<Integer>property(dnHypPropertyName).orElse(null);
444                                                 if (val != null) { 
445                                                         meth.invoke(clazz, val);
446                                                 }
447                                         } else if (retType.toLowerCase().contains("short")) { 
448                                                 Short val = (Short)vert.<Short>property(dnHypPropertyName).orElse(null);
449                                                 if (val != null) { 
450                                                         meth.invoke(clazz, val);
451                                                 }
452                                         }
453                                 }
454                         }
455                 }
456         }
457
458         /**
459          * Gets the dynamic topology object.
460          *
461          * @param aaiRes the aai res
462          * @param meObjectType the me object type
463          * @param _dbRulesNodeNameProps the db rules node name props
464          * @param _dbRulesNodeKeyProps the db rules node key props
465          * @param _propertyDataTypeMap the property data type map
466          * @param vert the vert
467          * @return the dynamic topology object
468          * @throws AAIException the AAI exception
469          */
470         public DynamicEntity getDynamicTopologyObject(AAIResource aaiRes, DynamicType meObjectType, Multimap<String, String> _dbRulesNodeNameProps,
471                                                   Multimap<String, String> _dbRulesNodeKeyProps, Map<String, String> _propertyDataTypeMap, TitanVertex vert) throws AAIException {
472                 
473                 DynamicEntity meObject = meObjectType.newDynamicEntity();
474                 
475                 List<String> includeProps = new ArrayList<String>();
476                                 
477                 if ("false".equals(AAIConfig.get("aai.notification.topology.allAttrs", "false"))) {
478                         String dnHypClassName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,meObjectType.getJavaClass().getSimpleName());
479                         Collection<String> keepProps = _dbRulesNodeNameProps.get(dnHypClassName);
480                         Iterator <String> keepPropI = keepProps.iterator();
481                         while( keepPropI.hasNext() ){
482                                 includeProps.add(keepPropI.next());
483                         }
484                         Collection<String> keepProps2 = _dbRulesNodeKeyProps.get(dnHypClassName);
485                         Iterator <String> keepPropI2 = keepProps2.iterator();
486                         while( keepPropI2.hasNext() ) {
487                                 includeProps.add(keepPropI2.next());
488                         }
489                 }
490                 
491         
492                 
493                 for (String attrName : aaiRes.getStringFields()) { 
494                         if (includeProps.contains(attrName)) { 
495                                 meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), vert.<String>property(attrName).orElse(null));
496                         }
497                 }
498                 // the attrName might need to be converted to camel case!!!
499                 for (String attrName : aaiRes.getLongFields()) {
500                         if (includeProps.contains(attrName)) { 
501                                 String titanType = _propertyDataTypeMap.get(attrName);          
502
503                                 Long val = null;
504                                 // we have a case where the type in titan is "Integer" but in the POJO it's Long or long
505                                 if (titanType.toLowerCase().contains("int")) {
506                                         Integer intVal = (Integer)vert.<Integer>property(attrName).orElse(null);
507                                         if (intVal != null) { 
508                                                 val = intVal.longValue();
509                                         }
510                                 } else { 
511                                         val = (Long)vert.<Long>property(attrName).orElse(null);
512                                 }
513                                 meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), val);
514                         }
515                 }
516
517                 for (String attrName : aaiRes.getIntFields()) { 
518                         if (includeProps.contains(attrName)) { 
519                                 Integer val = (Integer)vert.<Integer>property(attrName).orElse(null);
520                                 meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), val);
521                         }
522                 }
523
524                 for (String attrName : aaiRes.getShortFields()) { 
525                         if (includeProps.contains(attrName)) { 
526                                 String titanType = _propertyDataTypeMap.get(attrName);          
527
528                                 Short val = null;
529                                 // we have a case where the type in titan is "Integer" but in the POJO it's Long or long
530                                 if (titanType.toLowerCase().contains("int")) {
531                                         Integer intVal = (Integer)vert.<Integer>property(attrName).orElse(null);
532                                         if (intVal != null) { 
533                                                 val = intVal.shortValue();
534                                         }
535                                 } else { 
536                                         val = (Short)vert.<Short>property(attrName).orElse(null);
537                                 }
538                                 meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), val);
539                         }
540                 }
541
542                 for (String attrName : aaiRes.getBooleanFields()) {
543                         if (includeProps.contains(attrName)) { 
544                                 Boolean val = (Boolean)vert.<Boolean>property(attrName).orElse(null);
545                                 meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)),  val);
546                         }
547                 }
548                 return meObject;        
549         }
550
551         /**
552          * Gets the aai dynamic object from vertex.
553          *
554          * @param aaiRes the aai res
555          * @param meObject the me object
556          * @param vert the vert
557          * @param _propertyDataTypeMap the property data type map
558          * @return the aai dynamic object from vertex
559          */
560         public void getAaiDynamicObjectFromVertex(AAIResource aaiRes, DynamicEntity meObject, TitanVertex vert,
561                                               Map<String, String> _propertyDataTypeMap) {
562                 getAaiDynamicObjectFromVertex(aaiRes, meObject, vert, _propertyDataTypeMap, null);
563         }
564         
565         /**
566          * Gets the aai dynamic object from vertex.
567          *
568          * @param aaiRes the aai res
569          * @param meObject the me object
570          * @param vert the vert
571          * @param _propertyDataTypeMap the property data type map
572          * @param propertyOverRideHash the property over ride hash
573          * @return the aai dynamic object from vertex
574          */
575         @SuppressWarnings("unchecked")
576         public void getAaiDynamicObjectFromVertex(AAIResource aaiRes, DynamicEntity meObject, TitanVertex vert,
577                                               Map<String, String> _propertyDataTypeMap, HashMap<String, Object> propertyOverRideHash) {
578                         
579                 for (String attrName : aaiRes.getStringFields()) { 
580                         if (propertyOverRideHash == null || (propertyOverRideHash != null && propertyOverRideHash.containsKey(attrName))) {
581                                 meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), vert.<String>property(dbAliasWorkaround(attrName)).orElse(null));
582                         }
583                 }
584                 
585                 for (String attrName : aaiRes.getStringListFields()) { 
586                         if (propertyOverRideHash == null || (propertyOverRideHash != null && propertyOverRideHash.containsKey(attrName))) {
587                                 meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), vert.<ArrayList<String>>property(attrName).orElse(null));
588                         }
589                 }
590                 
591                 // the attrName might need to be converted to camel case!!!
592                 for (String attrName : aaiRes.getLongFields()) { 
593                         String titanType = _propertyDataTypeMap.get(attrName);          
594                         Long val = null;
595                         // we have a case where the type in titan is "Integer" but in the POJO it's Long or long
596                         if (titanType.toLowerCase().contains("int")) {
597                                 Object vertexVal = vert.property(attrName).orElse(null);
598                                 if (vertexVal != null) {
599                                         if (vertexVal instanceof Integer) {
600                                                 val = ((Integer)vertexVal).longValue();
601                                                 
602                                         } else {
603                                                 val = (Long)vert.<Long>property(attrName).orElse(null); 
604                                         }
605                                 }
606                         } else {
607                                 val = (Long)vert.<Long>property(attrName).orElse(null); 
608                         }
609                         if (val != null) { 
610                                 if (propertyOverRideHash == null || (propertyOverRideHash != null && propertyOverRideHash.containsKey(attrName))) {
611                                         meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), val);
612                                 }
613                         }
614                 }
615                 
616                 for (String attrName : aaiRes.getIntFields()) { 
617                         Integer val = (Integer)vert.<Integer>property(attrName).orElse(null);
618                         if (val != null) { 
619                                 if (propertyOverRideHash == null || (propertyOverRideHash != null && propertyOverRideHash.containsKey(attrName))) {
620                                         meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), val);
621                                 }
622                         }
623                 }
624                 
625                 for (String attrName : aaiRes.getShortFields()) { 
626                         String titanType = _propertyDataTypeMap.get(attrName);          
627                         Short val = null;
628                         // we have a case where the type in titan is "Integer" but in the POJO it's Long or long
629                         if (titanType.toLowerCase().contains("int")) {
630                                 Integer intVal = (Integer)vert.<Integer>property(attrName).orElse(null);
631                                 if (intVal != null) { 
632                                         val = intVal.shortValue();
633                                 }
634                         } else { 
635                                 val = (Short)vert.<Short>property(attrName).orElse(null);
636                         }
637                         if (val != null) { 
638                                 if (propertyOverRideHash == null || (propertyOverRideHash != null && propertyOverRideHash.containsKey(attrName))) {
639                                         meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)), val);
640                                 }
641                         }
642                 }
643                 
644                 for (String attrName : aaiRes.getBooleanFields()) {
645                         Boolean val = (Boolean)vert.<Boolean>property(attrName).orElse(null);
646                         // This is not ideal, but moxy isn't marshalling these attributes.
647                         // TODO: Figure out how to see the default-value from the OXM at startup (or at runtime).
648                         String dnHypClassName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,aaiRes.getSimpleName());
649                         if (val == null && AAIConfig.getDefaultBools().containsKey(dnHypClassName)) {
650                                 if (AAIConfig.getDefaultBools().get(dnHypClassName).contains(attrName)) {
651                                         val = false;
652                                 }
653                         }
654                         if (val != null) { 
655                                 if (propertyOverRideHash == null || (propertyOverRideHash != null && propertyOverRideHash.containsKey(attrName))) {
656                                         meObject.set((CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,attrName)),  val);
657                                 }
658                         }
659                 }
660                         
661         }
662         
663         private String dbAliasWorkaround(String propName) {
664                 final String modelInvariantIdLocal = "model-invariant-id-local";
665                 final String modelVersionIdLocal = "model-version-id-local";
666                 final String modelInvariantId = "model-invariant-id";
667                 final String modelVersionId = "model-version-id";
668                 
669                 if (propName.equals(modelInvariantId)) {
670                         return modelInvariantIdLocal;
671                 }
672                 if (propName.equals(modelVersionId)) {
673                         return modelVersionIdLocal;
674                 }
675                 
676                 return propName;
677                 
678         }
679
680         /**
681          * Gets the dynamic example object.
682          *
683          * @param childObject the child object
684          * @param aaiRes the aai res
685          * @param singleton the singleton
686          * @return the dynamic example object
687          */
688         public void getDynamicExampleObject(DynamicEntity childObject, AAIResource aaiRes, boolean singleton) {
689                 // TODO Auto-generated method stub
690
691                 Random rand = new Random();
692                 Integer randInt = rand.nextInt(100000);
693                 long range = 100000000L;
694                 long randLong = (long)(rand.nextDouble()*range);
695                 Integer randShrt = rand.nextInt(20000);
696                 short randShort = randShrt.shortValue();
697
698                 for (String dnHypAttrName : aaiRes.getStringFields()) { 
699                         
700                         if (singleton && ("resource-version").equals(dnHypAttrName)) {
701                                 continue;
702                         }
703                         
704                         String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName);
705                         childObject.set(dnCamAttrName, "example-" + dnHypAttrName + "-val-" +  randInt);
706
707                 }
708                 
709                 for (String dnHypAttrName : aaiRes.getStringListFields()) { 
710                         ArrayList<String> exampleList = new ArrayList<String>();
711                         exampleList.add("example-" + dnHypAttrName + "-val-" + randInt + "-" + 1);
712                         exampleList.add("example-" + dnHypAttrName + "-val-" + randInt + "-" + 2);
713                         String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName);
714                         childObject.set(dnCamAttrName, exampleList);
715                 }
716                 
717                 // the attrName might need to be converted to camel case!!!
718                 for (String dnHypAttrName : aaiRes.getLongFields()) { 
719                         String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName);
720                         childObject.set(dnCamAttrName, randLong);
721                 }
722
723                 for (String dnHypAttrName : aaiRes.getIntFields()) { 
724                         String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName);
725                         childObject.set(dnCamAttrName, randInt);
726                 }
727
728                 for (String dnHypAttrName : aaiRes.getShortFields()) { 
729                         String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName);
730                         childObject.set(dnCamAttrName, randShort);
731                 }
732
733                 for (String dnHypAttrName : aaiRes.getBooleanFields()) {
734                         String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName);
735                         childObject.set(dnCamAttrName, Boolean.TRUE);
736                 }
737         }
738 }