1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.portletconfiguration.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.model.Layout;
20  import com.liferay.portal.model.Portlet;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portlet.PortletPreferencesFactoryUtil;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletPreferences;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="EditSharingAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Jorge Ferrer
41   */
42  public class EditSharingAction extends EditConfigurationAction {
43  
44      public void processAction(
45              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
46              ActionRequest actionRequest, ActionResponse actionResponse)
47          throws Exception {
48  
49          Portlet portlet = null;
50  
51          try {
52              portlet = getPortlet(actionRequest);
53          }
54          catch (PrincipalException pe) {
55              SessionErrors.add(
56                  actionRequest, PrincipalException.class.getName());
57  
58              setForward(actionRequest, "portlet.portlet_configuration.error");
59          }
60  
61          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
62              WebKeys.THEME_DISPLAY);
63  
64          Layout layout = themeDisplay.getLayout();
65  
66          PortletPreferences preferences =
67              PortletPreferencesFactoryUtil.getLayoutPortletSetup(
68                  layout, portlet.getPortletId());
69  
70          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
71  
72          if (tabs2.equals("any-website")) {
73              updateAnyWebsite(actionRequest, preferences);
74          }
75          else if (tabs2.equals("facebook")) {
76              updateFacebook(actionRequest, preferences);
77          }
78          else if (tabs2.equals("friends")) {
79              updateFriends(actionRequest, preferences);
80          }
81          else if (tabs2.equals("google-gadget")) {
82              updateGoogleGadget(actionRequest, preferences);
83          }
84          else if (tabs2.equals("netvibes")) {
85              updateNetvibes(actionRequest, preferences);
86          }
87  
88          preferences.store();
89  
90          sendRedirect(actionRequest, actionResponse);
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
95              RenderRequest renderRequest, RenderResponse renderResponse)
96          throws Exception {
97  
98          Portlet portlet = null;
99  
100         try {
101             portlet = getPortlet(renderRequest);
102         }
103         catch (PrincipalException pe) {
104             SessionErrors.add(
105                 renderRequest, PrincipalException.class.getName());
106 
107             return mapping.findForward("portlet.portlet_configuration.error");
108         }
109 
110         renderResponse.setTitle(getTitle(portlet, renderRequest));
111 
112         return mapping.findForward(getForward(
113             renderRequest, "portlet.portlet_configuration.edit_sharing"));
114     }
115 
116     protected void updateAnyWebsite(
117             ActionRequest actionRequest, PortletPreferences preferences)
118         throws Exception {
119 
120         boolean widgetShowAddAppLink = ParamUtil.getBoolean(
121             actionRequest, "widgetShowAddAppLink");
122 
123         preferences.setValue(
124             "lfr-widget-show-add-app-link",
125             String.valueOf(widgetShowAddAppLink));
126     }
127 
128     protected void updateFacebook(
129             ActionRequest actionRequest, PortletPreferences preferences)
130         throws Exception {
131 
132         String facebookAPIKey = ParamUtil.getString(
133             actionRequest, "facebookAPIKey");
134         String facebookCanvasPageURL = ParamUtil.getString(
135             actionRequest, "facebookCanvasPageURL");
136         boolean facebookShowAddAppLink = ParamUtil.getBoolean(
137             actionRequest, "facebookShowAddAppLink");
138 
139         preferences.setValue("lfr-facebook-api-key", facebookAPIKey);
140         preferences.setValue(
141             "lfr-facebook-canvas-page-url", facebookCanvasPageURL);
142         preferences.setValue(
143             "lfr-facebook-show-add-app-link",
144             String.valueOf(facebookShowAddAppLink));
145     }
146 
147     protected void updateFriends(
148             ActionRequest actionRequest, PortletPreferences preferences)
149         throws Exception {
150 
151         boolean appShowShareWithFriendsLink = ParamUtil.getBoolean(
152             actionRequest, "appShowShareWithFriendsLink");
153 
154         preferences.setValue(
155             "lfr-app-show-share-with-friends-link",
156             String.valueOf(appShowShareWithFriendsLink));
157     }
158 
159     protected void updateGoogleGadget(
160             ActionRequest actionRequest, PortletPreferences preferences)
161         throws Exception {
162 
163         boolean iGoogleShowAddAppLink = ParamUtil.getBoolean(
164             actionRequest, "iGoogleShowAddAppLink");
165 
166         preferences.setValue(
167             "lfr-igoogle-show-add-app-link",
168             String.valueOf(iGoogleShowAddAppLink));
169     }
170 
171     protected void updateNetvibes(
172             ActionRequest actionRequest, PortletPreferences preferences)
173         throws Exception {
174 
175         boolean netvibesShowAddAppLink = ParamUtil.getBoolean(
176             actionRequest, "netvibesShowAddAppLink");
177 
178         preferences.setValue(
179             "lfr-netvibes-show-add-app-link",
180             String.valueOf(netvibesShowAddAppLink));
181     }
182 
183 }