2 * Sone - CreateSonePage.java - Copyright © 2010–2016 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 static java.util.logging.Logger.getLogger;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
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;
40 * The “create Sone” page lets the user create a new Sone.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class CreateSonePage extends SoneTemplatePage {
47 private static final Logger logger = getLogger(CreateSonePage.class.getName());
50 * Creates a new “create Sone” page.
53 * The template to render
55 * The Sone web interface
57 public CreateSonePage(Template template, WebInterface webInterface) {
58 super("createSone.html", template, "Page.CreateSone.Title", webInterface, false);
66 * Returns a sorted list of all own identities that do not have the “Sone”
71 * @return The list of own identities without the “Sone” context
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);
81 Collections.sort(identitiesWithoutSone, new Comparator<OwnIdentity>() {
84 public int compare(OwnIdentity leftIdentity, OwnIdentity rightIdentity) {
85 return (leftIdentity.getNickname() + "@" + leftIdentity.getId()).compareToIgnoreCase(rightIdentity.getNickname() + "@" + rightIdentity.getId());
88 return identitiesWithoutSone;
92 // TEMPLATEPAGE METHODS
99 protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
100 List<Sone> localSones = new ArrayList<Sone>(webInterface.getCore().getLocalSones());
101 Collections.sort(localSones, Sone.NICE_NAME_COMPARATOR);
102 templateContext.set("sones", localSones);
103 List<OwnIdentity> ownIdentitiesWithoutSone = getOwnIdentitiesWithoutSone(webInterface.getCore());
104 templateContext.set("identitiesWithoutSone", ownIdentitiesWithoutSone);
105 if (request.getMethod() == Method.POST) {
106 String id = request.getHttpRequest().getPartAsStringFailsafe("identity", 44);
107 OwnIdentity selectedIdentity = null;
108 for (OwnIdentity ownIdentity : ownIdentitiesWithoutSone) {
109 if (ownIdentity.getId().equals(id)) {
110 selectedIdentity = ownIdentity;
114 if (selectedIdentity == null) {
115 templateContext.set("errorNoIdentity", true);
119 Sone sone = webInterface.getCore().createSone(selectedIdentity);
121 logger.log(Level.SEVERE, String.format("Could not create Sone for OwnIdentity: %s", selectedIdentity));
122 /* TODO - go somewhere else */
125 /* log in the new Sone. */
126 setCurrentSone(request.getToadletContext(), sone);
127 throw new RedirectException("index.html");
135 public boolean isEnabled(ToadletContext toadletContext) {
136 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
139 return (getCurrentSone(toadletContext, false) == null) || (webInterface.getCore().getLocalSones().size() == 1);