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.theme.ThemeDisplay;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portal.util.WebKeys;
036 import com.liferay.portlet.PortletConfigFactoryUtil;
037 import com.liferay.portlet.PortletPreferencesFactoryUtil;
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 EditConfigurationAction {
062
063 @Override
064 public void processAction(
065 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
066 ActionRequest actionRequest, ActionResponse actionResponse)
067 throws Exception {
068
069 Portlet portlet = null;
070
071 try {
072 portlet = getPortlet(actionRequest);
073 }
074 catch (PrincipalException pe) {
075 SessionErrors.add(
076 actionRequest, PrincipalException.class.getName());
077
078 setForward(actionRequest, "portlet.portlet_configuration.error");
079 }
080
081 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
082
083 if (cmd.equals(Constants.SAVE)) {
084 updateScope(actionRequest, portlet);
085 }
086
087 if (SessionErrors.isEmpty(actionRequest)) {
088 LiferayPortletConfig liferayPortletConfig =
089 (LiferayPortletConfig)portletConfig;
090
091 String portletResource = ParamUtil.getString(
092 actionRequest, "portletResource");
093
094 SessionMessages.add(
095 actionRequest,
096 liferayPortletConfig.getPortletId() +
097 SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
098 portletResource);
099
100 SessionMessages.add(
101 actionRequest,
102 liferayPortletConfig.getPortletId() +
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 mapping, ActionForm form, PortletConfig portletConfig,
117 RenderRequest renderRequest, RenderResponse renderResponse)
118 throws Exception {
119
120 Portlet portlet = null;
121
122 try {
123 portlet = getPortlet(renderRequest);
124 }
125 catch (PrincipalException pe) {
126 SessionErrors.add(
127 renderRequest, PrincipalException.class.getName());
128
129 return mapping.findForward("portlet.portlet_configuration.error");
130 }
131
132 renderResponse.setTitle(getTitle(portlet, renderRequest));
133
134 return mapping.findForward(getForward(
135 renderRequest, "portlet.portlet_configuration.edit_scope"));
136 }
137
138 protected Tuple getNewScope(ActionRequest actionRequest) throws Exception {
139 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
140 WebKeys.THEME_DISPLAY);
141
142 Layout layout = themeDisplay.getLayout();
143
144 String scopeType = ParamUtil.getString(actionRequest, "scopeType");
145
146 long scopeGroupId = 0;
147 String scopeName = null;
148
149 if (Validator.isNull(scopeType)) {
150 scopeGroupId = layout.getGroupId();
151 }
152 else if (scopeType.equals("company")) {
153 scopeGroupId = themeDisplay.getCompanyGroupId();
154 scopeName = themeDisplay.translate("global");
155 }
156 else if (scopeType.equals("layout")) {
157 String scopeLayoutUuid = ParamUtil.getString(
158 actionRequest, "scopeLayoutUuid");
159
160 Layout scopeLayout =
161 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
162 scopeLayoutUuid, layout.getGroupId());
163
164 if (!scopeLayout.hasScopeGroup()) {
165 String name = String.valueOf(scopeLayout.getPlid());
166
167 GroupLocalServiceUtil.addGroup(
168 themeDisplay.getUserId(), Layout.class.getName(),
169 scopeLayout.getPlid(), name, null, 0, null, false, true,
170 null);
171 }
172
173 scopeGroupId = scopeLayout.getGroupId();
174 scopeName = scopeLayout.getName(themeDisplay.getLocale());
175 }
176 else {
177 throw new IllegalArgumentException(
178 "Scope type " + scopeType + " is invalid");
179 }
180
181 return new Tuple(scopeGroupId, scopeName);
182 }
183
184 protected String getOldScopeName(
185 ActionRequest actionRequest, Portlet portlet)
186 throws Exception {
187
188 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
189 WebKeys.THEME_DISPLAY);
190
191 Layout layout = themeDisplay.getLayout();
192
193 PortletPreferences preferences =
194 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
195 layout, portlet.getPortletId());
196
197 String scopeType = GetterUtil.getString(
198 preferences.getValue("lfrScopeType", null));
199
200 if (Validator.isNull(scopeType)) {
201 return null;
202 }
203
204 String scopeName = null;
205
206 if (scopeType.equals("company")) {
207 scopeName = themeDisplay.translate("global");
208 }
209 else if (scopeType.equals("layout")) {
210 String scopeLayoutUuid = GetterUtil.getString(
211 preferences.getValue("lfrScopeLayoutUuid", null));
212
213 try {
214 Layout scopeLayout =
215 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
216 scopeLayoutUuid, layout.getGroupId());
217
218 scopeName = scopeLayout.getName(themeDisplay.getLocale());
219 }
220 catch (NoSuchLayoutException nsle) {
221 }
222 }
223 else {
224 throw new IllegalArgumentException(
225 "Scope type " + scopeType + " is invalid");
226 }
227
228 return scopeName;
229 }
230
231 protected String getPortletTitle(
232 PortletRequest portletRequest, Portlet portlet,
233 PortletPreferences preferences) {
234
235 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
236 WebKeys.THEME_DISPLAY);
237
238 String portletTitle = PortletConfigurationUtil.getPortletTitle(
239 preferences, themeDisplay.getLanguageId());
240
241 if (Validator.isNull(portletTitle)) {
242 ServletContext servletContext =
243 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
244
245 PortletConfig portletConfig = PortletConfigFactoryUtil.create(
246 portlet, servletContext);
247
248 ResourceBundle resourceBundle = portletConfig.getResourceBundle(
249 themeDisplay.getLocale());
250
251 portletTitle = resourceBundle.getString(
252 JavaConstants.JAVAX_PORTLET_TITLE);
253 }
254
255 return portletTitle;
256 }
257
258 protected void updateScope(ActionRequest actionRequest, Portlet portlet)
259 throws Exception {
260
261 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
262 WebKeys.THEME_DISPLAY);
263
264 Layout layout = themeDisplay.getLayout();
265
266 PortletPreferences preferences =
267 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
268 layout, portlet.getPortletId());
269
270 String scopeType = ParamUtil.getString(actionRequest, "scopeType");
271
272 preferences.setValue("lfrScopeType", scopeType);
273
274 String scopeLayoutUuid = ParamUtil.getString(
275 actionRequest, "scopeLayoutUuid");
276
277 if (!scopeType.equals("layout")) {
278 scopeLayoutUuid = StringPool.BLANK;
279 }
280
281 preferences.setValue("lfrScopeLayoutUuid", scopeLayoutUuid);
282
283 String portletTitle = getPortletTitle(
284 actionRequest, portlet, preferences);
285
286 Tuple newScopeTuple = getNewScope(actionRequest);
287
288 long newScopeGroupId = (Long)newScopeTuple.getObject(0);
289
290 preferences.setValue("groupId", String.valueOf(newScopeGroupId));
291
292 String oldScopeName = getOldScopeName(actionRequest, portlet);
293 String newScopeName = (String)newScopeTuple.getObject(1);
294
295 String newPortletTitle = PortalUtil.getNewPortletTitle(
296 portletTitle, oldScopeName, newScopeName);
297
298 if (!newPortletTitle.equals(portletTitle)) {
299 preferences.setValue(
300 "portletSetupTitle_" + themeDisplay.getLanguageId(),
301 newPortletTitle);
302 preferences.setValue(
303 "portletSetupUseCustomTitle", Boolean.TRUE.toString());
304 }
305
306 preferences.store();
307 }
308
309 }