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.DuplicateRoleException;
18  import com.liferay.portal.NoSuchRoleException;
19  import com.liferay.portal.RequiredRoleException;
20  import com.liferay.portal.RoleNameException;
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.model.RoleConstants;
25  import com.liferay.portal.security.auth.PrincipalException;
26  import com.liferay.portal.service.RoleServiceUtil;
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="EditRoleAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class EditRoleAction 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                  updateRole(actionRequest);
56              }
57              else if (cmd.equals(Constants.DELETE)) {
58                  deleteRole(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 DuplicateRoleException ||
70                       e instanceof NoSuchRoleException ||
71                       e instanceof RequiredRoleException ||
72                       e instanceof RoleNameException) {
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.getRole(renderRequest);
94          }
95          catch (Exception e) {
96              if (e instanceof NoSuchRoleException ||
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(
109             getForward(renderRequest, "portlet.enterprise_admin.edit_role"));
110     }
111 
112     protected void deleteRole(ActionRequest actionRequest) throws Exception {
113         long roleId = ParamUtil.getLong(actionRequest, "roleId");
114 
115         RoleServiceUtil.deleteRole(roleId);
116     }
117 
118     protected void updateRole(ActionRequest actionRequest) throws Exception {
119         long roleId = ParamUtil.getLong(actionRequest, "roleId");
120 
121         String name = ParamUtil.getString(actionRequest, "name");
122         String description = ParamUtil.getString(actionRequest, "description");
123         int type = ParamUtil.getInteger(
124             actionRequest, "type", RoleConstants.TYPE_REGULAR);
125 
126         if (roleId <= 0) {
127 
128             // Add role
129 
130             RoleServiceUtil.addRole(name, description, type);
131         }
132         else {
133 
134             // Update role
135 
136             RoleServiceUtil.updateRole(roleId, name, description);
137         }
138     }
139 
140 }