1
19
20 package com.liferay.portlet;
21
22 import com.liferay.portal.kernel.json.JSONFactoryUtil;
23 import com.liferay.portal.kernel.json.JSONObject;
24 import com.liferay.portal.kernel.language.LanguageUtil;
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.LocaleUtil;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.Validator;
31
32 import java.util.Locale;
33
34 import javax.portlet.PortletPreferences;
35
36
42 public class PortletSetupUtil {
43
44 public static final JSONObject cssToJSON(
45 PortletPreferences portletSetup, String css)
46 throws Exception {
47
48 return _toJSONObject(portletSetup, css);
49 }
50
51 public static final String cssToString(PortletPreferences portletSetup) {
52 String css = portletSetup.getValue(
53 "portlet-setup-css", StringPool.BLANK);
54
55 try {
56 if (Validator.isNotNull(css)) {
57 return _toJSONObject(portletSetup, css).toString();
58 }
59 }
60 catch (Exception e) {
61 css = null;
62
63 if (_log.isWarnEnabled()) {
64 _log.warn(e);
65 }
66 }
67
68 return css;
69 }
70
71 private static final JSONObject _toJSONObject(
72 PortletPreferences portletSetup, String css)
73 throws Exception {
74
75 if (_log.isDebugEnabled()) {
76 _log.debug("Transform CSS to JSON " + css);
77 }
78
79 JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
80
81 JSONObject portletData = JSONFactoryUtil.createJSONObject();
82
83 jsonObj.put("portletData", portletData);
84
85 JSONObject titles = JSONFactoryUtil.createJSONObject();
86
87 portletData.put("titles", titles);
88
89 Locale[] locales = LanguageUtil.getAvailableLocales();
90
91 for (int i = 0; i < locales.length; i++) {
92 String languageId = LocaleUtil.toLanguageId(locales[i]);
93
94 String title = portletSetup.getValue(
95 "portlet-setup-title-" + languageId, null);
96
97 if (Validator.isNotNull(languageId)) {
98 titles.put(languageId, title);
99 }
100 }
101
102 boolean useCustomTitle = GetterUtil.getBoolean(
103 portletSetup.getValue("portlet-setup-use-custom-title", null));
104 boolean showBorders = GetterUtil.getBoolean(
105 portletSetup.getValue("portlet-setup-show-borders", null), true);
106 long linkToLayoutId = GetterUtil.getLong(
107 portletSetup.getValue("portlet-setup-link-to-layout-id", null));
108
109 portletData.put("useCustomTitle", useCustomTitle);
110 portletData.put("showBorders", showBorders);
111 portletData.put("portletLinksTarget", linkToLayoutId);
112
113 return jsonObj;
114 }
115
116 private static Log _log = LogFactoryUtil.getLog(PortletSetupUtil.class);
117
118 }