1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.json.JSONFactoryUtil;
18  import com.liferay.portal.kernel.json.JSONObject;
19  import com.liferay.portal.kernel.language.LanguageUtil;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.LocaleUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  
27  import java.util.Locale;
28  
29  import javax.portlet.PortletPreferences;
30  
31  /**
32   * <a href="PortletSetupUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class PortletSetupUtil {
37  
38      public static final JSONObject cssToJSON(
39              PortletPreferences portletSetup, String css)
40          throws Exception {
41  
42          return _toJSONObject(portletSetup, css);
43      }
44  
45      public static final String cssToString(PortletPreferences portletSetup) {
46          String css = portletSetup.getValue(
47              "portlet-setup-css", StringPool.BLANK);
48  
49          try {
50              if (Validator.isNotNull(css)) {
51                  return _toJSONObject(portletSetup, css).toString();
52              }
53          }
54          catch (Exception e) {
55              css = null;
56  
57              if (_log.isWarnEnabled()) {
58                  _log.warn(e);
59              }
60          }
61  
62          return css;
63      }
64  
65      private static final JSONObject _toJSONObject(
66              PortletPreferences portletSetup, String css)
67          throws Exception {
68  
69          if (_log.isDebugEnabled()) {
70              _log.debug("Transform CSS to JSON " + css);
71          }
72  
73          JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
74  
75          JSONObject portletData = JSONFactoryUtil.createJSONObject();
76  
77          jsonObj.put("portletData", portletData);
78  
79          JSONObject titles = JSONFactoryUtil.createJSONObject();
80  
81          portletData.put("titles", titles);
82  
83          Locale[] locales = LanguageUtil.getAvailableLocales();
84  
85          for (int i = 0; i < locales.length; i++) {
86              String languageId = LocaleUtil.toLanguageId(locales[i]);
87  
88              String title = portletSetup.getValue(
89                  "portlet-setup-title-" + languageId, null);
90  
91              if (Validator.isNotNull(languageId)) {
92                  titles.put(languageId, title);
93              }
94          }
95  
96          boolean useCustomTitle = GetterUtil.getBoolean(
97              portletSetup.getValue("portlet-setup-use-custom-title", null));
98          boolean showBorders = GetterUtil.getBoolean(
99              portletSetup.getValue("portlet-setup-show-borders", null), true);
100         long linkToLayoutId = GetterUtil.getLong(
101             portletSetup.getValue("portlet-setup-link-to-layout-id", null));
102 
103         portletData.put("useCustomTitle", useCustomTitle);
104         portletData.put("showBorders", showBorders);
105         portletData.put("portletLinksTarget", linkToLayoutId);
106 
107         return jsonObj;
108     }
109 
110     private static Log _log = LogFactoryUtil.getLog(PortletSetupUtil.class);
111 
112 }