001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 15/08/2003 / 20:56:33
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.admin;
044:
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.Enumeration;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import java.util.Properties;
053:
054: import net.jforum.context.RequestContext;
055: import net.jforum.context.ResponseContext;
056: import net.jforum.entities.Category;
057: import net.jforum.entities.Forum;
058: import net.jforum.exceptions.ForumException;
059: import net.jforum.repository.ForumRepository;
060: import net.jforum.repository.TopicRepository;
061: import net.jforum.util.I18n;
062: import net.jforum.util.preferences.ConfigKeys;
063: import net.jforum.util.preferences.SystemGlobals;
064: import net.jforum.util.preferences.TemplateKeys;
065: import freemarker.template.SimpleHash;
066:
067: /**
068: * @author Rafael Steil
069: * @version $Id: ConfigAction.java,v 1.21 2007/07/28 14:17:10 rafaelsteil Exp $
070: */
071: public class ConfigAction extends AdminCommand {
072: public ConfigAction() {
073: }
074:
075: public ConfigAction(RequestContext request,
076: ResponseContext response, SimpleHash context) {
077: this .request = request;
078: this .response = response;
079: this .context = context;
080: }
081:
082: public void list() {
083: Properties p = new Properties();
084: Iterator iter = SystemGlobals.fetchConfigKeyIterator();
085:
086: while (iter.hasNext()) {
087: String key = (String) iter.next();
088: String value = SystemGlobals.getValue(key);
089: p.put(key, value);
090: }
091:
092: Properties locales = new Properties();
093:
094: FileInputStream fis = null;
095:
096: try {
097: fis = new FileInputStream(SystemGlobals
098: .getValue(ConfigKeys.CONFIG_DIR)
099: + "/languages/locales.properties");
100: locales.load(fis);
101: } catch (IOException e) {
102: throw new ForumException(e);
103: } finally {
104: if (fis != null) {
105: try {
106: fis.close();
107: } catch (Exception e) {
108: }
109: }
110: }
111:
112: List localesList = new ArrayList();
113:
114: for (Enumeration e = locales.keys(); e.hasMoreElements();) {
115: localesList.add(e.nextElement());
116: }
117:
118: this .context.put("config", p);
119: this .context.put("locales", localesList);
120: this .setTemplateName(TemplateKeys.CONFIG_LIST);
121: }
122:
123: public void editSave() {
124: this .updateData(this .getConfig());
125: this .list();
126: }
127:
128: Properties getConfig() {
129: Properties p = new Properties();
130:
131: Enumeration e = this .request.getParameterNames();
132: while (e.hasMoreElements()) {
133: String name = (String) e.nextElement();
134:
135: if (name.startsWith("p_")) {
136: p.setProperty(name.substring(name.indexOf('_') + 1),
137: this .request.getParameter(name));
138: }
139: }
140:
141: return p;
142: }
143:
144: void updateData(Properties p) {
145: int oldTopicsPerPage = SystemGlobals
146: .getIntValue(ConfigKeys.TOPICS_PER_PAGE);
147:
148: for (Iterator iter = p.entrySet().iterator(); iter.hasNext();) {
149: Map.Entry entry = (Map.Entry) iter.next();
150:
151: SystemGlobals.setValue((String) entry.getKey(),
152: (String) entry.getValue());
153: }
154:
155: SystemGlobals.saveInstallation();
156: I18n.changeBoardDefault(SystemGlobals
157: .getValue(ConfigKeys.I18N_DEFAULT));
158:
159: // If topicsPerPage has changed, force a reload in all forums
160: if (oldTopicsPerPage != SystemGlobals
161: .getIntValue(ConfigKeys.TOPICS_PER_PAGE)) {
162: List categories = ForumRepository.getAllCategories();
163:
164: for (Iterator iter = categories.iterator(); iter.hasNext();) {
165: Category c = (Category) iter.next();
166:
167: for (Iterator iter2 = c.getForums().iterator(); iter2
168: .hasNext();) {
169: Forum f = (Forum) iter2.next();
170: TopicRepository.clearCache(f.getId());
171: }
172: }
173: }
174: }
175: }
|