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.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.language.LanguageUtil;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.LocaleUtil;
32  import com.liferay.portal.kernel.util.ParamUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.Layout;
35  import com.liferay.portal.security.permission.ActionKeys;
36  import com.liferay.portal.security.permission.PermissionChecker;
37  import com.liferay.portal.service.permission.PortletPermissionUtil;
38  import com.liferay.portal.struts.JSONAction;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.InvokerPortletImpl;
42  import com.liferay.portlet.PortletPreferencesFactoryUtil;
43  
44  import java.util.Locale;
45  
46  import javax.portlet.PortletPreferences;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  import javax.servlet.http.HttpSession;
51  
52  import org.apache.struts.action.ActionForm;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="UpdateLookAndFeelAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Wilson Man
60   *
61   */
62  public class UpdateLookAndFeelAction extends JSONAction {
63  
64      public String getJSON(
65              ActionMapping mapping, ActionForm form, HttpServletRequest request,
66              HttpServletResponse response)
67          throws Exception {
68  
69          HttpSession session = request.getSession();
70  
71          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
72              WebKeys.THEME_DISPLAY);
73  
74          Layout layout = themeDisplay.getLayout();
75  
76          PermissionChecker permissionChecker =
77              themeDisplay.getPermissionChecker();
78  
79          String portletId = ParamUtil.getString(request, "portletId");
80  
81          if (!PortletPermissionUtil.contains(
82                  permissionChecker, themeDisplay.getPlid(), portletId,
83                  ActionKeys.CONFIGURATION)) {
84  
85              return null;
86          }
87  
88          PortletPreferences portletSetup =
89              PortletPreferencesFactoryUtil.getLayoutPortletSetup(
90                  layout, portletId);
91  
92          String css = ParamUtil.getString(request, "css");
93  
94          if (_log.isDebugEnabled()) {
95              _log.debug("Updating css " + css);
96          }
97  
98          JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
99  
100         JSONObject portletData = jsonObj.getJSONObject("portletData");
101 
102         jsonObj.remove("portletData");
103 
104         css = jsonObj.toString();
105 
106         boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
107         boolean showBorders = portletData.getBoolean("showBorders");
108         long linkToPlid = GetterUtil.getLong(
109             portletData.getString("portletLinksTarget"));
110 
111         JSONObject titles = portletData.getJSONObject("titles");
112 
113         Locale[] locales = LanguageUtil.getAvailableLocales();
114 
115         for (int i = 0; i < locales.length; i++) {
116             String languageId = LocaleUtil.toLanguageId(locales[i]);
117 
118             String title = null;
119 
120             if (titles.has(languageId)) {
121                 title = GetterUtil.getString(titles.getString(languageId));
122             }
123 
124             if (Validator.isNotNull(title)) {
125                 portletSetup.setValue(
126                     "portlet-setup-title-" + languageId, title);
127             }
128             else {
129                 portletSetup.reset("portlet-setup-title-" + languageId);
130             }
131         }
132 
133         portletSetup.setValue(
134             "portlet-setup-use-custom-title", String.valueOf(useCustomTitle));
135         portletSetup.setValue(
136             "portlet-setup-show-borders", String.valueOf(showBorders));
137 
138         if (linkToPlid > 0) {
139             portletSetup.setValue(
140                 "portlet-setup-link-to-plid", String.valueOf(linkToPlid));
141         }
142         else {
143             portletSetup.reset("portlet-setup-link-to-plid");
144         }
145 
146         portletSetup.setValue("portlet-setup-css", css);
147 
148         JSONObject wapData = jsonObj.getJSONObject("wapData");
149 
150         String wapTitle = wapData.getString("title");
151         String wapInitialWindowState = wapData.getString("initialWindowState");
152 
153         portletSetup.setValue("lfr-wap-title", wapTitle);
154         portletSetup.setValue(
155             "lfr-wap-initial-window-state", wapInitialWindowState);
156 
157         portletSetup.store();
158 
159         InvokerPortletImpl.clearResponse(
160             session, layout.getPrimaryKey(), portletId,
161             LanguageUtil.getLanguageId(request));
162 
163         return null;
164     }
165 
166     private static Log _log =
167          LogFactoryUtil.getLog(UpdateLookAndFeelAction.class);
168 
169 }