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