From d2eabe2d9e42e8b988e7b63c39e4797455fa1ef8 Mon Sep 17 00:00:00 2001 From: Tom Gross <itconsense@gmail.com> Date: Tue, 5 Nov 2019 06:56:28 +0100 Subject: [PATCH] Factor out description shortener to helper method --- .../java/org/olat/core/util/StringHelper.java | 18 +++++++++++++++++- .../ui/author/AuthoringEntryRow.java | 13 ++----------- .../ui/catalog/CatalogEntryRow.java | 14 ++------------ .../ui/list/RepositoryEntryRow.java | 12 +----------- .../org/olat/core/util/StringHelperTest.java | 19 +++++++++++++++++++ 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/olat/core/util/StringHelper.java b/src/main/java/org/olat/core/util/StringHelper.java index aa718043ee2..741cb9dabd5 100644 --- a/src/main/java/org/olat/core/util/StringHelper.java +++ b/src/main/java/org/olat/core/util/StringHelper.java @@ -51,6 +51,7 @@ import org.olat.core.logging.AssertException; import org.apache.logging.log4j.Logger; import org.olat.core.logging.Tracing; import org.olat.core.util.filter.impl.HtmlScanner; +import org.olat.core.util.filter.FilterFactory; import org.olat.core.util.filter.impl.OWASPAntiSamyXSSFilter; import org.olat.user.UserManager; @@ -625,4 +626,19 @@ public class StringHelper { return formatAsCSVString(rv); } -} \ No newline at end of file + + public static String truncateText(String text) { + String shortenedText = ""; + if (text != null) { + String shortText = FilterFactory.getHtmlTagsFilter().filter(text); + if(shortText.length() > 255) { + shortenedText = shortText.substring(0, 255); + } else { + shortenedText = shortText; + } + } else { + shortenedText = ""; + } + return shortenedText; + } +} diff --git a/src/main/java/org/olat/repository/ui/author/AuthoringEntryRow.java b/src/main/java/org/olat/repository/ui/author/AuthoringEntryRow.java index b950968ac01..a4e902be521 100644 --- a/src/main/java/org/olat/repository/ui/author/AuthoringEntryRow.java +++ b/src/main/java/org/olat/repository/ui/author/AuthoringEntryRow.java @@ -25,7 +25,7 @@ import java.util.List; import org.olat.core.commons.services.license.License; import org.olat.core.gui.components.form.flexible.elements.FormLink; import org.olat.core.id.OLATResourceable; -import org.olat.core.util.filter.FilterFactory; +import org.olat.core.util.StringHelper; import org.olat.core.util.resource.OresHelper; import org.olat.repository.RepositoryEntryAuthorView; import org.olat.repository.RepositoryEntryLight; @@ -94,16 +94,7 @@ public class AuthoringEntryRow implements RepositoryEntryRef, RepositoryEntryLig author = fullnameAuthor; authors = view.getAuthors(); location = view.getLocation(); - if(view.getDescription() != null) { - String shortDesc = FilterFactory.getHtmlTagsFilter().filter(view.getDescription()); - if(shortDesc.length() > 255) { - shortenedDescription = shortDesc.substring(0, 255); - } else { - shortenedDescription = shortDesc; - } - } else { - shortenedDescription = ""; - } + shortenedDescription = StringHelper.truncateText(view.getDescription()); lastUsage = view.getLastUsage(); creationDate = view.getCreationDate(); diff --git a/src/main/java/org/olat/repository/ui/catalog/CatalogEntryRow.java b/src/main/java/org/olat/repository/ui/catalog/CatalogEntryRow.java index 9adcebc4db2..6561b24910e 100644 --- a/src/main/java/org/olat/repository/ui/catalog/CatalogEntryRow.java +++ b/src/main/java/org/olat/repository/ui/catalog/CatalogEntryRow.java @@ -23,7 +23,7 @@ import java.util.Date; import java.util.List; import org.olat.core.id.OLATResourceable; -import org.olat.core.util.filter.FilterFactory; +import org.olat.core.util.StringHelper; import org.olat.core.util.resource.OresHelper; import org.olat.repository.RepositoryEntry; import org.olat.repository.RepositoryEntryLight; @@ -72,17 +72,7 @@ public class CatalogEntryRow implements RepositoryEntryRef, RepositoryEntryLight key = view.getKey(); name = view.getDisplayname(); authors = view.getAuthors(); - if(view.getDescription() != null) { - String shortDesc = FilterFactory.getHtmlTagsFilter().filter(view.getDescription()); - if(shortDesc.length() > 255) { - shortenedDescription = shortDesc.substring(0, 255); - } else { - shortenedDescription = shortDesc; - } - } else { - shortenedDescription = ""; - } - + shortenedDescription = StringHelper.truncateText(view.getDescription()); creationDate = view.getCreationDate(); externalId = view.getExternalId(); diff --git a/src/main/java/org/olat/repository/ui/list/RepositoryEntryRow.java b/src/main/java/org/olat/repository/ui/list/RepositoryEntryRow.java index 503405a8ae2..4ff27173e75 100644 --- a/src/main/java/org/olat/repository/ui/list/RepositoryEntryRow.java +++ b/src/main/java/org/olat/repository/ui/list/RepositoryEntryRow.java @@ -26,7 +26,6 @@ import org.olat.core.gui.components.form.flexible.FormItem; import org.olat.core.gui.components.form.flexible.elements.FormLink; import org.olat.core.id.OLATResourceable; import org.olat.core.util.StringHelper; -import org.olat.core.util.filter.FilterFactory; import org.olat.core.util.resource.OresHelper; import org.olat.course.assessment.AssessmentHelper; import org.olat.repository.RepositoryEntryMyView; @@ -93,16 +92,7 @@ public class RepositoryEntryRow implements RepositoryEntryRef { externalId = entry.getExternalId(); externalRef = entry.getExternalRef(); name = entry.getDisplayname(); - if(entry.getDescription() != null) { - String shortDesc = FilterFactory.getHtmlTagsFilter().filter(entry.getDescription()); - if(shortDesc.length() > 255) { - shortenedDescription = shortDesc.substring(0, 255); - } else { - shortenedDescription = shortDesc; - } - } else { - shortenedDescription = ""; - } + shortenedDescription = StringHelper.truncateText(entry.getDescription()); setOLATResourceable(OresHelper.clone(entry.getOlatResource())); authors = entry.getAuthors(); location = entry.getLocation(); diff --git a/src/test/java/org/olat/core/util/StringHelperTest.java b/src/test/java/org/olat/core/util/StringHelperTest.java index 4322a8ea12e..618d6ec1e21 100644 --- a/src/test/java/org/olat/core/util/StringHelperTest.java +++ b/src/test/java/org/olat/core/util/StringHelperTest.java @@ -204,4 +204,23 @@ public class StringHelperTest { String csv = StringHelper.formatAsEscapedCSVString(entries); Assert.assertEquals("Hell\"\"o\"\",\"Test,dru,\",Final", csv); } + + @Test + public void truncateText() { + Assert.assertEquals(StringHelper.truncateText("foobar"), "foobar"); + } + + @Test + public void truncateText_null() { + Assert.assertEquals(StringHelper.truncateText(null), ""); + } + + @Test + public void truncateText_long() { + Assert.assertEquals( + StringHelper.truncateText( + "Destination event Execute Pending What I Ideate Conceptualize discovered was Brand Goals the Fyre manner Understand Festival. A a unique music, art revenue in and food capture brand festival taking allows to place on 360 methodology the private Festival. Our island of for Fyre Fyre Cay."), + "Destination event Execute Pending What I Ideate Conceptualize discovered was Brand Goals the Fyre manner Understand Festival. A a unique music, art revenue in and food capture brand festival taking allows to place on 360 methodology the private Festival. "); + } + } -- GitLab