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