Add method to process properties from a request.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / page / admin / AdminBasePage.java
1 /*
2  * DemosceneMusic - AdminBasePage.java - Copyright © 2012 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.page.admin;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map.Entry;
23
24 import net.pterodactylus.demoscenemusic.core.Core;
25 import net.pterodactylus.demoscenemusic.data.Base;
26 import net.pterodactylus.demoscenemusic.data.User;
27 import net.pterodactylus.demoscenemusic.page.BasePage;
28 import net.pterodactylus.demoscenemusic.page.ServletRequest;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContextFactory;
31
32 /**
33  * Base page for all admin pages.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class AdminBasePage extends BasePage {
38
39         /**
40          * Creates a new admin page.
41          *
42          * @param core
43          *            The core
44          * @param templateContextFactory
45          *            The template context factory
46          * @param template
47          *            The template to render
48          * @param pageName
49          *            The name of the page
50          */
51         public AdminBasePage(Core core, TemplateContextFactory templateContextFactory, Template template, String pageName) {
52                 super(core, templateContextFactory, template, pageName);
53         }
54
55         //
56         // PROTECTED METHODS
57         //
58
59         /**
60          * Processes properties from the given requests and adjusts the properties
61          * of the given object.
62          *
63          * @param request
64          *            The request to process
65          * @param object
66          *            The object whose properties to modify
67          */
68         protected void processProperties(ServletRequest request, Base object) {
69                 /* check if properties were changed. */
70                 List<String> propertiesToDelete = new ArrayList<String>();
71                 for (Entry<String, String> property : object.getProperties()) {
72                         if (request.getServletRequest().getParameter("delete." + property.getKey()) != null) {
73                                 propertiesToDelete.add(property.getKey());
74                                 continue;
75                         }
76                         String value = request.getServletRequest().getParameter("value." + property.getKey()).trim();
77                         object.getProperties().set(property.getKey(), value);
78                 }
79
80                 /* check if properties are deleted. */
81                 if (!propertiesToDelete.isEmpty()) {
82                         for (String propertyToDelete : propertiesToDelete) {
83                                 object.getProperties().remove(propertyToDelete);
84                         }
85                 }
86
87                 /* check for a new property. */
88                 if ("true".equals(request.getServletRequest().getParameter("new-property"))) {
89                         String property = request.getServletRequest().getParameter("property").trim();
90                         String value = request.getServletRequest().getParameter("value").trim();
91                         if ((property.length() > 0) && (value.length() > 0)) {
92                                 object.getProperties().set(property, value);
93                         }
94                 }
95         }
96
97         //
98         // BASEPAGE METHODS
99         //
100
101         /**
102          * {@inheritDoc}
103          */
104         @Override
105         protected int getRequiredUserLevel() {
106                 return User.LEVEL_GOD;
107         }
108
109 }