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.Collection;
22 import java.util.Collections;
23 import java.util.List;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.web.page.FreenetRequest;
27 import net.pterodactylus.util.collection.Pagination;
28 import net.pterodactylus.util.collection.ReverseComparator;
29 import net.pterodactylus.util.number.Numbers;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateContext;
33 import com.google.common.base.Predicate;
34 import com.google.common.base.Predicates;
35 import com.google.common.collect.Collections2;
38 * This page shows all known Sones.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class KnownSonesPage extends SoneTemplatePage {
45 * Creates a “known Sones” page.
48 * The template to render
50 * The Sone web interface
52 public KnownSonesPage(Template template, WebInterface webInterface) {
53 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
57 // TEMPLATEPAGE METHODS
64 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
65 super.processTemplate(request, templateContext);
66 String sortField = request.getHttpRequest().getParam("sort");
67 String sortOrder = request.getHttpRequest().getParam("order");
68 String filter = request.getHttpRequest().getParam("filter");
69 templateContext.set("sort", (sortField != null) ? sortField : "name");
70 templateContext.set("order", (sortOrder != null) ? sortOrder : "asc");
71 templateContext.set("filter", filter);
72 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
73 Collection<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
74 if ((currentSone != null) && "followed".equals(filter)) {
75 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
78 public boolean apply(Sone sone) {
79 return currentSone.hasFriend(sone.getId());
82 } else if ((currentSone != null) && "not-followed".equals(filter)) {
83 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
86 public boolean apply(Sone sone) {
87 return !currentSone.hasFriend(sone.getId());
90 } else if ("new".equals(filter)) {
91 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
97 public boolean apply(Sone sone) {
98 return !sone.isKnown();
101 } else if ("not-new".equals(filter)) {
102 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
108 public boolean apply(Sone sone) {
109 return sone.isKnown();
112 } else if ("own".equals(filter)) {
113 knownSones = Collections2.filter(knownSones, Sone.LOCAL_SONE_FILTER);
114 } else if ("not-own".equals(filter)) {
115 knownSones = Collections2.filter(knownSones, Predicates.not(Sone.LOCAL_SONE_FILTER));
117 List<Sone> sortedSones = new ArrayList<Sone>(knownSones);
118 if ("activity".equals(sortField)) {
119 if ("asc".equals(sortOrder)) {
120 Collections.sort(sortedSones, new ReverseComparator<Sone>(Sone.LAST_ACTIVITY_COMPARATOR));
122 Collections.sort(sortedSones, Sone.LAST_ACTIVITY_COMPARATOR);
124 } else if ("posts".equals(sortField)) {
125 if ("asc".equals(sortOrder)) {
126 Collections.sort(sortedSones, new ReverseComparator<Sone>(Sone.POST_COUNT_COMPARATOR));
128 Collections.sort(sortedSones, Sone.POST_COUNT_COMPARATOR);
130 } else if ("images".equals(sortField)) {
131 if ("asc".equals(sortOrder)) {
132 Collections.sort(sortedSones, new ReverseComparator<Sone>(Sone.IMAGE_COUNT_COMPARATOR));
134 Collections.sort(sortedSones, Sone.IMAGE_COUNT_COMPARATOR);
137 if ("desc".equals(sortOrder)) {
138 Collections.sort(sortedSones, new ReverseComparator<Sone>(Sone.NICE_NAME_COMPARATOR));
140 Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
143 Pagination<Sone> sonePagination = new Pagination<Sone>(sortedSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
144 templateContext.set("pagination", sonePagination);
145 templateContext.set("knownSones", sonePagination.getItems());