2 * Sone - KnownSonesPage.java - Copyright © 2010–2012 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.sone.web.page.FreenetRequest;
26 import net.pterodactylus.util.collection.Pagination;
27 import net.pterodactylus.util.collection.ReverseComparator;
28 import net.pterodactylus.util.collection.filter.Filter;
29 import net.pterodactylus.util.collection.filter.Filters;
30 import net.pterodactylus.util.number.Numbers;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
35 * This page shows all known Sones.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class KnownSonesPage extends SoneTemplatePage {
42 * Creates a “known Sones” page.
45 * The template to render
47 * The Sone web interface
49 public KnownSonesPage(Template template, WebInterface webInterface) {
50 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
54 // TEMPLATEPAGE METHODS
61 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
62 super.processTemplate(request, templateContext);
63 String sortField = request.getHttpRequest().getParam("sort");
64 String sortOrder = request.getHttpRequest().getParam("order");
65 String filter = request.getHttpRequest().getParam("filter");
66 templateContext.set("sort", (sortField != null) ? sortField : "name");
67 templateContext.set("order", (sortOrder != null) ? sortOrder : "asc");
68 templateContext.set("filter", filter);
69 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
70 List<Sone> knownSones = Filters.filteredList(new ArrayList<Sone>(webInterface.getCore().getSones()), Sone.EMPTY_SONE_FILTER);
71 if ((currentSone != null) && "followed".equals(filter)) {
72 knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
75 public boolean filterObject(Sone sone) {
76 return currentSone.hasFriend(sone.getId());
79 } else if ((currentSone != null) && "not-followed".equals(filter)) {
80 knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
83 public boolean filterObject(Sone sone) {
84 return !currentSone.hasFriend(sone.getId());
87 } else if ("new".equals(filter)) {
88 knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
93 public boolean filterObject(Sone sone) {
94 return !sone.isKnown();
97 } else if ("not-new".equals(filter)) {
98 knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
103 public boolean filterObject(Sone sone) {
104 return sone.isKnown();
108 if ("activity".equals(sortField)) {
109 if ("asc".equals(sortOrder)) {
110 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.LAST_ACTIVITY_COMPARATOR));
112 Collections.sort(knownSones, Sone.LAST_ACTIVITY_COMPARATOR);
114 } else if ("posts".equals(sortField)) {
115 if ("asc".equals(sortOrder)) {
116 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.POST_COUNT_COMPARATOR));
118 Collections.sort(knownSones, Sone.POST_COUNT_COMPARATOR);
120 } else if ("images".equals(sortField)) {
121 if ("asc".equals(sortOrder)) {
122 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.IMAGE_COUNT_COMPARATOR));
124 Collections.sort(knownSones, Sone.IMAGE_COUNT_COMPARATOR);
127 if ("desc".equals(sortOrder)) {
128 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.NICE_NAME_COMPARATOR));
130 Collections.sort(knownSones, Sone.NICE_NAME_COMPARATOR);
133 Pagination<Sone> sonePagination = new Pagination<Sone>(knownSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
134 templateContext.set("pagination", sonePagination);
135 templateContext.set("knownSones", sonePagination.getItems());