X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FEditProfilePage.java;h=df5a35220070673f60246834e6e1e1b3b7cdb409;hb=2e03e9dddbea4b81aacaf1aa316f5c3ccffd4bf9;hp=fc1a0bbf620040d5bb664f0d81c890c12504456f;hpb=1f7dda6c7d0b81059a5b8f70ebf6d25d90231bae;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index fc1a0bb..df5a352 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -1,5 +1,5 @@ /* - * 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 @@ -17,16 +17,23 @@ package net.pterodactylus.sone.web; -import java.util.Map; +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.DataProvider; 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. * @@ -50,12 +57,9 @@ public class EditProfilePage extends SoneTemplatePage { // TEMPLATEPAGE METHODS // - /** - * {@inheritDoc} - */ @Override - protected void processTemplate(Request request, DataProvider dataProvider) throws RedirectException { - super.processTemplate(request, dataProvider); + protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { + super.processTemplate(request, templateContext); ToadletContext toadletContenxt = request.getToadletContext(); Sone currentSone = getCurrentSone(toadletContenxt); Profile profile = currentSone.getProfile(); @@ -65,7 +69,8 @@ public class EditProfilePage extends SoneTemplatePage { Integer birthDay = profile.getBirthDay(); Integer birthMonth = profile.getBirthMonth(); Integer birthYear = profile.getBirthYear(); - Map fields = profile.getFields(); + String avatarId = profile.getAvatar(); + List fields = profile.getFields(); if (request.getMethod() == Method.POST) { if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) { firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim(); @@ -74,58 +79,72 @@ public class EditProfilePage extends SoneTemplatePage { 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); - for (int fieldIndex = 0; fieldIndex < profile.getFieldNames().size(); ++fieldIndex) { - String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + fieldIndex, 400); - profile.setField(fieldIndex, value); + 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); + profile.setField(field, value); } currentSone.setProfile(profile); - webInterface.getCore().saveSone(currentSone); - throw new RedirectException("index.html"); + 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(); try { 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) { - dataProvider.set("fieldName", fieldName); - dataProvider.set("duplicateFieldName", true); + templateContext.set("fieldName", fieldName); + templateContext.set("duplicateFieldName", true); } } else { - int deleteFieldIndex = getFieldIndex(request, "delete-field-"); - if (deleteFieldIndex > -1) { - throw new RedirectException("deleteProfileField.html?field=" + deleteFieldIndex); + String id = getFieldId(request, "delete-field-"); + if (id != null) { + throw new RedirectException("deleteProfileField.html?field=" + id); } - int moveUpFieldIndex = getFieldIndex(request, "move-up-field-"); - if (moveUpFieldIndex > -1) { - profile.moveFieldUp(moveUpFieldIndex); + id = getFieldId(request, "move-up-field-"); + if (id != null) { + Optional field = profile.getFieldById(id); + if (!field.isPresent()) { + throw new RedirectException("invalid.html"); + } + profile.moveFieldUp(field.get()); currentSone.setProfile(profile); throw new RedirectException("editProfile.html#profile-fields"); } - int moveDownFieldIndex = getFieldIndex(request, "move-down-field-"); - if (moveDownFieldIndex > -1) { - profile.moveFieldDown(moveDownFieldIndex); + id = getFieldId(request, "move-down-field-"); + if (id != null) { + Optional field = profile.getFieldById(id); + if (!field.isPresent()) { + throw new RedirectException("invalid.html"); + } + profile.moveFieldDown(field.get()); currentSone.setProfile(profile); throw new RedirectException("editProfile.html#profile-fields"); } - int editFieldIndex = getFieldIndex(request, "edit-field-"); - if (editFieldIndex > -1) { - throw new RedirectException("editProfileField.html?field=" + editFieldIndex); + id = getFieldId(request, "edit-field-"); + if (id != null) { + throw new RedirectException("editProfileField.html?field=" + id); } } } - dataProvider.set("firstName", firstName); - dataProvider.set("middleName", middleName); - dataProvider.set("lastName", lastName); - dataProvider.set("birthDay", birthDay); - dataProvider.set("birthMonth", birthMonth); - dataProvider.set("birthYear", birthYear); - dataProvider.set("fields", fields); + templateContext.set("firstName", firstName); + templateContext.set("middleName", middleName); + templateContext.set("lastName", lastName); + 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; } // @@ -134,21 +153,21 @@ public class EditProfilePage extends SoneTemplatePage { /** * Searches for a part whose names starts with the given {@code String} and - * extracts the number from the located name. + * extracts the ID from the located name. * * @param request * The request to get the parts from * @param partNameStart * The start of the name of the requested part - * @return The parsed number, or {@code -1} if the number could not be - * parsed + * @return The parsed ID, or {@code null} if there was no part matching the + * given string */ - private int getFieldIndex(Request request, String partNameStart) { + private static String getFieldId(FreenetRequest request, String partNameStart) { for (String partName : request.getHttpRequest().getParts()) { if (partName.startsWith(partNameStart)) { - return Numbers.safeParseInteger(partName.substring(partNameStart.length()), -1); + return partName.substring(partNameStart.length()); } } - return -1; + return null; } }