1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.layout.impl;
18
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.HashMap;
22 import java.util.Set;
23 import java.util.Iterator;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.jetspeed.JetspeedActions;
28 import org.apache.jetspeed.ajax.AjaxAction;
29 import org.apache.jetspeed.ajax.AjaxBuilder;
30 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
31 import org.apache.jetspeed.page.document.NodeNotFoundException;
32 import org.apache.jetspeed.portalsite.Menu;
33 import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
34 import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
35 import org.apache.jetspeed.request.RequestContext;
36
37 /***
38 * Get menus action retrieves all menu names defined for the addressed page.
39 *
40 * AJAX Parameters:
41 * none
42 *
43 * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
44 * @version $Id: $
45 */
46 public class GetMenusAction extends BasePortletAction
47 implements AjaxAction, AjaxBuilder, Constants
48 {
49 protected static final Log log = LogFactory.getLog(GetMenusAction.class);
50
51 public GetMenusAction(String template,
52 String errorTemplate,
53 PortletActionSecurityBehavior securityBehavior)
54 {
55 super(template, errorTemplate, securityBehavior);
56 }
57
58 public boolean run(RequestContext requestContext, Map resultMap)
59 {
60 boolean success = true;
61 String status = "success";
62 try
63 {
64
65 resultMap.put(ACTION, "getmenus");
66
67
68 if (!checkAccess(requestContext, JetspeedActions.VIEW))
69 {
70 success = false;
71 resultMap.put(REASON, "Insufficient access to get menus");
72 return success;
73 }
74
75
76 PortalSiteRequestContext siteRequestContext = (PortalSiteRequestContext)requestContext.getAttribute(ProfilerValveImpl.PORTAL_SITE_REQUEST_CONTEXT_ATTR_KEY);
77 if (siteRequestContext == null)
78 {
79 success = false;
80 resultMap.put(REASON, "Missing portal site request context from ProfilerValve");
81 return success;
82 }
83
84
85 Set standardMenuNames = siteRequestContext.getStandardMenuNames();
86 Set customMenuNames = null;
87 try
88 {
89 customMenuNames = siteRequestContext.getCustomMenuNames();
90 }
91 catch (NodeNotFoundException nnfe)
92 {
93 }
94
95
96 resultMap.put(STANDARD_MENUS, standardMenuNames);
97 resultMap.put(CUSTOM_MENUS, customMenuNames);
98
99
100 String includeMenuDefinitions = getActionParameter(requestContext, INCLUDE_MENU_DEFS);
101 if ( includeMenuDefinitions != null && includeMenuDefinitions.toLowerCase().equals( "true" ) )
102 {
103
104 Locale locale = requestContext.getLocale();
105
106 HashMap menuDefinitionsMap = new HashMap();
107
108 StringBuffer failReason = new StringBuffer();
109 Iterator menuNamesIter = standardMenuNames.iterator();
110 while ( menuNamesIter.hasNext() )
111 {
112 String menuName = (String)menuNamesIter.next();
113 Menu menuDefinition = getMenuDefinition( menuName, siteRequestContext, failReason );
114 if ( menuDefinition != null )
115 menuDefinitionsMap.put( menuName, menuDefinition );
116 }
117 menuNamesIter = customMenuNames.iterator();
118 while ( menuNamesIter.hasNext() )
119 {
120 String menuName = (String)menuNamesIter.next();
121 Menu menuDefinition = getMenuDefinition( menuName, siteRequestContext, failReason );
122 if ( menuDefinition != null )
123 menuDefinitionsMap.put( menuName, menuDefinition );
124 }
125
126 if ( failReason.length() > 0 )
127 {
128 success = false;
129 resultMap.put(REASON, failReason.toString() );
130 return success;
131 }
132 resultMap.put( INCLUDE_MENU_DEFS, new Boolean( true ) );
133 resultMap.put( MENU_DEFINITIONS, menuDefinitionsMap );
134 resultMap.put( MENU_CONTEXT, siteRequestContext );
135 resultMap.put( MENU_LOCALE, locale );
136 }
137 else
138 {
139 resultMap.put( INCLUDE_MENU_DEFS, new Boolean( false ) );
140 }
141 resultMap.put(STATUS, status);
142 }
143 catch (Exception e)
144 {
145 log.error("Exception while getting page menus info", e);
146 success = false;
147 }
148
149 return success;
150 }
151
152 private Menu getMenuDefinition( String menuName, PortalSiteRequestContext siteRequestContext, StringBuffer failReason )
153 {
154
155 Menu menuDefinition = null;
156 try
157 {
158 menuDefinition = siteRequestContext.getMenu( menuName );
159 }
160 catch ( NodeNotFoundException nnfe )
161 {
162 }
163 if ( menuDefinition == null && failReason != null )
164 {
165 if ( failReason.length() == 0 )
166 failReason.append( "Unable to lookup specified menus: " ).append( menuName );
167 else
168 failReason.append( ", " ).append( menuName );
169 }
170 return menuDefinition;
171 }
172 }