001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.layout.impl;
018:
019: import java.util.Locale;
020: import java.util.Map;
021: import java.util.HashMap;
022: import java.util.Set;
023: import java.util.Iterator;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.jetspeed.JetspeedActions;
028: import org.apache.jetspeed.ajax.AjaxAction;
029: import org.apache.jetspeed.ajax.AjaxBuilder;
030: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
031: import org.apache.jetspeed.page.document.NodeNotFoundException;
032: import org.apache.jetspeed.portalsite.Menu;
033: import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
034: import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
035: import org.apache.jetspeed.request.RequestContext;
036:
037: /**
038: * Get menus action retrieves all menu names defined for the addressed page.
039: *
040: * AJAX Parameters:
041: * none
042: *
043: * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
044: * @version $Id: $
045: */
046: public class GetMenusAction extends BasePortletAction implements
047: AjaxAction, AjaxBuilder, Constants {
048: protected static final Log log = LogFactory
049: .getLog(GetMenusAction.class);
050:
051: public GetMenusAction(String template, String errorTemplate,
052: PortletActionSecurityBehavior securityBehavior) {
053: super (template, errorTemplate, securityBehavior);
054: }
055:
056: public boolean run(RequestContext requestContext, Map resultMap) {
057: boolean success = true;
058: String status = "success";
059: try {
060: // generate action result
061: resultMap.put(ACTION, "getmenus");
062:
063: // check permission to use ajax api
064: if (!checkAccess(requestContext, JetspeedActions.VIEW)) {
065: success = false;
066: resultMap.put(REASON,
067: "Insufficient access to get menus");
068: return success;
069: }
070:
071: // get request context
072: PortalSiteRequestContext siteRequestContext = (PortalSiteRequestContext) requestContext
073: .getAttribute(ProfilerValveImpl.PORTAL_SITE_REQUEST_CONTEXT_ATTR_KEY);
074: if (siteRequestContext == null) {
075: success = false;
076: resultMap
077: .put(REASON,
078: "Missing portal site request context from ProfilerValve");
079: return success;
080: }
081:
082: // get menu names
083: Set standardMenuNames = siteRequestContext
084: .getStandardMenuNames();
085: Set customMenuNames = null;
086: try {
087: customMenuNames = siteRequestContext
088: .getCustomMenuNames();
089: } catch (NodeNotFoundException nnfe) {
090: }
091:
092: // return menu names action results
093: resultMap.put(STANDARD_MENUS, standardMenuNames);
094: resultMap.put(CUSTOM_MENUS, customMenuNames);
095:
096: // get action parameter
097: String includeMenuDefinitions = getActionParameter(
098: requestContext, INCLUDE_MENU_DEFS);
099: if (includeMenuDefinitions != null
100: && includeMenuDefinitions.toLowerCase().equals(
101: "true")) {
102: // get request locale
103: Locale locale = requestContext.getLocale();
104:
105: HashMap menuDefinitionsMap = new HashMap();
106:
107: StringBuffer failReason = new StringBuffer();
108: Iterator menuNamesIter = standardMenuNames.iterator();
109: while (menuNamesIter.hasNext()) {
110: String menuName = (String) menuNamesIter.next();
111: Menu menuDefinition = getMenuDefinition(menuName,
112: siteRequestContext, failReason);
113: if (menuDefinition != null)
114: menuDefinitionsMap
115: .put(menuName, menuDefinition);
116: }
117: menuNamesIter = customMenuNames.iterator();
118: while (menuNamesIter.hasNext()) {
119: String menuName = (String) menuNamesIter.next();
120: Menu menuDefinition = getMenuDefinition(menuName,
121: siteRequestContext, failReason);
122: if (menuDefinition != null)
123: menuDefinitionsMap
124: .put(menuName, menuDefinition);
125: }
126:
127: if (failReason.length() > 0) {
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: } else {
137: resultMap.put(INCLUDE_MENU_DEFS, new Boolean(false));
138: }
139: resultMap.put(STATUS, status);
140: } catch (Exception e) {
141: log.error("Exception while getting page menus info", e);
142: success = false;
143: }
144:
145: return success;
146: }
147:
148: private Menu getMenuDefinition(String menuName,
149: PortalSiteRequestContext siteRequestContext,
150: StringBuffer failReason) {
151: // get menu definition
152: Menu menuDefinition = null;
153: try {
154: menuDefinition = siteRequestContext.getMenu(menuName);
155: } catch (NodeNotFoundException nnfe) {
156: }
157: if (menuDefinition == null && failReason != null) {
158: if (failReason.length() == 0)
159: failReason.append("Unable to lookup specified menus: ")
160: .append(menuName);
161: else
162: failReason.append(", ").append(menuName);
163: }
164: return menuDefinition;
165: }
166: }
|