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