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.language.LanguageUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portlet.PortletSetupUtil;
024    
025    import java.util.HashMap;
026    import java.util.Locale;
027    import java.util.Map;
028    
029    import javax.portlet.PortletPreferences;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PortletConfigurationUtil {
035    
036            public static String getPortletCustomCSSClassName(
037                            PortletPreferences portletSetup)
038                    throws Exception {
039    
040                    String customCSSClassName = StringPool.BLANK;
041    
042                    String css = portletSetup.getValue("portletSetupCss", StringPool.BLANK);
043    
044                    if (Validator.isNotNull(css)) {
045                            JSONObject cssJSONObject = PortletSetupUtil.cssToJSONObject(
046                                    portletSetup, css);
047    
048                            JSONObject advancedDataJSONObject = cssJSONObject.getJSONObject(
049                                    "advancedData");
050    
051                            if (advancedDataJSONObject != null) {
052                                    customCSSClassName = advancedDataJSONObject.getString(
053                                            "customCSSClassName");
054                            }
055                    }
056    
057                    return customCSSClassName;
058            }
059    
060            public static String getPortletTitle(
061                    PortletPreferences portletSetup, String languageId) {
062    
063                    if (!isUseCustomTitle(portletSetup)) {
064                            return null;
065                    }
066    
067                    return portletSetup.getValue("portletSetupTitle_" + languageId, null);
068            }
069    
070            public static Map<Locale, String> getPortletTitleMap(
071                    PortletPreferences portletSetup) {
072    
073                    if (!isUseCustomTitle(portletSetup)) {
074                            return null;
075                    }
076    
077                    Map<Locale, String> map = new HashMap<>();
078    
079                    boolean empty = true;
080    
081                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
082                            String portletTitle = GetterUtil.getString(
083                                    getPortletTitle(portletSetup, LocaleUtil.toLanguageId(locale)));
084    
085                            map.put(locale, portletTitle);
086    
087                            if (Validator.isNotNull(portletTitle)) {
088                                    empty = false;
089                            }
090                    }
091    
092                    if (!empty) {
093                            return map;
094                    }
095    
096                    return null;
097            }
098    
099            protected static boolean isUseCustomTitle(PortletPreferences portletSetup) {
100                    return GetterUtil.getBoolean(
101                            portletSetup.getValue("portletSetupUseCustomTitle", null));
102            }
103    
104    }