001    /**
002     * Copyright (c) 2000-2010 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.requests.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.service.GroupLocalServiceUtil;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    import com.liferay.portal.service.permission.UserPermissionUtil;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.social.NoSuchRequestException;
030    import com.liferay.portlet.social.service.SocialRequestLocalServiceUtil;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class UpdateRequestAction extends PortletAction {
043    
044            public void processAction(
045                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
046                            ActionRequest actionRequest, ActionResponse actionResponse)
047                    throws Exception {
048    
049                    try {
050                            ThemeDisplay themeDisplay =
051                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
052    
053                            Group group = GroupLocalServiceUtil.getGroup(
054                                    themeDisplay.getScopeGroupId());
055    
056                            User user = themeDisplay.getUser();
057    
058                            if (group.isUser()) {
059                                    user = UserLocalServiceUtil.getUserById(group.getClassPK());
060                            }
061    
062                            if (!UserPermissionUtil.contains(
063                                            themeDisplay.getPermissionChecker(), user.getUserId(),
064                                            ActionKeys.UPDATE)) {
065    
066                                    throw new PrincipalException();
067                            }
068    
069                            updateRequest(actionRequest);
070    
071                            String redirect = ParamUtil.getString(actionRequest, "redirect");
072    
073                            actionResponse.sendRedirect(redirect);
074                    }
075                    catch (Exception e) {
076                            if (e instanceof NoSuchRequestException ||
077                                    e instanceof PrincipalException) {
078    
079                                    SessionErrors.add(actionRequest, e.getClass().getName());
080    
081                                    setForward(actionRequest, "portlet.requests.error");
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            protected void updateRequest(ActionRequest actionRequest) throws Exception {
090                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
091                            WebKeys.THEME_DISPLAY);
092    
093                    long requestId = ParamUtil.getLong(actionRequest, "requestId");
094                    int status = ParamUtil.getInteger(actionRequest, "status");
095    
096                    SocialRequestLocalServiceUtil.updateRequest(
097                            requestId, status, themeDisplay);
098            }
099    
100    }