From: David ‘Bombe’ Roden Date: Fri, 27 Jul 2012 23:33:13 +0000 (+0200) Subject: Add method to process properties from a request. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=65ad61b0d8c8dcf3e5ed24aed8f2a4423077432c;p=demoscenemusic.git Add method to process properties from a request. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/page/admin/AdminBasePage.java b/src/main/java/net/pterodactylus/demoscenemusic/page/admin/AdminBasePage.java index 040e641..2f4d526 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/page/admin/AdminBasePage.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/page/admin/AdminBasePage.java @@ -17,9 +17,15 @@ package net.pterodactylus.demoscenemusic.page.admin; +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; + import net.pterodactylus.demoscenemusic.core.Core; +import net.pterodactylus.demoscenemusic.data.Base; import net.pterodactylus.demoscenemusic.data.User; import net.pterodactylus.demoscenemusic.page.BasePage; +import net.pterodactylus.demoscenemusic.page.ServletRequest; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContextFactory; @@ -47,6 +53,48 @@ public class AdminBasePage extends BasePage { } // + // PROTECTED METHODS + // + + /** + * Processes properties from the given requests and adjusts the properties + * of the given object. + * + * @param request + * The request to process + * @param object + * The object whose properties to modify + */ + protected void processProperties(ServletRequest request, Base object) { + /* check if properties were changed. */ + List propertiesToDelete = new ArrayList(); + for (Entry property : object.getProperties()) { + if (request.getServletRequest().getParameter("delete." + property.getKey()) != null) { + propertiesToDelete.add(property.getKey()); + continue; + } + String value = request.getServletRequest().getParameter("value." + property.getKey()).trim(); + object.getProperties().set(property.getKey(), value); + } + + /* check if properties are deleted. */ + if (!propertiesToDelete.isEmpty()) { + for (String propertyToDelete : propertiesToDelete) { + object.getProperties().remove(propertyToDelete); + } + } + + /* check for a new property. */ + if ("true".equals(request.getServletRequest().getParameter("new-property"))) { + String property = request.getServletRequest().getParameter("property").trim(); + String value = request.getServletRequest().getParameter("value").trim(); + if ((property.length() > 0) && (value.length() > 0)) { + object.getProperties().set(property, value); + } + } + } + + // // BASEPAGE METHODS //