1
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
67 public class EditScopeAction extends EditConfigurationAction {
68
69 public void processAction(
70 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71 ActionRequest actionRequest, ActionResponse actionResponse)
72 throws Exception {
73
74 Portlet portlet = null;
75
76 try {
77 portlet = getPortlet(actionRequest);
78 }
79 catch (PrincipalException pe) {
80 SessionErrors.add(
81 actionRequest, PrincipalException.class.getName());
82
83 setForward(actionRequest, "portlet.portlet_configuration.error");
84 }
85
86 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
87
88 if (cmd.equals(Constants.SAVE)) {
89 updateScope(actionRequest, portlet);
90
91 sendRedirect(actionRequest, actionResponse);
92 }
93 }
94
95 public ActionForward render(
96 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
97 RenderRequest renderRequest, RenderResponse renderResponse)
98 throws Exception {
99
100 Portlet portlet = null;
101
102 try {
103 portlet = getPortlet(renderRequest);
104 }
105 catch (PrincipalException pe) {
106 SessionErrors.add(
107 renderRequest, PrincipalException.class.getName());
108
109 return mapping.findForward("portlet.portlet_configuration.error");
110 }
111
112 renderResponse.setTitle(getTitle(portlet, renderRequest));
113
114 return mapping.findForward(getForward(
115 renderRequest, "portlet.portlet_configuration.edit_scope"));
116 }
117
118 protected String getPortletTitle(
119 PortletRequest portletRequest, Portlet portlet,
120 PortletPreferences preferences) {
121
122 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
123 WebKeys.THEME_DISPLAY);
124
125 String portletTitle = PortletConfigurationUtil.getPortletTitle(
126 preferences, themeDisplay.getLanguageId());
127
128 if (Validator.isNull(portletTitle)) {
129 ServletContext servletContext =
130 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
131
132 PortletConfig portletConfig = PortletConfigFactory.create(
133 portlet, servletContext);
134
135 ResourceBundle resourceBundle = portletConfig.getResourceBundle(
136 themeDisplay.getLocale());
137
138 portletTitle = resourceBundle.getString(
139 JavaConstants.JAVAX_PORTLET_TITLE);
140 }
141
142 return portletTitle;
143 }
144
145 protected void updateScope(ActionRequest actionRequest, Portlet portlet)
146 throws Exception {
147
148 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
149 WebKeys.THEME_DISPLAY);
150
151 Layout layout = themeDisplay.getLayout();
152
153 PortletPreferences preferences =
154 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
155 layout, portlet.getPortletId());
156
157 long scopeLayoutId = ParamUtil.getLong(actionRequest, "scopeLayoutId");
158 long oldScopeLayoutId = GetterUtil.getLong(
159 preferences.getValue("lfr-scope-layout-id", null));
160 String title = getPortletTitle(actionRequest, portlet, preferences);
161 String newTitle = title;
162
163
165 if (oldScopeLayoutId > 0) {
166 try {
167 Layout oldScopeLayout = LayoutLocalServiceUtil.getLayout(
168 layout.getGroupId(), layout.isPrivateLayout(),
169 oldScopeLayoutId);
170
171 StringBuilder sb = new StringBuilder();
172
173 sb.append(StringPool.SPACE);
174 sb.append(StringPool.OPEN_PARENTHESIS);
175 sb.append(oldScopeLayout.getName(themeDisplay.getLocale()));
176 sb.append(StringPool.CLOSE_PARENTHESIS);
177
178 String suffix = sb.toString();
179
180 if (newTitle.endsWith(suffix)) {
181 newTitle = newTitle.substring(
182 0, title.length() - suffix.length());
183 }
184 }
185 catch (NoSuchLayoutException nsle) {
186 }
187 }
188
189
191 if (scopeLayoutId > 0) {
192 Layout scopeLayout = LayoutLocalServiceUtil.getLayout(
193 layout.getGroupId(), layout.isPrivateLayout(), scopeLayoutId);
194
195 if (!scopeLayout.hasScopeGroup()) {
196 String name = String.valueOf(scopeLayout.getPlid());
197
198 GroupLocalServiceUtil.addGroup(
199 themeDisplay.getUserId(), Layout.class.getName(),
200 scopeLayout.getPlid(), name, null, 0, null, true, null);
201 }
202
203 StringBuilder sb = new StringBuilder();
204
205 sb.append(newTitle);
206 sb.append(StringPool.SPACE);
207 sb.append(StringPool.OPEN_PARENTHESIS);
208 sb.append(scopeLayout.getName(themeDisplay.getLocale()));
209 sb.append(StringPool.CLOSE_PARENTHESIS);
210
211 newTitle = sb.toString();
212 }
213
214 preferences.setValue(
215 "lfr-scope-layout-id", String.valueOf(scopeLayoutId));
216
217 if (!newTitle.equals(title)) {
218 preferences.setValue(
219 "portlet-setup-title-" + themeDisplay.getLanguageId(),
220 newTitle);
221 preferences.setValue("portlet-setup-use-custom-title", "true");
222 }
223
224 preferences.store();
225 }
226
227 }