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