2 * Sone - EditProfilePage.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;
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;
31 * This page lets the user edit her profile.
33 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
35 public class EditProfilePage extends SoneTemplatePage {
38 * Creates a new āedit profileā page.
41 * The template to render
43 * The Sone web interface
45 public EditProfilePage(Template template, WebInterface webInterface) {
46 super("editProfile.html", template, "Page.EditProfile.Title", webInterface, true);
50 // TEMPLATEPAGE METHODS
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);
85 currentSone.setProfile(profile);
86 webInterface.getCore().saveSone(currentSone);
87 throw new RedirectException("index.html");
88 } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
89 String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
91 profile.addField(fieldName);
92 currentSone.setProfile(profile);
93 fields = profile.getFields();
94 webInterface.getCore().saveSone(currentSone);
95 } catch (IllegalArgumentException iae1) {
96 dataProvider.set("fieldName", fieldName);
97 dataProvider.set("duplicateFieldName", true);
100 int deleteFieldIndex = getFieldIndex(request, "delete-field-");
101 if (deleteFieldIndex > -1) {
102 throw new RedirectException("deleteProfileField.html?field=" + deleteFieldIndex);
104 int moveUpFieldIndex = getFieldIndex(request, "move-up-field-");
105 if (moveUpFieldIndex > -1) {
106 profile.moveFieldUp(moveUpFieldIndex);
107 currentSone.setProfile(profile);
108 throw new RedirectException("editProfile.html#profile-fields");
110 int moveDownFieldIndex = getFieldIndex(request, "move-down-field-");
111 if (moveDownFieldIndex > -1) {
112 profile.moveFieldDown(moveDownFieldIndex);
113 currentSone.setProfile(profile);
114 throw new RedirectException("editProfile.html#profile-fields");
116 int editFieldIndex = getFieldIndex(request, "edit-field-");
117 if (editFieldIndex > -1) {
118 throw new RedirectException("editProfileField.html?field=" + editFieldIndex);
122 dataProvider.set("firstName", firstName);
123 dataProvider.set("middleName", middleName);
124 dataProvider.set("lastName", lastName);
125 dataProvider.set("birthDay", birthDay);
126 dataProvider.set("birthMonth", birthMonth);
127 dataProvider.set("birthYear", birthYear);
128 dataProvider.set("fields", fields);
136 * Searches for a part whose names starts with the given {@code String} and
137 * extracts the number from the located name.
140 * The request to get the parts from
141 * @param partNameStart
142 * The start of the name of the requested part
143 * @return The parsed number, or {@code -1} if the number could not be
146 private int getFieldIndex(Request request, String partNameStart) {
147 for (String partName : request.getHttpRequest().getParts()) {
148 if (partName.startsWith(partNameStart)) {
149 return Numbers.safeParseInteger(partName.substring(partNameStart.length()), -1);