1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditUserGroupAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
60  
61          try {
62              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
63                  updateUserGroup(actionRequest);
64              }
65              else if (cmd.equals(Constants.DELETE)) {
66                  deleteUserGroups(actionRequest);
67              }
68  
69              sendRedirect(actionRequest, actionResponse);
70          }
71          catch (Exception e) {
72              if (e instanceof PrincipalException) {
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74  
75                  setForward(actionRequest, "portlet.enterprise_admin.error");
76              }
77              else if (e instanceof DuplicateUserGroupException ||
78                       e instanceof NoSuchUserGroupException ||
79                       e instanceof RequiredUserGroupException ||
80                       e instanceof UserGroupNameException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83  
84                  if (cmd.equals(Constants.DELETE)) {
85                      actionResponse.sendRedirect(
86                          ParamUtil.getString(actionRequest, "redirect"));
87                  }
88              }
89              else {
90                  throw e;
91              }
92          }
93      }
94  
95      public ActionForward render(
96              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
97              RenderRequest renderRequest, RenderResponse renderResponse)
98          throws Exception {
99  
100         try {
101             ActionUtil.getUserGroup(renderRequest);
102         }
103         catch (Exception e) {
104             if (e instanceof NoSuchUserGroupException ||
105                 e instanceof PrincipalException) {
106 
107                 SessionErrors.add(renderRequest, e.getClass().getName());
108 
109                 return mapping.findForward("portlet.enterprise_admin.error");
110             }
111             else {
112                 throw e;
113             }
114         }
115 
116         return mapping.findForward(getForward(
117             renderRequest, "portlet.enterprise_admin.edit_user_group"));
118     }
119 
120     protected void deleteUserGroups(ActionRequest actionRequest)
121         throws Exception {
122 
123         long[] deleteUserGroupIds = StringUtil.split(
124             ParamUtil.getString(actionRequest, "deleteUserGroupIds"), 0L);
125 
126         for (int i = 0; i < deleteUserGroupIds.length; i++) {
127             UserGroupServiceUtil.deleteUserGroup(deleteUserGroupIds[i]);
128         }
129     }
130 
131     protected void updateUserGroup(ActionRequest actionRequest)
132         throws Exception {
133 
134         long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
135 
136         String name = ParamUtil.getString(actionRequest, "name");
137         String description = ParamUtil.getString(actionRequest, "description");
138 
139         if (userGroupId <= 0) {
140 
141             // Add user group
142 
143             UserGroupServiceUtil.addUserGroup(name, description);
144         }
145         else {
146 
147             // Update user group
148 
149             UserGroupServiceUtil.updateUserGroup(
150                 userGroupId, name, description);
151         }
152     }
153 
154 }