001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.LocaleUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.Validator;
028:
029: import java.util.Locale;
030:
031: import javax.portlet.PortletPreferences;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import org.json.JSONObject;
037:
038: /**
039: * <a href="PortletSetupUtil.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class PortletSetupUtil {
045:
046: public static final JSONObject cssToJSON(
047: PortletPreferences portletSetup, String css)
048: throws Exception {
049:
050: return _toJSONObject(portletSetup, css);
051: }
052:
053: public static final String cssToString(
054: PortletPreferences portletSetup) {
055: String css = portletSetup.getValue("portlet-setup-css",
056: StringPool.BLANK);
057:
058: try {
059: if (Validator.isNotNull(css)) {
060: return _toJSONObject(portletSetup, css).toString();
061: }
062: } catch (Exception e) {
063: css = null;
064:
065: if (_log.isWarnEnabled()) {
066: _log.warn(e);
067: }
068: }
069:
070: return css;
071: }
072:
073: private static final JSONObject _toJSONObject(
074: PortletPreferences portletSetup, String css)
075: throws Exception {
076:
077: if (_log.isDebugEnabled()) {
078: _log.debug("Transform CSS to JSON " + css);
079: }
080:
081: JSONObject jsonObj = new JSONObject(css);
082:
083: JSONObject portletData = new JSONObject();
084:
085: jsonObj.put("portletData", portletData);
086:
087: JSONObject titles = new JSONObject();
088:
089: portletData.put("titles", titles);
090:
091: Locale[] locales = LanguageUtil.getAvailableLocales();
092:
093: for (int i = 0; i < locales.length; i++) {
094: String languageId = LocaleUtil.toLanguageId(locales[i]);
095:
096: String title = portletSetup.getValue("portlet-setup-title-"
097: + languageId, null);
098:
099: if (Validator.isNotNull(languageId)) {
100: titles.put(languageId, title);
101: }
102: }
103:
104: boolean useCustomTitle = GetterUtil.getBoolean(portletSetup
105: .getValue("portlet-setup-use-custom-title", null));
106: boolean showBorders = GetterUtil.getBoolean(portletSetup
107: .getValue("portlet-setup-show-borders", null), true);
108: long linkToPlid = GetterUtil.getLong(portletSetup.getValue(
109: "portlet-setup-link-to-plid", null));
110:
111: portletData.put("useCustomTitle", useCustomTitle);
112: portletData.put("showBorders", showBorders);
113: portletData.put("portletLinksTarget", linkToPlid);
114:
115: // The JSONObject class does not properly detect boolean attributes
116: // without going through serialization
117:
118: jsonObj = new JSONObject(jsonObj.toString());
119:
120: return jsonObj;
121: }
122:
123: private static Log _log = LogFactory.getLog(PortletSetupUtil.class);
124:
125: }
|