Merge branch 'next' into edit-wot-trust
[Sone.git] / src / main / java / net / pterodactylus / sone / web / CreateSonePage.java
1 /*
2  * FreenetSone - 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.Page.Request.Method;
32 import net.pterodactylus.util.logging.Logging;
33 import net.pterodactylus.util.template.Template;
34 import freenet.clients.http.ToadletContext;
35
36 /**
37  * The “create Sone” page lets the user create a new Sone.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class CreateSonePage extends SoneTemplatePage {
42
43         /** The logger. */
44         private static final Logger logger = Logging.getLogger(CreateSonePage.class);
45
46         /**
47          * Creates a new “create Sone” page.
48          *
49          * @param template
50          *            The template to render
51          * @param webInterface
52          *            The Sone web interface
53          */
54         public CreateSonePage(Template template, WebInterface webInterface) {
55                 super("createSone.html", template, "Page.CreateSone.Title", webInterface, false);
56         }
57
58         //
59         // STATIC ACCESSORS
60         //
61
62         /**
63          * Returns a sorted list of all own identities that do not have the “Sone”
64          * context.
65          *
66          * @param core
67          *            The core
68          * @return The list of own identities without the “Sone” context
69          */
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);
76                         }
77                 }
78                 Collections.sort(identitiesWithoutSone, new Comparator<OwnIdentity>() {
79
80                         @Override
81                         public int compare(OwnIdentity leftIdentity, OwnIdentity rightIdentity) {
82                                 return (leftIdentity.getNickname() + "@" + leftIdentity.getId()).compareToIgnoreCase(rightIdentity.getNickname() + "@" + rightIdentity.getId());
83                         }
84                 });
85                 return identitiesWithoutSone;
86         }
87
88         //
89         // TEMPLATEPAGE METHODS
90         //
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
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;
106                                         break;
107                                 }
108                         }
109                         if (selectedIdentity == null) {
110                                 template.set("errorNoIdentity", true);
111                                 return;
112                         }
113                         /* create Sone. */
114                         Sone sone = webInterface.getCore().createSone(selectedIdentity);
115                         if (sone == null) {
116                                 logger.log(Level.SEVERE, "Could not create Sone for OwnIdentity: %s", selectedIdentity);
117                                 /* TODO - go somewhere else */
118                         }
119
120                         /* log in the new Sone. */
121                         setCurrentSone(request.getToadletContext(), sone);
122                         throw new RedirectException("index.html");
123                 }
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         public boolean isEnabled(ToadletContext toadletContext) {
131                 return getCurrentSone(toadletContext) == null;
132         }
133
134 }