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