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;
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.
this.birthMonth = profile.birthMonth;
this.birthYear = profile.birthYear;
this.fields.addAll(profile.fields);
- this.fieldValues.putAll(profile.fieldValues);
}
//
}
/**
- * 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);
}
//
* @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);
}
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(")");
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();
+ }
+
+ }
+
}
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;
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();
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);
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);
}
}
}
/**
* 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;
}
}