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.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    }