c74f4b96ba3b7788a849dfe7c6b110c54961a44a
[Sone.git] / src / main / java / net / pterodactylus / sone / web / KnownSonesPage.java
1 /*
2  * Sone - KnownSonesPage.java - Copyright © 2010–2016 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 static net.pterodactylus.sone.utils.NumberParsers.parseInt;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.web.page.FreenetRequest;
29 import net.pterodactylus.util.collection.Pagination;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateContext;
32
33 import com.google.common.base.Predicate;
34 import com.google.common.base.Predicates;
35 import com.google.common.collect.Collections2;
36 import com.google.common.collect.Ordering;
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         private static final String defaultSortField = "activity";
46         private static final String defaultSortOrder = "desc";
47
48         /**
49          * Creates a “known Sones” page.
50          *
51          * @param template
52          *            The template to render
53          * @param webInterface
54          *            The Sone web interface
55          */
56         public KnownSonesPage(Template template, WebInterface webInterface) {
57                 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
58         }
59
60         //
61         // TEMPLATEPAGE METHODS
62         //
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
69                 String sortField = request.getHttpRequest().getParam("sort", defaultSortField);
70                 String sortOrder = request.getHttpRequest().getParam("order", defaultSortOrder);
71                 String filter = request.getHttpRequest().getParam("filter");
72                 templateContext.set("sort", sortField);
73                 templateContext.set("order", sortOrder);
74                 templateContext.set("filter", filter);
75                 final Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
76                 Collection<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
77                 if ((currentSone != null) && "followed".equals(filter)) {
78                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
79
80                                 @Override
81                                 public boolean apply(Sone sone) {
82                                         return currentSone.hasFriend(sone.getId());
83                                 }
84                         });
85                 } else if ((currentSone != null) && "not-followed".equals(filter)) {
86                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
87
88                                 @Override
89                                 public boolean apply(Sone sone) {
90                                         return !currentSone.hasFriend(sone.getId());
91                                 }
92                         });
93                 } else if ("new".equals(filter)) {
94                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
95
96                                 /**
97                                  * {@inheritDoc}
98                                  */
99                                 @Override
100                                 public boolean apply(Sone sone) {
101                                         return !sone.isKnown();
102                                 }
103                         });
104                 } else if ("not-new".equals(filter)) {
105                         knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
106
107                                 /**
108                                  * {@inheritDoc}
109                                  */
110                                 @Override
111                                 public boolean apply(Sone sone) {
112                                         return sone.isKnown();
113                                 }
114                         });
115                 } else if ("own".equals(filter)) {
116                         knownSones = Collections2.filter(knownSones, Sone.LOCAL_SONE_FILTER);
117                 } else if ("not-own".equals(filter)) {
118                         knownSones = Collections2.filter(knownSones, Predicates.not(Sone.LOCAL_SONE_FILTER));
119                 }
120                 List<Sone> sortedSones = new ArrayList<Sone>(knownSones);
121                 if ("activity".equals(sortField)) {
122                         if ("asc".equals(sortOrder)) {
123                                 Collections.sort(sortedSones, Ordering.from(Sone.LAST_ACTIVITY_COMPARATOR).reverse());
124                         } else {
125                                 Collections.sort(sortedSones, Sone.LAST_ACTIVITY_COMPARATOR);
126                         }
127                 } else if ("posts".equals(sortField)) {
128                         if ("asc".equals(sortOrder)) {
129                                 Collections.sort(sortedSones, Ordering.from(Sone.POST_COUNT_COMPARATOR).reverse());
130                         } else {
131                                 Collections.sort(sortedSones, Sone.POST_COUNT_COMPARATOR);
132                         }
133                 } else if ("images".equals(sortField)) {
134                         if ("asc".equals(sortOrder)) {
135                                 Collections.sort(sortedSones, Ordering.from(Sone.IMAGE_COUNT_COMPARATOR).reverse());
136                         } else {
137                                 Collections.sort(sortedSones, Sone.IMAGE_COUNT_COMPARATOR);
138                         }
139                 } else {
140                         if ("desc".equals(sortOrder)) {
141                                 Collections.sort(sortedSones, Ordering.from(Sone.NICE_NAME_COMPARATOR).reverse());
142                         } else {
143                                 Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
144                         }
145                 }
146                 Pagination<Sone> sonePagination = new Pagination<Sone>(sortedSones, 25).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
147                 templateContext.set("pagination", sonePagination);
148                 templateContext.set("knownSones", sonePagination.getItems());
149         }
150
151 }