001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.quicknote.action;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.security.permission.ActionKeys;
020    import com.liferay.portal.service.permission.PortletPermissionUtil;
021    import com.liferay.portal.struts.JSONAction;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.WebKeys;
024    import com.liferay.portlet.PortletPreferencesFactoryUtil;
025    
026    import javax.portlet.PortletPreferences;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionMapping;
033    
034    /**
035     * @author Alexander Chow
036     */
037    public class SaveAction extends JSONAction {
038    
039            @Override
040            public String getJSON(
041                            ActionMapping actionMapping, ActionForm actionForm,
042                            HttpServletRequest request, HttpServletResponse response)
043                    throws Exception {
044    
045                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
046                            WebKeys.THEME_DISPLAY);
047    
048                    String portletId = ParamUtil.getString(request, "portletId");
049    
050                    PortletPermissionUtil.check(
051                            themeDisplay.getPermissionChecker(), themeDisplay.getLayout(),
052                            portletId, ActionKeys.CONFIGURATION);
053    
054                    PortletPreferences preferences =
055                            PortletPreferencesFactoryUtil.getPortletSetup(request, portletId);
056    
057                    String color = ParamUtil.getString(request, "color");
058                    String data = ParamUtil.getString(request, "data");
059    
060                    if (Validator.isNotNull(color)) {
061                            preferences.setValue("color", color);
062                    }
063    
064                    if (Validator.isNotNull(data)) {
065                            preferences.setValue("data", data);
066                    }
067    
068                    preferences.store();
069    
070                    return null;
071            }
072    
073    }