Return an optional from the Sone parser.
[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.number.Numbers;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31
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
37 /**
38  * This page shows all known Sones.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class KnownSonesPage extends SoneTemplatePage {
43
44         /**
45          * Creates a “known Sones” page.
46          *
47          * @param template
48          *            The template to render
49          * @param webInterface
50          *            The Sone web interface
51          */
52         public KnownSonesPage(Template template, WebInterface webInterface) {
53                 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
54         }
55
56         //
57         // TEMPLATEPAGE METHODS
58         //
59
60         @Override
61         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
62                 super.processTemplate(request, templateContext);
63                 String sortField = request.getHttpRequest().getParam("sort");
64                 String sortOrder = request.getHttpRequest().getParam("order");
65                 String filter = request.getHttpRequest().getParam("filter");
66                 templateContext.set("sort", (sortField != null) ? sortField : "name");
67                 templateContext.set("order", (sortOrder != null) ? sortOrder : "asc");
68                 templateContext.set("filter", filter);
69                 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
70                 Collection<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
71                 if ((currentSone != null) && "followed".equals(filter)) {
72                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
73
74                                 @Override
75                                 public boolean apply(Sone sone) {
76                                         return currentSone.hasFriend(sone.getId());
77                                 }
78                         });
79                 } else if ((currentSone != null) && "not-followed".equals(filter)) {
80                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
81
82                                 @Override
83                                 public boolean apply(Sone sone) {
84                                         return !currentSone.hasFriend(sone.getId());
85                                 }
86                         });
87                 } else if ("new".equals(filter)) {
88                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
89
90                                 /**
91                                  * {@inheritDoc}
92                                  */
93                                 @Override
94                                 public boolean apply(Sone sone) {
95                                         return !sone.isKnown();
96                                 }
97                         });
98                 } else if ("not-new".equals(filter)) {
99                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
100
101                                 /**
102                                  * {@inheritDoc}
103                                  */
104                                 @Override
105                                 public boolean apply(Sone sone) {
106                                         return sone.isKnown();
107                                 }
108                         });
109                 } else if ("own".equals(filter)) {
110                         knownSones = Collections2.filter(knownSones, Sone.LOCAL_SONE_FILTER);
111                 } else if ("not-own".equals(filter)) {
112                         knownSones = Collections2.filter(knownSones, Predicates.not(Sone.LOCAL_SONE_FILTER));
113                 }
114                 List<Sone> sortedSones = new ArrayList<Sone>(knownSones);
115                 if ("activity".equals(sortField)) {
116                         if ("asc".equals(sortOrder)) {
117                                 Collections.sort(sortedSones, Ordering.from(Sone.LAST_ACTIVITY_COMPARATOR).reverse());
118                         } else {
119                                 Collections.sort(sortedSones, Sone.LAST_ACTIVITY_COMPARATOR);
120                         }
121                 } else if ("posts".equals(sortField)) {
122                         if ("asc".equals(sortOrder)) {
123                                 Collections.sort(sortedSones, Ordering.from(Sone.POST_COUNT_COMPARATOR).reverse());
124                         } else {
125                                 Collections.sort(sortedSones, Sone.POST_COUNT_COMPARATOR);
126                         }
127                 } else if ("images".equals(sortField)) {
128                         if ("asc".equals(sortOrder)) {
129                                 Collections.sort(sortedSones, Ordering.from(Sone.IMAGE_COUNT_COMPARATOR).reverse());
130                         } else {
131                                 Collections.sort(sortedSones, Sone.IMAGE_COUNT_COMPARATOR);
132                         }
133                 } else {
134                         if ("desc".equals(sortOrder)) {
135                                 Collections.sort(sortedSones, Ordering.from(Sone.NICE_NAME_COMPARATOR).reverse());
136                         } else {
137                                 Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
138                         }
139                 }
140                 Pagination<Sone> sonePagination = new Pagination<Sone>(sortedSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
141                 templateContext.set("pagination", sonePagination);
142                 templateContext.set("knownSones", sonePagination.getItems());
143         }
144
145 }