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