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