1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletSetupUtil {
45  
46      public static final JSONObject cssToJSON(
47              PortletPreferences portletSetup, String css)
48          throws Exception {
49  
50          return _toJSONObject(portletSetup, css);
51      }
52  
53      public static final String cssToString(PortletPreferences portletSetup) {
54          String css = portletSetup.getValue(
55              "portlet-setup-css", StringPool.BLANK);
56  
57          try {
58              if (Validator.isNotNull(css)) {
59                  return _toJSONObject(portletSetup, css).toString();
60              }
61          }
62          catch (Exception e) {
63              css = null;
64  
65              if (_log.isWarnEnabled()) {
66                  _log.warn(e);
67              }
68          }
69  
70          return css;
71      }
72  
73      private static final JSONObject _toJSONObject(
74              PortletPreferences portletSetup, String css)
75          throws Exception {
76  
77          if (_log.isDebugEnabled()) {
78              _log.debug("Transform CSS to JSON " + css);
79          }
80  
81          JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
82  
83          JSONObject portletData = JSONFactoryUtil.createJSONObject();
84  
85          jsonObj.put("portletData", portletData);
86  
87          JSONObject titles = JSONFactoryUtil.createJSONObject();
88  
89          portletData.put("titles", titles);
90  
91          Locale[] locales = LanguageUtil.getAvailableLocales();
92  
93          for (int i = 0; i < locales.length; i++) {
94              String languageId = LocaleUtil.toLanguageId(locales[i]);
95  
96              String title = portletSetup.getValue(
97                  "portlet-setup-title-" + languageId, null);
98  
99              if (Validator.isNotNull(languageId)) {
100                 titles.put(languageId, title);
101             }
102         }
103 
104         boolean useCustomTitle = GetterUtil.getBoolean(
105             portletSetup.getValue("portlet-setup-use-custom-title", null));
106         boolean showBorders = GetterUtil.getBoolean(
107             portletSetup.getValue("portlet-setup-show-borders", null), true);
108         long linkToLayoutId = GetterUtil.getLong(
109             portletSetup.getValue("portlet-setup-link-to-layout-id", null));
110 
111         portletData.put("useCustomTitle", useCustomTitle);
112         portletData.put("showBorders", showBorders);
113         portletData.put("portletLinksTarget", linkToLayoutId);
114 
115         return jsonObj;
116     }
117 
118     private static Log _log = LogFactoryUtil.getLog(PortletSetupUtil.class);
119 
120 }