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