If no current Sone exists, return an error.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / EditProfilePage.java
index c79a1e9..df5a352 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.web;
 
+import static net.pterodactylus.sone.data.Identified.GET_ID;
+
 import java.util.List;
 
 import net.pterodactylus.sone.data.Profile;
@@ -27,8 +29,11 @@ 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.
  *
@@ -52,9 +57,6 @@ public class EditProfilePage extends SoneTemplatePage {
        // TEMPLATEPAGE METHODS
        //
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
@@ -78,14 +80,12 @@ public class EditProfilePage extends SoneTemplatePage {
                                birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
                                birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
                                avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
-                               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);
-                               profile.setAvatar(webInterface.getCore().getImage(avatarId, false));
+                               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().touchConfiguration();
@@ -109,21 +109,21 @@ public class EditProfilePage extends SoneTemplatePage {
                                }
                                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");
                                }
@@ -143,6 +143,10 @@ public class EditProfilePage extends SoneTemplatePage {
                templateContext.set("fields", fields);
        }
 
+       private String getNameFromFormField(String name) {
+               return name.length() > 0 ? name : null;
+       }
+
        //
        // PRIVATE METHODS
        //