Allow sorting and filtering of “known Sones.”
[Sone.git] / src / main / java / net / pterodactylus / sone / web / KnownSonesPage.java
1 /*
2  * Sone - KnownSonesPage.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23
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;
32
33 /**
34  * This page shows all known Sones.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class KnownSonesPage extends SoneTemplatePage {
39
40         /**
41          * Creates a “known Sones” page.
42          *
43          * @param template
44          *            The template to render
45          * @param webInterface
46          *            The Sone web interface
47          */
48         public KnownSonesPage(Template template, WebInterface webInterface) {
49                 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
50         }
51
52         //
53         // TEMPLATEPAGE METHODS
54         //
55
56         /**
57          * {@inheritDoc}
58          */
59         @Override
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>() {
69
70                                 @Override
71                                 public boolean filterObject(Sone sone) {
72                                         return currentSone.hasFriend(sone.getId());
73                                 }
74                         });
75                         templateContext.set("followedSones", "show-only");
76                 } else if ((currentSone != null) && "hide".equals(followedSones)) {
77                         knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
78
79                                 @Override
80                                 public boolean filterObject(Sone sone) {
81                                         return !currentSone.hasFriend(sone.getId());
82                                 }
83                         });
84                         templateContext.set("followedSones", "hide");
85                 }
86                 if ("name".equals(sortField)) {
87                         if ("desc".equals(sortOrder)) {
88                                 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.NICE_NAME_COMPARATOR));
89                         } else {
90                                 Collections.sort(knownSones, Sone.NICE_NAME_COMPARATOR);
91                         }
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));
97                         } else {
98                                 Collections.sort(knownSones, Sone.LAST_ACTIVITY_COMPARATOR);
99                         }
100                         templateContext.set("sort", "activity");
101                         templateContext.set("order", "asc".equals(sortOrder) ? "asc" : "desc");
102                 }
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());
106         }
107 }