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