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    
105                    for (int i = 0; i < locales.length; i++) {
106                            String languageId = LocaleUtil.toLanguageId(locales[i]);
107    
108                            String title = null;
109    
110                            if (titles.has(languageId)) {
111                                    title = GetterUtil.getString(titles.getString(languageId));
112                            }
113    
114                            if (Validator.isNotNull(title)) {
115                                    portletSetup.setValue("portletSetupTitle_" + languageId, title);
116                            }
117                            else {
118                                    portletSetup.reset("portletSetupTitle_" + languageId);
119                            }
120                    }
121    
122                    portletSetup.setValue(
123                            "portletSetupUseCustomTitle", String.valueOf(useCustomTitle));
124    
125                    if (Validator.isNotNull(showBorders)) {
126                            boolean showBordersBoolean = portletData.getBoolean("showBorders");
127    
128                            portletSetup.setValue(
129                                    "portletSetupShowBorders", String.valueOf(showBordersBoolean));
130                    }
131                    else {
132                            portletSetup.reset("portletSetupShowBorders");
133                    }
134    
135                    if (Validator.isNotNull(linkToLayoutUuid)) {
136                            portletSetup.setValue(
137                                    "portletSetupLinkToLayoutUuid", linkToLayoutUuid);
138                    }
139                    else {
140                            portletSetup.reset("portletSetupLinkToLayoutUuid");
141                    }
142    
143                    portletSetup.setValue("portletSetupCss", css);
144    
145                    JSONObject wapData = jsonObj.getJSONObject("wapData");
146    
147                    String wapTitle = wapData.getString("title");
148                    String wapInitialWindowState = wapData.getString("initialWindowState");
149    
150                    portletSetup.setValue("lfrWapTitle", wapTitle);
151                    portletSetup.setValue(
152                            "lfrWapInitialWindowState", wapInitialWindowState);
153    
154                    portletSetup.store();
155    
156                    InvokerPortletImpl.clearResponse(
157                            session, layout.getPrimaryKey(), portletId,
158                            LanguageUtil.getLanguageId(request));
159    
160                    return null;
161            }
162    
163            private static Log _log = LogFactoryUtil.getLog(
164                    UpdateLookAndFeelAction.class);
165    
166    }