Add sort filter to template context factory.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 19 Apr 2012 11:58:55 +0000 (13:58 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 19 Apr 2012 11:58:55 +0000 (13:58 +0200)
src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java

index 917fad7..9cfd7f5 100644 (file)
@@ -26,6 +26,7 @@ import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.InvocationTargetException;
 import java.net.URISyntaxException;
+import java.util.Comparator;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
@@ -36,9 +37,11 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import net.pterodactylus.demoscenemusic.data.Artist;
 import net.pterodactylus.demoscenemusic.page.ServletRequest;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.io.StreamCopier;
+import net.pterodactylus.util.template.CollectionSortFilter;
 import net.pterodactylus.util.template.HtmlFilter;
 import net.pterodactylus.util.template.ReflectionAccessor;
 import net.pterodactylus.util.template.Template;
@@ -75,6 +78,16 @@ public class TemplateServlet extends HttpServlet {
                templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
 
                templateContextFactory.addFilter("html", new HtmlFilter());
+               CollectionSortFilter sortFilter = new CollectionSortFilter();
+               sortFilter.addComparator(Artist.class, new Comparator<Artist>() {
+
+                       @Override
+                       public int compare(Artist leftArtist, Artist rightArtist) {
+                               return leftArtist.name().compareToIgnoreCase(rightArtist.name());
+                       }
+
+               });
+               templateContextFactory.addFilter("sort", sortFilter);
 
                templateContextFactory.addTemplateObject("core", core);
                templateContextFactory.addTemplateObject("dataManager", core.getDataManager());