Add logging while executing trust update jobs.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / KnownSonesPage.java
1 /*
2  * Sone - KnownSonesPage.java - Copyright © 2010–2012 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.Collections;
22 import java.util.List;
23
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.sone.web.page.FreenetRequest;
26 import net.pterodactylus.util.collection.Pagination;
27 import net.pterodactylus.util.collection.ReverseComparator;
28 import net.pterodactylus.util.collection.filter.Filter;
29 import net.pterodactylus.util.collection.filter.Filters;
30 import net.pterodactylus.util.number.Numbers;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
33
34 /**
35  * This page shows all known Sones.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class KnownSonesPage extends SoneTemplatePage {
40
41         /**
42          * Creates a “known Sones” page.
43          *
44          * @param template
45          *            The template to render
46          * @param webInterface
47          *            The Sone web interface
48          */
49         public KnownSonesPage(Template template, WebInterface webInterface) {
50                 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
51         }
52
53         //
54         // TEMPLATEPAGE METHODS
55         //
56
57         /**
58          * {@inheritDoc}
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                 List<Sone> knownSones = Filters.filteredList(new ArrayList<Sone>(webInterface.getCore().getSones()), Sone.EMPTY_SONE_FILTER);
71                 if ((currentSone != null) && "followed".equals(filter)) {
72                         knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
73
74                                 @Override
75                                 public boolean filterObject(Sone sone) {
76                                         return currentSone.hasFriend(sone.getId());
77                                 }
78                         });
79                 } else if ((currentSone != null) && "not-followed".equals(filter)) {
80                         knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
81
82                                 @Override
83                                 public boolean filterObject(Sone sone) {
84                                         return !currentSone.hasFriend(sone.getId());
85                                 }
86                         });
87                 } else if ("new".equals(filter)) {
88                         knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
89                                 /**
90                                  * {@inheritDoc}
91                                  */
92                                 @Override
93                                 public boolean filterObject(Sone sone) {
94                                         return !sone.isKnown();
95                                 }
96                         });
97                 } else if ("not-new".equals(filter)) {
98                         knownSones = Filters.filteredList(knownSones, new Filter<Sone>() {
99                                 /**
100                                  * {@inheritDoc}
101                                  */
102                                 @Override
103                                 public boolean filterObject(Sone sone) {
104                                         return sone.isKnown();
105                                 }
106                         });
107                 }
108                 if ("activity".equals(sortField)) {
109                         if ("asc".equals(sortOrder)) {
110                                 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.LAST_ACTIVITY_COMPARATOR));
111                         } else {
112                                 Collections.sort(knownSones, Sone.LAST_ACTIVITY_COMPARATOR);
113                         }
114                 } else if ("posts".equals(sortField)) {
115                         if ("asc".equals(sortOrder)) {
116                                 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.POST_COUNT_COMPARATOR));
117                         } else {
118                                 Collections.sort(knownSones, Sone.POST_COUNT_COMPARATOR);
119                         }
120                 } else if ("images".equals(sortField)) {
121                         if ("asc".equals(sortOrder)) {
122                                 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.IMAGE_COUNT_COMPARATOR));
123                         } else {
124                                 Collections.sort(knownSones, Sone.IMAGE_COUNT_COMPARATOR);
125                         }
126                 } else {
127                         if ("desc".equals(sortOrder)) {
128                                 Collections.sort(knownSones, new ReverseComparator<Sone>(Sone.NICE_NAME_COMPARATOR));
129                         } else {
130                                 Collections.sort(knownSones, Sone.NICE_NAME_COMPARATOR);
131                         }
132                 }
133                 Pagination<Sone> sonePagination = new Pagination<Sone>(knownSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
134                 templateContext.set("pagination", sonePagination);
135                 templateContext.set("knownSones", sonePagination.getItems());
136         }
137
138 }