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