001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.portletconfiguration;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.ConfigurationAction;
020    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
021    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.servlet.SessionMessages;
024    import com.liferay.portal.kernel.settings.ArchivedSettings;
025    import com.liferay.portal.kernel.settings.ModifiableSettings;
026    import com.liferay.portal.kernel.settings.PortletInstanceSettingsLocator;
027    import com.liferay.portal.kernel.settings.Settings;
028    import com.liferay.portal.kernel.settings.SettingsFactoryUtil;
029    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.JavaConstants;
032    import com.liferay.portal.kernel.util.LocaleUtil;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Tuple;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.language.AggregateResourceBundle;
039    import com.liferay.portal.model.GroupConstants;
040    import com.liferay.portal.model.Layout;
041    import com.liferay.portal.model.Portlet;
042    import com.liferay.portal.model.PortletConstants;
043    import com.liferay.portal.model.PortletPreferencesIds;
044    import com.liferay.portal.model.PublicRenderParameter;
045    import com.liferay.portal.security.permission.PermissionPropagator;
046    import com.liferay.portal.service.GroupLocalServiceUtil;
047    import com.liferay.portal.service.LayoutLocalServiceUtil;
048    import com.liferay.portal.service.PortletLocalServiceUtil;
049    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
050    import com.liferay.portal.service.ResourceBlockLocalServiceUtil;
051    import com.liferay.portal.service.ResourceBlockServiceUtil;
052    import com.liferay.portal.service.ResourcePermissionServiceUtil;
053    import com.liferay.portal.servlet.filters.cache.CacheUtil;
054    import com.liferay.portal.theme.ThemeDisplay;
055    import com.liferay.portal.util.PortalUtil;
056    import com.liferay.portal.util.PropsValues;
057    import com.liferay.portal.util.WebKeys;
058    import com.liferay.portlet.PortletConfigFactoryUtil;
059    import com.liferay.portlet.PortletConfigImpl;
060    import com.liferay.portlet.PortletPreferencesFactoryConstants;
061    import com.liferay.portlet.PortletPreferencesFactoryUtil;
062    import com.liferay.portlet.portletconfiguration.action.ActionUtil;
063    import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
064    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
065    
066    import java.io.IOException;
067    
068    import java.util.ArrayList;
069    import java.util.Date;
070    import java.util.Enumeration;
071    import java.util.HashMap;
072    import java.util.List;
073    import java.util.Locale;
074    import java.util.Map;
075    import java.util.ResourceBundle;
076    import java.util.Set;
077    
078    import javax.portlet.ActionRequest;
079    import javax.portlet.ActionResponse;
080    import javax.portlet.EventRequest;
081    import javax.portlet.EventResponse;
082    import javax.portlet.PortletConfig;
083    import javax.portlet.PortletException;
084    import javax.portlet.PortletPreferences;
085    import javax.portlet.PortletRequest;
086    import javax.portlet.RenderRequest;
087    import javax.portlet.RenderResponse;
088    import javax.portlet.ResourceRequest;
089    import javax.portlet.ResourceResponse;
090    
091    import javax.servlet.ServletContext;
092    import javax.servlet.http.HttpServletRequest;
093    
094    /**
095     * @author Carlos Sierra Andr??s
096     */
097    public class PortletConfigurationPortlet extends MVCPortlet {
098    
099            public void deleteArchivedSetup(
100                            ActionRequest actionRequest, ActionResponse actionResponse)
101                    throws Exception {
102    
103                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
104    
105                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
106                            WebKeys.THEME_DISPLAY);
107    
108                    String name = ParamUtil.getString(actionRequest, "name");
109    
110                    ArchivedSettings archivedSettings =
111                            SettingsFactoryUtil.getPortletInstanceArchivedSettings(
112                                    themeDisplay.getSiteGroupId(), portlet.getRootPortletId(),
113                                    name);
114    
115                    archivedSettings.delete();
116            }
117    
118            public void editConfiguration(
119                            ActionRequest actionRequest, ActionResponse actionResponse)
120                    throws Exception {
121    
122                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
123    
124                    PortletConfig portletConfig = (PortletConfig)actionRequest.getAttribute(
125                            JavaConstants.JAVAX_PORTLET_CONFIG);
126    
127                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
128                            WebKeys.THEME_DISPLAY);
129    
130                    String settingsScope = ParamUtil.getString(
131                            actionRequest, "settingsScope");
132    
133                    PortletPreferences portletPreferences = getPortletPreferences(
134                            themeDisplay, portlet.getPortletId(), settingsScope);
135    
136                    actionRequest = ActionUtil.getWrappedActionRequest(
137                            actionRequest, portletPreferences);
138    
139                    ConfigurationAction configurationAction = getConfigurationAction(
140                            portlet);
141    
142                    if (configurationAction == null) {
143                            return;
144                    }
145    
146                    configurationAction.processAction(
147                            portletConfig, actionRequest, actionResponse);
148    
149                    Layout layout = themeDisplay.getLayout();
150    
151                    PortletLayoutListener portletLayoutListener =
152                            portlet.getPortletLayoutListenerInstance();
153    
154                    if (portletLayoutListener != null) {
155                            portletLayoutListener.onSetup(
156                                    portlet.getPortletId(), layout.getPlid());
157                    }
158            }
159    
160            public void editPublicRenderParameters(
161                            ActionRequest actionRequest, ActionResponse actionResponse)
162                    throws Exception {
163    
164                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
165    
166                    PortletPreferences portletPreferences =
167                            ActionUtil.getLayoutPortletSetup(actionRequest, portlet);
168    
169                    actionRequest = ActionUtil.getWrappedActionRequest(
170                            actionRequest, portletPreferences);
171    
172                    Enumeration<String> enu = portletPreferences.getNames();
173    
174                    while (enu.hasMoreElements()) {
175                            String name = enu.nextElement();
176    
177                            if (name.startsWith(
178                                            PublicRenderParameterConfiguration.IGNORE_PREFIX) ||
179                                    name.startsWith(
180                                            PublicRenderParameterConfiguration.MAPPING_PREFIX)) {
181    
182                                    portletPreferences.reset(name);
183                            }
184                    }
185    
186                    for (PublicRenderParameter publicRenderParameter :
187                                    portlet.getPublicRenderParameters()) {
188    
189                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
190                                    publicRenderParameter);
191    
192                            boolean ignoreValue = ParamUtil.getBoolean(
193                                    actionRequest, ignoreKey);
194    
195                            if (ignoreValue) {
196                                    portletPreferences.setValue(
197                                            ignoreKey, String.valueOf(Boolean.TRUE));
198                            }
199                            else {
200                                    String mappingKey =
201                                            PublicRenderParameterConfiguration.getMappingKey(
202                                                    publicRenderParameter);
203    
204                                    String mappingValue = ParamUtil.getString(
205                                            actionRequest, mappingKey);
206    
207                                    if (Validator.isNotNull(mappingValue)) {
208                                            portletPreferences.setValue(mappingKey, mappingValue);
209                                    }
210                            }
211                    }
212    
213                    if (SessionErrors.isEmpty(actionRequest)) {
214                            portletPreferences.store();
215                    }
216            }
217    
218            public void editScope(
219                            ActionRequest actionRequest, ActionResponse actionResponse)
220                    throws Exception {
221    
222                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
223    
224                    PortletPreferences portletPreferences =
225                            ActionUtil.getLayoutPortletSetup(actionRequest, portlet);
226    
227                    actionRequest = ActionUtil.getWrappedActionRequest(
228                            actionRequest, portletPreferences);
229    
230                    updateScope(actionRequest, portlet);
231    
232                    if (!SessionErrors.isEmpty(actionRequest)) {
233                            return;
234                    }
235    
236                    String portletResource = ParamUtil.getString(
237                            actionRequest, "portletResource");
238    
239                    SessionMessages.add(
240                            actionRequest,
241                            PortalUtil.getPortletId(actionRequest) +
242                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
243                            portletResource);
244    
245                    SessionMessages.add(
246                            actionRequest,
247                            PortalUtil.getPortletId(actionRequest) +
248                                    SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
249            }
250    
251            public void editSharing(
252                            ActionRequest actionRequest, ActionResponse actionResponse)
253                    throws Exception {
254    
255                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
256    
257                    PortletPreferences portletPreferences =
258                            ActionUtil.getLayoutPortletSetup(actionRequest, portlet);
259    
260                    actionRequest = ActionUtil.getWrappedActionRequest(
261                            actionRequest, portletPreferences);
262    
263                    updateAnyWebsite(actionRequest, portletPreferences);
264                    updateFacebook(actionRequest, portletPreferences);
265                    updateFriends(actionRequest, portletPreferences);
266                    updateGoogleGadget(actionRequest, portletPreferences);
267                    updateNetvibes(actionRequest, portletPreferences);
268    
269                    portletPreferences.store();
270    
271                    if (!SessionErrors.isEmpty(actionRequest)) {
272                            return;
273                    }
274    
275                    String portletResource = ParamUtil.getString(
276                            actionRequest, "portletResource");
277    
278                    SessionMessages.add(
279                            actionRequest,
280                            PortalUtil.getPortletId(actionRequest) +
281                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
282                            portletResource);
283    
284                    SessionMessages.add(
285                            actionRequest,
286                            PortalUtil.getPortletId(actionRequest) +
287                                    SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
288            }
289    
290            public void editSupportedClients(
291                            ActionRequest actionRequest, ActionResponse actionResponse)
292                    throws Exception {
293    
294                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
295    
296                    PortletPreferences portletPreferences = actionRequest.getPreferences();
297    
298                    actionRequest = ActionUtil.getWrappedActionRequest(
299                            actionRequest, portletPreferences);
300    
301                    Set<String> allPortletModes = portlet.getAllPortletModes();
302    
303                    for (String portletMode : allPortletModes) {
304                            String mobileDevicesParam =
305                                    "portletSetupSupportedClientsMobileDevices_" + portletMode;
306    
307                            boolean mobileDevices = ParamUtil.getBoolean(
308                                    actionRequest, mobileDevicesParam);
309    
310                            portletPreferences.setValue(
311                                    mobileDevicesParam, String.valueOf(mobileDevices));
312                    }
313    
314                    portletPreferences.store();
315            }
316    
317            @Override
318            public void init(PortletConfig portletConfig) throws PortletException {
319                    if (portletConfig instanceof PortletConfigImpl) {
320                            PortletConfigurationPortletPortletConfig
321                                    portletConfigurationPortletPortletConfig =
322                                            new PortletConfigurationPortletPortletConfig(
323                                                    (PortletConfigImpl)portletConfig);
324    
325                            super.init(portletConfigurationPortletPortletConfig);
326                    }
327                    else {
328                            super.init(portletConfig);
329                    }
330            }
331    
332            @Override
333            public void processAction(
334                            ActionRequest actionRequest, ActionResponse actionResponse)
335                    throws IOException, PortletException {
336    
337                    _portletRequestThreadLocal.set(actionRequest);
338    
339                    actionRequest.setAttribute(
340                            JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
341    
342                    super.processAction(actionRequest, actionResponse);
343            }
344    
345            @Override
346            public void processEvent(
347                            EventRequest eventRequest, EventResponse eventResponse)
348                    throws IOException, PortletException {
349    
350                    _portletRequestThreadLocal.set(eventRequest);
351    
352                    eventRequest.setAttribute(
353                            JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
354    
355                    super.processEvent(eventRequest, eventResponse);
356            }
357    
358            @Override
359            public void render(
360                            RenderRequest renderRequest, RenderResponse renderResponse)
361                    throws IOException, PortletException {
362    
363                    _portletRequestThreadLocal.set(renderRequest);
364    
365                    renderRequest.setAttribute(
366                            JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
367    
368                    super.render(renderRequest, renderResponse);
369            }
370    
371            public void restoreArchivedSetup(
372                            ActionRequest actionRequest, ActionResponse actionResponse)
373                    throws Exception {
374    
375                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
376    
377                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
378                            WebKeys.THEME_DISPLAY);
379    
380                    Settings portletInstanceSettings = SettingsFactoryUtil.getSettings(
381                            new PortletInstanceSettingsLocator(
382                                    themeDisplay.getLayout(), portlet.getPortletId()));
383    
384                    ModifiableSettings portletInstanceModifiableSettings =
385                            portletInstanceSettings.getModifiableSettings();
386    
387                    String name = ParamUtil.getString(actionRequest, "name");
388    
389                    ArchivedSettings archivedSettings =
390                            SettingsFactoryUtil.getPortletInstanceArchivedSettings(
391                                    themeDisplay.getSiteGroupId(), portlet.getRootPortletId(),
392                                    name);
393    
394                    portletInstanceModifiableSettings.setValues(archivedSettings);
395    
396                    portletInstanceModifiableSettings.store();
397            }
398    
399            @Override
400            public void serveResource(
401                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
402                    throws IOException, PortletException {
403    
404                    _portletRequestThreadLocal.set(resourceRequest);
405    
406                    resourceRequest.setAttribute(
407                            JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
408    
409                    super.serveResource(resourceRequest, resourceResponse);
410            }
411    
412            public void updateAnyWebsite(
413                            ActionRequest actionRequest, PortletPreferences portletPreferences)
414                    throws Exception {
415    
416                    boolean widgetShowAddAppLink = ParamUtil.getBoolean(
417                            actionRequest, "widgetShowAddAppLink");
418    
419                    portletPreferences.setValue(
420                            "lfrWidgetShowAddAppLink", String.valueOf(widgetShowAddAppLink));
421            }
422    
423            public void updateArchivedSetup(
424                            ActionRequest actionRequest, ActionResponse actionResponse)
425                    throws Exception {
426    
427                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
428    
429                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
430                            WebKeys.THEME_DISPLAY);
431    
432                    String name = ParamUtil.getString(actionRequest, "name");
433    
434                    ArchivedSettings archivedSettings =
435                            SettingsFactoryUtil.getPortletInstanceArchivedSettings(
436                                    themeDisplay.getSiteGroupId(), portlet.getRootPortletId(),
437                                    name);
438    
439                    Settings portletInstanceSettings = SettingsFactoryUtil.getSettings(
440                            new PortletInstanceSettingsLocator(
441                                    themeDisplay.getLayout(), portlet.getPortletId()));
442    
443                    ModifiableSettings portletInstanceModifiableSettings =
444                            portletInstanceSettings.getModifiableSettings();
445    
446                    archivedSettings.setValues(portletInstanceModifiableSettings);
447    
448                    archivedSettings.store();
449            }
450    
451            public void updateRolePermissions(
452                            ActionRequest actionRequest, ActionResponse actionResponse)
453                    throws Exception {
454    
455                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
456                            WebKeys.THEME_DISPLAY);
457    
458                    String portletResource = ParamUtil.getString(
459                            actionRequest, "portletResource");
460                    String modelResource = ParamUtil.getString(
461                            actionRequest, "modelResource");
462                    long[] roleIds = StringUtil.split(
463                            ParamUtil.getString(
464                                    actionRequest, "rolesSearchContainerPrimaryKeys"), 0L);
465    
466                    String selResource = PortletConstants.getRootPortletId(portletResource);
467    
468                    if (Validator.isNotNull(modelResource)) {
469                            selResource = modelResource;
470                    }
471    
472                    long resourceGroupId = ParamUtil.getLong(
473                            actionRequest, "resourceGroupId", themeDisplay.getScopeGroupId());
474                    String resourcePrimKey = ParamUtil.getString(
475                            actionRequest, "resourcePrimKey");
476    
477                    Map<Long, String[]> roleIdsToActionIds = new HashMap<>();
478    
479                    if (ResourceBlockLocalServiceUtil.isSupported(selResource)) {
480                            for (long roleId : roleIds) {
481                                    List<String> actionIds = getActionIdsList(
482                                            actionRequest, roleId, true);
483    
484                                    roleIdsToActionIds.put(
485                                            roleId, actionIds.toArray(new String[actionIds.size()]));
486                            }
487    
488                            ResourceBlockServiceUtil.setIndividualScopePermissions(
489                                    themeDisplay.getCompanyId(), resourceGroupId, selResource,
490                                    GetterUtil.getLong(resourcePrimKey), roleIdsToActionIds);
491                    }
492                    else {
493                            for (long roleId : roleIds) {
494                                    String[] actionIds = getActionIds(actionRequest, roleId, false);
495    
496                                    roleIdsToActionIds.put(roleId, actionIds);
497                            }
498    
499                            ResourcePermissionServiceUtil.setIndividualResourcePermissions(
500                                    resourceGroupId, themeDisplay.getCompanyId(), selResource,
501                                    resourcePrimKey, roleIdsToActionIds);
502                    }
503    
504                    updateLayoutModifiedDate(selResource, resourcePrimKey);
505    
506                    if (PropsValues.PERMISSIONS_PROPAGATION_ENABLED) {
507                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
508                                    themeDisplay.getCompanyId(), portletResource);
509    
510                            PermissionPropagator permissionPropagator =
511                                    portlet.getPermissionPropagatorInstance();
512    
513                            if (permissionPropagator != null) {
514                                    permissionPropagator.propagateRolePermissions(
515                                            actionRequest, modelResource, resourcePrimKey, roleIds);
516                            }
517                    }
518            }
519    
520            @Override
521            protected void doDispatch(
522                            RenderRequest renderRequest, RenderResponse renderResponse)
523                    throws IOException, PortletException {
524    
525                    try {
526                            String mvcPath = renderRequest.getParameter("mvcPath");
527    
528                            Portlet portlet = ActionUtil.getPortlet(renderRequest);
529    
530                            if (mvcPath.endsWith("edit_configuration.jsp")) {
531                                    renderRequest = ActionUtil.getWrappedRenderRequest(
532                                            renderRequest, null);
533    
534                                    renderEditConfiguration(renderRequest, portlet);
535                            }
536                            else {
537                                    PortletPreferences portletPreferences =
538                                            ActionUtil.getLayoutPortletSetup(renderRequest, portlet);
539    
540                                    renderRequest = ActionUtil.getWrappedRenderRequest(
541                                            renderRequest, portletPreferences);
542    
543                                    if (mvcPath.endsWith("edit_public_render_parameters.jsp")) {
544                                            renderEditPublicParameters(renderRequest, portlet);
545                                    }
546                            }
547    
548                            renderResponse.setTitle(
549                                    ActionUtil.getTitle(portlet, renderRequest));
550                    }
551                    catch (Exception ex) {
552                            _log.error(ex.getMessage());
553    
554                            include("/error.jsp", renderRequest, renderResponse);
555                    }
556    
557                    super.doDispatch(renderRequest, renderResponse);
558            }
559    
560            protected String[] getActionIds(
561                    ActionRequest actionRequest, long roleId, boolean includePreselected) {
562    
563                    List<String> actionIds = getActionIdsList(
564                            actionRequest, roleId, includePreselected);
565    
566                    return actionIds.toArray(new String[actionIds.size()]);
567            }
568    
569            protected List<String> getActionIdsList(
570                    ActionRequest actionRequest, long roleId, boolean includePreselected) {
571    
572                    List<String> actionIds = new ArrayList<>();
573    
574                    Enumeration<String> enu = actionRequest.getParameterNames();
575    
576                    while (enu.hasMoreElements()) {
577                            String name = enu.nextElement();
578    
579                            if (name.startsWith(roleId + ActionUtil.ACTION)) {
580                                    int pos = name.indexOf(ActionUtil.ACTION);
581    
582                                    String actionId = name.substring(
583                                            pos + ActionUtil.ACTION.length());
584    
585                                    actionIds.add(actionId);
586                            }
587                            else if (includePreselected &&
588                                             name.startsWith(roleId + ActionUtil.PRESELECTED)) {
589    
590                                    int pos = name.indexOf(ActionUtil.PRESELECTED);
591    
592                                    String actionId = name.substring(
593                                            pos + ActionUtil.PRESELECTED.length());
594    
595                                    actionIds.add(actionId);
596                            }
597                    }
598    
599                    return actionIds;
600            }
601    
602            protected ConfigurationAction getConfigurationAction(Portlet portlet)
603                    throws Exception {
604    
605                    if (portlet == null) {
606                            return null;
607                    }
608    
609                    ConfigurationAction configurationAction =
610                            portlet.getConfigurationActionInstance();
611    
612                    if (configurationAction == null) {
613                            _log.error(
614                                    "Configuration action for portlet " + portlet.getPortletId() +
615                                            " is null");
616                    }
617    
618                    return configurationAction;
619            }
620    
621            protected Tuple getNewScope(ActionRequest actionRequest) throws Exception {
622                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
623                            WebKeys.THEME_DISPLAY);
624    
625                    Layout layout = themeDisplay.getLayout();
626    
627                    String scopeType = ParamUtil.getString(actionRequest, "scopeType");
628    
629                    long scopeGroupId = 0;
630                    String scopeName = null;
631    
632                    if (Validator.isNull(scopeType)) {
633                            scopeGroupId = layout.getGroupId();
634                    }
635                    else if (scopeType.equals("company")) {
636                            scopeGroupId = themeDisplay.getCompanyGroupId();
637                            scopeName = themeDisplay.translate("global");
638                    }
639                    else if (scopeType.equals("layout")) {
640                            String scopeLayoutUuid = ParamUtil.getString(
641                                    actionRequest, "scopeLayoutUuid");
642    
643                            Layout scopeLayout =
644                                    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
645                                            scopeLayoutUuid, layout.getGroupId(),
646                                            layout.isPrivateLayout());
647    
648                            if (!scopeLayout.hasScopeGroup()) {
649                                    Map<Locale, String> nameMap = new HashMap<>();
650    
651                                    String name = String.valueOf(scopeLayout.getPlid());
652    
653                                    nameMap.put(LocaleUtil.getDefault(), name);
654    
655                                    GroupLocalServiceUtil.addGroup(
656                                            themeDisplay.getUserId(),
657                                            GroupConstants.DEFAULT_PARENT_GROUP_ID,
658                                            Layout.class.getName(), scopeLayout.getPlid(),
659                                            GroupConstants.DEFAULT_LIVE_GROUP_ID, nameMap, null, 0,
660                                            true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null,
661                                            false, true, null);
662                            }
663    
664                            scopeGroupId = scopeLayout.getGroupId();
665                            scopeName = scopeLayout.getName(themeDisplay.getLocale());
666                    }
667                    else {
668                            throw new IllegalArgumentException(
669                                    "Scope type " + scopeType + " is invalid");
670                    }
671    
672                    return new Tuple(scopeGroupId, scopeName);
673            }
674    
675            protected String getOldScopeName(
676                            ActionRequest actionRequest, Portlet portlet)
677                    throws Exception {
678    
679                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
680                            WebKeys.THEME_DISPLAY);
681    
682                    Layout layout = themeDisplay.getLayout();
683    
684                    PortletPreferences portletPreferences = actionRequest.getPreferences();
685    
686                    String scopeType = GetterUtil.getString(
687                            portletPreferences.getValue("lfrScopeType", null));
688    
689                    if (Validator.isNull(scopeType)) {
690                            return null;
691                    }
692    
693                    String scopeName = null;
694    
695                    if (scopeType.equals("company")) {
696                            scopeName = themeDisplay.translate("global");
697                    }
698                    else if (scopeType.equals("layout")) {
699                            String scopeLayoutUuid = GetterUtil.getString(
700                                    portletPreferences.getValue("lfrScopeLayoutUuid", null));
701    
702                            Layout scopeLayout =
703                                    LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
704                                            scopeLayoutUuid, layout.getGroupId(),
705                                            layout.isPrivateLayout());
706    
707                            if (scopeLayout != null) {
708                                    scopeName = scopeLayout.getName(themeDisplay.getLocale());
709                            }
710                    }
711                    else {
712                            throw new IllegalArgumentException(
713                                    "Scope type " + scopeType + " is invalid");
714                    }
715    
716                    return scopeName;
717            }
718    
719            protected PortletPreferences getPortletPreferences(
720                    ThemeDisplay themeDisplay, String portletId, String settingsScope) {
721    
722                    if (Validator.isNull(settingsScope) ||
723                            settingsScope.equals(
724                                    PortletPreferencesFactoryConstants.
725                                            SETTINGS_SCOPE_PORTLET_INSTANCE)) {
726    
727                            return null;
728                    }
729    
730                    PortletPreferencesIds portletPreferencesIds =
731                            PortletPreferencesFactoryUtil.getPortletPreferencesIds(
732                                    themeDisplay.getCompanyId(), themeDisplay.getSiteGroupId(),
733                                    themeDisplay.getPlid(), portletId, settingsScope);
734    
735                    return PortletPreferencesLocalServiceUtil.getPreferences(
736                            portletPreferencesIds);
737            }
738    
739            protected String getPortletTitle(
740                    PortletRequest portletRequest, Portlet portlet,
741                    PortletPreferences portletPreferences) {
742    
743                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
744                            WebKeys.THEME_DISPLAY);
745    
746                    String portletTitle = PortletConfigurationUtil.getPortletTitle(
747                            portletPreferences, themeDisplay.getLanguageId());
748    
749                    if (Validator.isNull(portletTitle)) {
750                            ServletContext servletContext =
751                                    (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
752    
753                            portletTitle = PortalUtil.getPortletTitle(
754                                    portlet, servletContext, themeDisplay.getLocale());
755                    }
756    
757                    return portletTitle;
758            }
759    
760            protected void renderEditConfiguration(
761                            RenderRequest renderRequest, Portlet portlet)
762                    throws Exception {
763    
764                    ConfigurationAction configurationAction = getConfigurationAction(
765                            portlet);
766    
767                    if (configurationAction != null) {
768                            renderRequest.setAttribute(
769                                    WebKeys.CONFIGURATION_ACTION, configurationAction);
770                    }
771                    else if (_log.isDebugEnabled()) {
772                            _log.debug("Configuration action is null");
773                    }
774            }
775    
776            protected void renderEditPublicParameters(
777                            RenderRequest renderRequest, Portlet portlet)
778                    throws Exception {
779    
780                    ActionUtil.getLayoutPublicRenderParameters(renderRequest);
781    
782                    ActionUtil.getPublicRenderParameterConfigurationList(
783                            renderRequest, portlet);
784            }
785    
786            protected void updateFacebook(
787                            ActionRequest actionRequest, PortletPreferences portletPreferences)
788                    throws Exception {
789    
790                    String facebookAPIKey = ParamUtil.getString(
791                            actionRequest, "facebookAPIKey");
792                    String facebookCanvasPageURL = ParamUtil.getString(
793                            actionRequest, "facebookCanvasPageURL");
794                    boolean facebookShowAddAppLink = ParamUtil.getBoolean(
795                            actionRequest, "facebookShowAddAppLink");
796    
797                    portletPreferences.setValue("lfrFacebookApiKey", facebookAPIKey);
798                    portletPreferences.setValue(
799                            "lfrFacebookCanvasPageUrl", facebookCanvasPageURL);
800                    portletPreferences.setValue(
801                            "lfrFacebookShowAddAppLink",
802                            String.valueOf(facebookShowAddAppLink));
803            }
804    
805            protected void updateFriends(
806                            ActionRequest actionRequest, PortletPreferences portletPreferences)
807                    throws Exception {
808    
809                    boolean appShowShareWithFriendsLink = ParamUtil.getBoolean(
810                            actionRequest, "appShowShareWithFriendsLink");
811    
812                    portletPreferences.setValue(
813                            "lfrAppShowShareWithFriendsLink",
814                            String.valueOf(appShowShareWithFriendsLink));
815            }
816    
817            protected void updateGoogleGadget(
818                            ActionRequest actionRequest, PortletPreferences portletPreferences)
819                    throws Exception {
820    
821                    boolean iGoogleShowAddAppLink = ParamUtil.getBoolean(
822                            actionRequest, "iGoogleShowAddAppLink");
823    
824                    portletPreferences.setValue(
825                            "lfrIgoogleShowAddAppLink", String.valueOf(iGoogleShowAddAppLink));
826            }
827    
828            protected void updateLayoutModifiedDate(
829                            String selResource, String resourcePrimKey)
830                    throws Exception {
831    
832                    long plid = 0;
833    
834                    int pos = resourcePrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
835    
836                    if (pos != -1) {
837                            plid = GetterUtil.getLong(resourcePrimKey.substring(0, pos));
838                    }
839                    else if (selResource.equals(Layout.class.getName())) {
840                            plid = GetterUtil.getLong(resourcePrimKey);
841                    }
842    
843                    if (plid <= 0) {
844                            return;
845                    }
846    
847                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
848    
849                    if (layout != null) {
850                            layout.setModifiedDate(new Date());
851    
852                            LayoutLocalServiceUtil.updateLayout(layout);
853    
854                            CacheUtil.clearCache(layout.getCompanyId());
855                    }
856            }
857    
858            protected void updateNetvibes(
859                            ActionRequest actionRequest, PortletPreferences portletPreferences)
860                    throws Exception {
861    
862                    boolean netvibesShowAddAppLink = ParamUtil.getBoolean(
863                            actionRequest, "netvibesShowAddAppLink");
864    
865                    portletPreferences.setValue(
866                            "lfrNetvibesShowAddAppLink",
867                            String.valueOf(netvibesShowAddAppLink));
868            }
869    
870            protected void updateScope(ActionRequest actionRequest, Portlet portlet)
871                    throws Exception {
872    
873                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
874                            WebKeys.THEME_DISPLAY);
875    
876                    String oldScopeName = getOldScopeName(actionRequest, portlet);
877    
878                    PortletPreferences portletPreferences = actionRequest.getPreferences();
879    
880                    String scopeType = ParamUtil.getString(actionRequest, "scopeType");
881    
882                    portletPreferences.setValue("lfrScopeType", scopeType);
883    
884                    String scopeLayoutUuid = ParamUtil.getString(
885                            actionRequest, "scopeLayoutUuid");
886    
887                    if (!scopeType.equals("layout")) {
888                            scopeLayoutUuid = StringPool.BLANK;
889                    }
890    
891                    portletPreferences.setValue("lfrScopeLayoutUuid", scopeLayoutUuid);
892    
893                    String portletTitle = getPortletTitle(
894                            actionRequest, portlet, portletPreferences);
895    
896                    Tuple newScopeTuple = getNewScope(actionRequest);
897    
898                    String newScopeName = (String)newScopeTuple.getObject(1);
899    
900                    String newPortletTitle = PortalUtil.getNewPortletTitle(
901                            portletTitle, oldScopeName, newScopeName);
902    
903                    if (!newPortletTitle.equals(portletTitle)) {
904                            portletPreferences.setValue(
905                                    "portletSetupTitle_" + themeDisplay.getLanguageId(),
906                                    newPortletTitle);
907                            portletPreferences.setValue(
908                                    "portletSetupUseCustomTitle", Boolean.TRUE.toString());
909                    }
910    
911                    portletPreferences.store();
912            }
913    
914            private static final Log _log = LogFactoryUtil.getLog(
915                    PortletConfigurationPortlet.class);
916    
917            private final ThreadLocal<PortletRequest> _portletRequestThreadLocal =
918                    new AutoResetThreadLocal<>("_portletRequestThreadLocal");
919    
920            private class PortletConfigurationPortletPortletConfig
921                    extends PortletConfigImpl {
922    
923                    @Override
924                    public ResourceBundle getResourceBundle(Locale locale) {
925                            try {
926                                    PortletRequest portletRequest =
927                                            _portletRequestThreadLocal.get();
928    
929                                    long companyId = PortalUtil.getCompanyId(portletRequest);
930    
931                                    String portletResource = ParamUtil.getString(
932                                            portletRequest, "portletResource");
933    
934                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
935                                            companyId, portletResource);
936    
937                                    HttpServletRequest httpServletRequest =
938                                            PortalUtil.getHttpServletRequest(portletRequest);
939    
940                                    PortletConfig portletConfig = PortletConfigFactoryUtil.create(
941                                            portlet, httpServletRequest.getServletContext());
942    
943                                    return new AggregateResourceBundle(
944                                            super.getResourceBundle(locale),
945                                            portletConfig.getResourceBundle(locale));
946                            }
947                            catch (Exception e) {
948                                    _log.error(e, e);
949                            }
950    
951                            return super.getResourceBundle(locale);
952                    }
953    
954                    private PortletConfigurationPortletPortletConfig(
955                            PortletConfigImpl portletConfigImpl) {
956    
957                            super(
958                                    portletConfigImpl.getPortlet(),
959                                    portletConfigImpl.getPortletContext());
960                    }
961    
962            }
963    
964    }