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.WebKeys;
033    import com.liferay.portlet.InvokerPortletImpl;
034    import com.liferay.portlet.PortletPreferencesFactoryUtil;
035    
036    import java.util.Locale;
037    
038    import javax.portlet.PortletPreferences;
039    
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    import javax.servlet.http.HttpSession;
043    
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Wilson Man
050     */
051    public class UpdateLookAndFeelAction extends JSONAction {
052    
053            @Override
054            public String getJSON(
055                            ActionMapping actionMapping, ActionForm actionForm,
056                            HttpServletRequest request, HttpServletResponse response)
057                    throws Exception {
058    
059                    HttpSession session = request.getSession();
060    
061                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
062                            WebKeys.THEME_DISPLAY);
063    
064                    Layout layout = themeDisplay.getLayout();
065    
066                    PermissionChecker permissionChecker =
067                            themeDisplay.getPermissionChecker();
068    
069                    String portletId = ParamUtil.getString(request, "portletId");
070    
071                    if (!PortletPermissionUtil.contains(
072                                    permissionChecker, layout, portletId,
073                                    ActionKeys.CONFIGURATION)) {
074    
075                            return null;
076                    }
077    
078                    PortletPreferences portletSetup =
079                            PortletPreferencesFactoryUtil.getStrictLayoutPortletSetup(
080                                    layout, portletId);
081    
082                    String css = ParamUtil.getString(request, "css");
083    
084                    if (_log.isDebugEnabled()) {
085                            _log.debug("Updating css " + css);
086                    }
087    
088                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
089    
090                    JSONObject portletData = jsonObj.getJSONObject("portletData");
091    
092                    jsonObj.remove("portletData");
093    
094                    css = jsonObj.toString();
095    
096                    boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
097                    String showBorders = portletData.getString("showBorders");
098                    String linkToLayoutUuid = GetterUtil.getString(
099                            portletData.getString("portletLinksTarget"));
100    
101                    JSONObject titles = portletData.getJSONObject("titles");
102    
103                    Locale[] locales = LanguageUtil.getAvailableLocales(
104                            themeDisplay.getSiteGroupId());
105    
106                    for (int i = 0; i < locales.length; i++) {
107                            String languageId = LocaleUtil.toLanguageId(locales[i]);
108    
109                            String title = null;
110    
111                            if (titles.has(languageId)) {
112                                    title = GetterUtil.getString(titles.getString(languageId));
113                            }
114    
115                            if (Validator.isNotNull(title)) {
116                                    portletSetup.setValue("portletSetupTitle_" + languageId, title);
117                            }
118                            else {
119                                    portletSetup.reset("portletSetupTitle_" + languageId);
120                            }
121                    }
122    
123                    portletSetup.setValue(
124                            "portletSetupUseCustomTitle", String.valueOf(useCustomTitle));
125    
126                    if (Validator.isNotNull(showBorders)) {
127                            boolean showBordersBoolean = portletData.getBoolean("showBorders");
128    
129                            portletSetup.setValue(
130                                    "portletSetupShowBorders", String.valueOf(showBordersBoolean));
131                    }
132                    else {
133                            portletSetup.reset("portletSetupShowBorders");
134                    }
135    
136                    if (Validator.isNotNull(linkToLayoutUuid)) {
137                            portletSetup.setValue(
138                                    "portletSetupLinkToLayoutUuid", linkToLayoutUuid);
139                    }
140                    else {
141                            portletSetup.reset("portletSetupLinkToLayoutUuid");
142                    }
143    
144                    portletSetup.setValue("portletSetupCss", 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("lfrWapTitle", wapTitle);
152                    portletSetup.setValue(
153                            "lfrWapInitialWindowState", 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 = LogFactoryUtil.getLog(
165                    UpdateLookAndFeelAction.class);
166    
167    }