Turn profile fields into their own container classes.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 14 Jan 2011 20:24:49 +0000 (21:24 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 14 Jan 2011 20:24:49 +0000 (21:24 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/data/Profile.java
src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java
src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
src/main/resources/templates/deleteProfileField.html
src/main/resources/templates/editProfile.html
src/main/resources/templates/editProfileField.html

index 781f414..a1282cc 100644 (file)
@@ -24,7 +24,6 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -35,6 +34,7 @@ import net.pterodactylus.sone.core.Options.OptionWatcher;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.wot.Identity;
@@ -1036,14 +1036,13 @@ public class Core implements IdentityListener, UpdateListener {
 
                /* load profile fields. */
                while (true) {
-                       String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFieldNames().size();
+                       String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
                        String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
                        if (fieldName == null) {
                                break;
                        }
                        String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
-                       profile.addField(fieldName);
-                       profile.setField(fieldName, fieldValue);
+                       profile.addField(fieldName).setValue(fieldValue);
                }
 
                /* load posts. */
@@ -1180,9 +1179,9 @@ public class Core implements IdentityListener, UpdateListener {
 
                        /* save profile fields. */
                        int fieldCounter = 0;
-                       for (Entry<String, String> profileField : profile.getFields().entrySet()) {
+                       for (Field profileField : profile.getFields()) {
                                String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
-                               configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getKey());
+                               configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
                                configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
                        }
                        configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
index 9b80438..af4155d 100644 (file)
@@ -321,12 +321,11 @@ public class SoneDownloader extends AbstractService {
                                        return null;
                                }
                                try {
-                                       profile.addField(fieldName);
+                                       profile.addField(fieldName).setValue(fieldValue);
                                } catch (IllegalArgumentException iae1) {
                                        logger.log(Level.WARNING, "Duplicate field: " + fieldName, iae1);
                                        return null;
                                }
-                               profile.setField(fieldName, fieldValue);
                        }
                }
 
index f6e2bed..8d4306d 100644 (file)
@@ -19,10 +19,8 @@ package net.pterodactylus.sone.data;
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
 import java.util.List;
-import java.util.Map;
+import java.util.UUID;
 
 import net.pterodactylus.util.validation.Validation;
 
@@ -53,10 +51,7 @@ public class Profile implements Fingerprintable {
        private volatile Integer birthYear;
 
        /** Additional fields in the profile. */
-       private final List<String> fields = Collections.synchronizedList(new ArrayList<String>());
-
-       /** The field values. */
-       private final Map<String, String> fieldValues = Collections.synchronizedMap(new HashMap<String, String>());
+       private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
 
        /**
         * Creates a new empty profile.
@@ -82,7 +77,6 @@ public class Profile implements Fingerprintable {
                this.birthMonth = profile.birthMonth;
                this.birthYear = profile.birthYear;
                this.fields.addAll(profile.fields);
-               this.fieldValues.putAll(profile.fieldValues);
        }
 
        //
@@ -216,168 +210,116 @@ public class Profile implements Fingerprintable {
        }
 
        /**
-        * Appends a new field to the list of fields.
-        *
-        * @param field
-        *            The field to add
-        * @throws IllegalArgumentException
-        *             if the name is not valid
-        */
-       public void addField(String field) throws IllegalArgumentException {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Unique", !fields.contains(field), true).check();
-               fields.add(field);
-       }
-
-       /**
-        * Moves the field with the given index up one position in the field list.
-        * The index of the field to move must be greater than {@code 0} (because
-        * you obviously can not move the first field further up).
+        * Returns the fields of this profile.
         *
-        * @param fieldIndex
-        *            The index of the field to move
+        * @return The fields of this profile
         */
-       public void moveFieldUp(int fieldIndex) {
-               Validation.begin().isGreater("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
-               String field = fields.remove(fieldIndex);
-               fields.add(fieldIndex - 1, field);
+       public List<Field> getFields() {
+               return new ArrayList<Field>(fields);
        }
 
        /**
-        * Moves the field with the given name up one position in the field list.
-        * The field must not be the first field (because you obviously can not move
-        * the first field further up).
+        * Returns whether this profile contains the given field.
         *
         * @param field
-        *            The name of the field to move
+        *            The field to check for
+        * @return {@code true} if this profile contains the field, false otherwise
         */
-       public void moveFieldUp(String field) {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
-               moveFieldUp(getFieldIndex(field));
+       public boolean hasField(Field field) {
+               return fields.contains(field);
        }
 
        /**
-        * Moves the field with the given index down one position in the field list.
-        * The index of the field to move must be less than the index of the last
-        * field (because you obviously can not move the last field further down).
+        * Returns the field with the given ID.
         *
-        * @param fieldIndex
-        *            The index of the field to move
+        * @param fieldId
+        *            The ID of the field to get
+        * @return The field, or {@code null} if this profile does not contain a
+        *         field with the given ID
         */
-       public void moveFieldDown(int fieldIndex) {
-               Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size() - 1).check();
-               String field = fields.remove(fieldIndex);
-               fields.add(fieldIndex + 1, field);
+       public Field getFieldById(String fieldId) {
+               Validation.begin().isNotNull("Field ID", fieldId).check();
+               for (Field field : fields) {
+                       if (field.getId().equals(fieldId)) {
+                               return field;
+                       }
+               }
+               return null;
        }
 
        /**
-        * Moves the field with the given name down one position in the field list.
-        * The field must not be the last field (because you obviously can not move
-        * the last field further down).
+        * Returns the field with the given name.
         *
-        * @param field
-        *            The name of the field to move
+        * @param fieldName
+        *            The name of the field to get
+        * @return The field, or {@code null} if this profile does not contain a
+        *         field with the given name
         */
-       public void moveFieldDown(String field) {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
-               moveFieldDown(getFieldIndex(field));
+       public Field getFieldByName(String fieldName) {
+               for (Field field : fields) {
+                       if (field.getName().equals(fieldName)) {
+                               return field;
+                       }
+               }
+               return null;
        }
 
        /**
-        * Removes the field at the given index.
+        * Appends a new field to the list of fields.
         *
-        * @param fieldIndex
-        *            The index of the field to remove
+        * @param fieldName
+        *            The name of the new field
+        * @return The new field
+        * @throws IllegalArgumentException
+        *             if the name is not valid
         */
-       public void removeField(int fieldIndex) {
-               Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
-               String field = fields.remove(fieldIndex);
-               fieldValues.remove(field);
+       public Field addField(String fieldName) throws IllegalArgumentException {
+               Validation.begin().isNotNull("Field Name", fieldName).check().isGreater("Field Name Length", fieldName.length(), 0).isNull("Field Name Unique", getFieldByName(fieldName)).check();
+               @SuppressWarnings("synthetic-access")
+               Field field = new Field().setName(fieldName);
+               fields.add(field);
+               return field;
        }
 
        /**
-        * Removes the field with the given name.
+        * Moves the given field up one position in the field list. The index of the
+        * field to move must be greater than {@code 0} (because you obviously can
+        * not move the first field further up).
         *
         * @param field
-        *            The name of the field
+        *            The field to move up
         */
-       public void removeField(String field) {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
-               removeField(getFieldIndex(field));
+       public void moveFieldUp(Field field) {
+               Validation.begin().isNotNull("Field", field).check().is("Field Existing", hasField(field)).isGreater("Field Index", getFieldIndex(field), 0).check();
+               int fieldIndex = getFieldIndex(field);
+               fields.remove(field);
+               fields.add(fieldIndex - 1, field);
        }
 
        /**
-        * Returns the value of the field with the given name.
+        * Moves the given field down one position in the field list. The index of
+        * the field to move must be less than the index of the last field (because
+        * you obviously can not move the last field further down).
         *
         * @param field
-        *            The name of the field
-        * @return The value of the field, or {@code null} if there is no such field
+        *            The field to move down
         */
-       public String getField(String field) {
-               return fieldValues.get(field);
-       }
-
-       /**
-        * Sets the value of the field with the given name.
-        *
-        * @param fieldIndex
-        *            The index of the field
-        * @param value
-        *            The value of the field
-        */
-       public void setField(int fieldIndex, String value) {
-               Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
-               setField(fields.get(fieldIndex), value);
+       public void moveFieldDown(Field field) {
+               Validation.begin().isNotNull("Field", field).check().is("Field Existing", hasField(field)).isLess("Field Index", getFieldIndex(field), fields.size() - 1).check();
+               int fieldIndex = getFieldIndex(field);
+               fields.remove(field);
+               fields.add(fieldIndex + 1, field);
        }
 
        /**
-        * Sets the value of the field with the given name.
+        * Removes the given field.
         *
         * @param field
-        *            The name of the field
-        * @param value
-        *            The value of the field
-        */
-       public void setField(String field, String value) {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
-               fieldValues.put(field, value);
-       }
-
-       /**
-        * Returns a list of all fields stored in this profile.
-        *
-        * @return The fields of this profile
+        *            The field to remove
         */
-       public List<String> getFieldNames() {
-               return Collections.unmodifiableList(fields);
-       }
-
-       /**
-        * Renames a fields.
-        *
-        * @param fieldIndex
-        *            The index of the field to rename
-        * @param fieldName
-        *            The new name of the field
-        */
-       public void setFieldName(int fieldIndex, String fieldName) {
-               Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).isNotNull("Field Name", fieldName).isEqual("New Field Name Unique", !fields.contains(fieldName) || (getFieldIndex(fieldName) == fieldIndex), true).check();
-               String value = fieldValues.remove(fields.get(fieldIndex));
-               fields.set(fieldIndex, fieldName);
-               fieldValues.put(fieldName, value);
-       }
-
-       /**
-        * Returns all field names and their values, ordered the same way
-        * {@link #getFieldNames()} returns the names of the fields.
-        *
-        * @return All field names and values
-        */
-       public Map<String, String> getFields() {
-               Map<String, String> fields = new LinkedHashMap<String, String>();
-               for (String field : getFieldNames()) {
-                       fields.put(field, getField(field));
-               }
-               return fields;
+       public void removeField(Field field) {
+               Validation.begin().isNotNull("Field", field).check().is("Field Existing", hasField(field)).check();
+               fields.remove(field);
        }
 
        //
@@ -392,7 +334,7 @@ public class Profile implements Fingerprintable {
         * @return The index of the field, or {@code -1} if there is no field with
         *         the given name
         */
-       private int getFieldIndex(String field) {
+       private int getFieldIndex(Field field) {
                return fields.indexOf(field);
        }
 
@@ -426,8 +368,8 @@ public class Profile implements Fingerprintable {
                        fingerprint.append("BirthYear(").append(birthYear).append(')');
                }
                fingerprint.append("ContactInformation(");
-               for (String field : fields) {
-                       fingerprint.append(field).append('(').append(fieldValues.get(field)).append(')');
+               for (Field field : fields) {
+                       fingerprint.append(field.getName()).append('(').append(field.getValue()).append(')');
                }
                fingerprint.append(")");
                fingerprint.append(")");
@@ -435,4 +377,120 @@ public class Profile implements Fingerprintable {
                return fingerprint.toString();
        }
 
+       /**
+        * Container for a profile field.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public class Field {
+
+               /** The ID of the field. */
+               private final String id;
+
+               /** The name of the field. */
+               private String name;
+
+               /** The value of the field. */
+               private String value;
+
+               /**
+                * Creates a new field with a random ID.
+                */
+               private Field() {
+                       this(UUID.randomUUID().toString());
+               }
+
+               /**
+                * Creates a new field with the given ID.
+                *
+                * @param id
+                *            The ID of the field
+                */
+               private Field(String id) {
+                       Validation.begin().isNotNull("Field ID", id).check();
+                       this.id = id;
+               }
+
+               /**
+                * Returns the ID of this field.
+                *
+                * @return The ID of this field
+                */
+               public String getId() {
+                       return id;
+               }
+
+               /**
+                * Returns the name of this field.
+                *
+                * @return The name of this field
+                */
+               public String getName() {
+                       return name;
+               }
+
+               /**
+                * Sets the name of this field. The name must not be {@code null} and
+                * must not match any other fields in this profile but my match the name
+                * of this field.
+                *
+                * @param name
+                *            The new name of this field
+                * @return This field
+                */
+               public Field setName(String name) {
+                       Validation.begin().isNotNull("Field Name", name).check().is("Field Unique", (getFieldByName(name) == null) || equals(getFieldByName(name))).check();
+                       this.name = name;
+                       return this;
+               }
+
+               /**
+                * Returns the value of this field.
+                *
+                * @return The value of this field
+                */
+               public String getValue() {
+                       return value;
+               }
+
+               /**
+                * Sets the value of this field. While {@code null} is allowed, no
+                * guarantees are made that {@code null} values are correctly persisted
+                * across restarts of the plugin!
+                *
+                * @param value
+                *            The new value of this field
+                * @return This field
+                */
+               public Field setValue(String value) {
+                       this.value = value;
+                       return this;
+               }
+
+               //
+               // OBJECT METHODS
+               //
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public boolean equals(Object object) {
+                       if (!(object instanceof Field)) {
+                               return false;
+                       }
+                       Field field = (Field) object;
+                       return id.equals(field.id);
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public int hashCode() {
+                       return id.hashCode();
+               }
+
+       }
+
 }
index 2d10b72..9b52b04 100644 (file)
@@ -18,9 +18,9 @@
 package net.pterodactylus.sone.web;
 
 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.util.number.Numbers;
 import net.pterodactylus.util.template.DataProvider;
 import net.pterodactylus.util.template.Template;
 
@@ -55,29 +55,30 @@ public class DeleteProfileFieldPage extends SoneTemplatePage {
                super.processTemplate(request, dataProvider);
                Sone currentSone = getCurrentSone(request.getToadletContext());
                Profile profile = currentSone.getProfile();
-               int fieldIndex = Numbers.safeParseInteger(request.getHttpRequest().getParam("field"), -1);
-               if (fieldIndex >= currentSone.getProfile().getFieldNames().size()) {
-                       fieldIndex = -1;
-               }
-               String fieldName = null;
-               String fieldValue = null;
-               if (fieldIndex > -1) {
-                       fieldName = profile.getFieldNames().get(fieldIndex);
-                       fieldValue = profile.getField(fieldName);
+
+               /* get parameters from request. */
+               String fieldId = request.getHttpRequest().getParam("field");
+               Field field = profile.getFieldById(fieldId);
+               if (field == null) {
+                       throw new RedirectException("invalid.html");
                }
+
+               /* process POST request. */
                if (request.getMethod() == Method.POST) {
                        if (request.getHttpRequest().getPartAsStringFailsafe("confirm", 4).equals("true")) {
-                               fieldIndex = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("field", 11), -1);
-                               if (fieldIndex > -1) {
-                                       profile.removeField(fieldIndex);
+                               fieldId = request.getHttpRequest().getParam("field");
+                               field = profile.getFieldById(fieldId);
+                               if (field == null) {
+                                       throw new RedirectException("invalid.html");
                                }
+                               profile.removeField(field);
                                currentSone.setProfile(profile);
                        }
                        throw new RedirectException("editProfile.html#profile-fields");
                }
-               dataProvider.set("fieldIndex", fieldIndex);
-               dataProvider.set("fieldName", fieldName);
-               dataProvider.set("fieldValue", fieldValue);
+
+               /* set current values in template. */
+               dataProvider.set("field", field);
        }
 
 }
index 2b72a81..1b64b08 100644 (file)
@@ -18,9 +18,9 @@
 package net.pterodactylus.sone.web;
 
 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.util.number.Numbers;
 import net.pterodactylus.util.template.DataProvider;
 import net.pterodactylus.util.template.Template;
 
@@ -57,15 +57,10 @@ public class EditProfileFieldPage extends SoneTemplatePage {
                Profile profile = currentSone.getProfile();
 
                /* get parameters from request. */
-               int fieldIndex = Numbers.safeParseInteger(request.getHttpRequest().getParam("field"), -1);
-               if (fieldIndex >= currentSone.getProfile().getFieldNames().size()) {
-                       fieldIndex = -1;
-               }
-               String fieldName = null;
-               String fieldValue = null;
-               if (fieldIndex > -1) {
-                       fieldName = profile.getFieldNames().get(fieldIndex);
-                       fieldValue = profile.getField(fieldName);
+               String fieldId = request.getHttpRequest().getParam("field");
+               Field field = profile.getFieldById(fieldId);
+               if (field == null) {
+                       throw new RedirectException("invalid.html");
                }
 
                /* process the POST request. */
@@ -73,14 +68,15 @@ public class EditProfileFieldPage extends SoneTemplatePage {
                        if (request.getHttpRequest().getPartAsStringFailsafe("cancel", 4).equals("true")) {
                                throw new RedirectException("editProfile.html#profile-fields");
                        }
-                       fieldIndex = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("field", 11), -1);
-                       String name = request.getHttpRequest().getPartAsStringFailsafe("name", 256);
-                       if (fieldIndex == -1) {
+                       fieldId = request.getHttpRequest().getPartAsStringFailsafe("field", 36);
+                       field = profile.getFieldById(fieldId);
+                       if (field == null) {
                                throw new RedirectException("invalid.html");
                        }
-                       int existingFieldIndex = profile.getFieldNames().indexOf(name);
-                       if ((existingFieldIndex == -1) || (existingFieldIndex == fieldIndex)) {
-                               profile.setFieldName(fieldIndex, name);
+                       String name = request.getHttpRequest().getPartAsStringFailsafe("name", 256);
+                       Field existingField = profile.getFieldByName(name);
+                       if ((existingField == null) || (existingField.equals(field))) {
+                               field.setName(name);
                                currentSone.setProfile(profile);
                                throw new RedirectException("editProfile.html#profile-fields");
                        }
@@ -88,9 +84,7 @@ public class EditProfileFieldPage extends SoneTemplatePage {
                }
 
                /* store current values in template. */
-               dataProvider.set("fieldIndex", fieldIndex);
-               dataProvider.set("fieldName", fieldName);
-               dataProvider.set("fieldValue", fieldValue);
+               dataProvider.set("field", field);
        }
 
 }
index c1bac4a..380e054 100644 (file)
 
 package net.pterodactylus.sone.web;
 
-import java.util.Map;
+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.util.number.Numbers;
@@ -65,7 +66,7 @@ public class EditProfilePage extends SoneTemplatePage {
                Integer birthDay = profile.getBirthDay();
                Integer birthMonth = profile.getBirthMonth();
                Integer birthYear = profile.getBirthYear();
-               Map<String, String> fields = profile.getFields();
+               List<Field> 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();
@@ -78,9 +79,9 @@ public class EditProfilePage extends SoneTemplatePage {
                                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);
+                               for (Field field : fields) {
+                                       String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
+                                       field.setValue(value);
                                }
                                currentSone.setProfile(profile);
                                webInterface.getCore().saveSone(currentSone);
@@ -98,25 +99,33 @@ public class EditProfilePage extends SoneTemplatePage {
                                        dataProvider.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) {
+                                       Field field = profile.getFieldById(id);
+                                       if (field == null) {
+                                               throw new RedirectException("invalid.html");
+                                       }
+                                       profile.moveFieldUp(field);
                                        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) {
+                                       Field field = profile.getFieldById(id);
+                                       if (field == null) {
+                                               throw new RedirectException("invalid.html");
+                                       }
+                                       profile.moveFieldDown(field);
                                        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);
                                }
                        }
                }
@@ -135,21 +144,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 String getFieldId(Request 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;
        }
 }
index b2bac28..006e1ff 100644 (file)
@@ -5,13 +5,13 @@
        <p><%= Page.DeleteProfileField.Text|l10n|html></p>
 
        <div class="profile-field">
-               <div class="name"><% fieldName|html></div>
-               <div class="value"><% fieldValue|html></div>
+               <div class="name"><% field.name|html></div>
+               <div class="value"><% field.value|html></div>
        </div>
 
        <form id="delete-profile-field" method="post">
                <input type="hidden" name="formPassword" value="<% formPassword|html>" />
-               <input type="hidden" name="field" value="<% fieldIndex>" />
+               <input type="hidden" name="field" value="<% field.id|html>" />
                <button type="submit" name="confirm" value="true"><%= Page.DeleteProfileField.Button.Yes|l10n|html></button>
                <button type="submit" name="cancel" value="true"><%= Page.DeleteProfileField.Button.No|l10n|html></button>
        </form>
index 4ae0ce6..189488e 100644 (file)
                <p><%= Page.EditProfile.Fields.Description|l10n|html></p>
 
                <%foreach fields field fieldLoop>
-                       <div class="profile-field">
-                               <div class="name"><% field.key|html></div>
-                               <div class="edit-field-name"><button type="submit" name="edit-field-<% fieldLoop.count>" value="true"><%= Page.EditProfile.Fields.Button.Edit|l10n|html></button></div>
-                               <div class="delete-field-name"><button type="submit" name="delete-field-<% fieldLoop.count>" value="true"><%= Page.EditProfile.Fields.Button.Delete|l10n|html></button></div>
-                               <div class="<%if fieldLoop.last>hidden <%/if>move-down-field"><button type="submit" name="move-down-field-<% fieldLoop.count>" value="true"><%= Page.EditProfile.Fields.Button.MoveDown|l10n|html></button></div>
-                               <div class="<%if fieldLoop.first>hidden <%/if>move-up-field"><button type="submit" name="move-up-field-<% fieldLoop.count>" value="true"><%= Page.EditProfile.Fields.Button.MoveUp|l10n|html></button></div>
-                               <div class="value"><input type="text" name="field-<% fieldLoop.count>" value="<% field.value|html>" /></div>
+                       <div class="profile-field" id="<% field.id|html>">
+                               <div class="name"><% field.name|html></div>
+                               <div class="edit-field-name"><button type="submit" name="edit-field-<% field.id|html>" value="true"><%= Page.EditProfile.Fields.Button.Edit|l10n|html></button></div>
+                               <div class="delete-field-name"><button type="submit" name="delete-field-<% field.id|html>" value="true"><%= Page.EditProfile.Fields.Button.Delete|l10n|html></button></div>
+                               <div class="<%if fieldLoop.last>hidden <%/if>move-down-field"><button type="submit" name="move-down-field-<% field.id|html>" value="true"><%= Page.EditProfile.Fields.Button.MoveDown|l10n|html></button></div>
+                               <div class="<%if fieldLoop.first>hidden <%/if>move-up-field"><button type="submit" name="move-up-field-<% field.id|html>" value="true"><%= Page.EditProfile.Fields.Button.MoveUp|l10n|html></button></div>
+                               <div class="value"><input type="text" name="field-<% field.id|html>" value="<% field.value|html>" /></div>
                        </div>
 
                        <%if fieldLoop.last>
index 5ad4453..6030f71 100644 (file)
@@ -10,9 +10,9 @@
 
        <form method="post">
                <input type="hidden" name="formPassword" value="<% formPassword|html>" />
-               <input type="hidden" name="field" value="<% fieldIndex>" />
+               <input type="hidden" name="field" value="<% field.id|html>" />
                <div>
-                       <input type="text" name="name" value="<% fieldName|html>" />
+                       <input type="text" name="name" value="<% field.name|html>" />
                        <button type="submit" name="save" value="true"><%= Page.EditProfileField.Button.Save|l10n|html></button>
                </div>
                <p>