001: /*
002: * Coefficient - facilitates project based collaboration
003: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004: * PO Box 395
005: * Pretoria 0001, RSA
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package za.org.coefficient.modules.configuration;
021:
022: import net.sf.hibernate.Hibernate;
023: import net.sf.hibernate.util.HibernateUtil;
024:
025: import za.org.coefficient.core.BaseConfigurationBackedConstants;
026: import za.org.coefficient.core.CoefficientConfiguration;
027: import za.org.coefficient.core.Constants;
028: import za.org.coefficient.authentication.Role;
029: import za.org.coefficient.interfaces.CoefficientContext;
030: import za.org.coefficient.modules.BaseModule;
031: import za.org.coefficient.util.ejb.SecurityUtil;
032: import za.org.coefficient.util.ejb.VelocityScreenUtil;
033:
034: import java.util.HashMap;
035: import java.util.List;
036:
037: /**
038: * @pojo2ejb.class
039: * name="Configuration"
040: * jndi-prefix="za/org/coefficient/admin/"
041: * interface-extends="za.org.coefficient.interfaces.Module"
042: * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
043: *
044: * @web.resource-env-ref
045: * name="za/org/coefficient/admin/Configuration"
046: * type="za.org.coefficient.modules.configuration.Configuration"
047: * @web.resource-env-ref
048: * name="Configuration"
049: * type="za.org.coefficient.modules.configuration.Configuration"
050: */
051: public class Configuration extends BaseModule {
052: //~ Methods ================================================================
053:
054: public String getMainMethod() {
055: return "view";
056: }
057:
058: public String getModuleDescription() {
059: return "Module that is used to setup the configuration of the site";
060: }
061:
062: public String getModuleDisplayName() {
063: return "Configuration Administration";
064: }
065:
066: public String canExecuteForRole(CoefficientContext ctx,
067: String methodName, Role usersHighestRole) {
068:
069: if (usersHighestRole != null) {// pvz this check is needed on a clean system with an empty db
070:
071: if (usersHighestRole.getRoleValue() != SecurityUtil.SITE_ADMIN_ROLE_VAL
072: && Constants.hasRequiredFields()) {
073: return "Only a site administrator can change site settings";
074: } else {
075: return null;
076: }
077: } else {
078: return null;
079: }
080: }
081:
082: public CoefficientContext view(CoefficientContext ctx)
083: throws Exception {
084: HashMap map = new HashMap();
085: CoefficientConfiguration cc = getConfiguration("za.org.coefficient.core.Constants");
086: map.put("configuration", cc);
087: BaseConfigurationBackedConstants conf = null;
088: try {
089: conf = (BaseConfigurationBackedConstants) Class.forName(
090: cc.getForClass()).newInstance();
091: } catch (Exception e) {
092: // do nothing
093: }
094: if (conf != null) {
095: map
096: .put(
097: "properties",
098: conf
099: .getPropertyFields("za.org.coefficient.core.Constants"));
100: if (Constants.hasRequiredFields()) {
101: map.put("hasRequiredFields", new Boolean(true));
102: }
103: }
104: StringBuffer sb = VelocityScreenUtil.getProcessedScreen(
105: "view.vm", map);
106: ctx.setModuleContent(sb.toString(), getModuleDisplayName());
107: return ctx;
108: }
109:
110: public CoefficientContext save(CoefficientContext ctx)
111: throws Exception {
112: Long id = ctx.getParameterAsLong("configId", -1);
113: if (id.longValue() > -1) {
114: CoefficientConfiguration cc = (CoefficientConfiguration) HibernateUtil
115: .load(CoefficientConfiguration.class, id);
116: int i = 0;
117: String propertyName = ctx.getParameter("propertyName" + i);
118: String propertyValue = ctx
119: .getParameter("propertyValue" + i);
120: while (propertyName != null && propertyValue != null) {
121: cc.addProperty(propertyName, propertyValue);
122: i++;
123: propertyValue = ctx.getParameter("propertyValue" + i);
124: propertyName = ctx.getParameter("propertyName" + i);
125: }
126: HibernateUtil.update(cc);
127: try {
128: BaseConfigurationBackedConstants conf = (BaseConfigurationBackedConstants) Class
129: .forName(cc.getForClass()).newInstance();
130: conf.initFromConfig(cc,
131: "za.org.coefficient.core.Constants");
132: } catch (Exception e) {
133: // do nothing
134: }
135: //Constants.initFromConfig(cc);
136: ctx.setModuleContent("Configuration saved",
137: getModuleDisplayName());
138: } else {
139: ctx.setError("You must provide a configuration to save");
140: }
141: return ctx;
142: }
143:
144: /**
145: *
146: * @return is the configuration if one exists, null otherwise
147: */
148: public CoefficientConfiguration getConfiguration(String forClass)
149: throws Exception {
150: CoefficientConfiguration cc = null;
151: List config = HibernateUtil.find("from "
152: + CoefficientConfiguration.class.getName()
153: + " as conf where conf.forClass = ?", forClass,
154: Hibernate.STRING);
155: if (config.size() == 1) {
156: cc = (CoefficientConfiguration) config.get(0);
157: } else {
158: cc = new CoefficientConfiguration();
159: cc.setForClass(forClass);
160: HibernateUtil.save(cc);
161: //HibernateUtil.finalizeSession();
162: }
163: return cc;
164: }
165:
166: public void saveConfiguration(CoefficientConfiguration cc)
167: throws Exception {
168: HibernateUtil.update(cc);
169: //HibernateUtil.finalizeSession();
170: }
171: }
|