/*
- * Sone - EditProfilePage.java - Copyright © 2010 David Roden
+ * Sone - EditProfilePage.java - Copyright © 2010–2013 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.data.Identified.GET_ID;
+
import java.util.List;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.web.page.Page.Request.Method;
+import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.web.Method;
+
import freenet.clients.http.ToadletContext;
+import com.google.common.base.Optional;
+
/**
* This page lets the user edit her profile.
*
// TEMPLATEPAGE METHODS
//
- /**
- * {@inheritDoc}
- */
@Override
- protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
+ protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
ToadletContext toadletContenxt = request.getToadletContext();
Sone currentSone = getCurrentSone(toadletContenxt);
Integer birthDay = profile.getBirthDay();
Integer birthMonth = profile.getBirthMonth();
Integer birthYear = profile.getBirthYear();
+ String avatarId = profile.getAvatar();
List<Field> fields = profile.getFields();
if (request.getMethod() == Method.POST) {
if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) {
birthDay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim());
birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
- profile.setFirstName(firstName.length() > 0 ? firstName : null);
- profile.setMiddleName(middleName.length() > 0 ? middleName : null);
- profile.setLastName(lastName.length() > 0 ? lastName : null);
- profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
+ avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
+ profile.modify().setFirstName(getNameFromFormField(firstName)).setMiddleName(getNameFromFormField(middleName)).setLastName(getNameFromFormField(lastName)).update();
+ profile.modify().setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear).update();
+ profile.setAvatar(webInterface.getCore().getImage(avatarId).transform(GET_ID));
for (Field field : fields) {
String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
- field.setValue(value);
+ profile.setField(field, value);
}
currentSone.setProfile(profile);
- webInterface.getCore().saveSone(currentSone);
+ webInterface.getCore().touchConfiguration();
throw new RedirectException("editProfile.html");
} else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
profile.addField(fieldName);
currentSone.setProfile(profile);
fields = profile.getFields();
- webInterface.getCore().saveSone(currentSone);
+ webInterface.getCore().touchConfiguration();
throw new RedirectException("editProfile.html#profile-fields");
} catch (IllegalArgumentException iae1) {
templateContext.set("fieldName", fieldName);
}
id = getFieldId(request, "move-up-field-");
if (id != null) {
- Field field = profile.getFieldById(id);
- if (field == null) {
+ Optional<Field> field = profile.getFieldById(id);
+ if (!field.isPresent()) {
throw new RedirectException("invalid.html");
}
- profile.moveFieldUp(field);
+ profile.moveFieldUp(field.get());
currentSone.setProfile(profile);
throw new RedirectException("editProfile.html#profile-fields");
}
id = getFieldId(request, "move-down-field-");
if (id != null) {
- Field field = profile.getFieldById(id);
- if (field == null) {
+ Optional<Field> field = profile.getFieldById(id);
+ if (!field.isPresent()) {
throw new RedirectException("invalid.html");
}
- profile.moveFieldDown(field);
+ profile.moveFieldDown(field.get());
currentSone.setProfile(profile);
throw new RedirectException("editProfile.html#profile-fields");
}
templateContext.set("birthDay", birthDay);
templateContext.set("birthMonth", birthMonth);
templateContext.set("birthYear", birthYear);
+ templateContext.set("avatarId", avatarId);
templateContext.set("fields", fields);
}
+ private String getNameFromFormField(String name) {
+ return name.length() > 0 ? name : null;
+ }
+
//
// PRIVATE METHODS
//
* @return The parsed ID, or {@code null} if there was no part matching the
* given string
*/
- private String getFieldId(Request request, String partNameStart) {
+ private static String getFieldId(FreenetRequest request, String partNameStart) {
for (String partName : request.getHttpRequest().getParts()) {
if (partName.startsWith(partNameStart)) {
return partName.substring(partNameStart.length());