001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
040                            "portlet-setup-css", StringPool.BLANK);
041    
042                    if (Validator.isNotNull(css)) {
043                            JSONObject cssJSON = PortletSetupUtil.cssToJSON(portletSetup, css);
044    
045                            JSONObject advancedDataJSON = cssJSON.getJSONObject("advancedData");
046    
047                            if (advancedDataJSON != null) {
048                                    customCSSClassName = advancedDataJSON.getString(
049                                            "customCSSClassName");
050                            }
051                    }
052    
053                    return customCSSClassName;
054            }
055    
056            public static String getPortletTitle(
057                    PortletPreferences portletSetup, String languageId) {
058    
059                    String useCustomTitle = GetterUtil.getString(
060                            portletSetup.getValue(
061                                    "portlet-setup-use-custom-title", StringPool.BLANK));
062    
063                    if (!useCustomTitle.equals("true")) {
064                            return null;
065                    }
066    
067                    String defaultLanguageId = LocaleUtil.toLanguageId(
068                            LocaleUtil.getDefault());
069    
070                    String defaultPortletTitle = portletSetup.getValue(
071                            "portlet-setup-title-" + defaultLanguageId, StringPool.BLANK);
072    
073                    String portletTitle = portletSetup.getValue(
074                            "portlet-setup-title-" + languageId, defaultPortletTitle);
075    
076                    if (Validator.isNull(portletTitle)) {
077    
078                            // For backwards compatibility
079    
080                            String oldPortletTitle = portletSetup.getValue(
081                                    "portlet-setup-title", null);
082    
083                            if (Validator.isNull(useCustomTitle) &&
084                                    Validator.isNotNull(oldPortletTitle)) {
085    
086                                    portletTitle = oldPortletTitle;
087    
088                                    try {
089                                            portletSetup.setValue(
090                                                    "portlet-setup-title-" + defaultLanguageId,
091                                                    portletTitle);
092                                            portletSetup.setValue(
093                                                    "portlet-setup-use-custom-title", "true");
094    
095                                            portletSetup.store();
096                                    }
097                                    catch (Exception e) {
098                                            _log.error(e);
099                                    }
100                            }
101                    }
102    
103                    return portletTitle;
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(
107                    PortletConfigurationUtil.class);
108    
109    }