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