KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > modules > navigation > BaseNavigate


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.navigation;
21
22 import za.org.coefficient.core.Constants;
23 import za.org.coefficient.interfaces.CoefficientContext;
24 import za.org.coefficient.modules.BaseModule;
25 import za.org.coefficient.util.common.InvokerFactory;
26 import za.org.coefficient.util.ejb.SecurityUtil;
27 import za.org.coefficient.util.ejb.VelocityScreenUtil;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import javax.naming.Context JavaDoc;
34 import javax.naming.NameClassPair JavaDoc;
35 import javax.naming.NamingEnumeration JavaDoc;
36
37 /**
38  * DOCUMENT ME!
39  *
40  * @author $author$
41  * @version $Revision: 1.10 $
42  */

43 public abstract class BaseNavigate extends BaseModule {
44     //~ Methods ================================================================
45

46     protected CoefficientContext buildNavigation(CoefficientContext ctx,
47         String JavaDoc[] contextNames, String JavaDoc lineVM, String JavaDoc indexVM, boolean admin) {
48         HashMap JavaDoc nameMod = new HashMap JavaDoc();
49
50         Context initialContext = null;
51
52         // Get the navigation modules and their names
53
try {
54             initialContext = za.org.coefficient.util.common.ContextUtil.getContext();
55             if (admin && (ctx.getCurrentUser() != null)
56                 && ctx.getCurrentUser()
57                       .getSystemRole()
58                       .getDescription()
59                       .equals(SecurityUtil.SITE_ADMIN_ROLE_DESC)) {
60                 getNamesFromContext(contextNames, nameMod, initialContext);
61             } else if (!admin) {
62                 getNamesFromContext(contextNames, nameMod, initialContext);
63                 // This is so there is always a default home link
64
//nameMod.put("", "Home");
65
// Always include a help link
66
nameMod.put("__DefaultScreenHelp", ctx.getParameter("module"));
67             }
68         } catch (Exception JavaDoc e) {
69             e.printStackTrace();
70         } finally {
71             try {
72                 initialContext.close();
73             } catch (Exception JavaDoc e) {
74                 e.printStackTrace();
75             }
76         }
77
78         HashMap JavaDoc map = new HashMap JavaDoc();
79         Vector JavaDoc lines = new Vector JavaDoc();
80         StringBuffer JavaDoc sb = null;
81
82         int modSize = nameMod.size();
83         if(modSize == 0) {
84             modSize = 1;
85         }
86         int width = 100 / modSize;
87
88         for (Iterator JavaDoc it = nameMod.keySet()
89                                   .iterator(); it.hasNext();) {
90             String JavaDoc mod = (String JavaDoc) it.next();
91             String JavaDoc name = (String JavaDoc) nameMod.get(mod);
92             map.put("name", name);
93             map.put("modulename", mod);
94             map.put("td_width", new Integer JavaDoc(width));
95             if ("__DefaultScreenHelp".equals(mod)) {
96                 map.put("op", "showHelp");
97                 map.put("name", "Help");
98                 map.put("modulename", name);
99             }
100             sb = VelocityScreenUtil.getProcessedScreenFullyQualified(lineVM, map);
101             lines.addElement(sb.toString());
102             map.clear();
103         }
104         map.put("module", this);
105         map.put("lines", lines);
106         if (lines.size() != 0) {
107             sb = VelocityScreenUtil.getProcessedScreenFullyQualified(indexVM, map);
108             ctx.setModuleContent(sb.toString(), getModuleDisplayName());
109         }
110         return ctx;
111     }
112
113     private void getNamesFromContext(String JavaDoc[] contextNames, HashMap JavaDoc nameMod,
114         Context initialContext) throws Exception JavaDoc {
115         for (int i = 0; i < contextNames.length; i++) {
116             try {
117                 for (NamingEnumeration JavaDoc ne =
118                         initialContext.list(Constants.JNDI_CONTEXT + "/"
119                                             + contextNames[i]); ne.hasMore();) {
120                     NameClassPair JavaDoc bind = (NameClassPair JavaDoc) ne.next();
121                     String JavaDoc mod = bind.getName();
122
123                     // Fire the module
124
String JavaDoc name = (String JavaDoc)InvokerFactory.getInvoker()
125                         .invokeGetterOnModule(mod, "moduleDisplayName");
126                     nameMod.put(mod, name);
127                 }
128             } catch (javax.naming.NameNotFoundException JavaDoc nnfe) {
129                 // no big deal, we didnt find any
130
}
131         }
132     }
133 }
134
Popular Tags