1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.portletconfiguration.action;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.ConfigurationAction;
25  import com.liferay.portal.kernel.servlet.SessionErrors;
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  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.PortletPreferences;
45  import javax.portlet.PortletRequest;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import javax.servlet.ServletContext;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="EditConfigurationAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class EditConfigurationAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              ActionRequest actionRequest, ActionResponse actionResponse)
66          throws Exception {
67  
68          Portlet portlet = null;
69  
70          try {
71              portlet = getPortlet(actionRequest);
72          }
73          catch (PrincipalException pe) {
74              SessionErrors.add(
75                  actionRequest, PrincipalException.class.getName());
76  
77              setForward(actionRequest, "portlet.portlet_configuration.error");
78          }
79  
80          ConfigurationAction configurationAction = getConfigurationAction(
81              portlet);
82  
83          if (configurationAction != null) {
84              configurationAction.processAction(
85                  portletConfig, actionRequest, actionResponse);
86          }
87      }
88  
89      public ActionForward render(
90              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
91              RenderRequest renderRequest, RenderResponse renderResponse)
92          throws Exception {
93  
94          Portlet portlet = null;
95  
96          try {
97              portlet = getPortlet(renderRequest);
98          }
99          catch (PrincipalException pe) {
100             SessionErrors.add(
101                 renderRequest, PrincipalException.class.getName());
102 
103             return mapping.findForward("portlet.portlet_configuration.error");
104         }
105 
106         renderResponse.setTitle(getTitle(portlet, renderRequest));
107 
108         ConfigurationAction configurationAction = getConfigurationAction(
109             portlet);
110 
111         if (configurationAction != null) {
112             String path = configurationAction.render(
113                 portletConfig, renderRequest, renderResponse);
114 
115             if (_log.isDebugEnabled()) {
116                 _log.debug("Configuration action returned render path " + path);
117             }
118 
119             if (Validator.isNotNull(path)) {
120                 renderRequest.setAttribute(
121                     WebKeys.CONFIGURATION_ACTION_PATH, path);
122             }
123             else {
124                 _log.error("Configuration action returned a null path");
125             }
126         }
127 
128         return mapping.findForward(getForward(
129             renderRequest, "portlet.portlet_configuration.edit_configuration"));
130     }
131 
132     protected ConfigurationAction getConfigurationAction(Portlet portlet)
133         throws Exception {
134 
135         ConfigurationAction configurationAction =
136             portlet.getConfigurationActionInstance();
137 
138         if (configurationAction == null) {
139             _log.error(
140                 "Configuration action for portlet " + portlet.getPortletId() +
141                     " is null");
142         }
143 
144         return configurationAction;
145     }
146 
147     protected Portlet getPortlet(PortletRequest portletRequest)
148         throws Exception {
149 
150         long companyId = PortalUtil.getCompanyId(portletRequest);
151 
152         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
153             WebKeys.THEME_DISPLAY);
154 
155         PermissionChecker permissionChecker =
156             themeDisplay.getPermissionChecker();
157 
158         String portletId = ParamUtil.getString(
159             portletRequest, "portletResource");
160 
161         if (!PortletPermissionUtil.contains(
162                 permissionChecker, themeDisplay.getPlid(), portletId,
163                 ActionKeys.CONFIGURATION)) {
164 
165             throw new PrincipalException();
166         }
167 
168         return PortletLocalServiceUtil.getPortletById(companyId, portletId);
169     }
170 
171     protected String getTitle(Portlet portlet, RenderRequest renderRequest)
172         throws Exception {
173 
174         ServletContext servletContext =
175             (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
176 
177         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
178             WebKeys.THEME_DISPLAY);
179 
180         PortletPreferences portletSetup =
181             PortletPreferencesFactoryUtil.getPortletSetup(
182                 renderRequest, portlet.getPortletId());
183 
184         String title = PortletConfigurationUtil.getPortletTitle(
185             portletSetup, themeDisplay.getLanguageId());
186 
187         if (Validator.isNull(title)) {
188             title = PortalUtil.getPortletTitle(
189                 portlet, servletContext, themeDisplay.getLocale());
190         }
191 
192         return title;
193     }
194 
195     private static Log _log =
196          LogFactoryUtil.getLog(EditConfigurationAction.class);
197 
198 }