001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.portletconfiguration.action;
022:
023: import com.liferay.portal.kernel.portlet.ConfigurationAction;
024: import com.liferay.portal.kernel.security.permission.ActionKeys;
025: import com.liferay.portal.kernel.security.permission.PermissionChecker;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.Portlet;
029: import com.liferay.portal.security.auth.PrincipalException;
030: import com.liferay.portal.service.PortletLocalServiceUtil;
031: import com.liferay.portal.service.permission.PortletPermissionUtil;
032: import com.liferay.portal.struts.PortletAction;
033: import com.liferay.portal.theme.ThemeDisplay;
034: import com.liferay.portal.util.PortalUtil;
035: import com.liferay.portal.util.WebKeys;
036: import com.liferay.portlet.PortletPreferencesFactoryUtil;
037: import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
038: import com.liferay.util.servlet.SessionErrors;
039:
040: import javax.portlet.ActionRequest;
041: import javax.portlet.ActionResponse;
042: import javax.portlet.PortletConfig;
043: import javax.portlet.PortletPreferences;
044: import javax.portlet.PortletRequest;
045: import javax.portlet.RenderRequest;
046: import javax.portlet.RenderResponse;
047:
048: import javax.servlet.ServletContext;
049:
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052: import org.apache.struts.action.ActionForm;
053: import org.apache.struts.action.ActionForward;
054: import org.apache.struts.action.ActionMapping;
055:
056: /**
057: * <a href="EditConfigurationAction.java.html"><b><i>View Source</i></b></a>
058: *
059: * @author Brian Wing Shun Chan
060: *
061: */
062: public class EditConfigurationAction extends PortletAction {
063:
064: public void processAction(ActionMapping mapping, ActionForm form,
065: PortletConfig config, ActionRequest req, ActionResponse res)
066: throws Exception {
067:
068: Portlet portlet = null;
069:
070: try {
071: portlet = getPortlet(req);
072: } catch (PrincipalException pe) {
073: SessionErrors.add(req, PrincipalException.class.getName());
074:
075: setForward(req, "portlet.portlet_configuration.error");
076: }
077:
078: ConfigurationAction configurationAction = getConfigurationAction(portlet);
079:
080: if (configurationAction != null) {
081: configurationAction.processAction(config, req, res);
082: }
083: }
084:
085: public ActionForward render(ActionMapping mapping, ActionForm form,
086: PortletConfig config, RenderRequest req, RenderResponse res)
087: throws Exception {
088:
089: Portlet portlet = null;
090:
091: try {
092: portlet = getPortlet(req);
093: } catch (PrincipalException pe) {
094: SessionErrors.add(req, PrincipalException.class.getName());
095:
096: return mapping
097: .findForward("portlet.portlet_configuration.error");
098: }
099:
100: res.setTitle(getTitle(portlet, req));
101:
102: ConfigurationAction configurationAction = getConfigurationAction(portlet);
103:
104: if (configurationAction != null) {
105: String path = configurationAction.render(config, req, res);
106:
107: if (_log.isDebugEnabled()) {
108: _log.debug("Configuration action returned render path "
109: + path);
110: }
111:
112: if (Validator.isNotNull(path)) {
113: req.setAttribute(WebKeys.CONFIGURATION_ACTION_PATH,
114: path);
115: } else {
116: _log.error("Configuration action returned a null path");
117: }
118: }
119:
120: return mapping.findForward(getForward(req,
121: "portlet.portlet_configuration.edit_configuration"));
122: }
123:
124: protected ConfigurationAction getConfigurationAction(Portlet portlet)
125: throws Exception {
126:
127: ConfigurationAction configurationAction = portlet
128: .getConfigurationActionInstance();
129:
130: if (configurationAction == null) {
131: _log.error("Configuration action for portlet "
132: + portlet.getPortletId() + " is null");
133: }
134:
135: return configurationAction;
136: }
137:
138: protected Portlet getPortlet(PortletRequest req) throws Exception {
139: long companyId = PortalUtil.getCompanyId(req);
140:
141: ThemeDisplay themeDisplay = (ThemeDisplay) req
142: .getAttribute(WebKeys.THEME_DISPLAY);
143:
144: PermissionChecker permissionChecker = themeDisplay
145: .getPermissionChecker();
146:
147: String portletId = ParamUtil.getString(req, "portletResource");
148:
149: if (!PortletPermissionUtil.contains(permissionChecker,
150: themeDisplay.getPlid(), portletId,
151: ActionKeys.CONFIGURATION)) {
152:
153: throw new PrincipalException();
154: }
155:
156: return PortletLocalServiceUtil.getPortletById(companyId,
157: portletId);
158: }
159:
160: protected String getTitle(Portlet portlet, RenderRequest req)
161: throws Exception {
162:
163: ServletContext ctx = (ServletContext) req
164: .getAttribute(WebKeys.CTX);
165:
166: ThemeDisplay themeDisplay = (ThemeDisplay) req
167: .getAttribute(WebKeys.THEME_DISPLAY);
168:
169: PortletPreferences portletSetup = PortletPreferencesFactoryUtil
170: .getPortletSetup(req, portlet.getPortletId(), true,
171: true);
172:
173: String title = PortletConfigurationUtil.getPortletTitle(
174: portletSetup, themeDisplay.getLanguageId());
175:
176: if (Validator.isNull(title)) {
177: title = PortalUtil.getPortletTitle(portlet, ctx,
178: themeDisplay.getLocale());
179: }
180:
181: return title;
182: }
183:
184: private static Log _log = LogFactory
185: .getLog(EditConfigurationAction.class);
186:
187: }
|