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 mapping, ActionForm form, PortletConfig portletConfig,
042                            ActionRequest actionRequest, ActionResponse actionResponse)
043                    throws Exception {
044    
045                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
046    
047                    try {
048                            if (cmd.equals(Constants.SUBSCRIBE)) {
049                                    subscribe((LiferayPortletConfig)portletConfig, actionRequest);
050                            }
051                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
052                                    unsubscribe((LiferayPortletConfig)portletConfig, actionRequest);
053                            }
054    
055                            sendRedirect(actionRequest, actionResponse);
056                    }
057                    catch (Exception e) {
058                            if (e instanceof PrincipalException) {
059                                    SessionErrors.add(actionRequest, e.getClass());
060    
061                                    setForward(actionRequest, "portlet.asset_publisher.error");
062                            }
063                            else {
064                                    throw e;
065                            }
066                    }
067            }
068    
069            private void subscribe(
070                            LiferayPortletConfig liferayPortletConfig,
071                            ActionRequest actionRequest)
072                    throws Exception {
073    
074                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
075                            WebKeys.THEME_DISPLAY);
076    
077                    AssetPublisherUtil.subscribe(
078                            themeDisplay.getPermissionChecker(), themeDisplay.getScopeGroupId(),
079                            themeDisplay.getPlid(), liferayPortletConfig.getPortletId());
080            }
081    
082            private void unsubscribe(
083                            LiferayPortletConfig liferayPortletConfig,
084                            ActionRequest actionRequest)
085                    throws Exception {
086    
087                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
088                            WebKeys.THEME_DISPLAY);
089    
090                    AssetPublisherUtil.unsubscribe(
091                            themeDisplay.getPermissionChecker(), themeDisplay.getPlid(),
092                            liferayPortletConfig.getPortletId());
093            }
094    
095    }