Skip to content
Snippets Groups Projects
Commit d2eabe2d authored by Tom Gross's avatar Tom Gross
Browse files

Factor out description shortener to helper method

parent edddba9a
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
......@@ -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();
......
......@@ -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();
......
......@@ -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();
......
......@@ -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. ");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment