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.assetpublisher.action;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionMapping;
033    
034    /**
035     * @author Roberto Diaz
036     */
037    public class EditSubscriptionAction extends PortletAction {
038    
039            @Override
040            public void processAction(
041                            ActionMapping actionMapping, ActionForm actionForm,
042                            PortletConfig portletConfig, ActionRequest actionRequest,
043                            ActionResponse actionResponse)
044                    throws Exception {
045    
046                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
047    
048                    try {
049                            if (cmd.equals(Constants.SUBSCRIBE)) {
050                                    subscribe((LiferayPortletConfig)portletConfig, actionRequest);
051                            }
052                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
053                                    unsubscribe((LiferayPortletConfig)portletConfig, actionRequest);
054                            }
055    
056                            sendRedirect(actionRequest, actionResponse);
057                    }
058                    catch (Exception e) {
059                            if (e instanceof PrincipalException) {
060                                    SessionErrors.add(actionRequest, e.getClass());
061    
062                                    setForward(actionRequest, "portlet.asset_publisher.error");
063                            }
064                            else {
065                                    throw e;
066                            }
067                    }
068            }
069    
070            private void subscribe(
071                            LiferayPortletConfig liferayPortletConfig,
072                            ActionRequest actionRequest)
073                    throws Exception {
074    
075                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
076                            WebKeys.THEME_DISPLAY);
077    
078                    AssetPublisherUtil.subscribe(
079                            themeDisplay.getPermissionChecker(), themeDisplay.getScopeGroupId(),
080                            themeDisplay.getPlid(), liferayPortletConfig.getPortletId());
081            }
082    
083            private void unsubscribe(
084                            LiferayPortletConfig liferayPortletConfig,
085                            ActionRequest actionRequest)
086                    throws Exception {
087    
088                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
089                            WebKeys.THEME_DISPLAY);
090    
091                    AssetPublisherUtil.unsubscribe(
092                            themeDisplay.getPermissionChecker(), themeDisplay.getPlid(),
093                            liferayPortletConfig.getPortletId());
094            }
095    
096    }