If no current Sone exists, return an error.
[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
33 import freenet.clients.http.ToadletContext;
34
35 import com.google.common.base.Optional;
36
37 /**
38  * This page lets the user edit her profile.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class EditProfilePage extends SoneTemplatePage {
43
44         /**
45          * Creates a new “edit profile” page.
46          *
47          * @param template
48          *            The template to render
49          * @param webInterface
50          *            The Sone web interface
51          */
52         public EditProfilePage(Template template, WebInterface webInterface) {
53                 super("editProfile.html", template, "Page.EditProfile.Title", webInterface, true);
54         }
55
56         //
57         // TEMPLATEPAGE METHODS
58         //
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 = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim());
80                                 birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
81                                 birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
82                                 avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
83                                 profile.modify().setFirstName(getNameFromFormField(firstName)).setMiddleName(getNameFromFormField(middleName)).setLastName(getNameFromFormField(lastName)).update();
84                                 profile.modify().setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear).update();
85                                 profile.setAvatar(webInterface.getCore().getImage(avatarId).transform(GET_ID));
86                                 for (Field field : fields) {
87                                         String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
88                                         profile.setField(field, value);
89                                 }
90                                 currentSone.setProfile(profile);
91                                 webInterface.getCore().touchConfiguration();
92                                 throw new RedirectException("editProfile.html");
93                         } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
94                                 String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
95                                 try {
96                                         profile.addField(fieldName);
97                                         currentSone.setProfile(profile);
98                                         fields = profile.getFields();
99                                         webInterface.getCore().touchConfiguration();
100                                         throw new RedirectException("editProfile.html#profile-fields");
101                                 } catch (IllegalArgumentException iae1) {
102                                         templateContext.set("fieldName", fieldName);
103                                         templateContext.set("duplicateFieldName", true);
104                                 }
105                         } else {
106                                 String id = getFieldId(request, "delete-field-");
107                                 if (id != null) {
108                                         throw new RedirectException("deleteProfileField.html?field=" + id);
109                                 }
110                                 id = getFieldId(request, "move-up-field-");
111                                 if (id != null) {
112                                         Optional<Field> field = profile.getFieldById(id);
113                                         if (!field.isPresent()) {
114                                                 throw new RedirectException("invalid.html");
115                                         }
116                                         profile.moveFieldUp(field.get());
117                                         currentSone.setProfile(profile);
118                                         throw new RedirectException("editProfile.html#profile-fields");
119                                 }
120                                 id = getFieldId(request, "move-down-field-");
121                                 if (id != null) {
122                                         Optional<Field> field = profile.getFieldById(id);
123                                         if (!field.isPresent()) {
124                                                 throw new RedirectException("invalid.html");
125                                         }
126                                         profile.moveFieldDown(field.get());
127                                         currentSone.setProfile(profile);
128                                         throw new RedirectException("editProfile.html#profile-fields");
129                                 }
130                                 id = getFieldId(request, "edit-field-");
131                                 if (id != null) {
132                                         throw new RedirectException("editProfileField.html?field=" + id);
133                                 }
134                         }
135                 }
136                 templateContext.set("firstName", firstName);
137                 templateContext.set("middleName", middleName);
138                 templateContext.set("lastName", lastName);
139                 templateContext.set("birthDay", birthDay);
140                 templateContext.set("birthMonth", birthMonth);
141                 templateContext.set("birthYear", birthYear);
142                 templateContext.set("avatarId", avatarId);
143                 templateContext.set("fields", fields);
144         }
145
146         private String getNameFromFormField(String name) {
147                 return name.length() > 0 ? name : null;
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 }