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