ref #4770 handle unprotected titlecache for person2Team converter and some finetuning...
authorAndreas Müller <a.mueller@bgbm.org>
Mon, 21 Dec 2020 21:44:24 +0000 (22:44 +0100)
committerAndreas Müller <a.mueller@bgbm.org>
Mon, 21 Dec 2020 21:45:31 +0000 (22:45 +0100)
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/AgentServiceImpl.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/AgentServiceImplTest.java

index 173f998c1b94e8135dc9b5986f7d1898037cc27d..1c85000250b53bbfc4c2a17b7b22b0fc6a5f47e8 100644 (file)
@@ -221,18 +221,21 @@ public class AgentServiceImpl
        public UpdateResult convertPerson2Team(Person person) throws MergeException, IllegalArgumentException {
            UpdateResult result = new UpdateResult();
         Team team = Team.NewInstance();
-               ConvertMergeStrategy strategy = ConvertMergeStrategy.NewInstance(TeamOrPersonBase.class);
-               strategy.setDefaultMergeMode(MergeMode.SECOND);
-               strategy.setDefaultCollectionMergeMode(MergeMode.SECOND);
-               strategy.setDeleteSecondObject(true);
+        ConvertMergeStrategy strategy = ConvertMergeStrategy.NewInstance(TeamOrPersonBase.class);
+        strategy.setDefaultMergeMode(MergeMode.SECOND);
+        strategy.setDefaultCollectionMergeMode(MergeMode.SECOND);
+        team.setProtectedTitleCache(true);
+        strategy.setMergeMode("protectedTitleCache", MergeMode.FIRST); //as we do not add team members, the titleCache of the new team should be always protected
+        strategy.setDeleteSecondObject(true);
 
                if (! genericDao.isMergeable(team, person, strategy)){
                        throw new MergeException("Person can not be transformed into team.");
                }
                try {
                        team.setProtectedNomenclaturalTitleCache(false);
-                       team.setProtectedTitleCache(true);
-                       team.setTitleCache(person.getTitleCache(), true);
+                       if (person.isProtectedTitleCache() || !person.getTitleCache().startsWith("Person#")){
+                           team.setTitleCache(person.getTitleCache(), true);
+                       }
                team.setNomenclaturalTitle(person.getNomenclaturalTitle(), true);
                        team = this.save(team);
                        genericDao.merge(team, person, strategy);
index 6c5ba75a4637e84546f22472ad247301b7f88c77..7812906222daa00c16402a08d3bc0ea84d6ad50c 100644 (file)
@@ -34,6 +34,7 @@ import eu.etaxonomy.cdm.model.location.Point;
 import eu.etaxonomy.cdm.model.name.Rank;
 import eu.etaxonomy.cdm.model.name.TaxonName;
 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
+import eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy;
 import eu.etaxonomy.cdm.strategy.merge.MergeException;
 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
@@ -73,25 +74,53 @@ public class AgentServiceImplTest extends CdmTransactionalIntegrationTest{
        name.setCombinationAuthorship(person);
        person.addAnnotation(annotation);
 
-       service.save(person);
        nameSerivce.save(name);
 
-
        Team team = null;
-       UpdateResult result = null;
-               try {
-                   result = service.convertPerson2Team(person);
-                   team = (Team) result.getCdmEntity();
-               } catch (MergeException e) {
-                       Assert.fail("No Merge exception should be thrown");
-               }
-       Assert.assertNotNull(team);
-       Assert.assertEquals("Title cache must be equal", fullAuthor, team.getTitleCache());
-       Assert.assertEquals("Nom. title must be equal", nomTitle, team.getNomenclaturalTitle());
-       Assert.assertTrue("Members must be empty", team.getTeamMembers().isEmpty());
-       Assert.assertEquals("Annotations should be moved", 1, team.getAnnotations().size());
-               Assert.assertNotNull("Contact must be copied too", team.getContact());
-       Assert.assertEquals("Team must be combination author now", team, name.getCombinationAuthorship());
+           try {
+               UpdateResult result = service.convertPerson2Team(person);
+               team = (Team) result.getCdmEntity();
+           } catch (MergeException e) {
+               Assert.fail("No Merge exception should be thrown");
+           }
+           Assert.assertNotNull(team);
+           Assert.assertEquals("Title cache must be equal", fullAuthor, team.getTitleCache());
+           Assert.assertEquals("Nom. title must be equal", nomTitle, team.getNomenclaturalTitle());
+           Assert.assertTrue("Members must be empty", team.getTeamMembers().isEmpty());
+           Assert.assertEquals("Annotations should be moved", 1, team.getAnnotations().size());
+           Assert.assertNotNull("Contact must be copied too", team.getContact());
+           Assert.assertEquals("Team must be combination author now", team, name.getCombinationAuthorship());
+
+           //test un-protected titleCache
+           Person person2 = person.clone();
+           person2.setProtectedTitleCache(false);
+           Assert.assertEquals("Title cache must be equal", nomTitle, person2.getTitleCache());
+           service.save(person2);
+
+           try{
+               UpdateResult result = service.convertPerson2Team(person2);
+               team = (Team) result.getCdmEntity();
+        } catch (MergeException e) {
+            Assert.fail("No Merge exception should be thrown, but was: " + e.getMessage());
+        }
+           Assert.assertEquals("Title cache must be equal", person2.getTitleCache(), team.getTitleCache());
+        Assert.assertEquals("Nom. title must be equal", nomTitle, team.getNomenclaturalTitle());
+
+        //test fully empty
+        person2 = person2.clone();
+        person2.setNomenclaturalTitle(null);  //now it is fully empty
+        Assert.assertEquals("Title cache must be equal", "Person#0<"+person2.getUuid()+">", person2.getTitleCache());  //expected value may change when toString() implementation changes for Person class
+
+        service.save(person2);
+        try{
+            UpdateResult result = service.convertPerson2Team(person2);
+            team = (Team) result.getCdmEntity();
+        } catch (MergeException e) {
+            Assert.fail("No Merge exception should be thrown, but was: " + e.getMessage());
+        }
+        Assert.assertEquals("If person was completely empty we don't expect the title cache to be taken from person.nomenclaturalTitle", TeamDefaultCacheStrategy.EMPTY_TEAM, team.getTitleCache());
+        Assert.assertFalse("If person was completely empty we don't expect the title cache to be protected", team.isProtectedTitleCache());
+        Assert.assertEquals("Nom. title must be equal", person2.getNomenclaturalTitle(), team.getNomenclaturalTitle());
     }
 
     private Contact getContact(){