KickJava   Java API By Example, From Geeks To Geeks.

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


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.co.csir.icomtek.workflow.WorkflowData;
25 import za.co.csir.icomtek.workflow.WorkflowEvaluator;
26
27 import za.org.coefficient.authentication.Role;
28 import za.org.coefficient.core.Project;
29 import za.org.coefficient.html.ThemeSupport;
30 import za.org.coefficient.interfaces.CoefficientContext;
31 import za.org.coefficient.modules.BaseModule;
32 import za.org.coefficient.util.common.InvokerFactory;
33 import za.org.coefficient.util.ejb.VelocityScreenUtil;
34
35 import java.io.InputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * @pojo2ejb.class
43  * name="ProjectModuleLinks"
44  * jndi-prefix="za/org/coefficient/permanent/"
45  * interface-extends="za.org.coefficient.interfaces.Module"
46  * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
47  *
48  * @web.resource-env-ref
49  * name="za/org/coefficient/permanent/ProjectModuleLinks"
50  * type="za.org.coefficient.modules.project.ProjectModuleLinks"
51  * @web.resource-env-ref
52  * name="ProjectModuleLinks"
53  * type="za.org.coefficient.modules.project.ProjectModuleLinks"
54  */

55 public class ProjectModuleLinks extends BaseModule {
56
57     public String JavaDoc getMainMethod() {
58         return "renderModuleLinks";
59     }
60
61     public String JavaDoc getModuleDescription() {
62         return "This will show a link bar to " + ProjectConstants.PROJECT_NAME
63             + " modules that exist in the current "
64             + ProjectConstants.PROJECT_NAME;
65     }
66
67     public String JavaDoc getModuleDisplayName() {
68         return StringUtils.capitalise(ProjectConstants.PROJECT_NAME)
69             + " Links";
70     }
71
72     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
73         Role usersHighestRole) {
74         // We do not need to worry about security for this module
75
return null;
76     }
77
78     /**
79      * This method only renders content if a project is in the current
80      * context. It will render links, whose appearance is dictated by
81      * the stylesheet classes. The stylesheet class will be the
82      * {name of the module}.link
83      * This module also relies on the RENDER_MODULE_LINKS_HORIZONTAL
84      * property in the ProjectConstants class. If this value is true
85      * then the module will render itself horizonally otherwise it will
86      * render vertically.
87      */

88     public CoefficientContext renderModuleLinks(CoefficientContext ctx) {
89         Project prj = ctx.getProject();
90
91         // Only show something if there is a project in the context and it
92
// is active
93
if(prj != null && prj.getActive()) {
94             HashMap JavaDoc modules = new HashMap JavaDoc();
95             HashMap JavaDoc summaries = new HashMap JavaDoc();
96             String JavaDoc modName = null;
97             try {
98                 WorkflowData wd = (WorkflowData)ctx
99                     .getSessionAttribute("__current_workflow_data__");
100
101                 // invoke the modules
102
for (Iterator JavaDoc it = prj.getModules().iterator(); it.hasNext();) {
103                     modName = (String JavaDoc) it.next();
104                     if (!prj.getIsWorkflow()
105                         || WorkflowEvaluator.isModuleViewableForState(ctx, wd, modName)) {
106                         String JavaDoc displayName = (String JavaDoc)InvokerFactory.getInvoker()
107                             .invokeGetterOnModule(modName, "moduleDisplayName");
108                         String JavaDoc summary = (String JavaDoc)InvokerFactory.getInvoker()
109                             .invokeOpOnModule(modName, "getSummaryForProject",
110                                               ctx);
111                         modules.put(modName, displayName);
112                         summaries.put(modName, summary);
113                     }
114                 }
115                 
116             } catch (Exception JavaDoc e) {
117                 System.err.println("There was an error getting the display name of module: " + modName);
118             }
119             
120             HashMap JavaDoc map = new HashMap JavaDoc();
121             HashMap JavaDoc extensions = new HashMap JavaDoc();
122             
123             // calculate the td width if horizontal
124
if(ProjectConstants.RENDER_MODULE_LINKS_HORIZONTAL) {
125                 int width = 100 / (modules.size() + 1);
126                 map.put("td_width", new Integer JavaDoc(width));
127             }
128             
129             String JavaDoc resourcePath = ThemeSupport.getCurrentPathToThemeResource();
130             map.put("resourcePath", resourcePath);
131             map.put("modules", modules);
132             map.put("project", prj);
133             map.put("summaries", summaries);
134             map.put("hasImages", hasAtLeastOneImage(modules, extensions, resourcePath).
135                     toString());
136             map.put("extensions", extensions);
137             if(ProjectConstants.RENDER_MODULE_LINKS_HORIZONTAL) {
138                 map.put("horizontal", "true");
139             }
140             
141             StringBuffer JavaDoc sb =
142                 VelocityScreenUtil.getProcessedScreen("renderModuleLinks.vm",
143                                                       map);
144             ctx.setModuleContent(sb.toString(), getModuleDisplayName());
145         }
146         return ctx;
147     }
148
149     private Boolean JavaDoc hasAtLeastOneImage(HashMap JavaDoc modules, HashMap JavaDoc extensions,
150                                        String JavaDoc pathToResource) {
151         boolean hasImage = false;
152         HashMap JavaDoc modulesPlus = new HashMap JavaDoc(modules);
153         String JavaDoc [] imageExtensions = new String JavaDoc[] {".gif", ".jpg", ".png"};
154         modulesPlus.put("Project", "Home");
155         for(Iterator JavaDoc it = modulesPlus.keySet().iterator(); it.hasNext(); ) {
156             String JavaDoc moduleName = (String JavaDoc)it.next();
157             for(int i = 0; i < imageExtensions.length; i++) {
158                 String JavaDoc imageURL = "/" + pathToResource + "/" + moduleName
159                     + imageExtensions[i];
160                 try {
161                     InputStream JavaDoc is = ProjectModuleLinks.class
162                         .getResourceAsStream(imageURL);
163                     if(is != null) {
164                         is.close();
165                         hasImage = true;
166                         extensions.put(moduleName, imageExtensions[i]);
167                         break;
168                     }
169                 } catch(IOException JavaDoc ioe) {
170                     ioe.printStackTrace();
171                     // let it go, we don't have the image
172
}
173             }
174         }
175         return new Boolean JavaDoc(hasImage);
176     }
177
178  }
Popular Tags