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;
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(
037                            PortletPreferences portletSetup, String css)
038                    throws Exception {
039    
040                    return _toJSONObject(portletSetup, css);
041            }
042    
043            public static String cssToJSONString(PortletPreferences portletSetup) {
044                    String css = portletSetup.getValue(
045                            "portletSetupCss", StringPool.BLANK);
046    
047                    try {
048                            JSONObject jsonObject = _toJSONObject(portletSetup, css);
049    
050                            return jsonObject.toString();
051                    }
052                    catch (Exception e) {
053                            css = null;
054    
055                            if (_log.isWarnEnabled()) {
056                                    _log.warn(e);
057                            }
058                    }
059    
060                    return css;
061            }
062    
063            private static JSONObject _toJSONObject(
064                            PortletPreferences portletSetup, String css)
065                    throws Exception {
066    
067                    if (_log.isDebugEnabled()) {
068                            _log.debug("Transform CSS to JSON " + css);
069                    }
070    
071                    JSONObject cssJSONObject = null;
072    
073                    if (Validator.isNotNull(css)) {
074                            cssJSONObject = JSONFactoryUtil.createJSONObject(css);
075    
076                            cssJSONObject.put("hasCssValue", true);
077                    }
078                    else {
079                            cssJSONObject = JSONFactoryUtil.createJSONObject();
080                    }
081    
082                    JSONObject portletDataJSONObject = JSONFactoryUtil.createJSONObject();
083    
084                    cssJSONObject.put("portletData", portletDataJSONObject);
085    
086                    JSONObject titlesJSONObject = JSONFactoryUtil.createJSONObject();
087    
088                    portletDataJSONObject.put("titles", titlesJSONObject);
089    
090                    Locale[] locales = LanguageUtil.getAvailableLocales();
091    
092                    for (int i = 0; i < locales.length; i++) {
093                            String languageId = LocaleUtil.toLanguageId(locales[i]);
094    
095                            String title = portletSetup.getValue(
096                                    "portletSetupTitle_" + languageId, null);
097    
098                            if (Validator.isNotNull(languageId)) {
099                                    titlesJSONObject.put(languageId, title);
100                            }
101                    }
102    
103                    boolean useCustomTitle = GetterUtil.getBoolean(
104                            portletSetup.getValue("portletSetupUseCustomTitle", null));
105                    boolean showBorders = GetterUtil.getBoolean(
106                            portletSetup.getValue("portletSetupShowBorders", null), true);
107                    String linkToLayoutUuid = GetterUtil.getString(
108                            portletSetup.getValue("portletSetupLinkToLayoutUuid", null));
109    
110                    portletDataJSONObject.put("useCustomTitle", useCustomTitle);
111                    portletDataJSONObject.put("showBorders", showBorders);
112                    portletDataJSONObject.put("portletLinksTarget", linkToLayoutUuid);
113    
114                    return cssJSONObject;
115            }
116    
117            private static Log _log = LogFactoryUtil.getLog(PortletSetupUtil.class);
118    
119    }