1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.portletconfiguration.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.PortletPreferencesFactoryUtil;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.PortletPreferences;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditSharingAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Jorge Ferrer
49   *
50   */
51  public class EditSharingAction extends EditConfigurationAction {
52  
53      public void processAction(
54              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
55              ActionRequest actionRequest, ActionResponse actionResponse)
56          throws Exception {
57  
58          Portlet portlet = null;
59  
60          try {
61              portlet = getPortlet(actionRequest);
62          }
63          catch (PrincipalException pe) {
64              SessionErrors.add(
65                  actionRequest, PrincipalException.class.getName());
66  
67              setForward(actionRequest, "portlet.portlet_configuration.error");
68          }
69  
70          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
71              WebKeys.THEME_DISPLAY);
72  
73          Layout layout = themeDisplay.getLayout();
74  
75          PortletPreferences preferences =
76              PortletPreferencesFactoryUtil.getLayoutPortletSetup(
77                  layout, portlet.getPortletId());
78  
79          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
80  
81          if (tabs2.equals("any-website")) {
82              updateAnyWebsite(actionRequest, preferences);
83          }
84          else if (tabs2.equals("facebook")) {
85              updateFacebook(actionRequest, preferences);
86          }
87          else if (tabs2.equals("friends")) {
88              updateFriends(actionRequest, preferences);
89          }
90  
91          preferences.store();
92  
93          sendRedirect(actionRequest, actionResponse);
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         Portlet portlet = null;
102 
103         try {
104             portlet = getPortlet(renderRequest);
105         }
106         catch (PrincipalException pe) {
107             SessionErrors.add(
108                 renderRequest, PrincipalException.class.getName());
109 
110             return mapping.findForward("portlet.portlet_configuration.error");
111         }
112 
113         renderResponse.setTitle(getTitle(portlet, renderRequest));
114 
115         return mapping.findForward(getForward(
116             renderRequest, "portlet.portlet_configuration.edit_sharing"));
117     }
118 
119     protected void updateAnyWebsite(
120             ActionRequest actionRequest, PortletPreferences preferences)
121         throws Exception {
122 
123         boolean widgetShowAddAppLink = ParamUtil.getBoolean(
124             actionRequest, "widgetShowAddAppLink");
125 
126         preferences.setValue(
127             "lfr-widget-show-add-app-link",
128             String.valueOf(widgetShowAddAppLink));
129     }
130 
131     protected void updateFacebook(
132             ActionRequest actionRequest, PortletPreferences preferences)
133         throws Exception {
134 
135         String facebookAPIKey = ParamUtil.getString(
136             actionRequest, "facebookAPIKey");
137         String facebookCanvasPageURL = ParamUtil.getString(
138             actionRequest, "facebookCanvasPageURL");
139         boolean facebookShowAddAppLink = ParamUtil.getBoolean(
140             actionRequest, "facebookShowAddAppLink");
141 
142         preferences.setValue("lfr-facebook-api-key", facebookAPIKey);
143         preferences.setValue(
144             "lfr-facebook-canvas-page-url", facebookCanvasPageURL);
145         preferences.setValue(
146             "lfr-facebook-show-add-app-link",
147             String.valueOf(facebookShowAddAppLink));
148     }
149 
150     protected void updateFriends(
151             ActionRequest actionRequest, PortletPreferences preferences)
152         throws Exception {
153 
154         boolean appShowShareWithFriendsLink = ParamUtil.getBoolean(
155             actionRequest, "appShowShareWithFriendsLink");
156 
157         preferences.setValue(
158             "lfr-app-show-share-with-friends-link",
159             String.valueOf(appShowShareWithFriendsLink));
160     }
161 
162 }