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
45 public class PortletSetupUtil {
46
47 public static final JSONObject cssToJSON(
48 PortletPreferences portletSetup, String css)
49 throws Exception {
50
51 return _toJSONObject(portletSetup, css);
52 }
53
54 public static final String cssToString(PortletPreferences portletSetup) {
55 String css = portletSetup.getValue(
56 "portlet-setup-css", StringPool.BLANK);
57
58 try {
59 if (Validator.isNotNull(css)) {
60 return _toJSONObject(portletSetup, css).toString();
61 }
62 }
63 catch (Exception e) {
64 css = null;
65
66 if (_log.isWarnEnabled()) {
67 _log.warn(e);
68 }
69 }
70
71 return css;
72 }
73
74 private static final JSONObject _toJSONObject(
75 PortletPreferences portletSetup, String css)
76 throws Exception {
77
78 if (_log.isDebugEnabled()) {
79 _log.debug("Transform CSS to JSON " + css);
80 }
81
82 JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
83
84 JSONObject portletData = JSONFactoryUtil.createJSONObject();
85
86 jsonObj.put("portletData", portletData);
87
88 JSONObject titles = JSONFactoryUtil.createJSONObject();
89
90 portletData.put("titles", titles);
91
92 Locale[] locales = LanguageUtil.getAvailableLocales();
93
94 for (int i = 0; i < locales.length; i++) {
95 String languageId = LocaleUtil.toLanguageId(locales[i]);
96
97 String title = portletSetup.getValue(
98 "portlet-setup-title-" + languageId, null);
99
100 if (Validator.isNotNull(languageId)) {
101 titles.put(languageId, title);
102 }
103 }
104
105 boolean useCustomTitle = GetterUtil.getBoolean(
106 portletSetup.getValue("portlet-setup-use-custom-title", null));
107 boolean showBorders = GetterUtil.getBoolean(
108 portletSetup.getValue("portlet-setup-show-borders", null), true);
109 long linkToPlid = GetterUtil.getLong(
110 portletSetup.getValue("portlet-setup-link-to-plid", null));
111
112 portletData.put("useCustomTitle", useCustomTitle);
113 portletData.put("showBorders", showBorders);
114 portletData.put("portletLinksTarget", linkToPlid);
115
116 return jsonObj;
117 }
118
119 private static Log _log = LogFactoryUtil.getLog(PortletSetupUtil.class);
120
121 }