001
014
015 package com.liferay.portlet.portletconfiguration.action;
016
017 import com.liferay.portal.NoSuchLayoutException;
018 import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
019 import com.liferay.portal.kernel.servlet.SessionErrors;
020 import com.liferay.portal.kernel.servlet.SessionMessages;
021 import com.liferay.portal.kernel.util.Constants;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.JavaConstants;
024 import com.liferay.portal.kernel.util.ParamUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Tuple;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.Layout;
029 import com.liferay.portal.model.Portlet;
030 import com.liferay.portal.security.auth.PrincipalException;
031 import com.liferay.portal.service.GroupLocalServiceUtil;
032 import com.liferay.portal.service.LayoutLocalServiceUtil;
033 import com.liferay.portal.struts.PortletAction;
034 import com.liferay.portal.theme.ThemeDisplay;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portal.util.WebKeys;
037 import com.liferay.portlet.PortletConfigFactoryUtil;
038 import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
039
040 import java.util.ResourceBundle;
041
042 import javax.portlet.ActionRequest;
043 import javax.portlet.ActionResponse;
044 import javax.portlet.PortletConfig;
045 import javax.portlet.PortletPreferences;
046 import javax.portlet.PortletRequest;
047 import javax.portlet.RenderRequest;
048 import javax.portlet.RenderResponse;
049
050 import javax.servlet.ServletContext;
051
052 import org.apache.struts.action.ActionForm;
053 import org.apache.struts.action.ActionForward;
054 import org.apache.struts.action.ActionMapping;
055
056
061 public class EditScopeAction extends PortletAction {
062
063 @Override
064 public void processAction(
065 ActionMapping actionMapping, ActionForm actionForm,
066 PortletConfig portletConfig, ActionRequest actionRequest,
067 ActionResponse actionResponse)
068 throws Exception {
069
070 Portlet portlet = null;
071
072 try {
073 portlet = ActionUtil.getPortlet(actionRequest);
074 }
075 catch (PrincipalException pe) {
076 SessionErrors.add(
077 actionRequest, PrincipalException.class.getName());
078
079 setForward(actionRequest, "portlet.portlet_configuration.error");
080 }
081
082 PortletPreferences portletPreferences =
083 ActionUtil.getLayoutPortletSetup(actionRequest, portlet);
084
085 actionRequest = ActionUtil.getWrappedActionRequest(
086 actionRequest, portletPreferences);
087
088 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
089
090 if (cmd.equals(Constants.SAVE)) {
091 updateScope(actionRequest, portlet);
092 }
093
094 if (SessionErrors.isEmpty(actionRequest)) {
095 LiferayPortletConfig liferayPortletConfig =
096 (LiferayPortletConfig)portletConfig;
097
098 String portletResource = ParamUtil.getString(
099 actionRequest, "portletResource");
100
101 SessionMessages.add(
102 actionRequest,
103 liferayPortletConfig.getPortletId() +
104 SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
105 portletResource);
106
107 SessionMessages.add(
108 actionRequest,
109 liferayPortletConfig.getPortletId() +
110 SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
111
112 String redirect = PortalUtil.escapeRedirect(
113 ParamUtil.getString(actionRequest, "redirect"));
114
115 if (Validator.isNotNull(redirect)) {
116 actionResponse.sendRedirect(redirect);
117 }
118 }
119 }
120
121 @Override
122 public ActionForward render(
123 ActionMapping actionMapping, ActionForm actionForm,
124 PortletConfig portletConfig, RenderRequest renderRequest,
125 RenderResponse renderResponse)
126 throws Exception {
127
128 Portlet portlet = null;
129
130 try {
131 portlet = ActionUtil.getPortlet(renderRequest);
132 }
133 catch (PrincipalException pe) {
134 SessionErrors.add(
135 renderRequest, PrincipalException.class.getName());
136
137 return actionMapping.findForward(
138 "portlet.portlet_configuration.error");
139 }
140
141 PortletPreferences portletPreferences =
142 ActionUtil.getLayoutPortletSetup(renderRequest, portlet);
143
144 renderRequest = ActionUtil.getWrappedRenderRequest(
145 renderRequest, portletPreferences);
146
147 renderResponse.setTitle(ActionUtil.getTitle(portlet, renderRequest));
148
149 return actionMapping.findForward(
150 getForward(
151 renderRequest, "portlet.portlet_configuration.edit_scope"));
152 }
153
154 protected Tuple getNewScope(ActionRequest actionRequest) throws Exception {
155 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
156 WebKeys.THEME_DISPLAY);
157
158 Layout layout = themeDisplay.getLayout();
159
160 String scopeType = ParamUtil.getString(actionRequest, "scopeType");
161
162 long scopeGroupId = 0;
163 String scopeName = null;
164
165 if (Validator.isNull(scopeType)) {
166 scopeGroupId = layout.getGroupId();
167 }
168 else if (scopeType.equals("company")) {
169 scopeGroupId = themeDisplay.getCompanyGroupId();
170 scopeName = themeDisplay.translate("global");
171 }
172 else if (scopeType.equals("layout")) {
173 String scopeLayoutUuid = ParamUtil.getString(
174 actionRequest, "scopeLayoutUuid");
175
176 Layout scopeLayout =
177 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
178 scopeLayoutUuid, layout.getGroupId(),
179 layout.isPrivateLayout());
180
181 if (!scopeLayout.hasScopeGroup()) {
182 String name = String.valueOf(scopeLayout.getPlid());
183
184 GroupLocalServiceUtil.addGroup(
185 themeDisplay.getUserId(), Layout.class.getName(),
186 scopeLayout.getPlid(), name, null, 0, null, false, true,
187 null);
188 }
189
190 scopeGroupId = scopeLayout.getGroupId();
191 scopeName = scopeLayout.getName(themeDisplay.getLocale());
192 }
193 else {
194 throw new IllegalArgumentException(
195 "Scope type " + scopeType + " is invalid");
196 }
197
198 return new Tuple(scopeGroupId, scopeName);
199 }
200
201 protected String getOldScopeName(
202 ActionRequest actionRequest, Portlet portlet)
203 throws Exception {
204
205 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
206 WebKeys.THEME_DISPLAY);
207
208 Layout layout = themeDisplay.getLayout();
209
210 PortletPreferences portletPreferences = actionRequest.getPreferences();
211
212 String scopeType = GetterUtil.getString(
213 portletPreferences.getValue("lfrScopeType", null));
214
215 if (Validator.isNull(scopeType)) {
216 return null;
217 }
218
219 String scopeName = null;
220
221 if (scopeType.equals("company")) {
222 scopeName = themeDisplay.translate("global");
223 }
224 else if (scopeType.equals("layout")) {
225 String scopeLayoutUuid = GetterUtil.getString(
226 portletPreferences.getValue("lfrScopeLayoutUuid", null));
227
228 try {
229 Layout scopeLayout =
230 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
231 scopeLayoutUuid, layout.getGroupId(),
232 layout.isPrivateLayout());
233
234 scopeName = scopeLayout.getName(themeDisplay.getLocale());
235 }
236 catch (NoSuchLayoutException nsle) {
237 }
238 }
239 else {
240 throw new IllegalArgumentException(
241 "Scope type " + scopeType + " is invalid");
242 }
243
244 return scopeName;
245 }
246
247 protected String getPortletTitle(
248 PortletRequest portletRequest, Portlet portlet,
249 PortletPreferences preferences) {
250
251 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
252 WebKeys.THEME_DISPLAY);
253
254 String portletTitle = PortletConfigurationUtil.getPortletTitle(
255 preferences, themeDisplay.getLanguageId());
256
257 if (Validator.isNull(portletTitle)) {
258 ServletContext servletContext =
259 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
260
261 PortletConfig portletConfig = PortletConfigFactoryUtil.create(
262 portlet, servletContext);
263
264 ResourceBundle resourceBundle = portletConfig.getResourceBundle(
265 themeDisplay.getLocale());
266
267 portletTitle = resourceBundle.getString(
268 JavaConstants.JAVAX_PORTLET_TITLE);
269 }
270
271 return portletTitle;
272 }
273
274 protected void updateScope(ActionRequest actionRequest, Portlet portlet)
275 throws Exception {
276
277 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
278 WebKeys.THEME_DISPLAY);
279
280 Layout layout = themeDisplay.getLayout();
281
282 String oldScopeName = getOldScopeName(actionRequest, portlet);
283
284 PortletPreferences portletPreferences = actionRequest.getPreferences();
285
286 String scopeType = ParamUtil.getString(actionRequest, "scopeType");
287
288 portletPreferences.setValue("lfrScopeType", scopeType);
289
290 String scopeLayoutUuid = ParamUtil.getString(
291 actionRequest, "scopeLayoutUuid");
292
293 if (!scopeType.equals("layout")) {
294 scopeLayoutUuid = StringPool.BLANK;
295 }
296
297 portletPreferences.setValue("lfrScopeLayoutUuid", scopeLayoutUuid);
298
299 String portletTitle = getPortletTitle(
300 actionRequest, portlet, portletPreferences);
301
302 Tuple newScopeTuple = getNewScope(actionRequest);
303
304 long newScopeGroupId = (Long)newScopeTuple.getObject(0);
305
306 portletPreferences.setValue("groupId", String.valueOf(newScopeGroupId));
307
308 String newScopeName = (String)newScopeTuple.getObject(1);
309
310 String newPortletTitle = PortalUtil.getNewPortletTitle(
311 portletTitle, oldScopeName, newScopeName);
312
313 if (!newPortletTitle.equals(portletTitle)) {
314 portletPreferences.setValue(
315 "portletSetupTitle_" + themeDisplay.getLanguageId(),
316 newPortletTitle);
317 portletPreferences.setValue(
318 "portletSetupUseCustomTitle", Boolean.TRUE.toString());
319 }
320
321 portletPreferences.store();
322 }
323
324 }