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