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.portletconfiguration.action;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.security.permission.ActionKeys;
025: import com.liferay.portal.kernel.security.permission.PermissionChecker;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.LocaleUtil;
028: import com.liferay.portal.kernel.util.ParamUtil;
029: import com.liferay.portal.kernel.util.Validator;
030: import com.liferay.portal.model.Layout;
031: import com.liferay.portal.service.permission.PortletPermissionUtil;
032: import com.liferay.portal.struts.JSONAction;
033: import com.liferay.portal.theme.ThemeDisplay;
034: import com.liferay.portal.util.WebKeys;
035: import com.liferay.portlet.CachePortlet;
036: import com.liferay.portlet.PortletPreferencesFactoryUtil;
037:
038: import java.util.Locale;
039:
040: import javax.portlet.PortletPreferences;
041:
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044: import javax.servlet.http.HttpSession;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048: import org.apache.struts.action.ActionForm;
049: import org.apache.struts.action.ActionMapping;
050:
051: import org.json.JSONObject;
052:
053: /**
054: * <a href="UpdateLookAndFeelAction.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Brian Wing Shun Chan
057: *
058: */
059: public class UpdateLookAndFeelAction extends JSONAction {
060:
061: public String getJSON(ActionMapping mapping, ActionForm form,
062: HttpServletRequest req, HttpServletResponse res)
063: throws Exception {
064:
065: HttpSession ses = req.getSession();
066:
067: ThemeDisplay themeDisplay = (ThemeDisplay) req
068: .getAttribute(WebKeys.THEME_DISPLAY);
069:
070: Layout layout = themeDisplay.getLayout();
071:
072: PermissionChecker permissionChecker = themeDisplay
073: .getPermissionChecker();
074:
075: String portletId = ParamUtil.getString(req, "portletId");
076:
077: if (!PortletPermissionUtil.contains(permissionChecker,
078: themeDisplay.getPlid(), portletId,
079: ActionKeys.CONFIGURATION)) {
080:
081: return null;
082: }
083:
084: PortletPreferences portletSetup = PortletPreferencesFactoryUtil
085: .getPortletSetup(layout, portletId);
086:
087: String css = ParamUtil.getString(req, "css");
088:
089: if (_log.isDebugEnabled()) {
090: _log.debug("Updating css " + css);
091: }
092:
093: JSONObject jsonObj = new JSONObject(css);
094:
095: JSONObject portletData = jsonObj.getJSONObject("portletData");
096:
097: jsonObj.remove("portletData");
098:
099: css = jsonObj.toString();
100:
101: boolean useCustomTitle = portletData
102: .getBoolean("useCustomTitle");
103: boolean showBorders = portletData.getBoolean("showBorders");
104: long linkToPlid = GetterUtil.getLong(portletData
105: .getString("portletLinksTarget"));
106:
107: JSONObject titles = portletData.getJSONObject("titles");
108:
109: Locale[] locales = LanguageUtil.getAvailableLocales();
110:
111: for (int i = 0; i < locales.length; i++) {
112: String languageId = LocaleUtil.toLanguageId(locales[i]);
113:
114: String title = null;
115:
116: if (titles.has(languageId)) {
117: title = GetterUtil.getString(titles
118: .getString(languageId));
119: }
120:
121: if (Validator.isNotNull(title)) {
122: portletSetup.setValue("portlet-setup-title-"
123: + languageId, title);
124: } else {
125: portletSetup.reset("portlet-setup-title-" + languageId);
126: }
127: }
128:
129: portletSetup.setValue("portlet-setup-use-custom-title", String
130: .valueOf(useCustomTitle));
131: portletSetup.setValue("portlet-setup-show-borders", String
132: .valueOf(showBorders));
133:
134: if (linkToPlid > 0) {
135: portletSetup.setValue("portlet-setup-link-to-plid", String
136: .valueOf(linkToPlid));
137: } else {
138: portletSetup.reset("portlet-setup-link-to-plid");
139: }
140:
141: portletSetup.setValue("portlet-setup-css", css);
142:
143: portletSetup.store();
144:
145: CachePortlet.clearResponse(ses, layout.getPrimaryKey(),
146: portletId, LanguageUtil.getLanguageId(req));
147:
148: return null;
149: }
150:
151: private static Log _log = LogFactory
152: .getLog(UpdateLookAndFeelAction.class);
153:
154: }
|