KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > modules > configuration > Configuration


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.modules.configuration;
21
22 import net.sf.hibernate.Hibernate;
23 import net.sf.hibernate.util.HibernateUtil;
24
25 import za.org.coefficient.core.BaseConfigurationBackedConstants;
26 import za.org.coefficient.core.CoefficientConfiguration;
27 import za.org.coefficient.core.Constants;
28 import za.org.coefficient.authentication.Role;
29 import za.org.coefficient.interfaces.CoefficientContext;
30 import za.org.coefficient.modules.BaseModule;
31 import za.org.coefficient.util.ejb.SecurityUtil;
32 import za.org.coefficient.util.ejb.VelocityScreenUtil;
33
34 import java.util.HashMap JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * @pojo2ejb.class
39  * name="Configuration"
40  * jndi-prefix="za/org/coefficient/admin/"
41  * interface-extends="za.org.coefficient.interfaces.Module"
42  * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
43  *
44  * @web.resource-env-ref
45  * name="za/org/coefficient/admin/Configuration"
46  * type="za.org.coefficient.modules.configuration.Configuration"
47  * @web.resource-env-ref
48  * name="Configuration"
49  * type="za.org.coefficient.modules.configuration.Configuration"
50  */

51 public class Configuration extends BaseModule {
52     //~ Methods ================================================================
53

54     public String JavaDoc getMainMethod() {
55         return "view";
56     }
57
58     public String JavaDoc getModuleDescription() {
59         return "Module that is used to setup the configuration of the site";
60     }
61
62     public String JavaDoc getModuleDisplayName() {
63         return "Configuration Administration";
64     }
65
66     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
67         Role usersHighestRole) {
68         
69         if (usersHighestRole != null) {// pvz this check is needed on a clean system with an empty db
70

71             if (usersHighestRole.getRoleValue() != SecurityUtil.SITE_ADMIN_ROLE_VAL
72                     && Constants.hasRequiredFields()) {
73                 return "Only a site administrator can change site settings";
74             } else {
75                 return null;
76             }
77         } else {
78             return null;
79         }
80     }
81
82     public CoefficientContext view(CoefficientContext ctx) throws Exception JavaDoc {
83         HashMap JavaDoc map = new HashMap JavaDoc();
84         CoefficientConfiguration cc =
85             getConfiguration("za.org.coefficient.core.Constants");
86         map.put("configuration", cc);
87         BaseConfigurationBackedConstants conf = null;
88         try {
89             conf = (BaseConfigurationBackedConstants)Class
90                 .forName(cc.getForClass()).newInstance();
91         } catch(Exception JavaDoc e) {
92             // do nothing
93
}
94         if(conf != null) {
95             map.put("properties", conf.getPropertyFields("za.org.coefficient.core.Constants"));
96             if (Constants.hasRequiredFields()) {
97                 map.put("hasRequiredFields", new Boolean JavaDoc(true));
98             }
99         }
100         StringBuffer JavaDoc sb =
101             VelocityScreenUtil.getProcessedScreen("view.vm", map);
102         ctx.setModuleContent(sb.toString(), getModuleDisplayName());
103         return ctx;
104     }
105
106     public CoefficientContext save(CoefficientContext ctx) throws Exception JavaDoc {
107         Long JavaDoc id = ctx.getParameterAsLong("configId", -1);
108         if (id.longValue() > -1) {
109             CoefficientConfiguration cc = (CoefficientConfiguration)
110                 HibernateUtil.load(CoefficientConfiguration.class, id);
111             int i = 0;
112             String JavaDoc propertyName = ctx.getParameter("propertyName"+i);
113             String JavaDoc propertyValue = ctx.getParameter("propertyValue"+i);
114             while (propertyName != null && propertyValue != null) {
115                 cc.addProperty(propertyName, propertyValue);
116                 i++;
117                 propertyValue = ctx.getParameter("propertyValue"+i);
118                 propertyName = ctx.getParameter("propertyName"+i);
119             }
120             HibernateUtil.update(cc);
121             try {
122                 BaseConfigurationBackedConstants conf =
123                     (BaseConfigurationBackedConstants)Class
124                     .forName(cc.getForClass()).newInstance();
125                 conf.initFromConfig(cc, "za.org.coefficient.core.Constants");
126             } catch(Exception JavaDoc e) {
127                 // do nothing
128
}
129             //Constants.initFromConfig(cc);
130
ctx.setModuleContent("Configuration saved", getModuleDisplayName());
131         } else {
132             ctx.setError("You must provide a configuration to save");
133         }
134         return ctx;
135     }
136
137     /**
138      *
139      * @return is the configuration if one exists, null otherwise
140      */

141     public CoefficientConfiguration getConfiguration(String JavaDoc forClass)
142         throws Exception JavaDoc {
143         CoefficientConfiguration cc = null;
144         List JavaDoc config = HibernateUtil.find("from "
145                                          + CoefficientConfiguration.class.getName()
146                                          +" as conf where conf.forClass = ?",
147                                          forClass, Hibernate.STRING);
148         if (config.size() == 1) {
149             cc = (CoefficientConfiguration)config.get(0);
150         } else {
151             cc = new CoefficientConfiguration();
152             cc.setForClass(forClass);
153             HibernateUtil.save(cc);
154             //HibernateUtil.finalizeSession();
155
}
156         return cc;
157     }
158
159     public void saveConfiguration (CoefficientConfiguration cc) throws Exception JavaDoc {
160         HibernateUtil.update(cc);
161         //HibernateUtil.finalizeSession();
162
}
163 }
164
Popular Tags