From 65ad61b0d8c8dcf3e5ed24aed8f2a4423077432c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 28 Jul 2012 01:33:13 +0200 Subject: [PATCH] Add method to process properties from a request. --- .../demoscenemusic/page/admin/AdminBasePage.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) 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 // -- 2.7.4