2 * FreenetSone - CreateSonePage.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.List;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
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.Page.Request.Method;
32 import net.pterodactylus.util.logging.Logging;
33 import net.pterodactylus.util.template.Template;
34 import freenet.clients.http.ToadletContext;
37 * The “create Sone” page lets the user create a new Sone.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class CreateSonePage extends SoneTemplatePage {
44 private static final Logger logger = Logging.getLogger(CreateSonePage.class);
47 * Creates a new “create Sone” page.
50 * The template to render
52 * The Sone web interface
54 public CreateSonePage(Template template, WebInterface webInterface) {
55 super("createSone.html", template, "Page.CreateSone.Title", webInterface, false);
63 * Returns a sorted list of all own identities that do not have the “Sone”
68 * @return The list of own identities without the “Sone” context
70 public static List<OwnIdentity> getOwnIdentitiesWithoutSone(Core core) {
71 List<OwnIdentity> identitiesWithoutSone = new ArrayList<OwnIdentity>();
72 Set<OwnIdentity> allOwnIdentity = core.getIdentityManager().getAllOwnIdentities();
73 for (OwnIdentity ownIdentity : allOwnIdentity) {
74 if (!ownIdentity.hasContext("Sone")) {
75 identitiesWithoutSone.add(ownIdentity);
78 Collections.sort(identitiesWithoutSone, new Comparator<OwnIdentity>() {
81 public int compare(OwnIdentity leftIdentity, OwnIdentity rightIdentity) {
82 return (leftIdentity.getNickname() + "@" + leftIdentity.getId()).compareToIgnoreCase(rightIdentity.getNickname() + "@" + rightIdentity.getId());
85 return identitiesWithoutSone;
89 // TEMPLATEPAGE METHODS
96 protected void processTemplate(Request request, Template template) throws RedirectException {
97 super.processTemplate(request, template);
98 List<OwnIdentity> ownIdentitiesWithoutSone = getOwnIdentitiesWithoutSone(webInterface.getCore());
99 template.set("identitiesWithoutSone", ownIdentitiesWithoutSone);
100 if (request.getMethod() == Method.POST) {
101 String id = request.getHttpRequest().getPartAsStringFailsafe("identity", 44);
102 OwnIdentity selectedIdentity = null;
103 for (OwnIdentity ownIdentity : ownIdentitiesWithoutSone) {
104 if (ownIdentity.getId().equals(id)) {
105 selectedIdentity = ownIdentity;
109 if (selectedIdentity == null) {
110 template.set("errorNoIdentity", true);
114 webInterface.getCore().getIdentityManager().addContext(selectedIdentity, "Sone");
115 Sone sone = webInterface.getCore().createSone(selectedIdentity);
117 logger.log(Level.SEVERE, "Could not create Sone for OwnIdentity: %s", selectedIdentity);
118 /* TODO - go somewhere else */
121 /* log in the new Sone. */
122 setCurrentSone(request.getToadletContext(), sone);
123 throw new RedirectException("index.html");
131 public boolean isEnabled(ToadletContext toadletContext) {
132 return getCurrentSone(toadletContext) == null;