1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.portletconfiguration.action;
16  
17  import com.liferay.portal.kernel.json.JSONFactoryUtil;
18  import com.liferay.portal.kernel.json.JSONObject;
19  import com.liferay.portal.kernel.language.LanguageUtil;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.LocaleUtil;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.Layout;
27  import com.liferay.portal.security.permission.ActionKeys;
28  import com.liferay.portal.security.permission.PermissionChecker;
29  import com.liferay.portal.service.permission.PortletPermissionUtil;
30  import com.liferay.portal.struts.JSONAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.InvokerPortletImpl;
34  import com.liferay.portlet.PortletPreferencesFactoryUtil;
35  
36  import java.util.Locale;
37  
38  import javax.portlet.PortletPreferences;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import javax.servlet.http.HttpSession;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="UpdateLookAndFeelAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Wilson Man
52   */
53  public class UpdateLookAndFeelAction extends JSONAction {
54  
55      public String getJSON(
56              ActionMapping mapping, ActionForm form, HttpServletRequest request,
57              HttpServletResponse response)
58          throws Exception {
59  
60          HttpSession session = request.getSession();
61  
62          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
63              WebKeys.THEME_DISPLAY);
64  
65          Layout layout = themeDisplay.getLayout();
66  
67          PermissionChecker permissionChecker =
68              themeDisplay.getPermissionChecker();
69  
70          String portletId = ParamUtil.getString(request, "portletId");
71  
72          if (!PortletPermissionUtil.contains(
73                  permissionChecker, themeDisplay.getPlid(), portletId,
74                  ActionKeys.CONFIGURATION)) {
75  
76              return null;
77          }
78  
79          PortletPreferences portletSetup =
80              PortletPreferencesFactoryUtil.getLayoutPortletSetup(
81                  layout, portletId);
82  
83          String css = ParamUtil.getString(request, "css");
84  
85          if (_log.isDebugEnabled()) {
86              _log.debug("Updating css " + css);
87          }
88  
89          JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
90  
91          JSONObject portletData = jsonObj.getJSONObject("portletData");
92  
93          jsonObj.remove("portletData");
94  
95          css = jsonObj.toString();
96  
97          boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
98          boolean showBorders = portletData.getBoolean("showBorders");
99          long linkToLayoutId = GetterUtil.getLong(
100             portletData.getString("portletLinksTarget"));
101 
102         JSONObject titles = portletData.getJSONObject("titles");
103 
104         Locale[] locales = LanguageUtil.getAvailableLocales();
105 
106         for (int i = 0; i < locales.length; i++) {
107             String languageId = LocaleUtil.toLanguageId(locales[i]);
108 
109             String title = null;
110 
111             if (titles.has(languageId)) {
112                 title = GetterUtil.getString(titles.getString(languageId));
113             }
114 
115             if (Validator.isNotNull(title)) {
116                 portletSetup.setValue(
117                     "portlet-setup-title-" + languageId, title);
118             }
119             else {
120                 portletSetup.reset("portlet-setup-title-" + languageId);
121             }
122         }
123 
124         portletSetup.setValue(
125             "portlet-setup-use-custom-title", String.valueOf(useCustomTitle));
126         portletSetup.setValue(
127             "portlet-setup-show-borders", String.valueOf(showBorders));
128 
129         if (linkToLayoutId > 0) {
130             portletSetup.setValue(
131                 "portlet-setup-link-to-layout-id",
132                 String.valueOf(linkToLayoutId));
133         }
134         else {
135             portletSetup.reset("portlet-setup-link-to-layout-id");
136         }
137 
138         portletSetup.setValue("portlet-setup-css", css);
139 
140         JSONObject wapData = jsonObj.getJSONObject("wapData");
141 
142         String wapTitle = wapData.getString("title");
143         String wapInitialWindowState = wapData.getString("initialWindowState");
144 
145         portletSetup.setValue("lfr-wap-title", wapTitle);
146         portletSetup.setValue(
147             "lfr-wap-initial-window-state", wapInitialWindowState);
148 
149         portletSetup.store();
150 
151         InvokerPortletImpl.clearResponse(
152             session, layout.getPrimaryKey(), portletId,
153             LanguageUtil.getLanguageId(request));
154 
155         return null;
156     }
157 
158     private static Log _log = LogFactoryUtil.getLog(
159         UpdateLookAndFeelAction.class);
160 
161 }