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 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.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
31 import com.google.common.base.Optional;
32 import com.google.common.base.Predicate;
33 import com.google.common.base.Predicates;
34 import com.google.common.collect.Collections2;
35 import com.google.common.collect.Ordering;
36 import com.google.common.primitives.Ints;
39 * This page shows all known Sones.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class KnownSonesPage extends SoneTemplatePage {
46 * Creates a “known Sones” page.
49 * The template to render
51 * The Sone web interface
53 public KnownSonesPage(Template template, WebInterface webInterface) {
54 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
58 // TEMPLATEPAGE METHODS
65 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
66 super.processTemplate(request, templateContext);
67 String sortField = request.getHttpRequest().getParam("sort");
68 String sortOrder = request.getHttpRequest().getParam("order");
69 String filter = request.getHttpRequest().getParam("filter");
70 templateContext.set("sort", (sortField != null) ? sortField : "name");
71 templateContext.set("order", (sortOrder != null) ? sortOrder : "asc");
72 templateContext.set("filter", filter);
73 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
74 Collection<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
75 if ((currentSone != null) && "followed".equals(filter)) {
76 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
79 public boolean apply(Sone sone) {
80 return currentSone.hasFriend(sone.getId());
83 } else if ((currentSone != null) && "not-followed".equals(filter)) {
84 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
87 public boolean apply(Sone sone) {
88 return !currentSone.hasFriend(sone.getId());
91 } else if ("new".equals(filter)) {
92 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
98 public boolean apply(Sone sone) {
99 return !sone.isKnown();
102 } else if ("not-new".equals(filter)) {
103 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
109 public boolean apply(Sone sone) {
110 return sone.isKnown();
113 } else if ("own".equals(filter)) {
114 knownSones = Collections2.filter(knownSones, Sone.LOCAL_SONE_FILTER);
115 } else if ("not-own".equals(filter)) {
116 knownSones = Collections2.filter(knownSones, Predicates.not(Sone.LOCAL_SONE_FILTER));
118 List<Sone> sortedSones = new ArrayList<Sone>(knownSones);
119 if ("activity".equals(sortField)) {
120 if ("asc".equals(sortOrder)) {
121 Collections.sort(sortedSones, Ordering.from(Sone.LAST_ACTIVITY_COMPARATOR).reverse());
123 Collections.sort(sortedSones, Sone.LAST_ACTIVITY_COMPARATOR);
125 } else if ("posts".equals(sortField)) {
126 if ("asc".equals(sortOrder)) {
127 Collections.sort(sortedSones, Ordering.from(Sone.POST_COUNT_COMPARATOR).reverse());
129 Collections.sort(sortedSones, Sone.POST_COUNT_COMPARATOR);
131 } else if ("images".equals(sortField)) {
132 if ("asc".equals(sortOrder)) {
133 Collections.sort(sortedSones, Ordering.from(Sone.IMAGE_COUNT_COMPARATOR).reverse());
135 Collections.sort(sortedSones, Sone.IMAGE_COUNT_COMPARATOR);
138 if ("desc".equals(sortOrder)) {
139 Collections.sort(sortedSones, Ordering.from(Sone.NICE_NAME_COMPARATOR).reverse());
141 Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
144 Pagination<Sone> sonePagination = new Pagination<Sone>(sortedSones, 25).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(0));
145 templateContext.set("pagination", sonePagination);
146 templateContext.set("knownSones", sonePagination.getItems());