KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > modules > project > GlobalProjectAdmin


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.project;
21
22 import org.apache.commons.lang.StringUtils;
23
24 import za.org.coefficient.authentication.Role;
25 import za.org.coefficient.core.CoefficientConfiguration;
26 import za.org.coefficient.core.BaseConfigurationBackedConstants;
27 import za.org.coefficient.interfaces.CoefficientContext;
28 import za.org.coefficient.modules.BaseModule;
29 import za.org.coefficient.util.ejb.VelocityScreenUtil;
30 import za.org.coefficient.util.ejb.SecurityUtil;
31 import za.org.coefficient.modules.configuration.Configuration;
32 import za.org.coefficient.util.common.InvokerFactory;
33
34 import net.sf.hibernate.util.HibernateUtil;
35
36 import java.util.HashMap JavaDoc;
37
38 /**
39  * @pojo2ejb.class
40  * name="GlobalProjectAdmin"
41  * jndi-prefix="za/org/coefficient/admin/"
42  * interface-extends="za.org.coefficient.interfaces.Module"
43  * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
44  *
45  * @web.resource-env-ref
46  * name="za/org/coefficient/admin/GlobalProjectAdmin"
47  * type="za.org.coefficient.modules.project.GlobalProjectAdmin"
48  * @web.resource-env-ref
49  * name="GlobalProjectAdmin"
50  * type="za.org.coefficient.modules.project.GlobalProjectAdmin"
51  */

52 public class GlobalProjectAdmin extends BaseModule {
53     //~ Methods ================================================================
54

55     public String JavaDoc getMainMethod() {
56         return "adminProjects";
57     }
58
59     public String JavaDoc getModuleDescription() {
60         return "This module allows site admins manage which fields are visible for projects";
61     }
62
63     public String JavaDoc getModuleDisplayName() {
64         return "Global " + StringUtils.capitalise(ProjectConstants.PROJECT_NAME)
65             + " Administration";
66     }
67
68     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
69         Role usersHighestRole) {
70         if (usersHighestRole.getRoleValue() != SecurityUtil.SITE_ADMIN_ROLE_VAL) {
71             return "Only a site administrator can administrate global "
72                 + ProjectConstants.PROJECT_NAME + " information";
73         } else {
74             return null;
75         }
76     }
77
78     public CoefficientContext adminProjects(CoefficientContext ctx) throws Exception JavaDoc {
79         HashMap JavaDoc map = new HashMap JavaDoc();
80
81         CoefficientConfiguration projectConfiguration =
82             (CoefficientConfiguration)InvokerFactory.getInvoker()
83             .invokeMethodOnModule("Configuration", "getConfiguration",
84                                   new Object JavaDoc[]{"za.org.coefficient.modules.project.ProjectConstants"});
85         map.put("projectConfiguration", projectConfiguration);
86         java.lang.reflect.Field JavaDoc[] fields = ProjectConstants.class.getFields();
87         for (int i = 0; i < fields.length; i++) {
88             String JavaDoc name = fields[i].getName();
89             String JavaDoc value = fields[i].get(null).toString();
90             map.put(name, value);
91         }
92         map.put("projectContants", new ProjectConstants());
93         map.put("project_name_cap",
94                 StringUtils.capitalise(ProjectConstants.PROJECT_NAME));
95
96         StringBuffer JavaDoc sb =
97             VelocityScreenUtil.getProcessedScreen("globalProjectAdmin.vm", map);
98
99         // Set the html into the context
100
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
101         return ctx;
102     }
103
104     private void transfer(CoefficientConfiguration projectConfiguration,
105             CoefficientContext ctx, String JavaDoc st) {
106         String JavaDoc tmp = ctx.getParameter(st);
107         if ( tmp != null ) {
108             if("on".equals(tmp)) {
109                 projectConfiguration.addProperty(st, "true");
110             } else {
111                 projectConfiguration.addProperty(st, tmp);
112             }
113         } else {
114             projectConfiguration.addProperty(st, "false");
115         }
116     }
117
118     public CoefficientContext save(CoefficientContext ctx) throws Exception JavaDoc {
119         HashMap JavaDoc map = new HashMap JavaDoc();
120         map.put("project_name", ProjectConstants.PROJECT_NAME);
121         map.put("project_name_cap",
122                 StringUtils.capitalise(ProjectConstants.PROJECT_NAME));
123         Long JavaDoc id = ctx.getParameterAsLong("configId", -1);
124         if (id.longValue() > -1) {
125             CoefficientConfiguration cc = (CoefficientConfiguration)
126                 HibernateUtil.load(CoefficientConfiguration.class, id);
127             java.lang.reflect.Field JavaDoc[] fields = ProjectConstants.class.getFields();
128             for (int i = 0; i < fields.length; i++) {
129                 String JavaDoc name = fields[i].getName();
130                 transfer(cc, ctx, name);
131             }
132
133             HibernateUtil.update(cc);
134             try {
135                 BaseConfigurationBackedConstants conf =
136                     (BaseConfigurationBackedConstants)Class
137                     .forName(cc.getForClass()).newInstance();
138                 conf.initFromConfig(cc, "za.org.coefficient.modules.project.ProjectConstants");
139             } catch(Exception JavaDoc e) {
140                 // do nothing
141
}
142             StringBuffer JavaDoc sb =
143                 VelocityScreenUtil.getProcessedScreen("globalProjectAdminSaved.vm", map);
144
145             // Set the html into the context
146
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
147         } else {
148             ctx.setError("You must provide a configuration to save");
149         }
150         return ctx;
151     }
152 }
153
Popular Tags