Store the avatar ID differently in profile.
[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.data.Identified.GET_ID;
21
22 import java.util.List;
23
24 import net.pterodactylus.sone.data.Profile;
25 import net.pterodactylus.sone.data.Profile.Field;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import net.pterodactylus.util.number.Numbers;
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         @Override
58         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
59                 super.processTemplate(request, templateContext);
60                 ToadletContext toadletContenxt = request.getToadletContext();
61                 Sone currentSone = getCurrentSone(toadletContenxt);
62                 Profile profile = currentSone.getProfile();
63                 String firstName = profile.getFirstName();
64                 String middleName = profile.getMiddleName();
65                 String lastName = profile.getLastName();
66                 Integer birthDay = profile.getBirthDay();
67                 Integer birthMonth = profile.getBirthMonth();
68                 Integer birthYear = profile.getBirthYear();
69                 String avatarId = profile.getAvatar();
70                 List<Field> fields = profile.getFields();
71                 if (request.getMethod() == Method.POST) {
72                         if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) {
73                                 firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim();
74                                 middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim();
75                                 lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim();
76                                 birthDay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim());
77                                 birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
78                                 birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
79                                 avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
80                                 profile.modify().setFirstName(getNameFromFormField(firstName)).setMiddleName(getNameFromFormField(middleName)).setLastName(getNameFromFormField(lastName)).update();
81                                 profile.modify().setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear).update();
82                                 profile.setAvatar(webInterface.getCore().getImage(avatarId).transform(GET_ID));
83                                 for (Field field : fields) {
84                                         String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
85                                         field.setValue(value);
86                                 }
87                                 currentSone.setProfile(profile);
88                                 webInterface.getCore().touchConfiguration();
89                                 throw new RedirectException("editProfile.html");
90                         } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
91                                 String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
92                                 try {
93                                         profile.addField(fieldName);
94                                         currentSone.setProfile(profile);
95                                         fields = profile.getFields();
96                                         webInterface.getCore().touchConfiguration();
97                                         throw new RedirectException("editProfile.html#profile-fields");
98                                 } catch (IllegalArgumentException iae1) {
99                                         templateContext.set("fieldName", fieldName);
100                                         templateContext.set("duplicateFieldName", true);
101                                 }
102                         } else {
103                                 String id = getFieldId(request, "delete-field-");
104                                 if (id != null) {
105                                         throw new RedirectException("deleteProfileField.html?field=" + id);
106                                 }
107                                 id = getFieldId(request, "move-up-field-");
108                                 if (id != null) {
109                                         Field field = profile.getFieldById(id);
110                                         if (field == null) {
111                                                 throw new RedirectException("invalid.html");
112                                         }
113                                         profile.moveFieldUp(field);
114                                         currentSone.setProfile(profile);
115                                         throw new RedirectException("editProfile.html#profile-fields");
116                                 }
117                                 id = getFieldId(request, "move-down-field-");
118                                 if (id != null) {
119                                         Field field = profile.getFieldById(id);
120                                         if (field == null) {
121                                                 throw new RedirectException("invalid.html");
122                                         }
123                                         profile.moveFieldDown(field);
124                                         currentSone.setProfile(profile);
125                                         throw new RedirectException("editProfile.html#profile-fields");
126                                 }
127                                 id = getFieldId(request, "edit-field-");
128                                 if (id != null) {
129                                         throw new RedirectException("editProfileField.html?field=" + id);
130                                 }
131                         }
132                 }
133                 templateContext.set("firstName", firstName);
134                 templateContext.set("middleName", middleName);
135                 templateContext.set("lastName", lastName);
136                 templateContext.set("birthDay", birthDay);
137                 templateContext.set("birthMonth", birthMonth);
138                 templateContext.set("birthYear", birthYear);
139                 templateContext.set("avatarId", avatarId);
140                 templateContext.set("fields", fields);
141         }
142
143         private String getNameFromFormField(String name) {
144                 return name.length() > 0 ? name : null;
145         }
146
147         //
148         // PRIVATE METHODS
149         //
150
151         /**
152          * Searches for a part whose names starts with the given {@code String} and
153          * extracts the ID from the located name.
154          *
155          * @param request
156          *            The request to get the parts from
157          * @param partNameStart
158          *            The start of the name of the requested part
159          * @return The parsed ID, or {@code null} if there was no part matching the
160          *         given string
161          */
162         private static String getFieldId(FreenetRequest request, String partNameStart) {
163                 for (String partName : request.getHttpRequest().getParts()) {
164                         if (partName.startsWith(partNameStart)) {
165                                 return partName.substring(partNameStart.length());
166                         }
167                 }
168                 return null;
169         }
170 }