ref #6581 adapt TaxonGraphBeforeTransactionCompleteProcess to nomenclaturalSource
authorAndreas Müller <a.mueller@bgbm.org>
Thu, 27 Aug 2020 21:45:19 +0000 (23:45 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Thu, 27 Aug 2020 21:45:19 +0000 (23:45 +0200)
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/taxonGraph/TaxonGraphBeforeTransactionCompleteProcess.java

index 326fa404c9d5d1e65fb96566e6bed38ba4993911..ad18b13b9ecb3026a4ccd35d87c6393dbe4b3d72 100644 (file)
@@ -8,8 +8,10 @@
 */
 package eu.etaxonomy.cdm.api.service.taxonGraph;
 
 */
 package eu.etaxonomy.cdm.api.service.taxonGraph;
 
+import java.lang.reflect.InvocationTargetException;
 import java.util.Objects;
 
 import java.util.Objects;
 
+import org.apache.commons.beanutils.BeanUtils;
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
@@ -41,7 +43,7 @@ public class TaxonGraphBeforeTransactionCompleteProcess
     private static final Logger logger = Logger.getLogger(TaxonGraphBeforeTransactionCompleteProcess.class);
 
     private String[] NAMEPARTS_OR_RANK_PROPS = new String[]{"genusOrUninomial", "specificEpithet", "rank"};
     private static final Logger logger = Logger.getLogger(TaxonGraphBeforeTransactionCompleteProcess.class);
 
     private String[] NAMEPARTS_OR_RANK_PROPS = new String[]{"genusOrUninomial", "specificEpithet", "rank"};
-    private String[] NOMREF_PROP = new String[]{"nomenclaturalSource.citation"};
+    private String NOMREF_PROP = "nomenclaturalSource.citation";
 
     private Session temporarySession;
 
 
     private Session temporarySession;
 
@@ -92,7 +94,7 @@ public class TaxonGraphBeforeTransactionCompleteProcess
                     onNameOrRankChange(entity);
                     getSession().flush();
                 }
                     onNameOrRankChange(entity);
                     getSession().flush();
                 }
-                int changedNomRefIndex = checkStateChange(NOMREF_PROP);
+                int changedNomRefIndex = checkStateChangeNomRef(NOMREF_PROP);
                 if(changedNomRefIndex > -1){
                     createTempSession(session);
                     onNomReferenceChange(entity, (Reference)oldState[changedNomRefIndex]);
                 if(changedNomRefIndex > -1){
                     createTempSession(session);
                     onNomReferenceChange(entity, (Reference)oldState[changedNomRefIndex]);
@@ -133,6 +135,41 @@ public class TaxonGraphBeforeTransactionCompleteProcess
         throw new RuntimeException("TaxonName class misses at least one property of: " + ArrayUtils.toString(propertyNamesToCheck));
     }
 
         throw new RuntimeException("TaxonName class misses at least one property of: " + ArrayUtils.toString(propertyNamesToCheck));
     }
 
+    private int checkStateChangeNomRef(String propertyPath){
+
+        if(oldState == null){
+            return -1;
+        }
+
+        try {
+            String[] path = propertyPath.split("\\.");
+            for (int i=1; i < propertyNames.length; i++) {
+                if(propertyNames[i].equals(path[0])){
+                    if (oldState[i] == null && state[i] == null){
+                        return -1;
+                    }else{
+                        //TODO make it recursive (until now only a 2 step path is allowed, but should be enough for the given use-case)
+                        Object oldStatePathObj = (oldState[i]==null) ? null: BeanUtils.getProperty(oldState[i],path[1]);
+                        Object newStatePathObj = (state[i]==null) ? null: BeanUtils.getProperty(state[i],path[1]);
+                        if (oldStatePathObj == null && newStatePathObj == null){
+                            return -1;
+                        }else{
+                            if(!Objects.equals(oldStatePathObj, newStatePathObj)){
+                                return 0;
+                            }else{
+                                return -1;
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
+            throw new RuntimeException("The nomenclatural reference path seems to be invalid: " + propertyPath);
+        }
+        // this exception should be raised during the unit tests already and thus will never occur in production
+        throw new RuntimeException("TaxonName class misses at least one property of: " + propertyPath);
+    }
+
     /**
      * Concept of creation of sub-sessions found in AuditProcess.doBeforeTransactionCompletion(SessionImplementor session)
      * and adapted to make it work for this case.
     /**
      * Concept of creation of sub-sessions found in AuditProcess.doBeforeTransactionCompletion(SessionImplementor session)
      * and adapted to make it work for this case.