fixing further bean field property mapping probelm, contributes to #3841: resolve...
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / dao / initializer / AdvancedBeanInitializer.java
index eb7cc9b3beefc906648aa65a31051d824d25c24b..5e75c9dbd9dc90ad89c112d6df6670cab129529d 100644 (file)
@@ -222,7 +222,7 @@ public class AdvancedBeanInitializer extends HibernateBeanInitializer {
                 //new\r
                 for (Object parentBean : parentBeans){\r
                     try{\r
-                        Object propertyValue = PropertyUtils.getProperty(parentBean, property);\r
+                        Object propertyValue = PropertyUtils.getProperty(parentBean, mapFieldToPropertyName(property, parentBean.getClass().getSimpleName()));\r
                         preparePropertyValueForBulkLoadOrStore(node, parentBean, property, propertyValue);\r
                     } catch (IllegalAccessException e) {\r
                         logger.error("Illegal access on property " + property);\r
@@ -329,110 +329,123 @@ public class AdvancedBeanInitializer extends HibernateBeanInitializer {
             invokePropertyAutoInitializers(bean);\r
         }\r
 \r
-        private void storeInitializedCollection(AbstractPersistentCollection persistedCollection,\r
-                BeanInitNode node, String param) {\r
-            Collection<?> collection;\r
-\r
-            if (persistedCollection  instanceof Collection) {\r
-                collection = (Collection<?>) persistedCollection;\r
-            }else if (persistedCollection instanceof Map) {\r
-                collection = ((Map<?,?>)persistedCollection).values();\r
-            }else{\r
-                throw new RuntimeException ("Non Map and non Collection cas not handled in storeInitializedCollection()");\r
-            }\r
-            for (Object value : collection){\r
-                preparePropertyValueForBulkLoadOrStore(node, null, param, value);\r
-            }\r
-        }\r
-\r
-        private void bulkLoadLazies(BeanInitNode node) {\r
-\r
-            if (logger.isTraceEnabled()){logger.trace("bulk load " +  node);}\r
-\r
-            //beans\r
-            for (Class<?> clazz : node.getLazyBeans().keySet()){\r
-                Set<Serializable> idSet = node.getLazyBeans().get(clazz);\r
-                if (idSet != null && ! idSet.isEmpty()){\r
-\r
-                    if (logger.isTraceEnabled()){logger.trace("bulk load beans of class " +  clazz.getSimpleName());}\r
-                    //TODO use entity name\r
-                    String hql = " SELECT c FROM %s as c %s WHERE c.id IN (:idSet) ";\r
-                    hql = String.format(hql, clazz.getSimpleName(), addAutoinitFetchLoading(clazz, "c"));\r
-                    Query query = genericDao.getHqlQuery(hql);\r
-                    query.setParameterList("idSet", idSet);\r
-                    List<Object> list = query.list();\r
-\r
-                    if (logger.isTraceEnabled()){logger.trace("initialize bulk loaded beans of class " +  clazz.getSimpleName());}\r
-                    for (Object object : list){\r
-                        if (object instanceof HibernateProxy){  //TODO remove hibernate dependency\r
-                            object = initializeInstance(object);\r
-                        }\r
-                        autoinitializeBean(object);\r
-                        node.addBean(object);\r
-                    }\r
-                    if (logger.isTraceEnabled()){logger.trace("bulk load - DONE");}\r
-                }\r
-            }\r
-            node.resetLazyBeans();\r
-\r
-            //collections\r
-            for (Class<?> ownerClazz : node.getLazyCollections().keySet()){\r
-                Map<String, Set<Serializable>> lazyParams = node.getLazyCollections().get(ownerClazz);\r
-                for (String param : lazyParams.keySet()){\r
-                    Set<Serializable> idSet = lazyParams.get(param);\r
-                    if (idSet != null && ! idSet.isEmpty()){\r
-                        if (logger.isTraceEnabled()){logger.trace("bulk load " + node + " collections ; ownerClass=" +  ownerClazz.getSimpleName() + " ; param = " + param);}\r
-\r
-                        //TODO use entity name ??\r
-                        //get from repository\r
-                        List<Object[]> list;\r
-                        String hql = "SELECT oc, oc.%s " +\r
-                                " FROM %s as oc LEFT JOIN FETCH oc.%s as col %s " +\r
-                                " WHERE oc.id IN (:idSet) ";\r
+               private void storeInitializedCollection(AbstractPersistentCollection persistedCollection,\r
+                               BeanInitNode node, String param) {\r
+                       Collection<?> collection;\r
+\r
+                       if (persistedCollection  instanceof Collection) {\r
+                               collection = (Collection<?>) persistedCollection;\r
+                       }else if (persistedCollection instanceof Map) {\r
+                               collection = ((Map<?,?>)persistedCollection).values();\r
+                       }else{\r
+                               throw new RuntimeException ("Non Map and non Collection cas not handled in storeInitializedCollection()");\r
+                       }\r
+                       for (Object value : collection){\r
+                               preparePropertyValueForBulkLoadOrStore(node, null, param, value);\r
+                       }\r
+               }\r
+\r
+               private void bulkLoadLazies(BeanInitNode node) {\r
+\r
+                       if (logger.isTraceEnabled()){logger.trace("bulk load " +  node);}\r
+\r
+                       //beans\r
+                       for (Class<?> clazz : node.getLazyBeans().keySet()){\r
+                               Set<Serializable> idSet = node.getLazyBeans().get(clazz);\r
+                               if (idSet != null && ! idSet.isEmpty()){\r
+\r
+                                       if (logger.isTraceEnabled()){logger.trace("bulk load beans of class " +  clazz.getSimpleName());}\r
+                                       //TODO use entity name\r
+                                       String hql = " SELECT c FROM %s as c %s WHERE c.id IN (:idSet) ";\r
+                                       hql = String.format(hql, clazz.getSimpleName(), addAutoinitFetchLoading(clazz, "c"));\r
+                                       if (logger.isTraceEnabled()){logger.trace(hql);}\r
+                                       Query query = genericDao.getHqlQuery(hql);\r
+                                       query.setParameterList("idSet", idSet);\r
+                                       List<Object> list = query.list();\r
+\r
+                                       if (logger.isTraceEnabled()){logger.trace("initialize bulk loaded beans of class " +  clazz.getSimpleName());}\r
+                                       for (Object object : list){\r
+                                               if (object instanceof HibernateProxy){  //TODO remove hibernate dependency\r
+                                                       object = initializeInstance(object);\r
+                                               }\r
+                                               autoinitializeBean(object);\r
+                                               node.addBean(object);\r
+                                       }\r
+                                       if (logger.isTraceEnabled()){logger.trace("bulk load - DONE");}\r
+                               }\r
+                       }\r
+                       node.resetLazyBeans();\r
+\r
+                       //collections\r
+                       for (Class<?> ownerClazz : node.getLazyCollections().keySet()){\r
+                               Map<String, Set<Serializable>> lazyParams = node.getLazyCollections().get(ownerClazz);\r
+                               for (String param : lazyParams.keySet()){\r
+                                       Set<Serializable> idSet = lazyParams.get(param);\r
+                                       if (idSet != null && ! idSet.isEmpty()){\r
+                                               if (logger.isTraceEnabled()){logger.trace("bulk load " + node + " collections ; ownerClass=" +  ownerClazz.getSimpleName() + " ; param = " + param);}\r
+\r
+                                               //TODO use entity name ??\r
+                                               //get from repository\r
+                                               List<Object[]> list;\r
+                                               String hql = "SELECT oc " +\r
+                                                               " FROM %s as oc JOIN FETCH oc.%s as col %s " +\r
+                                                               " WHERE oc.id IN (:idSet) ";\r
 \r
 //                                             String hql = "SELECT oc.%s " +\r
 //                                                             " FROM %s as oc WHERE oc.id IN (:idSet) ";\r
-                        hql = String.format(hql, param, ownerClazz.getSimpleName(), param,\r
-                                "" /*addAutoinitFetchLoading(clazz, "col")*/);\r
-\r
-                        try {\r
-\r
-                            Query query = genericDao.getHqlQuery(hql);\r
-                            query.setParameterList("idSet", idSet);\r
-                            list = query.list();\r
-                        } catch (HibernateException e) {\r
-                            e.printStackTrace();\r
-                            throw e;\r
-                        }\r
-\r
-                        //getTarget and add to child node\r
-                        if (logger.isTraceEnabled()){logger.trace("initialize bulk loaded " + node + " collections - DONE");}\r
-                        for (Object[] listItems : list){\r
-                            Object newBean = listItems[1];\r
-                            if (newBean == null){\r
-                                System.out.println("Collection is null");\r
-                            }\r
-                            if (newBean instanceof HibernateProxy){\r
-                                newBean = initializeInstance(newBean);\r
+                                               hql = String.format(hql, ownerClazz.getSimpleName(), param,\r
+                                                               "" /*addAutoinitFetchLoading(clazz, "col")*/);\r
+\r
+                                               try {\r
+                                                       if (logger.isTraceEnabled()){logger.trace(hql);}\r
+                                                       Query query = genericDao.getHqlQuery(hql);\r
+                                                       query.setParameterList("idSet", idSet);\r
+                                                       list = query.list();\r
+                                               } catch (HibernateException e) {\r
+                                                       e.printStackTrace();\r
+                                                       throw e;\r
+                                               }\r
+\r
+                                               //getTarget and add to child node\r
+                                               if (logger.isTraceEnabled()){logger.trace("initialize bulk loaded " + node + " collections - DONE");}\r
+                                               for (Object parentBean : list){\r
+                            try {\r
+                                                           Object propValue = PropertyUtils.getProperty(\r
+                                                                   parentBean,\r
+                                                                   mapFieldToPropertyName(param, parentBean.getClass().getSimpleName())\r
+                                                                 );\r
+\r
+                                                       if (propValue == null){\r
+                                                           logger.trace("Collection is null");\r
+                                                       }else {\r
+                                                           for(Object newBean : (Collection<Object>)propValue ) {\r
+                                                               if (newBean instanceof HibernateProxy){\r
+                                                                   newBean = initializeInstance(newBean);\r
+                                                               }\r
+                                                               autoinitializeBean(newBean);\r
+                                                               node.addBean(newBean);\r
+                                                           }\r
+                                                       }\r
+                            } catch (Exception e) {\r
+                                // TODO better throw an exception ?\r
+                                logger.error("error while getting collection property", e);\r
                             }\r
-                            autoinitializeBean(newBean);\r
-                            node.addBean(newBean);\r
-                        }\r
-                        if (logger.isTraceEnabled()){logger.trace("bulk load " + node + " collections - DONE");}\r
-                    }\r
-                }\r
-            }\r
-            for (AbstractPersistentCollection collection : node.getUninitializedCollections()){\r
-                if (! collection.wasInitialized()){  //should not happen anymore\r
-                    collection.forceInitialization();\r
-                }\r
-            }\r
+                                               }\r
+                                               if (logger.isTraceEnabled()){logger.trace("bulk load " + node + " collections - DONE");}\r
+                                       }\r
+                               }\r
+                       }\r
+                       for (AbstractPersistentCollection collection : node.getUninitializedCollections()){\r
+                               if (! collection.wasInitialized()){  //should not happen anymore\r
+                                       collection.forceInitialization();\r
+                               }\r
+                       }\r
 \r
-            node.resetLazyCollections();\r
+                       node.resetLazyCollections();\r
 \r
-            if (logger.isTraceEnabled()){logger.trace("bulk load " +  node + " - DONE ");}\r
+                       if (logger.isDebugEnabled()){logger.debug("bulk load " +  node + " - DONE ");}\r
 \r
-        }\r
+               }\r
 \r
 \r
         private String addAutoinitFetchLoading(Class<?> clazz, String beanAlias) {\r
@@ -455,19 +468,20 @@ public class AdvancedBeanInitializer extends HibernateBeanInitializer {
         }\r
 \r
         /**\r
-         * Rename bean attributes to hibernate (field) attribute, due to bean inconsistencies\r
+         * Rename hibernate (field) attribute to Bean property name, due to bean inconsistencies\r
          * #3841\r
          * @param param\r
          * @param ownerClass\r
          * @return\r
          */\r
-        private String workAroundBeanInconsistency(String param, String ownerClass) {\r
-            if (ownerClass.contains("Description") && param.equals("elements")){\r
-                //DescriptionBase.descriptionElements -> elements\r
-                return "descriptionElements";\r
-            }else if(ownerClass.equals("Classification") && param.equals("childNodes")){\r
-                return "rootNodes";\r
-            }else{\r
+        private String mapFieldToPropertyName(String param, String ownerClass) {\r
+            if (ownerClass.contains("Description") && param.equals("descriptionElements")){\r
+                return "elements";\r
+            }\r
+            if (ownerClass.startsWith("FeatureNode") && param.equals("children")) {\r
+                return "childNodes";\r
+            }\r
+            else{\r
                 return param;\r
             }\r
         }\r