Replace utils’ Numbers by Guava’s Optional and Ints/Longs.tryParse.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / KnownSonesPage.java
1 /*
2  * Sone - KnownSonesPage.java - Copyright © 2010–2013 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.Collection;
22 import java.util.Collections;
23 import java.util.List;
24
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;
30
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;
37
38 /**
39  * This page shows all known Sones.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class KnownSonesPage extends SoneTemplatePage {
44
45         /**
46          * Creates a “known Sones” page.
47          *
48          * @param template
49          *            The template to render
50          * @param webInterface
51          *            The Sone web interface
52          */
53         public KnownSonesPage(Template template, WebInterface webInterface) {
54                 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
55         }
56
57         //
58         // TEMPLATEPAGE METHODS
59         //
60
61         /**
62          * {@inheritDoc}
63          */
64         @Override
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>() {
77
78                                 @Override
79                                 public boolean apply(Sone sone) {
80                                         return currentSone.hasFriend(sone.getId());
81                                 }
82                         });
83                 } else if ((currentSone != null) && "not-followed".equals(filter)) {
84                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
85
86                                 @Override
87                                 public boolean apply(Sone sone) {
88                                         return !currentSone.hasFriend(sone.getId());
89                                 }
90                         });
91                 } else if ("new".equals(filter)) {
92                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
93
94                                 /**
95                                  * {@inheritDoc}
96                                  */
97                                 @Override
98                                 public boolean apply(Sone sone) {
99                                         return !sone.isKnown();
100                                 }
101                         });
102                 } else if ("not-new".equals(filter)) {
103                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
104
105                                 /**
106                                  * {@inheritDoc}
107                                  */
108                                 @Override
109                                 public boolean apply(Sone sone) {
110                                         return sone.isKnown();
111                                 }
112                         });
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));
117                 }
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());
122                         } else {
123                                 Collections.sort(sortedSones, Sone.LAST_ACTIVITY_COMPARATOR);
124                         }
125                 } else if ("posts".equals(sortField)) {
126                         if ("asc".equals(sortOrder)) {
127                                 Collections.sort(sortedSones, Ordering.from(Sone.POST_COUNT_COMPARATOR).reverse());
128                         } else {
129                                 Collections.sort(sortedSones, Sone.POST_COUNT_COMPARATOR);
130                         }
131                 } else if ("images".equals(sortField)) {
132                         if ("asc".equals(sortOrder)) {
133                                 Collections.sort(sortedSones, Ordering.from(Sone.IMAGE_COUNT_COMPARATOR).reverse());
134                         } else {
135                                 Collections.sort(sortedSones, Sone.IMAGE_COUNT_COMPARATOR);
136                         }
137                 } else {
138                         if ("desc".equals(sortOrder)) {
139                                 Collections.sort(sortedSones, Ordering.from(Sone.NICE_NAME_COMPARATOR).reverse());
140                         } else {
141                                 Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
142                         }
143                 }
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());
147         }
148
149 }