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.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 defaultLanguageId = LocaleUtil.toLanguageId(
069                            LocaleUtil.getDefault());
070    
071                    String defaultPortletTitle = portletSetup.getValue(
072                            "portletSetupTitle_" + defaultLanguageId, StringPool.BLANK);
073    
074                    String portletTitle = portletSetup.getValue(
075                            "portletSetupTitle_" + languageId, defaultPortletTitle);
076    
077                    if (Validator.isNotNull(portletTitle)) {
078                            return portletTitle;
079                    }
080    
081                    // For backwards compatibility
082    
083                    String oldPortletTitle = portletSetup.getValue(
084                            "portletSetupTitle", null);
085    
086                    if (Validator.isNull(useCustomTitle) &&
087                            Validator.isNotNull(oldPortletTitle)) {
088    
089                            portletTitle = oldPortletTitle;
090    
091                            try {
092                                    portletSetup.setValue(
093                                            "portletSetupTitle_" + defaultLanguageId, portletTitle);
094                                    portletSetup.setValue(
095                                            "portletSetupUseCustomTitle", Boolean.TRUE.toString());
096    
097                                    portletSetup.store();
098                            }
099                            catch (Exception e) {
100                                    _log.error(e, e);
101                            }
102                    }
103    
104                    return portletTitle;
105            }
106    
107            private static Log _log = LogFactoryUtil.getLog(
108                    PortletConfigurationUtil.class);
109    
110    }