1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.communities.action;
16  
17  import com.liferay.portal.DuplicateTeamException;
18  import com.liferay.portal.NoSuchTeamException;
19  import com.liferay.portal.TeamNameException;
20  import com.liferay.portal.kernel.servlet.SessionErrors;
21  import com.liferay.portal.kernel.util.Constants;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.TeamServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.ActionResponse;
29  import javax.portlet.PortletConfig;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="EditTeamAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class EditTeamAction extends PortletAction {
43  
44      public void processAction(
45              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
46              ActionRequest actionRequest, ActionResponse actionResponse)
47          throws Exception {
48  
49          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
50  
51          try {
52              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
53                  updateTeam(actionRequest);
54              }
55              else if (cmd.equals(Constants.DELETE)) {
56                  deleteTeam(actionRequest);
57              }
58  
59              sendRedirect(actionRequest, actionResponse);
60          }
61          catch (Exception e) {
62              if (e instanceof PrincipalException) {
63                  SessionErrors.add(actionRequest, e.getClass().getName());
64  
65                  setForward(actionRequest, "portlet.communities.error");
66              }
67              else if (e instanceof DuplicateTeamException ||
68                       e instanceof NoSuchTeamException ||
69                       e instanceof TeamNameException) {
70  
71                  SessionErrors.add(actionRequest, e.getClass().getName());
72  
73                  if (cmd.equals(Constants.DELETE)) {
74                      actionResponse.sendRedirect(
75                          ParamUtil.getString(actionRequest, "redirect"));
76                  }
77              }
78              else {
79                  throw e;
80              }
81          }
82      }
83  
84      public ActionForward render(
85              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
86              RenderRequest renderRequest, RenderResponse renderResponse)
87          throws Exception {
88  
89          try {
90              ActionUtil.getTeam(renderRequest);
91          }
92          catch (Exception e) {
93              if (e instanceof NoSuchTeamException ||
94                  e instanceof PrincipalException) {
95  
96                  SessionErrors.add(renderRequest, e.getClass().getName());
97  
98                  return mapping.findForward("portlet.communities.error");
99              }
100             else {
101                 throw e;
102             }
103         }
104 
105         return mapping.findForward(getForward(
106             renderRequest, "portlet.communities.edit_team"));
107     }
108 
109     protected void deleteTeam(ActionRequest actionRequest)
110         throws Exception {
111 
112         long teamId = ParamUtil.getLong(actionRequest, "teamId");
113 
114         TeamServiceUtil.deleteTeam(teamId);
115     }
116 
117     protected void updateTeam(ActionRequest actionRequest)
118         throws Exception {
119 
120         long teamId = ParamUtil.getLong(actionRequest, "teamId");
121 
122         long groupId = ParamUtil.getLong(actionRequest, "groupId");
123         String name = ParamUtil.getString(actionRequest, "name");
124         String description = ParamUtil.getString(actionRequest, "description");
125 
126         if (teamId <= 0) {
127 
128             // Add team
129 
130             TeamServiceUtil.addTeam(groupId, name, description);
131         }
132         else {
133 
134             // Update team
135 
136             TeamServiceUtil.updateTeam(teamId, name, description);
137         }
138     }
139 
140 }