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.util;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portlet.PortletSetupUtil;
025    
026    import javax.portlet.PortletPreferences;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class PortletConfigurationUtil {
032    
033            public static String getPortletCustomCSSClassName(
034                            PortletPreferences portletSetup)
035                    throws Exception {
036    
037                    String customCSSClassName = StringPool.BLANK;
038    
039                    String css = portletSetup.getValue("portletSetupCss", StringPool.BLANK);
040    
041                    if (Validator.isNotNull(css)) {
042                            JSONObject cssJSONObject = PortletSetupUtil.cssToJSONObject(
043                                    portletSetup, css);
044    
045                            JSONObject advancedDataJSONObject = cssJSONObject.getJSONObject(
046                                    "advancedData");
047    
048                            if (advancedDataJSONObject != null) {
049                                    customCSSClassName = advancedDataJSONObject.getString(
050                                            "customCSSClassName");
051                            }
052                    }
053    
054                    return customCSSClassName;
055            }
056    
057            public static String getPortletTitle(
058                    PortletPreferences portletSetup, String languageId) {
059    
060                    String useCustomTitle = GetterUtil.getString(
061                            portletSetup.getValue(
062                                    "portletSetupUseCustomTitle", StringPool.BLANK));
063    
064                    if (!useCustomTitle.equals("true")) {
065                            return null;
066                    }
067    
068                    String portletTitle = portletSetup.getValue(
069                            "portletSetupTitle_" + languageId, null);
070    
071                    if (Validator.isNotNull(portletTitle)) {
072                            return portletTitle;
073                    }
074    
075                    // For backwards compatibility
076    
077                    String oldPortletTitle = portletSetup.getValue(
078                            "portletSetupTitle", null);
079    
080                    if (Validator.isNull(useCustomTitle) &&
081                            Validator.isNotNull(oldPortletTitle)) {
082    
083                            portletTitle = oldPortletTitle;
084    
085                            try {
086                                    String defaultLanguageId = LocaleUtil.toLanguageId(
087                                            LocaleUtil.getSiteDefault());
088    
089                                    portletSetup.setValue(
090                                            "portletSetupTitle_" + defaultLanguageId, portletTitle);
091                                    portletSetup.setValue(
092                                            "portletSetupUseCustomTitle", Boolean.TRUE.toString());
093    
094                                    portletSetup.store();
095                            }
096                            catch (Exception e) {
097                                    _log.error(e, e);
098                            }
099                    }
100    
101                    return portletTitle;
102            }
103    
104            private static final Log _log = LogFactoryUtil.getLog(
105                    PortletConfigurationUtil.class);
106    
107    }