Add method to process properties from a request.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 23:33:13 +0000 (01:33 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 23:33:13 +0000 (01:33 +0200)
src/main/java/net/pterodactylus/demoscenemusic/page/admin/AdminBasePage.java

index 040e641..2f4d526 100644 (file)
 
 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<String> propertiesToDelete = new ArrayList<String>();
+               for (Entry<String, String> 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
        //