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;
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 final JSONObject cssToJSON(
037                            PortletPreferences portletSetup, String css)
038                    throws Exception {
039    
040                    return _toJSONObject(portletSetup, css);
041            }
042    
043            public static final String cssToString(PortletPreferences portletSetup) {
044                    String css = portletSetup.getValue(
045                            "portlet-setup-css", StringPool.BLANK);
046    
047                    try {
048                            if (Validator.isNotNull(css)) {
049                                    return _toJSONObject(portletSetup, css).toString();
050                            }
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 final 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 jsonObj = JSONFactoryUtil.createJSONObject(css);
072    
073                    JSONObject portletData = JSONFactoryUtil.createJSONObject();
074    
075                    jsonObj.put("portletData", portletData);
076    
077                    JSONObject titles = JSONFactoryUtil.createJSONObject();
078    
079                    portletData.put("titles", titles);
080    
081                    Locale[] locales = LanguageUtil.getAvailableLocales();
082    
083                    for (int i = 0; i < locales.length; i++) {
084                            String languageId = LocaleUtil.toLanguageId(locales[i]);
085    
086                            String title = portletSetup.getValue(
087                                    "portlet-setup-title-" + languageId, null);
088    
089                            if (Validator.isNotNull(languageId)) {
090                                    titles.put(languageId, title);
091                            }
092                    }
093    
094                    boolean useCustomTitle = GetterUtil.getBoolean(
095                            portletSetup.getValue("portlet-setup-use-custom-title", null));
096                    boolean showBorders = GetterUtil.getBoolean(
097                            portletSetup.getValue("portlet-setup-show-borders", null), true);
098                    String linkToLayoutUuid = GetterUtil.getString(
099                            portletSetup.getValue("portlet-setup-link-to-layout-uuid", null));
100    
101                    portletData.put("useCustomTitle", useCustomTitle);
102                    portletData.put("showBorders", showBorders);
103                    portletData.put("portletLinksTarget", linkToLayoutUuid);
104    
105                    return jsonObj;
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(PortletSetupUtil.class);
109    
110    }