001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            SessionMessages.add(
094                                    actionRequest,
095                                    portletConfig.getPortletName() + ".doConfigure");
096    
097                            String portletResource = ParamUtil.getString(
098                                    actionRequest, "portletResource");
099    
100                            SessionMessages.add(
101                                    actionRequest, portletConfig.getPortletName() + ".doRefresh",
102                                    portletResource);
103    
104                            String redirect = PortalUtil.escapeRedirect(
105                                    ParamUtil.getString(actionRequest, "redirect"));
106    
107                            if (Validator.isNotNull(redirect)) {
108                                    actionResponse.sendRedirect(redirect);
109                            }
110                    }
111            }
112    
113            @Override
114            public ActionForward render(
115                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
116                            RenderRequest renderRequest, RenderResponse renderResponse)
117                    throws Exception {
118    
119                    Portlet portlet = null;
120    
121                    try {
122                            portlet = getPortlet(renderRequest);
123                    }
124                    catch (PrincipalException pe) {
125                            SessionErrors.add(
126                                    renderRequest, PrincipalException.class.getName());
127    
128                            return mapping.findForward("portlet.portlet_configuration.error");
129                    }
130    
131                    renderResponse.setTitle(getTitle(portlet, renderRequest));
132    
133                    return mapping.findForward(getForward(
134                            renderRequest, "portlet.portlet_configuration.edit_sharing"));
135            }
136    
137            protected void updateAnyWebsite(
138                            ActionRequest actionRequest, PortletPreferences preferences)
139                    throws Exception {
140    
141                    boolean widgetShowAddAppLink = ParamUtil.getBoolean(
142                            actionRequest, "widgetShowAddAppLink");
143    
144                    preferences.setValue(
145                            "lfrWidgetShowAddAppLink", String.valueOf(widgetShowAddAppLink));
146            }
147    
148            protected void updateFacebook(
149                            ActionRequest actionRequest, PortletPreferences preferences)
150                    throws Exception {
151    
152                    String facebookAPIKey = ParamUtil.getString(
153                            actionRequest, "facebookAPIKey");
154                    String facebookCanvasPageURL = ParamUtil.getString(
155                            actionRequest, "facebookCanvasPageURL");
156                    boolean facebookShowAddAppLink = ParamUtil.getBoolean(
157                            actionRequest, "facebookShowAddAppLink");
158    
159                    preferences.setValue("lfrFacebookApiKey", facebookAPIKey);
160                    preferences.setValue("lfrFacebookCanvasPageUrl", facebookCanvasPageURL);
161                    preferences.setValue(
162                            "lfrFacebookShowAddAppLink",
163                            String.valueOf(facebookShowAddAppLink));
164            }
165    
166            protected void updateFriends(
167                            ActionRequest actionRequest, PortletPreferences preferences)
168                    throws Exception {
169    
170                    boolean appShowShareWithFriendsLink = ParamUtil.getBoolean(
171                            actionRequest, "appShowShareWithFriendsLink");
172    
173                    preferences.setValue(
174                            "lfrAppShowShareWithFriendsLink",
175                            String.valueOf(appShowShareWithFriendsLink));
176            }
177    
178            protected void updateGoogleGadget(
179                            ActionRequest actionRequest, PortletPreferences preferences)
180                    throws Exception {
181    
182                    boolean iGoogleShowAddAppLink = ParamUtil.getBoolean(
183                            actionRequest, "iGoogleShowAddAppLink");
184    
185                    preferences.setValue(
186                            "lfrIgoogleShowAddAppLink", String.valueOf(iGoogleShowAddAppLink));
187            }
188    
189            protected void updateNetvibes(
190                            ActionRequest actionRequest, PortletPreferences preferences)
191                    throws Exception {
192    
193                    boolean netvibesShowAddAppLink = ParamUtil.getBoolean(
194                            actionRequest, "netvibesShowAddAppLink");
195    
196                    preferences.setValue(
197                            "lfrNetvibesShowAddAppLink",
198                            String.valueOf(netvibesShowAddAppLink));
199            }
200    
201    }