1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.DuplicateUserGroupException;
18  import com.liferay.portal.NoSuchUserGroupException;
19  import com.liferay.portal.RequiredUserGroupException;
20  import com.liferay.portal.UserGroupNameException;
21  import com.liferay.portal.kernel.servlet.SessionErrors;
22  import com.liferay.portal.kernel.util.Constants;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.security.auth.PrincipalException;
26  import com.liferay.portal.service.UserGroupServiceUtil;
27  import com.liferay.portal.struts.PortletAction;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  
39  /**
40   * <a href="EditUserGroupAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Charles May
43   */
44  public class EditUserGroupAction extends PortletAction {
45  
46      public void processAction(
47              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
48              ActionRequest actionRequest, ActionResponse actionResponse)
49          throws Exception {
50  
51          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
52  
53          try {
54              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
55                  updateUserGroup(actionRequest);
56              }
57              else if (cmd.equals(Constants.DELETE)) {
58                  deleteUserGroups(actionRequest);
59              }
60  
61              sendRedirect(actionRequest, actionResponse);
62          }
63          catch (Exception e) {
64              if (e instanceof PrincipalException) {
65                  SessionErrors.add(actionRequest, e.getClass().getName());
66  
67                  setForward(actionRequest, "portlet.enterprise_admin.error");
68              }
69              else if (e instanceof DuplicateUserGroupException ||
70                       e instanceof NoSuchUserGroupException ||
71                       e instanceof RequiredUserGroupException ||
72                       e instanceof UserGroupNameException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75  
76                  if (cmd.equals(Constants.DELETE)) {
77                      actionResponse.sendRedirect(
78                          ParamUtil.getString(actionRequest, "redirect"));
79                  }
80              }
81              else {
82                  throw e;
83              }
84          }
85      }
86  
87      public ActionForward render(
88              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
89              RenderRequest renderRequest, RenderResponse renderResponse)
90          throws Exception {
91  
92          try {
93              ActionUtil.getUserGroup(renderRequest);
94          }
95          catch (Exception e) {
96              if (e instanceof NoSuchUserGroupException ||
97                  e instanceof PrincipalException) {
98  
99                  SessionErrors.add(renderRequest, e.getClass().getName());
100 
101                 return mapping.findForward("portlet.enterprise_admin.error");
102             }
103             else {
104                 throw e;
105             }
106         }
107 
108         return mapping.findForward(getForward(
109             renderRequest, "portlet.enterprise_admin.edit_user_group"));
110     }
111 
112     protected void deleteUserGroups(ActionRequest actionRequest)
113         throws Exception {
114 
115         long[] deleteUserGroupIds = StringUtil.split(
116             ParamUtil.getString(actionRequest, "deleteUserGroupIds"), 0L);
117 
118         for (int i = 0; i < deleteUserGroupIds.length; i++) {
119             UserGroupServiceUtil.deleteUserGroup(deleteUserGroupIds[i]);
120         }
121     }
122 
123     protected void updateUserGroup(ActionRequest actionRequest)
124         throws Exception {
125 
126         long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
127 
128         String name = ParamUtil.getString(actionRequest, "name");
129         String description = ParamUtil.getString(actionRequest, "description");
130 
131         if (userGroupId <= 0) {
132 
133             // Add user group
134 
135             UserGroupServiceUtil.addUserGroup(name, description);
136         }
137         else {
138 
139             // Update user group
140 
141             UserGroupServiceUtil.updateUserGroup(
142                 userGroupId, name, description);
143         }
144     }
145 
146 }