001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.auth.PrincipalException;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.service.permission.PortletPermissionUtil;
022    import com.liferay.portal.struts.JSONAction;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.PortletPreferencesFactoryUtil;
026    import com.liferay.portlet.StrictPortletPreferencesImpl;
027    
028    import javax.portlet.PortletPreferences;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Alexander Chow
038     */
039    public class SaveAction extends JSONAction {
040    
041            @Override
042            public String getJSON(
043                            ActionMapping actionMapping, ActionForm actionForm,
044                            HttpServletRequest request, HttpServletResponse response)
045                    throws Exception {
046    
047                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
048                            WebKeys.THEME_DISPLAY);
049    
050                    String portletId = ParamUtil.getString(request, "portletId");
051    
052                    PortletPermissionUtil.check(
053                            themeDisplay.getPermissionChecker(), themeDisplay.getLayout(),
054                            portletId, ActionKeys.CONFIGURATION);
055    
056                    PortletPreferences portletPreferences =
057                            PortletPreferencesFactoryUtil.getStrictPortletSetup(
058                                    themeDisplay.getLayout(), portletId);
059    
060                    if (portletPreferences instanceof StrictPortletPreferencesImpl) {
061                            throw new PrincipalException();
062                    }
063    
064                    String color = ParamUtil.getString(request, "color");
065                    String data = ParamUtil.getString(request, "data");
066    
067                    if (Validator.isNotNull(color)) {
068                            portletPreferences.setValue("color", color);
069                    }
070    
071                    if (Validator.isNotNull(data)) {
072                            portletPreferences.setValue("data", data);
073                    }
074    
075                    portletPreferences.store();
076    
077                    return null;
078            }
079    
080    }