001    /**
002     * Copyright (c) 2000-2010 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            public String getJSON(
054                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
055                            HttpServletResponse response)
056                    throws Exception {
057    
058                    HttpSession session = request.getSession();
059    
060                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
061                            WebKeys.THEME_DISPLAY);
062    
063                    Layout layout = themeDisplay.getLayout();
064    
065                    PermissionChecker permissionChecker =
066                            themeDisplay.getPermissionChecker();
067    
068                    String portletId = ParamUtil.getString(request, "portletId");
069    
070                    if (!PortletPermissionUtil.contains(
071                                    permissionChecker, themeDisplay.getPlid(), portletId,
072                                    ActionKeys.CONFIGURATION)) {
073    
074                            return null;
075                    }
076    
077                    PortletPreferences portletSetup =
078                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
079                                    layout, portletId);
080    
081                    String css = ParamUtil.getString(request, "css");
082    
083                    if (_log.isDebugEnabled()) {
084                            _log.debug("Updating css " + css);
085                    }
086    
087                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
088    
089                    JSONObject portletData = jsonObj.getJSONObject("portletData");
090    
091                    jsonObj.remove("portletData");
092    
093                    css = jsonObj.toString();
094    
095                    boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
096                    boolean showBorders = portletData.getBoolean("showBorders");
097                    String linkToLayoutUuid = GetterUtil.getString(
098                            portletData.getString("portletLinksTarget"));
099    
100                    JSONObject titles = portletData.getJSONObject("titles");
101    
102                    Locale[] locales = LanguageUtil.getAvailableLocales();
103    
104                    for (int i = 0; i < locales.length; i++) {
105                            String languageId = LocaleUtil.toLanguageId(locales[i]);
106    
107                            String title = null;
108    
109                            if (titles.has(languageId)) {
110                                    title = GetterUtil.getString(titles.getString(languageId));
111                            }
112    
113                            if (Validator.isNotNull(title)) {
114                                    portletSetup.setValue(
115                                            "portlet-setup-title-" + languageId, title);
116                            }
117                            else {
118                                    portletSetup.reset("portlet-setup-title-" + languageId);
119                            }
120                    }
121    
122                    portletSetup.setValue(
123                            "portlet-setup-use-custom-title", String.valueOf(useCustomTitle));
124                    portletSetup.setValue(
125                            "portlet-setup-show-borders", String.valueOf(showBorders));
126    
127                    if (Validator.isNotNull(linkToLayoutUuid)) {
128                            portletSetup.setValue(
129                                    "portlet-setup-link-to-layout-uuid",linkToLayoutUuid);
130                    }
131                    else {
132                            portletSetup.reset("portlet-setup-link-to-layout-uuid");
133                    }
134    
135                    portletSetup.setValue("portlet-setup-css", css);
136    
137                    JSONObject wapData = jsonObj.getJSONObject("wapData");
138    
139                    String wapTitle = wapData.getString("title");
140                    String wapInitialWindowState = wapData.getString("initialWindowState");
141    
142                    portletSetup.setValue("lfr-wap-title", wapTitle);
143                    portletSetup.setValue(
144                            "lfr-wap-initial-window-state", wapInitialWindowState);
145    
146                    portletSetup.store();
147    
148                    InvokerPortletImpl.clearResponse(
149                            session, layout.getPrimaryKey(), portletId,
150                            LanguageUtil.getLanguageId(request));
151    
152                    return null;
153            }
154    
155            private static Log _log = LogFactoryUtil.getLog(
156                    UpdateLookAndFeelAction.class);
157    
158    }