ConfigAction.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  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. import java.io.FileInputStream;
  45. import java.io.IOException;
  46. import java.util.ArrayList;
  47. import java.util.Enumeration;
  48. import java.util.Iterator;
  49. import java.util.List;
  50. import java.util.Map;
  51. import java.util.Properties;
  52. import net.jforum.context.RequestContext;
  53. import net.jforum.context.ResponseContext;
  54. import net.jforum.entities.Category;
  55. import net.jforum.entities.Forum;
  56. import net.jforum.exceptions.ForumException;
  57. import net.jforum.repository.ForumRepository;
  58. import net.jforum.repository.TopicRepository;
  59. import net.jforum.util.I18n;
  60. import net.jforum.util.preferences.ConfigKeys;
  61. import net.jforum.util.preferences.SystemGlobals;
  62. import net.jforum.util.preferences.TemplateKeys;
  63. import freemarker.template.SimpleHash;
  64. /**
  65.  * @author Rafael Steil
  66.  * @version $Id: ConfigAction.java,v 1.21 2007/07/28 14:17:10 rafaelsteil Exp $
  67.  */
  68. public class ConfigAction extends AdminCommand 
  69. {
  70. public ConfigAction() {}
  71. public ConfigAction(RequestContext request,
  72. ResponseContext response, 
  73. SimpleHash context)
  74. {
  75. this.request = request;
  76. this.response = response;
  77. this.context = context;
  78. }
  79. public void list() {
  80. Properties p = new Properties();
  81. Iterator iter = SystemGlobals.fetchConfigKeyIterator();
  82. while (iter.hasNext()) {
  83. String key = (String) iter.next();
  84. String value = SystemGlobals.getValue(key);
  85. p.put(key, value);
  86. }
  87. Properties locales = new Properties();
  88. FileInputStream fis = null;
  89. try {
  90. fis = new FileInputStream(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR)
  91. + "/languages/locales.properties");
  92. locales.load(fis);
  93. }
  94. catch (IOException e) {
  95. throw new ForumException(e);
  96. }
  97. finally {
  98. if (fis != null) {
  99. try { fis.close(); } catch (Exception e) {}
  100. }
  101. }
  102. List localesList = new ArrayList();
  103. for (Enumeration e = locales.keys(); e.hasMoreElements();) {
  104. localesList.add(e.nextElement());
  105. }
  106. this.context.put("config", p);
  107. this.context.put("locales", localesList);
  108. this.setTemplateName(TemplateKeys.CONFIG_LIST);
  109. }
  110. public void editSave()
  111. {
  112. this.updateData(this.getConfig());
  113. this.list();
  114. }
  115. Properties getConfig()
  116. {
  117. Properties p = new Properties();
  118. Enumeration e = this.request.getParameterNames();
  119. while (e.hasMoreElements()) {
  120. String name = (String) e.nextElement();
  121. if (name.startsWith("p_")) {
  122. p.setProperty(name.substring(name.indexOf('_') + 1), this.request.getParameter(name));
  123. }
  124. }
  125. return p;
  126. }
  127. void updateData(Properties p)
  128. {
  129. int oldTopicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
  130. for (Iterator iter = p.entrySet().iterator(); iter.hasNext(); ) {
  131. Map.Entry entry = (Map.Entry)iter.next();
  132. SystemGlobals.setValue((String)entry.getKey(), (String)entry.getValue());
  133. }
  134. SystemGlobals.saveInstallation();
  135. I18n.changeBoardDefault(SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT));
  136. // If topicsPerPage has changed, force a reload in all forums
  137. if (oldTopicsPerPage != SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE)) {
  138. List categories = ForumRepository.getAllCategories();
  139. for (Iterator iter = categories.iterator(); iter.hasNext(); ) {
  140. Category c = (Category)iter.next();
  141. for (Iterator iter2 = c.getForums().iterator(); iter2.hasNext(); ) {
  142. Forum f = (Forum)iter2.next();
  143. TopicRepository.clearCache(f.getId());
  144. }
  145. }
  146. }
  147. }
  148. }