Redirect back to profile after adding a field.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / EditProfilePage.java
1 /*
2  * Sone - EditProfilePage.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.Map;
21
22 import net.pterodactylus.sone.data.Profile;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.web.page.Page.Request.Method;
25 import net.pterodactylus.util.number.Numbers;
26 import net.pterodactylus.util.template.DataProvider;
27 import net.pterodactylus.util.template.Template;
28 import freenet.clients.http.ToadletContext;
29
30 /**
31  * This page lets the user edit her profile.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class EditProfilePage extends SoneTemplatePage {
36
37         /**
38          * Creates a new “edit profile” page.
39          *
40          * @param template
41          *            The template to render
42          * @param webInterface
43          *            The Sone web interface
44          */
45         public EditProfilePage(Template template, WebInterface webInterface) {
46                 super("editProfile.html", template, "Page.EditProfile.Title", webInterface, true);
47         }
48
49         //
50         // TEMPLATEPAGE METHODS
51         //
52
53         /**
54          * {@inheritDoc}
55          */
56         @Override
57         protected void processTemplate(Request request, DataProvider dataProvider) throws RedirectException {
58                 super.processTemplate(request, dataProvider);
59                 ToadletContext toadletContenxt = request.getToadletContext();
60                 Sone currentSone = getCurrentSone(toadletContenxt);
61                 Profile profile = currentSone.getProfile();
62                 String firstName = profile.getFirstName();
63                 String middleName = profile.getMiddleName();
64                 String lastName = profile.getLastName();
65                 Integer birthDay = profile.getBirthDay();
66                 Integer birthMonth = profile.getBirthMonth();
67                 Integer birthYear = profile.getBirthYear();
68                 Map<String, String> fields = profile.getFields();
69                 if (request.getMethod() == Method.POST) {
70                         if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) {
71                                 firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim();
72                                 middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim();
73                                 lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim();
74                                 birthDay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim());
75                                 birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
76                                 birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
77                                 profile.setFirstName(firstName.length() > 0 ? firstName : null);
78                                 profile.setMiddleName(middleName.length() > 0 ? middleName : null);
79                                 profile.setLastName(lastName.length() > 0 ? lastName : null);
80                                 profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
81                                 for (int fieldIndex = 0; fieldIndex < profile.getFieldNames().size(); ++fieldIndex) {
82                                         String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + fieldIndex, 400);
83                                         profile.setField(fieldIndex, value);
84                                 }
85                                 currentSone.setProfile(profile);
86                                 webInterface.getCore().saveSone(currentSone);
87                                 throw new RedirectException("editProfile.html");
88                         } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
89                                 String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
90                                 try {
91                                         profile.addField(fieldName);
92                                         currentSone.setProfile(profile);
93                                         fields = profile.getFields();
94                                         webInterface.getCore().saveSone(currentSone);
95                                         throw new RedirectException("editProfile.html#profile-fields");
96                                 } catch (IllegalArgumentException iae1) {
97                                         dataProvider.set("fieldName", fieldName);
98                                         dataProvider.set("duplicateFieldName", true);
99                                 }
100                         } else {
101                                 int deleteFieldIndex = getFieldIndex(request, "delete-field-");
102                                 if (deleteFieldIndex > -1) {
103                                         throw new RedirectException("deleteProfileField.html?field=" + deleteFieldIndex);
104                                 }
105                                 int moveUpFieldIndex = getFieldIndex(request, "move-up-field-");
106                                 if (moveUpFieldIndex > -1) {
107                                         profile.moveFieldUp(moveUpFieldIndex);
108                                         currentSone.setProfile(profile);
109                                         throw new RedirectException("editProfile.html#profile-fields");
110                                 }
111                                 int moveDownFieldIndex = getFieldIndex(request, "move-down-field-");
112                                 if (moveDownFieldIndex > -1) {
113                                         profile.moveFieldDown(moveDownFieldIndex);
114                                         currentSone.setProfile(profile);
115                                         throw new RedirectException("editProfile.html#profile-fields");
116                                 }
117                                 int editFieldIndex = getFieldIndex(request, "edit-field-");
118                                 if (editFieldIndex > -1) {
119                                         throw new RedirectException("editProfileField.html?field=" + editFieldIndex);
120                                 }
121                         }
122                 }
123                 dataProvider.set("firstName", firstName);
124                 dataProvider.set("middleName", middleName);
125                 dataProvider.set("lastName", lastName);
126                 dataProvider.set("birthDay", birthDay);
127                 dataProvider.set("birthMonth", birthMonth);
128                 dataProvider.set("birthYear", birthYear);
129                 dataProvider.set("fields", fields);
130         }
131
132         //
133         // PRIVATE METHODS
134         //
135
136         /**
137          * Searches for a part whose names starts with the given {@code String} and
138          * extracts the number from the located name.
139          *
140          * @param request
141          *            The request to get the parts from
142          * @param partNameStart
143          *            The start of the name of the requested part
144          * @return The parsed number, or {@code -1} if the number could not be
145          *         parsed
146          */
147         private int getFieldIndex(Request request, String partNameStart) {
148                 for (String partName : request.getHttpRequest().getParts()) {
149                         if (partName.startsWith(partNameStart)) {
150                                 return Numbers.safeParseInteger(partName.substring(partNameStart.length()), -1);
151                         }
152                 }
153                 return -1;
154         }
155 }