2 * Sone - KnownSonesPage.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.util.collection.Pagination;
26 import net.pterodactylus.util.collection.ReverseComparator;
27 import net.pterodactylus.util.filter.Filter;
28 import net.pterodactylus.util.filter.Filters;
29 import net.pterodactylus.util.number.Numbers;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateContext;
34 * This page shows all known Sones.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class KnownSonesPage extends SoneTemplatePage {
41 * Creates a “known Sones” page.
44 * The template to render
46 * The Sone web interface
48 public KnownSonesPage(Template template, WebInterface webInterface) {
49 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
53 // TEMPLATEPAGE METHODS
60 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
61 super.processTemplate(request, templateContext);
62 String sortField = request.getHttpRequest().getParam("sort");
63 String sortOrder = request.getHttpRequest().getParam("order");
64 String followedSones = request.getHttpRequest().getParam("followedSones");
65 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
66 List<Sone> knownSones = Filters.filteredList(new ArrayList<Sone>(webInterface.getCore().getSones()), Sone.EMPTY_SONE_FILTER);
67 if ((currentSone != null) && "show-only".equals(followedSones)) {
68 knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
71 public boolean filterObject(Sone sone) {
72 return currentSone.hasFriend(sone.getId());
75 templateContext.set("followedSones", "show-only");
76 } else if ((currentSone != null) && "hide".equals(followedSones)) {
77 knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
80 public boolean filterObject(Sone sone) {
81 return !currentSone.hasFriend(sone.getId());
84 templateContext.set("followedSones", "hide");
86 if ("name".equals(sortField)) {
87 if ("desc".equals(sortOrder)) {
88 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.NICE_NAME_COMPARATOR));
90 Collections.sort(knownSones, Sone.NICE_NAME_COMPARATOR);
92 templateContext.set("sort", "name");
93 templateContext.set("order", "desc".equals(sortOrder) ? "desc" : "asc");
94 } else if ("activity".equals(sortField)) {
95 if ("asc".equals(sortOrder)) {
96 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.LAST_ACTIVITY_COMPARATOR));
98 Collections.sort(knownSones, Sone.LAST_ACTIVITY_COMPARATOR);
100 templateContext.set("sort", "activity");
101 templateContext.set("order", "asc".equals(sortOrder) ? "asc" : "desc");
103 Pagination<Sone> sonePagination = new Pagination<Sone>(knownSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
104 templateContext.set("pagination", sonePagination);
105 templateContext.set("knownSones", sonePagination.getItems());