1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.json.JSONFactoryUtil;
23  import com.liferay.portal.kernel.json.JSONObject;
24  import com.liferay.portal.kernel.language.LanguageUtil;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.LocaleUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import java.util.Locale;
33  
34  import javax.portlet.PortletPreferences;
35  
36  /**
37   * <a href="PortletSetupUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class PortletSetupUtil {
43  
44      public static final JSONObject cssToJSON(
45              PortletPreferences portletSetup, String css)
46          throws Exception {
47  
48          return _toJSONObject(portletSetup, css);
49      }
50  
51      public static final String cssToString(PortletPreferences portletSetup) {
52          String css = portletSetup.getValue(
53              "portlet-setup-css", StringPool.BLANK);
54  
55          try {
56              if (Validator.isNotNull(css)) {
57                  return _toJSONObject(portletSetup, css).toString();
58              }
59          }
60          catch (Exception e) {
61              css = null;
62  
63              if (_log.isWarnEnabled()) {
64                  _log.warn(e);
65              }
66          }
67  
68          return css;
69      }
70  
71      private static final JSONObject _toJSONObject(
72              PortletPreferences portletSetup, String css)
73          throws Exception {
74  
75          if (_log.isDebugEnabled()) {
76              _log.debug("Transform CSS to JSON " + css);
77          }
78  
79          JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
80  
81          JSONObject portletData = JSONFactoryUtil.createJSONObject();
82  
83          jsonObj.put("portletData", portletData);
84  
85          JSONObject titles = JSONFactoryUtil.createJSONObject();
86  
87          portletData.put("titles", titles);
88  
89          Locale[] locales = LanguageUtil.getAvailableLocales();
90  
91          for (int i = 0; i < locales.length; i++) {
92              String languageId = LocaleUtil.toLanguageId(locales[i]);
93  
94              String title = portletSetup.getValue(
95                  "portlet-setup-title-" + languageId, null);
96  
97              if (Validator.isNotNull(languageId)) {
98                  titles.put(languageId, title);
99              }
100         }
101 
102         boolean useCustomTitle = GetterUtil.getBoolean(
103             portletSetup.getValue("portlet-setup-use-custom-title", null));
104         boolean showBorders = GetterUtil.getBoolean(
105             portletSetup.getValue("portlet-setup-show-borders", null), true);
106         long linkToLayoutId = GetterUtil.getLong(
107             portletSetup.getValue("portlet-setup-link-to-layout-id", null));
108 
109         portletData.put("useCustomTitle", useCustomTitle);
110         portletData.put("showBorders", showBorders);
111         portletData.put("portletLinksTarget", linkToLayoutId);
112 
113         return jsonObj;
114     }
115 
116     private static Log _log = LogFactoryUtil.getLog(PortletSetupUtil.class);
117 
118 }