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