001    /**
002     * Copyright (c) 2000-present 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.model.PortletConstants;
028    import com.liferay.portal.security.permission.ActionKeys;
029    import com.liferay.portal.security.permission.PermissionChecker;
030    import com.liferay.portal.service.permission.PortletPermissionUtil;
031    import com.liferay.portal.struts.JSONAction;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.PropsValues;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.InvokerPortletImpl;
037    import com.liferay.portlet.PortletPreferencesFactoryUtil;
038    
039    import java.util.Locale;
040    
041    import javax.portlet.PortletPreferences;
042    
043    import javax.servlet.http.HttpServletRequest;
044    import javax.servlet.http.HttpServletResponse;
045    import javax.servlet.http.HttpSession;
046    
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     * @author Wilson Man
053     */
054    public class UpdateLookAndFeelAction extends JSONAction {
055    
056            @Override
057            public String getJSON(
058                            ActionMapping actionMapping, ActionForm actionForm,
059                            HttpServletRequest request, HttpServletResponse response)
060                    throws Exception {
061    
062                    HttpSession session = request.getSession();
063    
064                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
065                            WebKeys.THEME_DISPLAY);
066    
067                    Layout layout = themeDisplay.getLayout();
068    
069                    PermissionChecker permissionChecker =
070                            themeDisplay.getPermissionChecker();
071    
072                    String portletId = ParamUtil.getString(request, "portletId");
073    
074                    if (!PortletPermissionUtil.contains(
075                                    permissionChecker, layout, portletId,
076                                    ActionKeys.CONFIGURATION)) {
077    
078                            return null;
079                    }
080    
081                    PortletPreferences portletSetup =
082                            PortletPreferencesFactoryUtil.getStrictLayoutPortletSetup(
083                                    layout, portletId);
084    
085                    String css = ParamUtil.getString(request, "css");
086    
087                    if (_log.isDebugEnabled()) {
088                            _log.debug("Updating css " + css);
089                    }
090    
091                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
092    
093                    JSONObject portletData = jsonObj.getJSONObject("portletData");
094    
095                    jsonObj.remove("portletData");
096    
097                    css = jsonObj.toString();
098    
099                    boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
100                    String showBorders = portletData.getString("showBorders");
101                    String linkToLayoutUuid = GetterUtil.getString(
102                            portletData.getString("portletLinksTarget"));
103    
104                    JSONObject titles = portletData.getJSONObject("titles");
105    
106                    Locale[] locales = LanguageUtil.getAvailableLocales(
107                            themeDisplay.getSiteGroupId());
108    
109                    for (int i = 0; i < locales.length; i++) {
110                            String languageId = LocaleUtil.toLanguageId(locales[i]);
111    
112                            String title = null;
113    
114                            if (titles.has(languageId)) {
115                                    title = GetterUtil.getString(titles.getString(languageId));
116                            }
117    
118                            String rootPortletId = PortletConstants.getRootPortletId(portletId);
119    
120                            String defaultPortletTitle = PortalUtil.getPortletTitle(
121                                    rootPortletId, languageId);
122    
123                            if ((title != null) &&
124                                    !Validator.equals(defaultPortletTitle, title)) {
125    
126                                    portletSetup.setValue("portletSetupTitle_" + languageId, title);
127                            }
128                            else {
129                                    portletSetup.reset("portletSetupTitle_" + languageId);
130                            }
131                    }
132    
133                    portletSetup.setValue(
134                            "portletSetupUseCustomTitle", String.valueOf(useCustomTitle));
135    
136                    if (Validator.isNotNull(showBorders)) {
137                            boolean showBordersBoolean = portletData.getBoolean("showBorders");
138    
139                            portletSetup.setValue(
140                                    "portletSetupShowBorders", String.valueOf(showBordersBoolean));
141                    }
142                    else {
143                            portletSetup.reset("portletSetupShowBorders");
144                    }
145    
146                    if (Validator.isNotNull(linkToLayoutUuid)) {
147                            portletSetup.setValue(
148                                    "portletSetupLinkToLayoutUuid", linkToLayoutUuid);
149                    }
150                    else {
151                            portletSetup.reset("portletSetupLinkToLayoutUuid");
152                    }
153    
154                    portletSetup.setValue("portletSetupCss", css);
155    
156                    if (PropsValues.MOBILE_DEVICE_STYLING_WAP_ENABLED) {
157                            JSONObject wapData = jsonObj.getJSONObject("wapData");
158    
159                            String wapInitialWindowState = wapData.getString(
160                                    "initialWindowState");
161                            String wapTitle = wapData.getString("title");
162    
163                            portletSetup.setValue(
164                                    "lfrWapInitialWindowState", wapInitialWindowState);
165                            portletSetup.setValue("lfrWapTitle", wapTitle);
166                    }
167                    else {
168                            portletSetup.reset("lfrWapInitialWindowState");
169                            portletSetup.reset("lfrWapTitle");
170                    }
171    
172                    portletSetup.store();
173    
174                    InvokerPortletImpl.clearResponse(
175                            session, layout.getPrimaryKey(), portletId,
176                            LanguageUtil.getLanguageId(request));
177    
178                    return null;
179            }
180    
181            private static Log _log = LogFactoryUtil.getLog(
182                    UpdateLookAndFeelAction.class);
183    
184    }