Merge branch 'release-0.9.5'
[Sone.git] / src / main / java / net / pterodactylus / sone / web / CreateSonePage.java
1 /*
2  * Sone - CreateSonePage.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 java.util.logging.Logger.getLogger;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sone.core.Core;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
33 import net.pterodactylus.sone.web.page.FreenetRequest;
34 import net.pterodactylus.util.template.Template;
35 import net.pterodactylus.util.template.TemplateContext;
36 import net.pterodactylus.util.web.Method;
37 import freenet.clients.http.ToadletContext;
38
39 /**
40  * The “create Sone” page lets the user create a new Sone.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class CreateSonePage extends SoneTemplatePage {
45
46         /** The logger. */
47         private static final Logger logger = getLogger(CreateSonePage.class.getName());
48
49         /**
50          * Creates a new “create Sone” page.
51          *
52          * @param template
53          *            The template to render
54          * @param webInterface
55          *            The Sone web interface
56          */
57         public CreateSonePage(Template template, WebInterface webInterface) {
58                 super("createSone.html", template, "Page.CreateSone.Title", webInterface, false);
59         }
60
61         //
62         // STATIC ACCESSORS
63         //
64
65         /**
66          * Returns a sorted list of all own identities that do not have the “Sone”
67          * context.
68          *
69          * @param core
70          *            The core
71          * @return The list of own identities without the “Sone” context
72          */
73         public static List<OwnIdentity> getOwnIdentitiesWithoutSone(Core core) {
74                 List<OwnIdentity> identitiesWithoutSone = new ArrayList<OwnIdentity>();
75                 Set<OwnIdentity> allOwnIdentity = core.getIdentityManager().getAllOwnIdentities();
76                 for (OwnIdentity ownIdentity : allOwnIdentity) {
77                         if (!ownIdentity.hasContext("Sone")) {
78                                 identitiesWithoutSone.add(ownIdentity);
79                         }
80                 }
81                 Collections.sort(identitiesWithoutSone, new Comparator<OwnIdentity>() {
82
83                         @Override
84                         public int compare(OwnIdentity leftIdentity, OwnIdentity rightIdentity) {
85                                 return (leftIdentity.getNickname() + "@" + leftIdentity.getId()).compareToIgnoreCase(rightIdentity.getNickname() + "@" + rightIdentity.getId());
86                         }
87                 });
88                 return identitiesWithoutSone;
89         }
90
91         //
92         // TEMPLATEPAGE METHODS
93         //
94
95         /**
96          * {@inheritDoc}
97          */
98         @Override
99         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
100                 super.processTemplate(request, templateContext);
101                 List<Sone> localSones = new ArrayList<Sone>(webInterface.getCore().getLocalSones());
102                 Collections.sort(localSones, Sone.NICE_NAME_COMPARATOR);
103                 templateContext.set("sones", localSones);
104                 List<OwnIdentity> ownIdentitiesWithoutSone = getOwnIdentitiesWithoutSone(webInterface.getCore());
105                 templateContext.set("identitiesWithoutSone", ownIdentitiesWithoutSone);
106                 if (request.getMethod() == Method.POST) {
107                         String id = request.getHttpRequest().getPartAsStringFailsafe("identity", 44);
108                         OwnIdentity selectedIdentity = null;
109                         for (OwnIdentity ownIdentity : ownIdentitiesWithoutSone) {
110                                 if (ownIdentity.getId().equals(id)) {
111                                         selectedIdentity = ownIdentity;
112                                         break;
113                                 }
114                         }
115                         if (selectedIdentity == null) {
116                                 templateContext.set("errorNoIdentity", true);
117                                 return;
118                         }
119                         /* create Sone. */
120                         Sone sone = webInterface.getCore().createSone(selectedIdentity);
121                         if (sone == null) {
122                                 logger.log(Level.SEVERE, String.format("Could not create Sone for OwnIdentity: %s", selectedIdentity));
123                                 /* TODO - go somewhere else */
124                         }
125
126                         /* log in the new Sone. */
127                         setCurrentSone(request.getToadletContext(), sone);
128                         throw new RedirectException("index.html");
129                 }
130         }
131
132         /**
133          * {@inheritDoc}
134          */
135         @Override
136         public boolean isEnabled(ToadletContext toadletContext) {
137                 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
138                         return false;
139                 }
140                 return (getCurrentSone(toadletContext, false) == null) || (webInterface.getCore().getLocalSones().size() == 1);
141         }
142
143 }