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