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