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.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(portletSetup.getValue(
060                            "portlet-setup-use-custom-title", StringPool.BLANK));
061    
062                    if (useCustomTitle.equals("false")) {
063                            return StringPool.BLANK;
064                    }
065    
066                    String defaultLanguageId = LocaleUtil.toLanguageId(
067                            LocaleUtil.getDefault());
068    
069                    String defaultPortletTitle = portletSetup.getValue(
070                            "portlet-setup-title-" + defaultLanguageId, StringPool.BLANK);
071    
072                    String portletTitle = portletSetup.getValue(
073                            "portlet-setup-title-" + languageId, defaultPortletTitle);
074    
075                    if (Validator.isNull(portletTitle)) {
076    
077                            // For backwards compatibility
078    
079                            String oldPortletTitle = portletSetup.getValue(
080                                    "portlet-setup-title", null);
081    
082                            if (Validator.isNull(useCustomTitle) &&
083                                    Validator.isNotNull(oldPortletTitle)) {
084    
085                                    portletTitle = oldPortletTitle;
086    
087                                    try {
088                                            portletSetup.setValue(
089                                                    "portlet-setup-title-" + defaultLanguageId,
090                                                    portletTitle);
091                                            portletSetup.setValue(
092                                                    "portlet-setup-use-custom-title", "true");
093    
094                                            portletSetup.store();
095                                    }
096                                    catch (Exception e) {
097                                            _log.error(e);
098                                    }
099                            }
100                    }
101    
102                    return portletTitle;
103            }
104    
105            private static Log _log = LogFactoryUtil.getLog(
106                    PortletConfigurationUtil.class);
107    
108    }