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