1   /**
2    * Copyright (c) 2000-2009 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.NoSuchLayoutException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.JavaConstants;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.Layout;
34  import com.liferay.portal.model.Portlet;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.service.GroupLocalServiceUtil;
37  import com.liferay.portal.service.LayoutLocalServiceUtil;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.WebKeys;
40  import com.liferay.portlet.PortletConfigFactory;
41  import com.liferay.portlet.PortletPreferencesFactoryUtil;
42  import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
43  
44  import java.util.ResourceBundle;
45  
46  import javax.portlet.ActionRequest;
47  import javax.portlet.ActionResponse;
48  import javax.portlet.PortletConfig;
49  import javax.portlet.PortletPreferences;
50  import javax.portlet.PortletRequest;
51  import javax.portlet.RenderRequest;
52  import javax.portlet.RenderResponse;
53  
54  import javax.servlet.ServletContext;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="EditScopeAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Jesper Weissglas
64   * @author Jorge Ferrer
65   */
66  public class EditScopeAction extends EditConfigurationAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              ActionRequest actionRequest, ActionResponse actionResponse)
71          throws Exception {
72  
73          Portlet portlet = null;
74  
75          try {
76              portlet = getPortlet(actionRequest);
77          }
78          catch (PrincipalException pe) {
79              SessionErrors.add(
80                  actionRequest, PrincipalException.class.getName());
81  
82              setForward(actionRequest, "portlet.portlet_configuration.error");
83          }
84  
85          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
86  
87          if (cmd.equals(Constants.SAVE)) {
88              updateScope(actionRequest, portlet);
89  
90              sendRedirect(actionRequest, actionResponse);
91          }
92      }
93  
94      public ActionForward render(
95              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
96              RenderRequest renderRequest, RenderResponse renderResponse)
97          throws Exception {
98  
99          Portlet portlet = null;
100 
101         try {
102             portlet = getPortlet(renderRequest);
103         }
104         catch (PrincipalException pe) {
105             SessionErrors.add(
106                 renderRequest, PrincipalException.class.getName());
107 
108             return mapping.findForward("portlet.portlet_configuration.error");
109         }
110 
111         renderResponse.setTitle(getTitle(portlet, renderRequest));
112 
113         return mapping.findForward(getForward(
114             renderRequest, "portlet.portlet_configuration.edit_scope"));
115     }
116 
117     protected String getPortletTitle(
118         PortletRequest portletRequest, Portlet portlet,
119         PortletPreferences preferences) {
120 
121         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
122             WebKeys.THEME_DISPLAY);
123 
124         String portletTitle = PortletConfigurationUtil.getPortletTitle(
125             preferences, themeDisplay.getLanguageId());
126 
127         if (Validator.isNull(portletTitle)) {
128             ServletContext servletContext =
129                 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
130 
131             PortletConfig portletConfig = PortletConfigFactory.create(
132                 portlet, servletContext);
133 
134             ResourceBundle resourceBundle = portletConfig.getResourceBundle(
135                 themeDisplay.getLocale());
136 
137             portletTitle = resourceBundle.getString(
138                 JavaConstants.JAVAX_PORTLET_TITLE);
139         }
140 
141         return portletTitle;
142     }
143 
144     protected void updateScope(ActionRequest actionRequest, Portlet portlet)
145         throws Exception {
146 
147         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
148             WebKeys.THEME_DISPLAY);
149 
150         Layout layout = themeDisplay.getLayout();
151 
152         PortletPreferences preferences =
153             PortletPreferencesFactoryUtil.getLayoutPortletSetup(
154                 layout, portlet.getPortletId());
155 
156         long scopeLayoutId = ParamUtil.getLong(actionRequest, "scopeLayoutId");
157         long oldScopeLayoutId = GetterUtil.getLong(
158             preferences.getValue("lfr-scope-layout-id", null));
159         String title = getPortletTitle(actionRequest, portlet, preferences);
160         String newTitle = title;
161 
162         // Remove old scope suffix from the title if present
163 
164         if (oldScopeLayoutId > 0) {
165             try {
166                 Layout oldScopeLayout = LayoutLocalServiceUtil.getLayout(
167                     layout.getGroupId(), layout.isPrivateLayout(),
168                     oldScopeLayoutId);
169 
170                 StringBuilder sb = new StringBuilder();
171 
172                 sb.append(StringPool.SPACE);
173                 sb.append(StringPool.OPEN_PARENTHESIS);
174                 sb.append(oldScopeLayout.getName(themeDisplay.getLocale()));
175                 sb.append(StringPool.CLOSE_PARENTHESIS);
176 
177                 String suffix = sb.toString();
178 
179                 if (newTitle.endsWith(suffix)) {
180                     newTitle = newTitle.substring(
181                         0, title.length() - suffix.length());
182                 }
183             }
184             catch (NoSuchLayoutException nsle) {
185             }
186         }
187 
188         // Add new scope suffix to the title
189 
190         if (scopeLayoutId > 0) {
191             Layout scopeLayout = LayoutLocalServiceUtil.getLayout(
192                 layout.getGroupId(), layout.isPrivateLayout(), scopeLayoutId);
193 
194             if (!scopeLayout.hasScopeGroup()) {
195                 String name = String.valueOf(scopeLayout.getPlid());
196 
197                 GroupLocalServiceUtil.addGroup(
198                     themeDisplay.getUserId(), Layout.class.getName(),
199                     scopeLayout.getPlid(), name, null, 0, null, true, null);
200             }
201 
202             StringBuilder sb = new StringBuilder();
203 
204             sb.append(newTitle);
205             sb.append(StringPool.SPACE);
206             sb.append(StringPool.OPEN_PARENTHESIS);
207             sb.append(scopeLayout.getName(themeDisplay.getLocale()));
208             sb.append(StringPool.CLOSE_PARENTHESIS);
209 
210             newTitle = sb.toString();
211         }
212 
213         preferences.setValue(
214             "lfr-scope-layout-id", String.valueOf(scopeLayoutId));
215 
216         if (!newTitle.equals(title)) {
217             preferences.setValue(
218                 "portlet-setup-title-" + themeDisplay.getLanguageId(),
219                 newTitle);
220             preferences.setValue("portlet-setup-use-custom-title", "true");
221         }
222 
223         preferences.store();
224     }
225 
226 }