1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  import com.liferay.util.LocalizationUtil;
29  
30  import java.util.Locale;
31  import java.util.Map;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="EditRoleAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class EditRoleAction extends PortletAction {
49  
50      public void processAction(
51              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws Exception {
54  
55          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
56  
57          try {
58              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
59                  updateRole(actionRequest);
60              }
61              else if (cmd.equals(Constants.DELETE)) {
62                  deleteRole(actionRequest);
63              }
64  
65              sendRedirect(actionRequest, actionResponse);
66          }
67          catch (Exception e) {
68              if (e instanceof PrincipalException) {
69                  SessionErrors.add(actionRequest, e.getClass().getName());
70  
71                  setForward(actionRequest, "portlet.enterprise_admin.error");
72              }
73              else if (e instanceof DuplicateRoleException ||
74                       e instanceof NoSuchRoleException ||
75                       e instanceof RequiredRoleException ||
76                       e instanceof RoleNameException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79  
80                  if (cmd.equals(Constants.DELETE)) {
81                      actionResponse.sendRedirect(
82                          ParamUtil.getString(actionRequest, "redirect"));
83                  }
84              }
85              else {
86                  throw e;
87              }
88          }
89      }
90  
91      public ActionForward render(
92              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93              RenderRequest renderRequest, RenderResponse renderResponse)
94          throws Exception {
95  
96          try {
97              ActionUtil.getRole(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchRoleException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(renderRequest, e.getClass().getName());
104 
105                 return mapping.findForward("portlet.enterprise_admin.error");
106             }
107             else {
108                 throw e;
109             }
110         }
111 
112         return mapping.findForward(
113             getForward(renderRequest, "portlet.enterprise_admin.edit_role"));
114     }
115 
116     protected void deleteRole(ActionRequest actionRequest) throws Exception {
117         long roleId = ParamUtil.getLong(actionRequest, "roleId");
118 
119         RoleServiceUtil.deleteRole(roleId);
120     }
121 
122     protected void updateRole(ActionRequest actionRequest) throws Exception {
123         long roleId = ParamUtil.getLong(actionRequest, "roleId");
124 
125         String name = ParamUtil.getString(actionRequest, "name");
126         Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
127             actionRequest, "title");
128         String description = ParamUtil.getString(actionRequest, "description");
129         int type = ParamUtil.getInteger(
130             actionRequest, "type", RoleConstants.TYPE_REGULAR);
131         String subtype = ParamUtil.getString(actionRequest, "subtype");
132 
133         if (roleId <= 0) {
134 
135             // Add role
136 
137             RoleServiceUtil.addRole(name, titleMap, description, type);
138         }
139         else {
140 
141             // Update role
142 
143             RoleServiceUtil.updateRole(
144                 roleId, name, titleMap, description, subtype);
145         }
146     }
147 
148 }