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