Sort Sones ignoring case.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / LoginPage.java
index df06c87..c859cf6 100644 (file)
 
 package net.pterodactylus.sone.web;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.template.SoneAccessor;
+import net.pterodactylus.sone.web.page.Page.Request.Method;
 import net.pterodactylus.util.template.Template;
+import freenet.clients.http.ToadletContext;
 
 /**
  * The login page manages logging the user in.
@@ -38,4 +47,52 @@ public class LoginPage extends SoneTemplatePage {
                super("login.html", template, "Page.Login.Title", webInterface);
        }
 
+       //
+       // TEMPLATEPAGE METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected void processTemplate(Request request, Template template) throws RedirectException {
+               super.processTemplate(request, template);
+               List<Sone> localSones = new ArrayList<Sone>(webInterface.core().getSones());
+               Collections.sort(localSones, new Comparator<Sone>() {
+
+                       @Override
+                       public int compare(Sone leftSone, Sone rightSone) {
+                               int diff = SoneAccessor.getNiceName(leftSone).compareToIgnoreCase(SoneAccessor.getNiceName(rightSone));
+                               if (diff != 0) {
+                                       return diff;
+                               }
+                               return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
+                       }
+
+               });
+               template.set("sones", localSones);
+               if (request.getMethod() == Method.POST) {
+                       String soneId = request.getHttpRequest().getPartAsStringFailsafe("sone-id", 100);
+                       Sone selectedSone = null;
+                       for (Sone sone : webInterface.core().getSones()) {
+                               if (sone.getId().equals(soneId)) {
+                                       selectedSone = sone;
+                                       break;
+                               }
+                       }
+                       if (selectedSone != null) {
+                               setCurrentSone(request.getToadletContext(), selectedSone);
+                               throw new RedirectException("index.html");
+                       }
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean isEnabled(ToadletContext toadletContext) {
+               return getCurrentSone(toadletContext) == null;
+       }
+
 }