ref #9604 split to OriginalSourceFormatter
authorAndreas Müller <a.mueller@bgbm.org>
Thu, 13 May 2021 09:16:27 +0000 (11:16 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Thu, 13 May 2021 09:16:27 +0000 (11:16 +0200)
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/cdmLight/CdmLightClassificationExport.java
cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/reference/OriginalSourceFormatter.java [new file with mode: 0644]
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/cache/reference/IReferenceCacheStrategy.java
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/cache/reference/ReferenceDefaultCacheStrategy.java
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/cache/taxon/TaxonBaseDefaultCacheStrategy.java
cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/reference/OriginalSourceFormatterTest.java [new file with mode: 0644]
cdmlib-model/src/test/java/eu/etaxonomy/cdm/strategy/cache/reference/ReferenceDefaultCacheStrategyTest.java
cdmlib-model/src/test/java/eu/etaxonomy/cdm/strategy/cache/taxon/TaxonBaseDefaultCacheStrategyTest.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/name/TypeDesignationSetFormatter.java

index a94805752e960a1d9a43dc25a379e0240a6f2b02..02087ec1bea37cba8662c114411822e72ddb57d1 100755 (executable)
@@ -33,6 +33,7 @@ import eu.etaxonomy.cdm.compare.name.TypeComparator;
 import eu.etaxonomy.cdm.compare.taxon.HomotypicGroupTaxonComparator;
 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
 import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
+import eu.etaxonomy.cdm.format.reference.OriginalSourceFormatter;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.io.common.CdmExportBase;
 import eu.etaxonomy.cdm.io.common.ExportResult.ExportResultState;
@@ -100,7 +101,6 @@ import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
 import eu.etaxonomy.cdm.strategy.cache.HTMLTagRules;
 import eu.etaxonomy.cdm.strategy.cache.TagEnum;
 import eu.etaxonomy.cdm.strategy.cache.TaggedText;
-import eu.etaxonomy.cdm.strategy.cache.reference.ReferenceDefaultCacheStrategy;
 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
 
 /**
@@ -1270,8 +1270,7 @@ public class CdmLightClassificationExport
                     int index = 1;
                     for (IdentifiableSource source: typeDesignation.getSources()){
                         if (source.getCitation() != null){
-                            ReferenceDefaultCacheStrategy cacheStrategy = ((ReferenceDefaultCacheStrategy)source.getCitation().getCacheStrategy());
-                            stringbuilder.append(cacheStrategy.getCitation(source.getCitation(), source.getCitationMicroReference()));
+                            stringbuilder.append(OriginalSourceFormatter.INSTANCE.format(source));
                         }
                         if (index < typeDesignation.getSources().size()) {
                             stringbuilder.append( ", ");
@@ -1660,8 +1659,7 @@ public class CdmLightClassificationExport
                             statusString += ": " + nameStatus.getRuleConsidered();
                         }
                         if (nameStatus.getCitation() != null) {
-                            String shortCitation = ((ReferenceDefaultCacheStrategy) nameStatus.getCitation().getCacheStrategy())
-                                    .createShortCitation(nameStatus.getCitation(), null, false);
+                            String shortCitation = OriginalSourceFormatter.INSTANCE.format(nameStatus.getCitation(), null);
                             statusString += " (" + shortCitation + ")";
                         }
 //                        if (nameStatus.getCitationMicroReference() != null
@@ -1743,10 +1741,8 @@ public class CdmLightClassificationExport
 
                 if (taxonBases.size() == 1){
                      taxonBase = HibernateProxyHelper.deproxy(taxonBases.iterator().next());
-                     Reference secRef = taxonBase.getSec();
-                     if (secRef != null){
-                         sec = ((ReferenceDefaultCacheStrategy) secRef.getCacheStrategy())
-                             .createShortCitation(secRef, taxonBase.getSecMicroReference(), true);
+                     if (taxonBase.getSec() != null){
+                         sec = OriginalSourceFormatter.INSTANCE_WITH_BRACKETS.format(taxonBase.getSecSource());
                      }
                      if (taxonBase.isDoubtful()){
                          doubtful = "?";
@@ -1771,10 +1767,8 @@ public class CdmLightClassificationExport
                 }else{
                     //there are names used more than once?
                     for (TaxonBase<?> tb: taxonBases){
-                        Reference secRef = tb.getSec();
-                        if (secRef != null){
-                            sec = ((ReferenceDefaultCacheStrategy) secRef.getCacheStrategy())
-                                .createShortCitation(secRef, tb.getSecMicroReference(), true);
+                        if (tb.getSec() != null){
+                            sec = OriginalSourceFormatter.INSTANCE_WITH_BRACKETS.format(tb.getSecSource());
                         }
                         if (tb.isDoubtful()){
                             doubtful = "?";
@@ -2066,9 +2060,7 @@ public class CdmLightClassificationExport
             String[] csvLine = new String[table.getSize()];
             csvLine[table.getIndex(CdmLightExportTable.REFERENCE_ID)] = getId(state, reference);
             // TODO short citations correctly
-            String shortCitation = ((ReferenceDefaultCacheStrategy) reference.getCacheStrategy())
-                    .createShortCitation(reference, null, true); // Should be Author(year)
-                                                     // like in Taxon.sec
+            String shortCitation = OriginalSourceFormatter.INSTANCE_WITH_BRACKETS.format(reference, null); // Should be Author(year) like in Taxon.sec
             csvLine[table.getIndex(CdmLightExportTable.BIBLIO_SHORT_CITATION)] = shortCitation;
             // TODO get preferred title
             csvLine[table.getIndex(CdmLightExportTable.REF_TITLE)] = reference.isProtectedTitleCache()
diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/reference/OriginalSourceFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/reference/OriginalSourceFormatter.java
new file mode 100644 (file)
index 0000000..8c8acde
--- /dev/null
@@ -0,0 +1,162 @@
+/**
+* Copyright (C) 2021 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.format.reference;
+
+import java.util.List;
+
+import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.format.CdmFormatterBase;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
+import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy;
+
+/**
+ * @author a.mueller
+ * @since 13.05.2021
+ */
+public class OriginalSourceFormatter extends CdmFormatterBase<OriginalSourceBase>{
+
+    private final boolean withYearBrackets;
+
+    public static OriginalSourceFormatter INSTANCE = new OriginalSourceFormatter(false);
+    public static OriginalSourceFormatter INSTANCE_WITH_BRACKETS = new OriginalSourceFormatter(true);
+
+    /**
+      * @param withYearBrackets if <code>false</code> the result comes without brackets (default is <code>false</code>)
+      */
+    private OriginalSourceFormatter(boolean withYearBrackets) {
+        this.withYearBrackets = withYearBrackets;
+    }
+
+    @Override
+    public String format(OriginalSourceBase source) {
+        if (source == null){
+            return null;
+        }
+        Reference reference = source.getCitation();
+        String microReference = source.getCitationMicroReference();
+        if (reference == null && isBlank(microReference)){
+            return null;
+        }
+        return format(reference, microReference);
+    }
+
+    /**
+     * Creates a citation in form <i>author year: detail</i> or <i>author (year: detail)</i>.
+     * <BR>
+     * If reference has protected titlecache only the titlecache is returned (may change in future).
+     * <BR>
+     * The author team is abbreviated with <code>et al.</code> if more than 2 authors exist in the team.
+     *
+     * @param reference the reference to format
+     * @param citationDetail the microreference (page, figure, etc.), if <code>null</code> also the colon separator is not used
+     */
+    public String format(Reference reference, String citationDetail){
+        if (reference == null){
+            return null;
+        }
+
+        if(reference.isProtectedTitleCache()){
+            return handleCitationDetailInTitleCache(reference.getTitleCache(), citationDetail);
+        }
+        TeamOrPersonBase<?> authorship = reference.getAuthorship();
+        String authorStr = "";
+        if (authorship == null) {
+            return handleCitationDetailInTitleCache(reference.getTitleCache(), citationDetail);
+        }
+        authorship = CdmBase.deproxy(authorship);
+        if (authorship instanceof Person){
+            authorStr = getPersonString((Person)authorship);
+        }
+        else if (authorship instanceof Team){
+
+            Team team = CdmBase.deproxy(authorship, Team.class);
+            if (team.isProtectedTitleCache()){
+                authorStr = team.getTitleCache();
+            }else{
+                List<Person> teamMembers = team.getTeamMembers();
+                int etAlPosition = 2;
+                for (int i = 1; i <= teamMembers.size() &&
+                        (i < etAlPosition || teamMembers.size() == etAlPosition && !team.isHasMoreMembers()) ; i++){
+                    Person teamMember = teamMembers.get(i-1);
+                    if(teamMember == null){
+                        // this can happen in UIs in the process of adding new members
+                        continue;
+                    }
+                    String concat = TeamDefaultCacheStrategy.concatString(team, teamMembers, i);
+                    authorStr += concat + getPersonString(teamMember);
+                }
+                if (teamMembers.size() == 0){
+                    authorStr = TeamDefaultCacheStrategy.EMPTY_TEAM;
+                } else if (team.isHasMoreMembers() || teamMembers.size() > etAlPosition){
+                    authorStr += TeamDefaultCacheStrategy.ET_AL_TEAM_CONCATINATION_FULL + "al.";
+                }
+            }
+        }
+        String result = CdmUtils.concat(" ", authorStr, getShortCitationDate(reference, withYearBrackets, citationDetail));
+
+        return result;
+    }
+
+    private String getShortCitationDate(Reference reference, boolean withBrackets, String citationDetail) {
+        String result = null;
+        if (reference.getDatePublished() != null && !reference.getDatePublished().isEmpty()) {
+            if (isNotBlank(reference.getDatePublished().getFreeText())){
+                result = reference.getDatePublished().getFreeText();
+            }else if (isNotBlank(reference.getYear()) ){
+                result = reference.getYear();
+            }
+            if (isNotBlank(citationDetail)){
+                result = Nz(result) + ": " + citationDetail;
+            }
+            if (isNotBlank(result) && withBrackets){
+                result = "(" + result + ")";
+            }
+        }else if (reference.getInReference() != null){
+            result = getShortCitationDate(reference.getInReference(), withBrackets, citationDetail);
+        }
+        return result;
+    }
+
+    private String getPersonString(Person person) {
+        String shortCitation;
+        shortCitation = person.getFamilyName();
+        if (isBlank(shortCitation) ){
+            shortCitation = person.getTitleCache();
+        }
+        return shortCitation;
+    }
+
+    /**
+     * Adds the citationDetail to the protected titleCache assuming that this is not accurately parsed.
+     * If the detail is contained in the titleCache it is not added again to the result, otherwise
+     * it is concatenated with separator ":"
+     * @return the concatenated string
+     */
+    private String handleCitationDetailInTitleCache(String titleCache, String citationDetail) {
+        if (isBlank(citationDetail)){
+            return titleCache;
+        }else if (isBlank(titleCache)){
+            return ": " + citationDetail;
+        }else if (citationDetail.length() <= 3){
+            if (titleCache.contains(": " + citationDetail)){
+                return titleCache;
+            }
+        }else{
+            if (titleCache.contains(citationDetail)){
+                return titleCache;
+            }
+        }
+        return titleCache + ": " + citationDetail;
+    }
+}
\ No newline at end of file
index 6bce011fe40026f8dbcace02eafbd3e0a8f5b57f..bafdf1483c3c14512cf52381c058ef256d178ed2 100644 (file)
@@ -9,6 +9,7 @@
 
 package eu.etaxonomy.cdm.strategy.cache.reference;
 
+import eu.etaxonomy.cdm.format.reference.NomenclaturalSourceFormatter;
 import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
 
@@ -19,40 +20,14 @@ import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
  */
 public interface IReferenceCacheStrategy extends IIdentifiableEntityCacheStrategy<Reference> {
 
-       /**
-        * Returns a short version of the reference, suitable for citation (e.g. ${authorname}, ${year})
-        * @deprecated consider using {@link #createShortCitation(Reference, String, Boolean)}. These methods
-        * will probably be merged in future
-        */
-    @Deprecated
-       public String getCitation(Reference reference, String microReference);
-
        /**
         * Returns the full abbreviated title string which
         * is a string representation of Reference which
         * preferably uses the {@link Reference#getAbbrevTitle() abbrev title}
         * of the reference instead of the title. This is not the same as
-        * the {@link INomenclaturalReferenceCacheStrategy#nomenclaturalTitleCache
-        * @return
+        * the {@link NomenclaturalSourceFormatter#format(Reference, String)
+        * @return the full abbreviated title cache
         */
        public String getFullAbbrevTitleString(Reference reference);
 
-
-    //TODO this method seems to be used only for type designations and/or cdmlight, it should be unified with getCitation()
-    /**
-     * Creates a citation in form <i>author year: detail</i> or <i>author (year: detail)</i>.
-     * <BR>
-     * If reference has protected titlecache only the titlecache is returned (may change in future).
-     * <BR>
-     * The author team is abbreviated with <code>et al.</code> if more than 2 authors exist in the team.
-     *
-     * @param reference the reference to format
-     * @param citationDetail the microreference (page, figure, etc.), if <code>null</code> also the colon separator is not used
-     * @param withYearBrackets if <code>false</code> the result comes without brackets (default is <code>false</code>)
-     * @return
-     */
-    public String createShortCitation(Reference reference, String citationDetail, Boolean withYearBrackets);
-
-
-
 }
index 9b4f1a3d5f26558011a5b62dd20c9306db3e123a..c49b3699c666becc09c6967076476fd7f7816d02 100644 (file)
@@ -8,7 +8,6 @@
 */\r
 package eu.etaxonomy.cdm.strategy.cache.reference;\r
 \r
-import java.util.List;\r
 import java.util.UUID;\r
 \r
 import org.apache.commons.lang3.StringUtils;\r
@@ -19,15 +18,11 @@ import org.joda.time.format.DateTimeFormatter;
 \r
 import eu.etaxonomy.cdm.common.CdmUtils;\r
 import eu.etaxonomy.cdm.common.UTF8;\r
-import eu.etaxonomy.cdm.model.agent.Person;\r
-import eu.etaxonomy.cdm.model.agent.Team;\r
 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;\r
-import eu.etaxonomy.cdm.model.common.CdmBase;\r
 import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;\r
 import eu.etaxonomy.cdm.model.reference.Reference;\r
 import eu.etaxonomy.cdm.model.reference.ReferenceType;\r
 import eu.etaxonomy.cdm.strategy.StrategyBase;\r
-import eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy;\r
 \r
 /**\r
  * #5833\r
@@ -217,141 +212,6 @@ public class ReferenceDefaultCacheStrategy
         return result;\r
     }\r
 \r
-    //TODO see comment on createShortCitation(...)\r
-    @Override\r
-    public String getCitation(Reference reference, String microReference) {\r
-        // mostly copied from nomRefCacheStrat, refCache, journalCache\r
-\r
-        if (reference == null){\r
-            return null;\r
-        }\r
-        StringBuilder result = new StringBuilder();\r
-        TeamOrPersonBase<?> team = reference.getAuthorship();\r
-\r
-        String nextConcat = "";\r
-\r
-        if (team != null &&  isNotBlank(team.getTitleCache())){\r
-            result.append(team.getTitleCache() );\r
-            //here is the difference between nomRef and others\r
-            if (isNomRef(reference.getType())) {\r
-                nextConcat = afterAuthor;\r
-            }else{\r
-                //FIXME check if this really makes sense\r
-                result.append(afterAuthor);\r
-                nextConcat = beforeYear;\r
-            }\r
-        }\r
-\r
-        String year = reference.getYear();\r
-        if (isNotBlank(year)){\r
-            result.append(nextConcat + year);\r
-        }\r
-        if (isNotBlank(microReference)){\r
-            result.append(": " + microReference);\r
-        }\r
-\r
-        return result.toString();\r
-    }\r
-\r
-    //TODO this method should probably be unified with getCitation(Reference reference, String microReference)\r
-    @Override\r
-    public String createShortCitation(Reference reference, String citationDetail, Boolean withYearBrackets) {\r
-        if (withYearBrackets == null){\r
-            withYearBrackets = false;\r
-        }\r
-        if(reference.isProtectedTitleCache()){\r
-            return handleCitationDetailInTitleCache(reference.getTitleCache(), citationDetail);\r
-        }\r
-        TeamOrPersonBase<?> authorship = reference.getAuthorship();\r
-        String shortCitation = "";\r
-        if (authorship == null) {\r
-            return handleCitationDetailInTitleCache(reference.getTitleCache(), citationDetail);\r
-        }\r
-        authorship = CdmBase.deproxy(authorship);\r
-        if (authorship instanceof Person){\r
-            shortCitation = getPersonString((Person)authorship);\r
-        }\r
-        else if (authorship instanceof Team){\r
-\r
-            Team team = CdmBase.deproxy(authorship, Team.class);\r
-            if (team.isProtectedTitleCache()){\r
-                shortCitation = team.getTitleCache();\r
-            }else{\r
-                List<Person> teamMembers = team.getTeamMembers();\r
-                int etAlPosition = 2;\r
-                for (int i = 1; i <= teamMembers.size() &&\r
-                        (i < etAlPosition || teamMembers.size() == etAlPosition && !team.isHasMoreMembers()) ; i++){\r
-                    Person teamMember = teamMembers.get(i-1);\r
-                    if(teamMember == null){\r
-                        // this can happen in UIs in the process of adding new members\r
-                        continue;\r
-                    }\r
-                    String concat = TeamDefaultCacheStrategy.concatString(team, teamMembers, i);\r
-                    shortCitation += concat + getPersonString(teamMember);\r
-                }\r
-                if (teamMembers.size() == 0){\r
-                    shortCitation = TeamDefaultCacheStrategy.EMPTY_TEAM;\r
-                } else if (team.isHasMoreMembers() || teamMembers.size() > etAlPosition){\r
-                    shortCitation += TeamDefaultCacheStrategy.ET_AL_TEAM_CONCATINATION_FULL + "al.";\r
-                }\r
-            }\r
-        }\r
-        shortCitation = CdmUtils.concat(" ", shortCitation, getShortCitationDate(reference, withYearBrackets, citationDetail));\r
-\r
-        return shortCitation;\r
-    }\r
-\r
-    /**\r
-     * Adds the citationDetail to the titleCache string that is returned from a method as data is not\r
-     * accurately parsed.\r
-     * @return\r
-     */\r
-    private String handleCitationDetailInTitleCache(String titleCache, String citationDetail) {\r
-        if (StringUtils.isBlank(citationDetail)){\r
-            return titleCache;\r
-        }else if (StringUtils.isBlank(titleCache)){\r
-            return ": " + citationDetail;\r
-        }else if (citationDetail.length() <= 3){\r
-            if (titleCache.contains(": " + citationDetail)){\r
-                return titleCache;\r
-            }\r
-        }else{\r
-            if (titleCache.contains(citationDetail)){\r
-                return titleCache;\r
-            }\r
-        }\r
-        return titleCache + ": " + citationDetail;\r
-    }\r
-\r
-    private String getShortCitationDate(Reference reference, boolean withBrackets, String citationDetail) {\r
-        String result = null;\r
-        if (reference.getDatePublished() != null && !reference.getDatePublished().isEmpty()) {\r
-            if (isNotBlank(reference.getDatePublished().getFreeText())){\r
-                result = reference.getDatePublished().getFreeText();\r
-            }else if (isNotBlank(reference.getYear()) ){\r
-                result = reference.getYear();\r
-            }\r
-            if (StringUtils.isNotEmpty(citationDetail)){\r
-                result = CdmUtils.Nz(result) + ": " + citationDetail;\r
-            }\r
-            if (StringUtils.isNotBlank(result) && withBrackets){\r
-                result = "(" + result + ")";\r
-            }\r
-        }else if (reference.getInReference() != null){\r
-            result = getShortCitationDate(reference.getInReference(), withBrackets, citationDetail);\r
-        }\r
-        return result;\r
-    }\r
-\r
-    private String getPersonString(Person person) {\r
-        String shortCitation;\r
-        shortCitation = person.getFamilyName();\r
-        if (isBlank(shortCitation) ){\r
-            shortCitation = person.getTitleCache();\r
-        }\r
-        return shortCitation;\r
-    }\r
-\r
 // ************************ TITLE CACHE SUBS ********************************************/\r
 \r
     private String titleCacheRealInRef(Reference reference, boolean isAbbrev) {\r
index 0a940a5c53039b1a9eb882a9fa3e6a9062ed2e65..2a41b338d6bed20fb1b42aa45313e0397b07d950 100644 (file)
@@ -13,6 +13,7 @@ import java.util.List;
 import java.util.UUID;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.format.reference.OriginalSourceFormatter;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.name.TaxonName;
@@ -134,7 +135,7 @@ public class TaxonBaseDefaultCacheStrategy<T extends TaxonBase>
                     sec.getAuthorship() != null &&
                     isNotBlank(sec.getAuthorship().getTitleCache()) &&
                     isNotBlank(sec.getYear())){
-                secRef = sec.getCacheStrategy().getCitation(sec, null);  //microRef is handled later
+                secRef = OriginalSourceFormatter.INSTANCE.format(sec, null);  //microRef is handled later
             }else if ((sec.isWebPage() || sec.isDatabase() || sec.isMap())
                     && titleExists(sec)){  //maybe we should also test protected caches (but which one, the abbrev cache or the titleCache?
                 secRef = isNotBlank(sec.getAbbrevTitle())? sec.getAbbrevTitle() : sec.getTitle();
diff --git a/cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/reference/OriginalSourceFormatterTest.java b/cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/reference/OriginalSourceFormatterTest.java
new file mode 100644 (file)
index 0000000..ac81708
--- /dev/null
@@ -0,0 +1,83 @@
+/**
+* Copyright (C) 2021 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.format.reference;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
+import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
+
+/**
+ * @author a.mueller
+ * @since 03.05.2021
+ */
+public class OriginalSourceFormatterTest {
+
+    //book // book section
+    private static Reference book1;
+    private static Team bookTeam1;
+
+    private static OriginalSourceFormatter formatter = OriginalSourceFormatter.INSTANCE;
+    private static OriginalSourceFormatter formatterWithBrackets = OriginalSourceFormatter.INSTANCE_WITH_BRACKETS;
+
+    @Before
+    public void setUp() throws Exception {
+
+        //book / section
+        book1 = ReferenceFactory.newBook();
+        bookTeam1 = Team.NewTitledInstance("Book Author", "TT.");
+    }
+
+    @Test
+    public void testCreateShortCitation(){
+        book1.setTitle("My book");
+        book1.setAuthorship(bookTeam1);
+        book1.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1975));
+        Assert.assertEquals("Unexpected title cache.", "Book Author, My book. 1975", book1.getTitleCache());
+
+        book1.setTitleCache(null, false);
+        book1.setEdition("ed. 3");
+
+        Assert.assertEquals("Unexpected title cache.", "Book Author 1975", formatter.format(book1, null));
+        Assert.assertEquals("Unexpected title cache.", "Book Author 1975", formatter.format(book1, ""));
+        Assert.assertEquals("Unexpected title cache.", "Book Author 1975: 55", formatter.format(book1, "55"));
+        Assert.assertEquals("Unexpected title cache.", "Book Author (1975: 55)",
+                formatterWithBrackets.format(book1, "55"));
+
+        //1 person
+        Person person1 = Person.NewInstance("Pers.", "Person", "P.", "Percy");
+        Team team = Team.NewInstance(person1);
+        book1.setAuthorship(team);
+        Assert.assertEquals("Unexpected title cache.", "Person 1975: 55", formatter.format(book1, "55"));
+        team.setHasMoreMembers(true);
+        Assert.assertEquals("Unexpected title cache.", "Person & al. 1975: 55", formatter.format(book1, "55"));
+        team.setHasMoreMembers(false);
+
+        //2 persons
+        Person person2 = Person.NewInstance("Lers.", "Lerson", "L.", "Lercy");
+        team.addTeamMember(person2);
+        Assert.assertEquals("Unexpected title cache.", "Person & Lerson 1975: 55", formatter.format(book1, "55"));
+        team.setHasMoreMembers(true);
+        Assert.assertEquals("Unexpected title cache.", "Person & al. 1975: 55", formatter.format(book1, "55"));
+        team.setHasMoreMembers(false);
+
+        //3 persons
+        Person person3 = Person.NewInstance("Gers.", "Gerson", "G.", "Gercy");
+        team.addTeamMember(person3);
+        Assert.assertEquals("Unexpected title cache.", "Person & al. 1975: 55", formatter.format(book1, "55"));
+        team.setHasMoreMembers(true);
+        Assert.assertEquals("Unexpected title cache.", "Person & al. 1975: 55", formatter.format(book1, "55"));
+        team.setHasMoreMembers(false);  //in case we want to continue test
+    }
+}
\ No newline at end of file
index a7b8b048c3b53d7a0cb496c79d99fa69cb176b9f..b5f6b96decf82ed32bc2947100ebb98e49327141 100644 (file)
@@ -475,44 +475,4 @@ public class ReferenceDefaultCacheStrategyTest {
 //      Assert.assertEquals("Unexpected title cache.", "auct.", webPage1.getTitleCache());
 //  }
 
-    @Test
-    public void testCreateShortCitation(){
-        book1.setTitle("My book");
-        book1.setAuthorship(bookTeam1);
-        book1.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1975));
-        Assert.assertEquals("Unexpected title cache.", "Book Author, My book. 1975", book1.getTitleCache());
-
-        book1.setTitleCache(null, false);
-        book1.setEdition("ed. 3");
-
-        Assert.assertEquals("Unexpected title cache.", "Book Author 1975", book1.getCacheStrategy().createShortCitation((Reference)book1, null, false));
-        Assert.assertEquals("Unexpected title cache.", "Book Author 1975", book1.getCacheStrategy().createShortCitation((Reference)book1, "", false));
-        Assert.assertEquals("Unexpected title cache.", "Book Author 1975: 55", book1.getCacheStrategy().createShortCitation((Reference)book1, "55", false));
-        Assert.assertEquals("Unexpected title cache.", "Book Author (1975: 55)", book1.getCacheStrategy().createShortCitation((Reference)book1, "55", true));
-
-        //1 person
-        Person person1 = Person.NewInstance("Pers.", "Person", "P.", "Percy");
-        Team team = Team.NewInstance(person1);
-        book1.setAuthorship(team);
-        Assert.assertEquals("Unexpected title cache.", "Person 1975: 55", book1.getCacheStrategy().createShortCitation((Reference)book1, "55", false));
-        team.setHasMoreMembers(true);
-        Assert.assertEquals("Unexpected title cache.", "Person & al. 1975: 55", book1.getCacheStrategy().createShortCitation((Reference)book1, "55", false));
-        team.setHasMoreMembers(false);
-
-        //2 persons
-        Person person2 = Person.NewInstance("Lers.", "Lerson", "L.", "Lercy");
-        team.addTeamMember(person2);
-        Assert.assertEquals("Unexpected title cache.", "Person & Lerson 1975: 55", book1.getCacheStrategy().createShortCitation((Reference)book1, "55", false));
-        team.setHasMoreMembers(true);
-        Assert.assertEquals("Unexpected title cache.", "Person & al. 1975: 55", book1.getCacheStrategy().createShortCitation((Reference)book1, "55", false));
-        team.setHasMoreMembers(false);
-
-        //3 persons
-        Person person3 = Person.NewInstance("Gers.", "Gerson", "G.", "Gercy");
-        team.addTeamMember(person3);
-        Assert.assertEquals("Unexpected title cache.", "Person & al. 1975: 55", book1.getCacheStrategy().createShortCitation((Reference)book1, "55", false));
-        team.setHasMoreMembers(true);
-        Assert.assertEquals("Unexpected title cache.", "Person & al. 1975: 55", book1.getCacheStrategy().createShortCitation((Reference)book1, "55", false));
-        team.setHasMoreMembers(false);  //in case we want to continue test
-    }
 }
