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.sites.action;
016    
017    import com.liferay.portal.DuplicateTeamException;
018    import com.liferay.portal.NoSuchGroupException;
019    import com.liferay.portal.NoSuchTeamException;
020    import com.liferay.portal.TeamNameException;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.TeamServiceUtil;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.util.PortalUtil;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class EditTeamAction extends PortletAction {
044    
045            @Override
046            public void processAction(
047                            ActionMapping actionMapping, ActionForm actionForm,
048                            PortletConfig portletConfig, ActionRequest actionRequest,
049                            ActionResponse actionResponse)
050                    throws Exception {
051    
052                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
053    
054                    try {
055                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
056                                    updateTeam(actionRequest);
057                            }
058                            else if (cmd.equals(Constants.DELETE)) {
059                                    deleteTeam(actionRequest);
060                            }
061    
062                            sendRedirect(actionRequest, actionResponse);
063                    }
064                    catch (Exception e) {
065                            if (e instanceof PrincipalException) {
066                                    SessionErrors.add(actionRequest, e.getClass());
067    
068                                    setForward(actionRequest, "portlet.sites_admin.error");
069                            }
070                            else if (e instanceof DuplicateTeamException ||
071                                             e instanceof NoSuchTeamException ||
072                                             e instanceof TeamNameException) {
073    
074                                    SessionErrors.add(actionRequest, e.getClass());
075    
076                                    if (cmd.equals(Constants.DELETE)) {
077                                            String redirect = PortalUtil.escapeRedirect(
078                                                    ParamUtil.getString(actionRequest, "redirect"));
079    
080                                            if (Validator.isNotNull(redirect)) {
081                                                    actionResponse.sendRedirect(redirect);
082                                            }
083                                    }
084                            }
085                            else {
086                                    throw e;
087                            }
088                    }
089            }
090    
091            @Override
092            public ActionForward render(
093                            ActionMapping actionMapping, ActionForm actionForm,
094                            PortletConfig portletConfig, RenderRequest renderRequest,
095                            RenderResponse renderResponse)
096                    throws Exception {
097    
098                    try {
099                            ActionUtil.getTeam(renderRequest);
100                    }
101                    catch (Exception e) {
102                            if (e instanceof NoSuchGroupException ||
103                                    e instanceof NoSuchTeamException ||
104                                    e instanceof PrincipalException) {
105    
106                                    SessionErrors.add(renderRequest, e.getClass());
107    
108                                    return actionMapping.findForward("portlet.sites_admin.error");
109                            }
110                            else {
111                                    throw e;
112                            }
113                    }
114    
115                    return actionMapping.findForward(
116                            getForward(renderRequest, "portlet.sites_admin.edit_team"));
117            }
118    
119            protected void deleteTeam(ActionRequest actionRequest) throws Exception {
120                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
121    
122                    TeamServiceUtil.deleteTeam(teamId);
123            }
124    
125            protected void updateTeam(ActionRequest actionRequest) throws Exception {
126                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
127    
128                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
129                    String name = ParamUtil.getString(actionRequest, "name");
130                    String description = ParamUtil.getString(actionRequest, "description");
131    
132                    if (teamId <= 0) {
133    
134                            // Add team
135    
136                            TeamServiceUtil.addTeam(groupId, name, description);
137                    }
138                    else {
139    
140                            // Update team
141    
142                            TeamServiceUtil.updateTeam(teamId, name, description);
143                    }
144            }
145    
146    }