1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class UpdateLookAndFeelAction extends JSONAction {
62  
63      public String getJSON(
64              ActionMapping mapping, ActionForm form, HttpServletRequest request,
65              HttpServletResponse response)
66          throws Exception {
67  
68          HttpSession session = request.getSession();
69  
70          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
71              WebKeys.THEME_DISPLAY);
72  
73          Layout layout = themeDisplay.getLayout();
74  
75          PermissionChecker permissionChecker =
76              themeDisplay.getPermissionChecker();
77  
78          String portletId = ParamUtil.getString(request, "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.getLayoutPortletSetup(
89                  layout, portletId);
90  
91          String css = ParamUtil.getString(request, "css");
92  
93          if (_log.isDebugEnabled()) {
94              _log.debug("Updating css " + css);
95          }
96  
97          JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
98  
99          JSONObject portletData = jsonObj.getJSONObject("portletData");
100 
101         jsonObj.remove("portletData");
102 
103         css = jsonObj.toString();
104 
105         boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
106         boolean showBorders = portletData.getBoolean("showBorders");
107         long linkToLayoutId = GetterUtil.getLong(
108             portletData.getString("portletLinksTarget"));
109 
110         JSONObject titles = portletData.getJSONObject("titles");
111 
112         Locale[] locales = LanguageUtil.getAvailableLocales();
113 
114         for (int i = 0; i < locales.length; i++) {
115             String languageId = LocaleUtil.toLanguageId(locales[i]);
116 
117             String title = null;
118 
119             if (titles.has(languageId)) {
120                 title = GetterUtil.getString(titles.getString(languageId));
121             }
122 
123             if (Validator.isNotNull(title)) {
124                 portletSetup.setValue(
125                     "portlet-setup-title-" + languageId, title);
126             }
127             else {
128                 portletSetup.reset("portlet-setup-title-" + languageId);
129             }
130         }
131 
132         portletSetup.setValue(
133             "portlet-setup-use-custom-title", String.valueOf(useCustomTitle));
134         portletSetup.setValue(
135             "portlet-setup-show-borders", String.valueOf(showBorders));
136 
137         if (linkToLayoutId > 0) {
138             portletSetup.setValue(
139                 "portlet-setup-link-to-layout-id",
140                 String.valueOf(linkToLayoutId));
141         }
142         else {
143             portletSetup.reset("portlet-setup-link-to-layout-id");
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 }