Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
Saving custom settings to the database and loading them when the appl…
Browse files Browse the repository at this point in the history
…ication starts
  • Loading branch information
Rafael Steil committed Feb 12, 2012
1 parent d944595 commit dfd0a31
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 118 deletions.
27 changes: 5 additions & 22 deletions src/main/java/net/jforum/controllers/ConfigController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
package net.jforum.controllers;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
Expand All @@ -21,7 +19,6 @@

import net.jforum.actions.helpers.Domain;
import net.jforum.core.SecurityConstraint;
import net.jforum.core.exceptions.ForumException;
import net.jforum.security.AdministrationRule;
import net.jforum.services.ConfigService;
import net.jforum.util.JForumConfig;
Expand All @@ -35,48 +32,34 @@
*/
@Resource
@Path(Domain.CONFIG_ADMIN)
// @InterceptedBy(ActionSecurityInterceptor.class)
@SecurityConstraint(value = AdministrationRule.class, displayLogin = true)
public class ConfigController {
private final ConfigService service;
private final HttpServletRequest request;
private final JForumConfig config;
private final Result result;

public ConfigController(ConfigService service, HttpServletRequest request,
JForumConfig config, Result result) {
public ConfigController(ConfigService service, HttpServletRequest request, JForumConfig config, Result result) {
this.service = service;
this.request = request;
this.config = config;
this.result = result;
}

public void list() {
public void list() throws Exception {
this.result.include("locales", this.loadLocaleNames());
this.result.include("config", this.config);
}

public void save() {
public void save() throws Exception {
this.service.save(this.request);
this.result.redirectTo(this).list();
}

private List<String> loadLocaleNames() {
private List<String> loadLocaleNames() throws Exception {
Properties locales = new Properties();

FileInputStream fis = null;

try {
locales.load(this.getClass().getResourceAsStream(
"/jforumConfig/languages/locales.properties"));
} catch (IOException e) {
throw new ForumException(e);
} finally {
if (fis != null) {
try { fis.close(); }
catch (Exception e) { }
}
}
locales.load(this.getClass().getResourceAsStream("/jforumConfig/languages/locales.properties"));

List<String> localesList = new ArrayList<String>();

Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/jforum/formatters/PostFormatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@ApplicationScoped
public class PostFormatters extends ArrayList<Formatter> {
public PostFormatters(List<Formatter> list) {
clear();
organizeOrder(list);
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/jforum/services/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public ConfigService(JForumConfig config, ConfigRepository repository, I18n i18n
}

public void save(HttpServletRequest request) {
for (Enumeration<?> e = request.getParameterNames(); e
.hasMoreElements();) {
for (Enumeration<?> e = request.getParameterNames(); e.hasMoreElements();) {
String key = (String) e.nextElement();

if (key.startsWith("p_")) {
Expand All @@ -51,7 +50,6 @@ public void save(HttpServletRequest request) {
}
}

this.i18n.changeBoardDefaultLanguage(this.config
.getValue(ConfigKeys.I18N_DEFAULT));
this.i18n.changeBoardDefaultLanguage(this.config.getValue(ConfigKeys.I18N_DEFAULT));
}
}
43 changes: 0 additions & 43 deletions src/main/java/net/jforum/util/HibernateAwareTask.java

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/java/net/jforum/util/HibernateRunnable.java

This file was deleted.

45 changes: 23 additions & 22 deletions src/main/java/net/jforum/util/JForumConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;
Expand All @@ -35,32 +38,18 @@
@Component
@ApplicationScoped
public class JForumConfig extends PropertiesConfiguration {
private ConfigRepository configRepository;
private static final Logger logger = Logger.getLogger(JForumConfig.class);
private final SessionFactory sessionFactory;

public JForumConfig(ServletContext servletContext /*ConfigRepository configRepository, HibernateAwareTask hibernateTask*/) {
public JForumConfig(ServletContext servletContext, SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
this.setReloadingStrategy(new FileChangedReloadingStrategy());
this.setDelimiterParsingDisabled(true);

try {
loadProps();
setProperty(ConfigKeys.APPLICATION_PATH, servletContext.getRealPath(""));

/*
//in test environment, hibernateTask could be null
if(hibernateTask != null){
hibernateTask.execute(new HibernateRunnable() {
@Override
public void run() {
try {
loadDatabaseProperties();
}
catch (Exception e) {
throw new ForumException(e);
}
}
});
}
*/
loadDatabaseProperties();
}
catch (Exception e) {
throw new ForumException(e);
Expand All @@ -87,15 +76,27 @@ private void loadCustomProperties() throws Exception {
}
}

private void loadDatabaseProperties() throws Exception{
if (configRepository != null) {
List<Config> databasesProperties = this.configRepository.getAll();
private void loadDatabaseProperties() {
Session session = null;

try {
session = sessionFactory.openSession();

ConfigRepository repository = new ConfigRepository(session);
List<Config> databasesProperties = repository.getAll();

for (Config config : databasesProperties) {
this.clearProperty(config.getName());
this.addProperty(config.getName(), config.getValue());
}
}
catch (Exception e) {
logger.error("Error while trying to load custom settings from the database: " + e.getMessage(), e);
}
finally {
try { session.close(); }
catch (Exception e) {}
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/jforumConfig/SystemGlobals.properties
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ username.max.length = 25
#####################

#setting for avatar allowd image info
avatar.maxHeight = 250
avatar.maxWidth = 200
avatar.maxHeight = 80
avatar.maxWidth = 80
avatar.minWidth = 20
avatar.minHeight = 20

# in bytes
avatar.maxSize = 61440
avatar.maxSize = 25600

# How many columns of avatars do you wish to display to the user
# when selecting a pre-defined avatar?
Expand Down
6 changes: 2 additions & 4 deletions webapp/templates/default/admin/menu.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@
</tr>

<tr>
<td class="row1">
<p><a id="tags" class="genmed" href="<jforum:url address='/adminTag/list'/>" target="main"><jforum:i18n key="Admin.Tags"/></a></p>
</td>
</tr>
<!--
<tr>
<td class="row1"><p><a id="avatars" class="genmed" href="<jforum:url address='/adminAvatar/list'/>" target="main"><jforum:i18n key="Admin.avatars"/></a></p></td>
</tr>
-->

<tr>
<td class="row1"><p><a id="smilies" class="genmed" href="<jforum:url address='/adminSearchStats/list'/>" target="main"><jforum:i18n key="Admin.search"/></a></p></td>
Expand Down
4 changes: 2 additions & 2 deletions webapp/templates/default/config/list.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function testEmail()
}
</script>

<form action="<jforum:url address='/jforum'/>?module=adminConfig&action=save" method="post" name="form" id="form">
<form action="<jforum:url address='/adminConfig/save'/>" method="post" name="form" id="form">

<table class="forumline" cellspacing="1" cellpadding="3" width="100%" border="0">
<tr>
Expand Down Expand Up @@ -129,7 +129,7 @@ function testEmail()

<tr>
<td class="row1" width="38%"><span class="gen"><jforum:i18n key="Config.Form.htmlTags"/></span></td>
<td class="row1" width="38%"><span class="gen"><input type="text" size="50" name="p_html.tags.welcome" value="${config.getListAsString$1["html.tags.welcome"]}" /></span></td>
<td class="row1" width="38%"><span class="gen"><input type="text" size="50" name="p_html.tags.welcome" value="${config.getValue$1["html.tags.welcome"]}" /></span></td>
</tr>

<tr>
Expand Down

0 comments on commit dfd0a31

Please sign in to comment.