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