KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > admin > ConfigAction


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 15/08/2003 / 20:56:33
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.admin;
44
45 import java.io.FileInputStream JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Enumeration JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.Properties JavaDoc;
52
53 import javax.servlet.http.HttpServletResponse JavaDoc;
54
55 import net.jforum.ActionServletRequest;
56 import net.jforum.entities.Category;
57 import net.jforum.entities.Forum;
58 import net.jforum.repository.ForumRepository;
59 import net.jforum.repository.TopicRepository;
60 import net.jforum.util.I18n;
61 import net.jforum.util.preferences.ConfigKeys;
62 import net.jforum.util.preferences.SystemGlobals;
63 import net.jforum.util.preferences.TemplateKeys;
64 import freemarker.template.SimpleHash;
65
66 /**
67  * @author Rafael Steil
68  * @version $Id: ConfigAction.java,v 1.14 2006/02/12 14:55:22 rafaelsteil Exp $
69  */

70 public class ConfigAction extends AdminCommand
71 {
72     public ConfigAction() {}
73     
74     public ConfigAction(ActionServletRequest request,
75             HttpServletResponse JavaDoc response,
76             SimpleHash context)
77     {
78         this.request = request;
79         this.response = response;
80         this.context = context;
81     }
82     
83     public void list() throws Exception JavaDoc {
84         Properties JavaDoc p = new Properties JavaDoc();
85         Iterator JavaDoc iter = SystemGlobals.fetchConfigKeyIterator();
86
87         while (iter.hasNext()) {
88             String JavaDoc key = (String JavaDoc) iter.next();
89             String JavaDoc value = SystemGlobals.getValue(key);
90             p.put(key, value);
91         }
92
93         Properties JavaDoc locales = new Properties JavaDoc();
94         locales.load(new FileInputStream JavaDoc(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR)
95                         + "/languages/locales.properties"));
96         List JavaDoc localesList = new ArrayList JavaDoc();
97
98         for (Enumeration JavaDoc e = locales.keys(); e.hasMoreElements();) {
99             localesList.add(e.nextElement());
100         }
101
102         this.context.put("config", p);
103         this.context.put("locales", localesList);
104         this.setTemplateName(TemplateKeys.CONFIG_LIST);
105     }
106
107     public void editSave() throws Exception JavaDoc
108     {
109         this.updateData(this.getConfig());
110         this.list();
111     }
112     
113     Properties JavaDoc getConfig()
114     {
115         Properties JavaDoc p = new Properties JavaDoc();
116
117         Enumeration JavaDoc e = this.request.getParameterNames();
118         while (e.hasMoreElements()) {
119             String JavaDoc name = (String JavaDoc) e.nextElement();
120
121             if (name.startsWith("p_")) {
122                 p.setProperty(name.substring(name.indexOf('_') + 1), this.request.getParameter(name));
123             }
124         }
125         
126         return p;
127     }
128     
129     void updateData(Properties JavaDoc p) throws Exception JavaDoc
130     {
131         int oldTopicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
132
133         for (Iterator JavaDoc iter = p.entrySet().iterator(); iter.hasNext(); ) {
134             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
135             
136             SystemGlobals.setValue((String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue());
137         }
138         
139         SystemGlobals.saveInstallation();
140         I18n.changeBoardDefault(SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT));
141         
142         // If topicsPerPage has changed, force a reload in all forums
143
if (oldTopicsPerPage != SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE)) {
144             List JavaDoc categories = ForumRepository.getAllCategories();
145             
146             for (Iterator JavaDoc iter = categories.iterator(); iter.hasNext(); ) {
147                 Category c = (Category)iter.next();
148                 
149                 for (Iterator JavaDoc iter2 = c.getForums().iterator(); iter2.hasNext(); ) {
150                     Forum f = (Forum)iter2.next();
151                     TopicRepository.clearCache(f.getId());
152                 }
153             }
154         }
155     }
156 }
Popular Tags