001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.portletconfiguration.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.security.permission.PermissionChecker;
029    import com.liferay.portal.service.permission.PortletPermissionUtil;
030    import com.liferay.portal.struts.JSONAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.InvokerPortletImpl;
035    import com.liferay.portlet.PortletPreferencesFactoryUtil;
036    
037    import java.util.Locale;
038    
039    import javax.portlet.PortletPreferences;
040    
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    import javax.servlet.http.HttpSession;
044    
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionMapping;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Wilson Man
051     */
052    public class UpdateLookAndFeelAction extends JSONAction {
053    
054            @Override
055            public String getJSON(
056                            ActionMapping actionMapping, ActionForm actionForm,
057                            HttpServletRequest request, HttpServletResponse response)
058                    throws Exception {
059    
060                    HttpSession session = request.getSession();
061    
062                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
063                            WebKeys.THEME_DISPLAY);
064    
065                    Layout layout = themeDisplay.getLayout();
066    
067                    PermissionChecker permissionChecker =
068                            themeDisplay.getPermissionChecker();
069    
070                    String portletId = ParamUtil.getString(request, "portletId");
071    
072                    if (!PortletPermissionUtil.contains(
073                                    permissionChecker, layout, portletId,
074                                    ActionKeys.CONFIGURATION)) {
075    
076                            return null;
077                    }
078    
079                    PortletPreferences portletSetup =
080                            PortletPreferencesFactoryUtil.getStrictLayoutPortletSetup(
081                                    layout, portletId);
082    
083                    String css = ParamUtil.getString(request, "css");
084    
085                    if (_log.isDebugEnabled()) {
086                            _log.debug("Updating css " + css);
087                    }
088    
089                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
090    
091                    JSONObject portletData = jsonObj.getJSONObject("portletData");
092    
093                    jsonObj.remove("portletData");
094    
095                    css = jsonObj.toString();
096    
097                    boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
098                    String showBorders = portletData.getString("showBorders");
099                    String linkToLayoutUuid = GetterUtil.getString(
100                            portletData.getString("portletLinksTarget"));
101    
102                    JSONObject titles = portletData.getJSONObject("titles");
103    
104                    Locale[] locales = LanguageUtil.getAvailableLocales(
105                            themeDisplay.getSiteGroupId());
106    
107                    for (int i = 0; i < locales.length; i++) {
108                            String languageId = LocaleUtil.toLanguageId(locales[i]);
109    
110                            String title = null;
111    
112                            if (titles.has(languageId)) {
113                                    title = GetterUtil.getString(titles.getString(languageId));
114                            }
115    
116                            if (Validator.isNotNull(title)) {
117                                    portletSetup.setValue("portletSetupTitle_" + languageId, title);
118                            }
119                            else {
120                                    portletSetup.reset("portletSetupTitle_" + languageId);
121                            }
122                    }
123    
124                    portletSetup.setValue(
125                            "portletSetupUseCustomTitle", String.valueOf(useCustomTitle));
126    
127                    if (Validator.isNotNull(showBorders)) {
128                            boolean showBordersBoolean = portletData.getBoolean("showBorders");
129    
130                            portletSetup.setValue(
131                                    "portletSetupShowBorders", String.valueOf(showBordersBoolean));
132                    }
133                    else {
134                            portletSetup.reset("portletSetupShowBorders");
135                    }
136    
137                    if (Validator.isNotNull(linkToLayoutUuid)) {
138                            portletSetup.setValue(
139                                    "portletSetupLinkToLayoutUuid", linkToLayoutUuid);
140                    }
141                    else {
142                            portletSetup.reset("portletSetupLinkToLayoutUuid");
143                    }
144    
145                    portletSetup.setValue("portletSetupCss", css);
146    
147                    if (PropsValues.MOBILE_DEVICE_STYLING_WAP_ENABLED) {
148                            JSONObject wapData = jsonObj.getJSONObject("wapData");
149    
150                            String wapInitialWindowState = wapData.getString(
151                                    "initialWindowState");
152                            String wapTitle = wapData.getString("title");
153    
154                            portletSetup.setValue(
155                                    "lfrWapInitialWindowState", wapInitialWindowState);
156                            portletSetup.setValue("lfrWapTitle", wapTitle);
157                    }
158                    else {
159                            portletSetup.reset("lfrWapInitialWindowState");
160                            portletSetup.reset("lfrWapTitle");
161                    }
162    
163                    portletSetup.store();
164    
165                    InvokerPortletImpl.clearResponse(
166                            session, layout.getPrimaryKey(), portletId,
167                            LanguageUtil.getLanguageId(request));
168    
169                    return null;
170            }
171    
172            private static Log _log = LogFactoryUtil.getLog(
173                    UpdateLookAndFeelAction.class);
174    
175    }