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