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.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 actionMapping, ActionForm actionForm,
047                            PortletConfig portletConfig, ActionRequest actionRequest,
048                            ActionResponse actionResponse)
049                    throws Exception {
050    
051                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
052    
053                    try {
054                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
055                                    updateTeam(actionRequest);
056                            }
057                            else if (cmd.equals(Constants.DELETE)) {
058                                    deleteTeam(actionRequest);
059                            }
060    
061                            sendRedirect(actionRequest, actionResponse);
062                    }
063                    catch (Exception e) {
064                            if (e instanceof PrincipalException) {
065                                    SessionErrors.add(actionRequest, e.getClass());
066    
067                                    setForward(actionRequest, "portlet.sites_admin.error");
068                            }
069                            else if (e instanceof DuplicateTeamException ||
070                                             e instanceof NoSuchTeamException ||
071                                             e instanceof TeamNameException) {
072    
073                                    SessionErrors.add(actionRequest, e.getClass());
074    
075                                    if (cmd.equals(Constants.DELETE)) {
076                                            String redirect = PortalUtil.escapeRedirect(
077                                                    ParamUtil.getString(actionRequest, "redirect"));
078    
079                                            if (Validator.isNotNull(redirect)) {
080                                                    actionResponse.sendRedirect(redirect);
081                                            }
082                                    }
083                            }
084                            else {
085                                    throw e;
086                            }
087                    }
088            }
089    
090            @Override
091            public ActionForward render(
092                            ActionMapping actionMapping, ActionForm actionForm,
093                            PortletConfig portletConfig, RenderRequest renderRequest,
094                            RenderResponse renderResponse)
095                    throws Exception {
096    
097                    try {
098                            ActionUtil.getTeam(renderRequest);
099                    }
100                    catch (Exception e) {
101                            if (e instanceof NoSuchTeamException ||
102                                    e instanceof PrincipalException) {
103    
104                                    SessionErrors.add(renderRequest, e.getClass());
105    
106                                    return actionMapping.findForward("portlet.sites_admin.error");
107                            }
108                            else {
109                                    throw e;
110                            }
111                    }
112    
113                    return actionMapping.findForward(
114                            getForward(renderRequest, "portlet.sites_admin.edit_team"));
115            }
116    
117            protected void deleteTeam(ActionRequest actionRequest) throws Exception {
118                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
119    
120                    TeamServiceUtil.deleteTeam(teamId);
121            }
122    
123            protected void updateTeam(ActionRequest actionRequest) throws Exception {
124                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
125    
126                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
127                    String name = ParamUtil.getString(actionRequest, "name");
128                    String description = ParamUtil.getString(actionRequest, "description");
129    
130                    if (teamId <= 0) {
131    
132                            // Add team
133    
134                            TeamServiceUtil.addTeam(groupId, name, description);
135                    }
136                    else {
137    
138                            // Update team
139    
140                            TeamServiceUtil.updateTeam(teamId, name, description);
141                    }
142            }
143    
144    }