1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.portletconfiguration.action;
24  
25  import com.liferay.portal.kernel.portlet.ConfigurationAction;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.security.permission.ActionKeys;
31  import com.liferay.portal.security.permission.PermissionChecker;
32  import com.liferay.portal.service.PortletLocalServiceUtil;
33  import com.liferay.portal.service.permission.PortletPermissionUtil;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.PortletPreferencesFactoryUtil;
39  import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
40  import com.liferay.util.servlet.SessionErrors;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.PortletPreferences;
46  import javax.portlet.PortletRequest;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  
50  import javax.servlet.ServletContext;
51  
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="EditConfigurationAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   *
63   */
64  public class EditConfigurationAction extends PortletAction {
65  
66      public void processAction(
67              ActionMapping mapping, ActionForm form, PortletConfig config,
68              ActionRequest req, ActionResponse res)
69          throws Exception {
70  
71          Portlet portlet = null;
72  
73          try {
74              portlet = getPortlet(req);
75          }
76          catch (PrincipalException pe) {
77              SessionErrors.add(req, PrincipalException.class.getName());
78  
79              setForward(req, "portlet.portlet_configuration.error");
80          }
81  
82          ConfigurationAction configurationAction = getConfigurationAction(
83              portlet);
84  
85          if (configurationAction != null) {
86              configurationAction.processAction(config, req, res);
87          }
88      }
89  
90      public ActionForward render(
91              ActionMapping mapping, ActionForm form, PortletConfig config,
92              RenderRequest req, RenderResponse res)
93          throws Exception {
94  
95          Portlet portlet = null;
96  
97          try {
98              portlet = getPortlet(req);
99          }
100         catch (PrincipalException pe) {
101             SessionErrors.add(req, PrincipalException.class.getName());
102 
103             return mapping.findForward("portlet.portlet_configuration.error");
104         }
105 
106         res.setTitle(getTitle(portlet, req));
107 
108         ConfigurationAction configurationAction = getConfigurationAction(
109             portlet);
110 
111         if (configurationAction != null) {
112             String path = configurationAction.render(config, req, res);
113 
114             if (_log.isDebugEnabled()) {
115                 _log.debug("Configuration action returned render path " + path);
116             }
117 
118             if (Validator.isNotNull(path)) {
119                 req.setAttribute(WebKeys.CONFIGURATION_ACTION_PATH, path);
120             }
121             else {
122                 _log.error("Configuration action returned a null path");
123             }
124         }
125 
126         return mapping.findForward(getForward(
127             req, "portlet.portlet_configuration.edit_configuration"));
128     }
129 
130     protected ConfigurationAction getConfigurationAction(Portlet portlet)
131         throws Exception {
132 
133         ConfigurationAction configurationAction =
134             portlet.getConfigurationActionInstance();
135 
136         if (configurationAction == null) {
137             _log.error(
138                 "Configuration action for portlet " + portlet.getPortletId() +
139                     " is null");
140         }
141 
142         return configurationAction;
143     }
144 
145     protected Portlet getPortlet(PortletRequest req) throws Exception {
146         long companyId = PortalUtil.getCompanyId(req);
147 
148         ThemeDisplay themeDisplay =
149             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
150 
151         PermissionChecker permissionChecker =
152             themeDisplay.getPermissionChecker();
153 
154         String portletId = ParamUtil.getString(req, "portletResource");
155 
156         if (!PortletPermissionUtil.contains(
157                 permissionChecker, themeDisplay.getPlid(), portletId,
158                 ActionKeys.CONFIGURATION)) {
159 
160             throw new PrincipalException();
161         }
162 
163         return PortletLocalServiceUtil.getPortletById(companyId, portletId);
164     }
165 
166     protected String getTitle(Portlet portlet, RenderRequest req)
167         throws Exception {
168 
169         ServletContext ctx =
170             (ServletContext)req.getAttribute(WebKeys.CTX);
171 
172         ThemeDisplay themeDisplay =
173             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
174 
175         PortletPreferences portletSetup =
176             PortletPreferencesFactoryUtil.getPortletSetup(
177                 req, portlet.getPortletId());
178 
179         String title = PortletConfigurationUtil.getPortletTitle(
180             portletSetup, themeDisplay.getLanguageId());
181 
182         if (Validator.isNull(title)) {
183             title = PortalUtil.getPortletTitle(
184                 portlet, ctx, themeDisplay.getLocale());
185         }
186 
187         return title;
188     }
189 
190     private static Log _log = LogFactory.getLog(EditConfigurationAction.class);
191 
192 }