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