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;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    
027    import java.util.Locale;
028    
029    import javax.portlet.PortletPreferences;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PortletSetupUtil {
035    
036            public static JSONObject cssToJSONObject(PortletPreferences portletSetup)
037                    throws Exception {
038    
039                    String css = portletSetup.getValue("portletSetupCss", StringPool.BLANK);
040    
041                    return _toJSONObject(portletSetup, css);
042            }
043    
044            public static JSONObject cssToJSONObject(
045                            PortletPreferences portletSetup, String css)
046                    throws Exception {
047    
048                    return _toJSONObject(portletSetup, css);
049            }
050    
051            public static String cssToJSONString(PortletPreferences portletSetup) {
052                    String css = portletSetup.getValue("portletSetupCss", StringPool.BLANK);
053    
054                    try {
055                            JSONObject jsonObject = _toJSONObject(portletSetup, css);
056    
057                            return jsonObject.toString();
058                    }
059                    catch (Exception e) {
060                            css = null;
061    
062                            if (_log.isWarnEnabled()) {
063                                    _log.warn(e);
064                            }
065                    }
066    
067                    return css;
068            }
069    
070            private static JSONObject _toJSONObject(
071                            PortletPreferences portletSetup, String css)
072                    throws Exception {
073    
074                    if (_log.isDebugEnabled()) {
075                            _log.debug("Transform CSS to JSON " + css);
076                    }
077    
078                    JSONObject cssJSONObject = null;
079    
080                    if (Validator.isNotNull(css)) {
081                            cssJSONObject = JSONFactoryUtil.createJSONObject(css);
082    
083                            cssJSONObject.put("hasCssValue", true);
084                    }
085                    else {
086                            cssJSONObject = JSONFactoryUtil.createJSONObject();
087                    }
088    
089                    JSONObject portletDataJSONObject = JSONFactoryUtil.createJSONObject();
090    
091                    cssJSONObject.put("portletData", portletDataJSONObject);
092    
093                    JSONObject titlesJSONObject = JSONFactoryUtil.createJSONObject();
094    
095                    portletDataJSONObject.put("titles", titlesJSONObject);
096    
097                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
098                            String languageId = LocaleUtil.toLanguageId(locale);
099    
100                            String title = portletSetup.getValue(
101                                    "portletSetupTitle_" + languageId, null);
102    
103                            if (Validator.isNotNull(languageId)) {
104                                    titlesJSONObject.put(languageId, title);
105                            }
106                    }
107    
108                    String linkToLayoutUuid = GetterUtil.getString(
109                            portletSetup.getValue("portletSetupLinkToLayoutUuid", null));
110                    boolean useCustomTitle = GetterUtil.getBoolean(
111                            portletSetup.getValue("portletSetupUseCustomTitle", null));
112                    String portletDecoratorId = GetterUtil.getString(
113                            portletSetup.getValue("portletSetupPortletDecoratorId", null));
114    
115                    portletDataJSONObject.put("portletDecoratorId", portletDecoratorId);
116                    portletDataJSONObject.put("portletLinksTarget", linkToLayoutUuid);
117                    portletDataJSONObject.put("useCustomTitle", useCustomTitle);
118    
119                    return cssJSONObject;
120            }
121    
122            private static final Log _log = LogFactoryUtil.getLog(
123                    PortletSetupUtil.class);
124    
125    }