\ No newline at end of file
index 3dadb407815c0466f519d2d9f67cb24feba57c36..dbedba8e16e280b199f730092497cce39426e973 100644 (file)
@@ -177,7 +177,7 @@ public class TaxonBaseDefaultCacheStrategyTest extends TermTestBase {
         sec.setAuthorship(Team.NewTitledInstance("Team", "T."));
         sec.setDatePublished(TimePeriodParser.parseStringVerbatim("1798"));
         taxonBase.setTitleCache(null);
-        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " sec. Team, 1798: p. 553",
+        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " sec. Team 1798: p. 553",
                 taxonBase.getTitleCache());
     }
 
index 27fd0e8a903c6b73393b03c73627426e9e687ec8..f3e7f7ee9546eeb957dbbabe55c9b3f01ac61a66 100644 (file)
@@ -18,6 +18,7 @@ import org.apache.commons.lang3.StringUtils;
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
 import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.common.UTF8;
+import eu.etaxonomy.cdm.format.reference.OriginalSourceFormatter;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
 import eu.etaxonomy.cdm.model.common.Language;
@@ -38,7 +39,6 @@ import eu.etaxonomy.cdm.strategy.cache.TagEnum;
 import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
 import eu.etaxonomy.cdm.strategy.cache.TaggedText;
 import eu.etaxonomy.cdm.strategy.cache.TaggedTextBuilder;
-import eu.etaxonomy.cdm.strategy.cache.reference.ReferenceDefaultCacheStrategy;
 
 /**
  * @author a.mueller
@@ -219,9 +219,8 @@ public class TypeDesignationSetFormatter {
             OriginalSourceBase source) {
         Reference ref = source.getCitation();
         if (ref != null){
-            ReferenceDefaultCacheStrategy strategy = ((ReferenceDefaultCacheStrategy)ref.getCacheStrategy());
-            String shortCitation = strategy.createShortCitation(ref, source.getCitationMicroReference(), false);
-            workingsetBuilder.add(TagEnum.reference, shortCitation, TypedEntityReference.fromEntity(ref, false));
+            String citation = OriginalSourceFormatter.INSTANCE.format(source);
+            workingsetBuilder.add(TagEnum.reference, citation, TypedEntityReference.fromEntity(ref, false));
         }
     }