1   /**
2    * Copyright (c) 2000-2008 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.util.GetterUtil;
29  import com.liferay.portal.kernel.util.LocaleUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  
33  import java.util.Locale;
34  
35  import javax.portlet.PortletPreferences;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="PortletSetupUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
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 = JSONFactoryUtil.createJSONObject(css);
84  
85          JSONObject portletData = JSONFactoryUtil.createJSONObject();
86  
87          jsonObj.put("portletData", portletData);
88  
89          JSONObject titles = JSONFactoryUtil.createJSONObject();
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         return jsonObj;
118     }
119 
120     private static Log _log = LogFactory.getLog(PortletSetupUtil.class);
121 
122 }