1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
40   * <a href="PortletSetupUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
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 }