2 * Sone - KnownSonesPage.java - Copyright © 2010–2013 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 static net.pterodactylus.sone.utils.NumberParsers.parseInt;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import net.pterodactylus.sone.data.LocalSone;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.web.page.FreenetRequest;
30 import net.pterodactylus.util.collection.Pagination;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
34 import com.google.common.base.Optional;
35 import com.google.common.base.Predicate;
36 import com.google.common.base.Predicates;
37 import com.google.common.collect.Collections2;
38 import com.google.common.collect.Ordering;
41 * This page shows all known Sones.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class KnownSonesPage extends SoneTemplatePage {
47 private static final String defaultSortField = "activity";
48 private static final String defaultSortOrder = "desc";
51 * Creates a “known Sones” page.
54 * The template to render
56 * The Sone web interface
58 public KnownSonesPage(Template template, WebInterface webInterface) {
59 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
63 // TEMPLATEPAGE METHODS
70 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
71 super.processTemplate(request, templateContext);
72 String sortField = request.getHttpRequest().getParam("sort", defaultSortField);
73 String sortOrder = request.getHttpRequest().getParam("order", defaultSortOrder);
74 String filter = request.getHttpRequest().getParam("filter");
75 templateContext.set("sort", sortField);
76 templateContext.set("order", sortOrder);
77 templateContext.set("filter", filter);
78 final Optional<LocalSone> currentSone = getCurrentSone(request.getToadletContext(), false);
79 Collection<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
80 if (currentSone.isPresent() && "followed".equals(filter)) {
81 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
84 public boolean apply(Sone sone) {
85 return currentSone.get().hasFriend(sone.getId());
88 } else if (currentSone.isPresent() && "not-followed".equals(filter)) {
89 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
92 public boolean apply(Sone sone) {
93 return !currentSone.get().hasFriend(sone.getId());
96 } else if ("new".equals(filter)) {
97 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
103 public boolean apply(Sone sone) {
104 return !sone.isKnown();
107 } else if ("not-new".equals(filter)) {
108 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
114 public boolean apply(Sone sone) {
115 return sone.isKnown();
118 } else if ("own".equals(filter)) {
119 knownSones = Collections2.filter(knownSones, Sone.LOCAL_SONE_FILTER);
120 } else if ("not-own".equals(filter)) {
121 knownSones = Collections2.filter(knownSones, Predicates.not(Sone.LOCAL_SONE_FILTER));
123 List<Sone> sortedSones = new ArrayList<Sone>(knownSones);
124 if ("activity".equals(sortField)) {
125 if ("asc".equals(sortOrder)) {
126 Collections.sort(sortedSones, Ordering.from(Sone.LAST_ACTIVITY_COMPARATOR).reverse());
128 Collections.sort(sortedSones, Sone.LAST_ACTIVITY_COMPARATOR);
130 } else if ("posts".equals(sortField)) {
131 if ("asc".equals(sortOrder)) {
132 Collections.sort(sortedSones, Ordering.from(Sone.POST_COUNT_COMPARATOR).reverse());
134 Collections.sort(sortedSones, Sone.POST_COUNT_COMPARATOR);
136 } else if ("images".equals(sortField)) {
137 if ("asc".equals(sortOrder)) {
138 Collections.sort(sortedSones, Ordering.from(Sone.IMAGE_COUNT_COMPARATOR).reverse());
140 Collections.sort(sortedSones, Sone.IMAGE_COUNT_COMPARATOR);
143 if ("desc".equals(sortOrder)) {
144 Collections.sort(sortedSones, Ordering.from(Sone.NICE_NAME_COMPARATOR).reverse());
146 Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
149 Pagination<Sone> sonePagination = new Pagination<Sone>(sortedSones, 25).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
150 templateContext.set("pagination", sonePagination);
151 templateContext.set("knownSones", sonePagination.getItems());