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