1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.portletconfiguration.action;
21  
22  import com.liferay.portal.kernel.json.JSONFactoryUtil;
23  import com.liferay.portal.kernel.json.JSONObject;
24  import com.liferay.portal.kernel.language.LanguageUtil;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.LocaleUtil;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.security.permission.ActionKeys;
33  import com.liferay.portal.security.permission.PermissionChecker;
34  import com.liferay.portal.service.permission.PortletPermissionUtil;
35  import com.liferay.portal.struts.JSONAction;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.InvokerPortletImpl;
39  import com.liferay.portlet.PortletPreferencesFactoryUtil;
40  
41  import java.util.Locale;
42  
43  import javax.portlet.PortletPreferences;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletResponse;
47  import javax.servlet.http.HttpSession;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="UpdateLookAndFeelAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   * @author Wilson Man
57   *
58   */
59  public class UpdateLookAndFeelAction extends JSONAction {
60  
61      public String getJSON(
62              ActionMapping mapping, ActionForm form, HttpServletRequest request,
63              HttpServletResponse response)
64          throws Exception {
65  
66          HttpSession session = request.getSession();
67  
68          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
69              WebKeys.THEME_DISPLAY);
70  
71          Layout layout = themeDisplay.getLayout();
72  
73          PermissionChecker permissionChecker =
74              themeDisplay.getPermissionChecker();
75  
76          String portletId = ParamUtil.getString(request, "portletId");
77  
78          if (!PortletPermissionUtil.contains(
79                  permissionChecker, themeDisplay.getPlid(), portletId,
80                  ActionKeys.CONFIGURATION)) {
81  
82              return null;
83          }
84  
85          PortletPreferences portletSetup =
86              PortletPreferencesFactoryUtil.getLayoutPortletSetup(
87                  layout, portletId);
88  
89          String css = ParamUtil.getString(request, "css");
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug("Updating css " + css);
93          }
94  
95          JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
96  
97          JSONObject portletData = jsonObj.getJSONObject("portletData");
98  
99          jsonObj.remove("portletData");
100 
101         css = jsonObj.toString();
102 
103         boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
104         boolean showBorders = portletData.getBoolean("showBorders");
105         long linkToLayoutId = GetterUtil.getLong(
106             portletData.getString("portletLinksTarget"));
107 
108         JSONObject titles = portletData.getJSONObject("titles");
109 
110         Locale[] locales = LanguageUtil.getAvailableLocales();
111 
112         for (int i = 0; i < locales.length; i++) {
113             String languageId = LocaleUtil.toLanguageId(locales[i]);
114 
115             String title = null;
116 
117             if (titles.has(languageId)) {
118                 title = GetterUtil.getString(titles.getString(languageId));
119             }
120 
121             if (Validator.isNotNull(title)) {
122                 portletSetup.setValue(
123                     "portlet-setup-title-" + languageId, title);
124             }
125             else {
126                 portletSetup.reset("portlet-setup-title-" + languageId);
127             }
128         }
129 
130         portletSetup.setValue(
131             "portlet-setup-use-custom-title", String.valueOf(useCustomTitle));
132         portletSetup.setValue(
133             "portlet-setup-show-borders", String.valueOf(showBorders));
134 
135         if (linkToLayoutId > 0) {
136             portletSetup.setValue(
137                 "portlet-setup-link-to-layout-id",
138                 String.valueOf(linkToLayoutId));
139         }
140         else {
141             portletSetup.reset("portlet-setup-link-to-layout-id");
142         }
143 
144         portletSetup.setValue("portlet-setup-css", css);
145 
146         JSONObject wapData = jsonObj.getJSONObject("wapData");
147 
148         String wapTitle = wapData.getString("title");
149         String wapInitialWindowState = wapData.getString("initialWindowState");
150 
151         portletSetup.setValue("lfr-wap-title", wapTitle);
152         portletSetup.setValue(
153             "lfr-wap-initial-window-state", wapInitialWindowState);
154 
155         portletSetup.store();
156 
157         InvokerPortletImpl.clearResponse(
158             session, layout.getPrimaryKey(), portletId,
159             LanguageUtil.getLanguageId(request));
160 
161         return null;
162     }
163 
164     private static Log _log =
165         LogFactoryUtil.getLog(UpdateLookAndFeelAction.class);
166 
167 }