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.DuplicateRoleException;
26  import com.liferay.portal.NoSuchRoleException;
27  import com.liferay.portal.RequiredRoleException;
28  import com.liferay.portal.RoleNameException;
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.model.RoleConstants;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.RoleServiceUtil;
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="EditRoleAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class EditRoleAction 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                  updateRole(actionRequest);
64              }
65              else if (cmd.equals(Constants.DELETE)) {
66                  deleteRole(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 DuplicateRoleException ||
78                       e instanceof NoSuchRoleException ||
79                       e instanceof RequiredRoleException ||
80                       e instanceof RoleNameException) {
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.getRole(renderRequest);
102         }
103         catch (Exception e) {
104             if (e instanceof NoSuchRoleException ||
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(
117             getForward(renderRequest, "portlet.enterprise_admin.edit_role"));
118     }
119 
120     protected void deleteRole(ActionRequest actionRequest) throws Exception {
121         long roleId = ParamUtil.getLong(actionRequest, "roleId");
122 
123         RoleServiceUtil.deleteRole(roleId);
124     }
125 
126     protected void updateRole(ActionRequest actionRequest) throws Exception {
127         long roleId = ParamUtil.getLong(actionRequest, "roleId");
128 
129         String name = ParamUtil.getString(actionRequest, "name");
130         String description = ParamUtil.getString(actionRequest, "description");
131         int type = ParamUtil.getInteger(
132             actionRequest, "type", RoleConstants.TYPE_REGULAR);
133 
134         if (roleId <= 0) {
135 
136             // Add role
137 
138             RoleServiceUtil.addRole(name, description, type);
139         }
140         else {
141 
142             // Update role
143 
144             RoleServiceUtil.updateRole(roleId, name, description);
145         }
146     }
147 
148 }