KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > portlet > jboss > PentahoMenuBar


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Feb 2, 2006
14  * @author James Dixon
15  */

16
17 package org.pentaho.ui.portlet.jboss;
18
19 import org.jboss.portal.core.api.JBossPortalNode;
20 import org.jboss.portal.core.model.portal.Context;
21 import org.jboss.portal.core.model.portal.Page;
22 import org.jboss.portal.core.model.portal.Portal;
23 import org.jboss.portal.core.model.portal.PortalObject;
24 import org.jboss.portal.core.model.portal.Window;
25 import org.jboss.portal.security.PortalPermissionFactory;
26 import org.jboss.portal.security.PortalPolicyException;
27 import org.jboss.portlet.JBossPortlet;
28 import org.jboss.portlet.JBossRenderRequest;
29 import org.jboss.portlet.JBossRenderResponse;
30 import org.jboss.portlet.PortalNode;
31
32 import javax.portlet.PortletException;
33 import javax.portlet.PortletPreferences;
34 import java.io.IOException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.Comparator JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 import org.pentaho.messages.Messages;
41
42 public class PentahoMenuBar extends JBossPortlet {
43
44     // private static final String RESOURCE_KEY = "key"; //$NON-NLS-1$
45
private static final String JavaDoc ORDER = "order"; //$NON-NLS-1$
46

47     private static final String JavaDoc LEVELS = "levels"; //$NON-NLS-1$
48

49     private static final String JavaDoc PORTALS = "portals-menu"; //$NON-NLS-1$
50

51     private PortalPermissionFactory permissionFactory;
52
53     public void init() throws PortletException {
54
55         permissionFactory = (PortalPermissionFactory) getPortletContext().getAttribute("PortalPermissionFactory"); //$NON-NLS-1$
56

57         //
58
if (permissionFactory == null) {
59             // throw new PortletException("No portal permission factory");
60
}
61     }
62
63     public void destroy() {
64         super.destroy();
65
66         //
67
permissionFactory = null;
68     }
69
70     public void render(JBossRenderRequest req, JBossRenderResponse resp) throws IOException JavaDoc {
71
72         resp.setContentType("text/html"); //$NON-NLS-1$
73

74         PortletPreferences prefs = req.getPreferences();
75         String JavaDoc menuLevels = prefs.getValue(LEVELS, "1"); //$NON-NLS-1$
76
String JavaDoc portalsPref = prefs.getValue(PORTALS, "true"); //$NON-NLS-1$
77

78         boolean secured = "true".equals( prefs.getValue( "secured", "false" ) ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
79

80         boolean doTransMenu = menuLevels.equals("2"); //$NON-NLS-1$
81
boolean doPortalsMenu = portalsPref.equals("true"); //$NON-NLS-1$
82

83         PortalNode current = req.getPortalNode();
84         PortalNode thisPage = current;
85         if (thisPage.getType() != PortalNode.TYPE_PAGE) {
86             thisPage = thisPage.getParent();
87         }
88
89         resp.setTitle("Navigation"); //$NON-NLS-1$
90
// get the list of portals
91
StringBuffer JavaDoc content = new StringBuffer JavaDoc();
92
93         content.append("<div id=\"topmenu\">\n<div id=\"mtm_menu\">\n"); //$NON-NLS-1$
94

95         try {
96             while (current != null && current.getType() != PortalNode.TYPE_PORTAL) {
97                 current = current.getParent();
98             }
99
100             if (current != null && current.getType() == PortalNode.TYPE_PORTAL) {
101                 // now do the pages
102

103                 ArrayList JavaDoc pages = new ArrayList JavaDoc();
104                 Iterator JavaDoc pageIterator = current.getChildren().iterator();
105                 for (; pageIterator.hasNext();) {
106                     PortalNode pageNode = (PortalNode) pageIterator.next();
107                         if( pageNode instanceof JBossPortalNode && allowed( (JBossPortalNode) pageNode, secured ) ) {
108                             pages.add(pageNode);
109                         }
110                 }
111
112                 Collections.sort(pages, new Comparator JavaDoc() {
113                     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
114                         Float JavaDoc high1 = getOrder(o1);
115                         Float JavaDoc high2 = getOrder(o2);
116                         if (high1.equals(high2)) {
117                             if (o1 instanceof PortalObject && o2 instanceof PortalObject) {
118                                 String JavaDoc name1 = ((PortalObject) o1).getName();
119                                 String JavaDoc name2 = ((PortalObject) o2).getName();
120                                 if (name1.charAt(0) == '[' && name1.charAt(3) == ']' && name2.charAt(0) == '[' && name2.charAt(3) == ']') {
121                                     String JavaDoc idx1 = name1.substring(1, 3);
122                                     String JavaDoc idx2 = name2.substring(1, 3);
123                                     return idx1.compareTo(idx2);
124                                 } else {
125                                     return ((PortalObject) o2).getName().compareTo(((PortalObject) o2).getName());
126                                 }
127                             } else if (o1 instanceof PortalNode && o2 instanceof PortalNode) {
128                                 String JavaDoc name1 = ((PortalNode) o1).getName();
129                                 String JavaDoc name2 = ((PortalNode) o2).getName();
130                                 if (name1.charAt(0) == '[' && name1.charAt(3) == ']' && name2.charAt(0) == '[' && name2.charAt(3) == ']') {
131                                     String JavaDoc idx1 = name1.substring(1, 3);
132                                     String JavaDoc idx2 = name2.substring(1, 3);
133                                     return idx1.compareTo(idx2);
134                                 } else {
135                                     return ((PortalNode) o2).getName().compareTo(((PortalNode) o2).getName());
136                                 }
137                             } else {
138                                 return 0;
139                             }
140                         } else {
141                             return high1.compareTo(high2);
142                         }
143                     }
144
145                 });
146
147                 for (int pageNo = 0; pageNo < pages.size(); pageNo++) {
148                     PortalNode pageNode = (PortalNode) pages.get(pageNo);
149                     String JavaDoc pageName = pageNode.getName().replace(' ', '_');
150                     if (pageNode.equals(thisPage)) {
151                         content.append("<span class=\"active\">\n"); //$NON-NLS-1$
152
}
153                     content.append("<a HREF=\"") //$NON-NLS-1$
154
.append(resp.createRenderURL(pageNode).toString()).append("\" id=\"page_") //$NON-NLS-1$
155
.append(pageName).append("\">") //$NON-NLS-1$
156
.append(getDisplayName(pageNode.getName())).append("</a>\n"); //$NON-NLS-1$
157
if (pageNode.equals(thisPage)) {
158                         content.append("</span>\n"); //$NON-NLS-1$
159
}
160                 }
161
162                 // get the top level
163
PortalNode top = current.getParent();
164                 boolean doPortalList = false;
165                 if (top != null && doPortalsMenu) {
166
167                     Iterator JavaDoc portalIterator = top.getChildren().iterator();
168                     for (; portalIterator.hasNext();) {
169                         PortalNode pageNode = (PortalNode) portalIterator.next();
170                             if( pageNode instanceof JBossPortalNode && allowed( (JBossPortalNode) pageNode, secured ) ) {
171                                 doPortalList = true;
172                                 break;
173                             }
174                     }
175
176                     if (doPortalList) {
177                         content.append("<span class=\"portals_menu\">\n<a HREF=\"javascript:void\" id=\"portal_menu") //$NON-NLS-1$
178
.append("\">") //$NON-NLS-1$
179
.append(Messages.getString("UI.USER_PORTALS")) //$NON-NLS-1$
180
.append("</a></span>\n\n"); //$NON-NLS-1$
181
}
182                 }
183
184                 doTransMenu |= doPortalList;
185                 content.append("</div>\n"); //$NON-NLS-1$
186
if (doTransMenu) {
187                     content.append("<script type=\"text/javascript\" language=\"javascript\">\n") //$NON-NLS-1$
188
.append("//<![CDATA[ \n") //$NON-NLS-1$
189

190                             // create the init function
191
.append("function initMenu() {\n") //$NON-NLS-1$
192
.append(" if (TransMenu.isSupported()) {\n") //$NON-NLS-1$
193
.append(" TransMenu.initialize();\n"); //$NON-NLS-1$
194

195                     for (int pageNo = 0; pageNo < pages.size(); pageNo++) {
196                         PortalNode pageNode = (PortalNode) pages.get(pageNo);
197                         // loop for every page
198

199                         if (pageNode.getChildren().size() > 0) {
200                             Iterator JavaDoc children = pageNode.getChildren().iterator();
201                             String JavaDoc pageName = pageNode.getName().replace(' ', '_');
202                             boolean done = false;
203                             for (; children.hasNext();) {
204                                 PortalNode subPageNode = (PortalNode) children.next();
205                                     if( subPageNode instanceof JBossPortalNode && allowed( (JBossPortalNode) subPageNode, secured ) ) {
206                                         if (subPageNode.getType() == PortalNode.TYPE_PAGE) {
207                                             // has sub-pages
208
content.append(" menu") //$NON-NLS-1$
209
.append(pageNo).append(".onactivate = function() { document.getElementById(\"page_") //$NON-NLS-1$
210
.append(pageName).append("\").className = \"hover\"; };\n") //$NON-NLS-1$
211
.append(" menu") //$NON-NLS-1$
212
.append(pageNo).append(".ondeactivate = function() { document.getElementById(\"page_") //$NON-NLS-1$
213
.append(pageName).append("\").className = \"\"; };\n"); //$NON-NLS-1$
214
done = true;
215                                             break;
216                                         }
217                                     }
218                             }
219                             if (!done) {
220                                 // no sub-pages
221
content.append(" document.getElementById(\"page_") //$NON-NLS-1$
222
.append(pageName).append("\").onmouseover = function() { ms.hideCurrent(); this.className = \"hover\"; }\n") //$NON-NLS-1$
223
.append(" document.getElementById(\"page_") //$NON-NLS-1$
224
.append(pageName).append("\").onmouseout = function() { this.className = \"\"; } \n"); //$NON-NLS-1$
225
}
226                         }
227
228                     }
229                     if (doPortalList) {
230                         content.append(" menu_portals.onactivate = function() { document.getElementById(\"portal_menu\").className = \"hover\"; };\n") //$NON-NLS-1$
231
.append(" menu_portals.ondeactivate = function() { document.getElementById(\"portal_menu\").className = \"\"; };\n"); //$NON-NLS-1$
232
}
233
234                     content.append(" }\n") //$NON-NLS-1$
235
.append("}\n"); //$NON-NLS-1$
236

237                     // now create the sub-menus
238
content.append("if (TransMenu.isSupported()) {\n") //$NON-NLS-1$
239
.append(" var ms = new TransMenuSet(TransMenu.direction.down, 1, 0, TransMenu.reference.bottomLeft);\n"); //$NON-NLS-1$
240

241                     // loop for every page
242
for (int pageNo = 0; pageNo < pages.size(); pageNo++) {
243                             PortalNode pageNode = (PortalNode) pages.get(pageNo);
244                             String JavaDoc pageName = pageNode.getName().replace(' ', '_');
245                             if (pageNode.getChildren().size() > 0) {
246                                 Iterator JavaDoc children = pageNode.getChildren().iterator();
247                                 boolean firstSubPage = true;
248                                 for (; children.hasNext();) {
249                                     PortalNode subPageNode = (PortalNode) children.next();
250                                     if( subPageNode instanceof JBossPortalNode && allowed( (JBossPortalNode) subPageNode, secured ) ) {
251                                         if (subPageNode.getType() == PortalNode.TYPE_PAGE) {
252                                             // has sub-pages
253
if (firstSubPage) {
254                                                 content.append(" var menu") //$NON-NLS-1$
255
.append(pageNo).append(" = ms.addMenu(document.getElementById(\"page_") //$NON-NLS-1$
256
.append(pageName).append("\"));\n"); //$NON-NLS-1$
257
firstSubPage = false;
258                                             }
259                                             content.append(" menu") //$NON-NLS-1$
260
.append(pageNo).append(".addItem(\"") //$NON-NLS-1$
261
.append(getDisplayName(subPageNode.getName())).append("\", \"") //$NON-NLS-1$
262
.append(resp.createRenderURL(subPageNode).toString()).append("\", \"0\");\n"); //$NON-NLS-1$
263
}
264                                     }
265
266                                 }
267                             }
268                         }
269
270                     if (doPortalList) {
271                         content.append(" var menu_portals = ms.addMenu(document.getElementById(\"portal_menu\"));\n"); //$NON-NLS-1$
272
Iterator JavaDoc portals = top.getChildren().iterator();
273                         for (; portals.hasNext();) {
274                             PortalNode portalNode = (PortalNode) portals.next();
275                             if (!portalNode.equals(current) ) {
276                                 if (portalNode instanceof JBossPortalNode && allowed( (JBossPortalNode) portalNode, secured ) ) {
277                                     // get the default page node for this portal
278
// to get a valid URL from it
279

280                                     JBossPortalNode jbossNode = (JBossPortalNode) portalNode;
281                                     PortalNode a = null;
282                                     // TODO make these options configurable from
283
// the objects.xml file
284
a = jbossNode.resolve("default"); //$NON-NLS-1$
285
if (a == null) {
286                                         a = jbossNode.resolve("Default"); //$NON-NLS-1$
287
}
288                                     if (a == null) {
289                                         a = jbossNode.resolve("Home"); //$NON-NLS-1$
290
}
291                                     if (a == null) {
292                                         a = jbossNode.resolve("home"); //$NON-NLS-1$
293
}
294                                     if (a == null) {
295                                         if (jbossNode.getChildren().size() > 0) {
296                                             PortalNode child = (PortalNode) jbossNode.getChildren().iterator().next();
297                                             a = jbossNode.resolve(child.getName());
298                                         }
299                                     }
300                                     if (a != null) {
301
302                                         content.append(" menu_portals.addItem(\"") //$NON-NLS-1$
303
.append(getDisplayName(portalNode.getName())).append("\", \"") //$NON-NLS-1$
304
.append(resp.createRenderURL(a).toString()).append("\", \"0\");\n"); //$NON-NLS-1$
305

306                                     }
307                                 }
308                             }
309                         }
310                     }
311
312                     content.append(" TransMenu.renderAll();\n") //$NON-NLS-1$
313
.append("}\n") //$NON-NLS-1$
314
.append("initFunctions.push(\"initMenu()\");") //$NON-NLS-1$
315
.append("// ]]>\n") //$NON-NLS-1$
316
.append("</script>\n"); //$NON-NLS-1$
317

318                 }
319                 content.append("</div>\n"); //$NON-NLS-1$
320

321             }
322
323         } catch (Exception JavaDoc e) {
324             e.printStackTrace();
325         }
326         resp.getWriter().print(content.toString());
327
328     }
329
330     private String JavaDoc getDisplayName(String JavaDoc name) {
331         if (name.length() > 4 && name.charAt(0) == '[' && name.charAt(3) == ']') {
332             return name.substring(4);
333         } else {
334             return name;
335         }
336     }
337
338     private boolean allowed(JBossPortalNode node, boolean secured ) throws PortalPolicyException {
339
340             if( !secured ) {
341                 return true;
342             }
343             if( node == null || permissionFactory == null ) {
344                 return false;
345             }
346        return permissionFactory.checkPermission("portalobject", node.getHandle(), "view"); //$NON-NLS-1$ //$NON-NLS-2$
347

348     }
349     
350     /*
351      * private String getNavElementName(PortalObjectImpl navElement,
352      * JBossRenderRequest request) { ResourceBundle resourceBundle =
353      * getPortletConfig().getResourceBundle(request.getLocale()); String
354      * resourceKey = navElement.getDeclaredProperty(RESOURCE_KEY); try { return
355      * resourceBundle.getString(resourceKey); } catch (Exception e) { return
356      * navElement.getName(); } }
357      *
358      * private void sortTabs(List navElements) { Collections.sort(navElements,
359      * new Comparator() { public int compare(Object o1, Object o2) { Float high1 =
360      * getOrder(o1); Float high2 = getOrder(o2); if (high1.equals(high2)) {
361      * String name1 = ((PortalObject) o1).getName(); String name2 =
362      * ((PortalObject) o1).getName(); if( name1.charAt( 0 ) == '[' &&
363      * name1.charAt( 3 ) == ']' && name2.charAt( 0 ) == '[' && name2.charAt( 3 ) ==
364      * ']' ) { String idx1 = name1.substring( 1,2 ); String idx2 =
365      * name2.substring( 1,2 ); return idx1.compareTo( idx2 ); } else { return
366      * ((PortalObject) o2).getName().compareTo(((PortalObject) o2).getName()); } }
367      * else { return high1.compareTo(high2); } } }); }
368      */

369     protected Float JavaDoc getOrder(Object JavaDoc o) {
370         if (o instanceof Context) {
371             return new Float JavaDoc(0);
372         }
373         if (o instanceof Portal) {
374             return new Float JavaDoc(1);
375         }
376         if (o instanceof Page) {
377             String JavaDoc orderProperty = ((Page) o).getDeclaredProperty(ORDER);
378             if (orderProperty == null) {
379                 return new Float JavaDoc(2.999f);
380             }
381             try {
382                 return new Float JavaDoc("2." + orderProperty); //$NON-NLS-1$
383
} catch (NumberFormatException JavaDoc e) {
384                 return new Float JavaDoc(2.999f);
385             }
386         }
387         if (o instanceof PortalObject) {
388             String JavaDoc orderProperty = ((PortalObject) o).getDeclaredProperty(ORDER);
389             if (orderProperty == null) {
390                 orderProperty = ((PortalObject) o).getProperty(ORDER);
391             }
392             if (orderProperty == null) {
393                 return new Float JavaDoc(2.999f);
394             }
395             try {
396                 return new Float JavaDoc("2." + orderProperty); //$NON-NLS-1$
397
} catch (NumberFormatException JavaDoc e) {
398                 return new Float JavaDoc(2.999f);
399             }
400         }
401         if (o instanceof Window) {
402             return new Float JavaDoc(3);
403         }
404         return new Float JavaDoc(4);
405     }
406 }
407
Popular Tags