1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.portal.DuplicateUserGroupException;
26  import com.liferay.portal.NoSuchUserGroupException;
27  import com.liferay.portal.RequiredUserGroupException;
28  import com.liferay.portal.UserGroupNameException;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.util.Constants;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.UserGroupServiceUtil;
35  import com.liferay.portal.struts.PortletAction;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditUserGroupAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Charles May
51   *
52   */
53  public class EditUserGroupAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
61  
62          try {
63              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64                  updateUserGroup(actionRequest);
65              }
66              else if (cmd.equals(Constants.DELETE)) {
67                  deleteUserGroups(actionRequest);
68              }
69  
70              sendRedirect(actionRequest, actionResponse);
71          }
72          catch (Exception e) {
73              if (e instanceof PrincipalException) {
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75  
76                  setForward(actionRequest, "portlet.enterprise_admin.error");
77              }
78              else if (e instanceof DuplicateUserGroupException ||
79                       e instanceof NoSuchUserGroupException ||
80                       e instanceof RequiredUserGroupException ||
81                       e instanceof UserGroupNameException) {
82  
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84  
85                  if (cmd.equals(Constants.DELETE)) {
86                      actionResponse.sendRedirect(
87                          ParamUtil.getString(actionRequest, "redirect"));
88                  }
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         try {
102             ActionUtil.getUserGroup(renderRequest);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchUserGroupException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(renderRequest, e.getClass().getName());
109 
110                 return mapping.findForward("portlet.enterprise_admin.error");
111             }
112             else {
113                 throw e;
114             }
115         }
116 
117         return mapping.findForward(getForward(
118             renderRequest, "portlet.enterprise_admin.edit_user_group"));
119     }
120 
121     protected void deleteUserGroups(ActionRequest actionRequest)
122         throws Exception {
123 
124         long[] deleteUserGroupIds = StringUtil.split(
125             ParamUtil.getString(actionRequest, "deleteUserGroupIds"), 0L);
126 
127         for (int i = 0; i < deleteUserGroupIds.length; i++) {
128             UserGroupServiceUtil.deleteUserGroup(deleteUserGroupIds[i]);
129         }
130     }
131 
132     protected void updateUserGroup(ActionRequest actionRequest)
133         throws Exception {
134 
135         long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
136 
137         String name = ParamUtil.getString(actionRequest, "name");
138         String description = ParamUtil.getString(actionRequest, "description");
139 
140         if (userGroupId <= 0) {
141 
142             // Add user group
143 
144             UserGroupServiceUtil.addUserGroup(name, description);
145         }
146         else {
147 
148             // Update user group
149 
150             UserGroupServiceUtil.updateUserGroup(
151                 userGroupId, name, description);
152         }
153     }
154 
155 }