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