47fd8c308e0b7cd74286a678c2da303ed5d7fb12
[Sone.git] / src / main / java / net / pterodactylus / sone / web / EditProfilePage.java
1 /*
2  * Sone - EditProfilePage.java - Copyright © 2010–2013 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 static net.pterodactylus.sone.text.TextFilter.filter;
21 import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
22
23 import java.util.List;
24
25 import net.pterodactylus.sone.data.IdBuilder;
26 import net.pterodactylus.sone.data.Profile;
27 import net.pterodactylus.sone.data.Profile.DuplicateField;
28 import net.pterodactylus.sone.data.Profile.Field;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.web.page.FreenetRequest;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
33 import net.pterodactylus.util.web.Method;
34 import freenet.clients.http.ToadletContext;
35
36 /**
37  * This page lets the user edit her profile.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class EditProfilePage extends SoneTemplatePage {
42
43         /**
44          * Creates a new “edit profile” page.
45          *
46          * @param template
47          *            The template to render
48          * @param webInterface
49          *            The Sone web interface
50          */
51         public EditProfilePage(Template template, WebInterface webInterface) {
52                 super("editProfile.html", template, "Page.EditProfile.Title", webInterface, true);
53         }
54
55         //
56         // TEMPLATEPAGE METHODS
57         //
58
59         /**
60          * {@inheritDoc}
61          */
62         @Override
63         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
64                 super.processTemplate(request, templateContext);
65                 ToadletContext toadletContenxt = request.getToadletContext();
66                 Sone currentSone = getCurrentSone(toadletContenxt);
67                 Profile profile = currentSone.getProfile();
68                 String firstName = profile.getFirstName();
69                 String middleName = profile.getMiddleName();
70                 String lastName = profile.getLastName();
71                 Integer birthDay = profile.getBirthDay();
72                 Integer birthMonth = profile.getBirthMonth();
73                 Integer birthYear = profile.getBirthYear();
74                 String avatarId = profile.getAvatar();
75                 List<Field> fields = profile.getFields();
76                 if (request.getMethod() == Method.POST) {
77                         if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) {
78                                 firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim();
79                                 middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim();
80                                 lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim();
81                                 birthDay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim(), null);
82                                 birthMonth = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim(), null);
83                                 birthYear = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim(), null);
84                                 avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", IdBuilder.ID_STRING_LENGTH);
85                                 profile.setFirstName(firstName.length() > 0 ? firstName : null);
86                                 profile.setMiddleName(middleName.length() > 0 ? middleName : null);
87                                 profile.setLastName(lastName.length() > 0 ? lastName : null);
88                                 profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
89                                 profile.setAvatar(currentSone.getImageByInternalId(avatarId).orNull());
90                                 for (Field field : fields) {
91                                         String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
92                                         String filteredValue = filter(request.getHttpRequest().getHeader("Host"), value);
93                                         field.setValue(filteredValue);
94                                 }
95                                 currentSone.setProfile(profile);
96                                 webInterface.getCore().touchConfiguration();
97                                 throw new RedirectException("editProfile.html");
98                         } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
99                                 String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
100                                 try {
101                                         profile.addField(fieldName);
102                                         currentSone.setProfile(profile);
103                                         webInterface.getCore().touchConfiguration();
104                                         throw new RedirectException("editProfile.html#profile-fields");
105                                 } catch (DuplicateField df1) {
106                                         templateContext.set("fieldName", fieldName);
107                                         templateContext.set("duplicateFieldName", true);
108                                 }
109                         } else {
110                                 String id = getFieldId(request, "delete-field-");
111                                 if (id != null) {
112                                         throw new RedirectException("deleteProfileField.html?field=" + id);
113                                 }
114                                 id = getFieldId(request, "move-up-field-");
115                                 if (id != null) {
116                                         Field field = profile.getFieldById(id);
117                                         if (field == null) {
118                                                 throw new RedirectException("invalid.html");
119                                         }
120                                         profile.moveFieldUp(field);
121                                         currentSone.setProfile(profile);
122                                         throw new RedirectException("editProfile.html#profile-fields");
123                                 }
124                                 id = getFieldId(request, "move-down-field-");
125                                 if (id != null) {
126                                         Field field = profile.getFieldById(id);
127                                         if (field == null) {
128                                                 throw new RedirectException("invalid.html");
129                                         }
130                                         profile.moveFieldDown(field);
131                                         currentSone.setProfile(profile);
132                                         throw new RedirectException("editProfile.html#profile-fields");
133                                 }
134                                 id = getFieldId(request, "edit-field-");
135                                 if (id != null) {
136                                         throw new RedirectException("editProfileField.html?field=" + id);
137                                 }
138                         }
139                 }
140                 templateContext.set("firstName", firstName);
141                 templateContext.set("middleName", middleName);
142                 templateContext.set("lastName", lastName);
143                 templateContext.set("birthDay", birthDay);
144                 templateContext.set("birthMonth", birthMonth);
145                 templateContext.set("birthYear", birthYear);
146                 templateContext.set("avatarId", avatarId);
147                 templateContext.set("fields", fields);
148         }
149
150         //
151         // PRIVATE METHODS
152         //
153
154         /**
155          * Searches for a part whose names starts with the given {@code String} and
156          * extracts the ID from the located name.
157          *
158          * @param request
159          *            The request to get the parts from
160          * @param partNameStart
161          *            The start of the name of the requested part
162          * @return The parsed ID, or {@code null} if there was no part matching the
163          *         given string
164          */
165         private static String getFieldId(FreenetRequest request, String partNameStart) {
166                 for (String partName : request.getHttpRequest().getParts()) {
167                         if (partName.startsWith(partNameStart)) {
168                                 return partName.substring(partNameStart.length());
169                         }
170                 }
171                 return null;
172         }
173 }