001    /**
002     * Copyright (c) 2000-2012 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.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.servlet.SessionMessages;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.Portlet;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.PortletPreferencesFactoryUtil;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.ActionResponse;
031    import javax.portlet.PortletConfig;
032    import javax.portlet.PortletPreferences;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Jorge Ferrer
042     */
043    public class EditSharingAction extends EditConfigurationAction {
044    
045            @Override
046            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    Portlet portlet = null;
052    
053                    try {
054                            portlet = getPortlet(actionRequest);
055                    }
056                    catch (PrincipalException pe) {
057                            SessionErrors.add(
058                                    actionRequest, PrincipalException.class.getName());
059    
060                            setForward(actionRequest, "portlet.portlet_configuration.error");
061                    }
062    
063                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
064                            WebKeys.THEME_DISPLAY);
065    
066                    Layout layout = themeDisplay.getLayout();
067    
068                    PortletPreferences preferences =
069                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
070                                    layout, portlet.getPortletId());
071    
072                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
073    
074                    if (tabs2.equals("any-website")) {
075                            updateAnyWebsite(actionRequest, preferences);
076                    }
077                    else if (tabs2.equals("facebook")) {
078                            updateFacebook(actionRequest, preferences);
079                    }
080                    else if (tabs2.equals("friends")) {
081                            updateFriends(actionRequest, preferences);
082                    }
083                    else if (tabs2.equals("opensocial-gadget")) {
084                            updateGoogleGadget(actionRequest, preferences);
085                    }
086                    else if (tabs2.equals("netvibes")) {
087                            updateNetvibes(actionRequest, preferences);
088                    }
089    
090                    preferences.store();
091    
092                    if (SessionErrors.isEmpty(actionRequest)) {
093                            String portletResource = ParamUtil.getString(
094                                    actionRequest, "portletResource");
095    
096                            SessionMessages.add(
097                                    actionRequest,
098                                    portletConfig.getPortletName() +
099                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
100                                    portletResource);
101    
102                            SessionMessages.add(
103                                    actionRequest,
104                                    portletConfig.getPortletName() +
105                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
106    
107                            String redirect = PortalUtil.escapeRedirect(
108                                    ParamUtil.getString(actionRequest, "redirect"));
109    
110                            if (Validator.isNotNull(redirect)) {
111                                    actionResponse.sendRedirect(redirect);
112                            }
113                    }
114            }
115    
116            @Override
117            public ActionForward render(
118                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
119                            RenderRequest renderRequest, RenderResponse renderResponse)
120                    throws Exception {
121    
122                    Portlet portlet = null;
123    
124                    try {
125                            portlet = getPortlet(renderRequest);
126                    }
127                    catch (PrincipalException pe) {
128                            SessionErrors.add(
129                                    renderRequest, PrincipalException.class.getName());
130    
131                            return mapping.findForward("portlet.portlet_configuration.error");
132                    }
133    
134                    renderResponse.setTitle(getTitle(portlet, renderRequest));
135    
136                    return mapping.findForward(getForward(
137                            renderRequest, "portlet.portlet_configuration.edit_sharing"));
138            }
139    
140            protected void updateAnyWebsite(
141                            ActionRequest actionRequest, PortletPreferences preferences)
142                    throws Exception {
143    
144                    boolean widgetShowAddAppLink = ParamUtil.getBoolean(
145                            actionRequest, "widgetShowAddAppLink");
146    
147                    preferences.setValue(
148                            "lfrWidgetShowAddAppLink", String.valueOf(widgetShowAddAppLink));
149            }
150    
151            protected void updateFacebook(
152                            ActionRequest actionRequest, PortletPreferences preferences)
153                    throws Exception {
154    
155                    String facebookAPIKey = ParamUtil.getString(
156                            actionRequest, "facebookAPIKey");
157                    String facebookCanvasPageURL = ParamUtil.getString(
158                            actionRequest, "facebookCanvasPageURL");
159                    boolean facebookShowAddAppLink = ParamUtil.getBoolean(
160                            actionRequest, "facebookShowAddAppLink");
161    
162                    preferences.setValue("lfrFacebookApiKey", facebookAPIKey);
163                    preferences.setValue("lfrFacebookCanvasPageUrl", facebookCanvasPageURL);
164                    preferences.setValue(
165                            "lfrFacebookShowAddAppLink",
166                            String.valueOf(facebookShowAddAppLink));
167            }
168    
169            protected void updateFriends(
170                            ActionRequest actionRequest, PortletPreferences preferences)
171                    throws Exception {
172    
173                    boolean appShowShareWithFriendsLink = ParamUtil.getBoolean(
174                            actionRequest, "appShowShareWithFriendsLink");
175    
176                    preferences.setValue(
177                            "lfrAppShowShareWithFriendsLink",
178                            String.valueOf(appShowShareWithFriendsLink));
179            }
180    
181            protected void updateGoogleGadget(
182                            ActionRequest actionRequest, PortletPreferences preferences)
183                    throws Exception {
184    
185                    boolean iGoogleShowAddAppLink = ParamUtil.getBoolean(
186                            actionRequest, "iGoogleShowAddAppLink");
187    
188                    preferences.setValue(
189                            "lfrIgoogleShowAddAppLink", String.valueOf(iGoogleShowAddAppLink));
190            }
191    
192            protected void updateNetvibes(
193                            ActionRequest actionRequest, PortletPreferences preferences)
194                    throws Exception {
195    
196                    boolean netvibesShowAddAppLink = ParamUtil.getBoolean(
197                            actionRequest, "netvibesShowAddAppLink");
198    
199                    preferences.setValue(
200                            "lfrNetvibesShowAddAppLink",
201                            String.valueOf(netvibesShowAddAppLink));
202            }
203    
204    }