1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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  import com.liferay.util.LocalizationUtil;
37  
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="EditRoleAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class EditRoleAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
65  
66          try {
67              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
68                  updateRole(actionRequest);
69              }
70              else if (cmd.equals(Constants.DELETE)) {
71                  deleteRole(actionRequest);
72              }
73  
74              sendRedirect(actionRequest, actionResponse);
75          }
76          catch (Exception e) {
77              if (e instanceof PrincipalException) {
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79  
80                  setForward(actionRequest, "portlet.enterprise_admin.error");
81              }
82              else if (e instanceof DuplicateRoleException ||
83                       e instanceof NoSuchRoleException ||
84                       e instanceof RequiredRoleException ||
85                       e instanceof RoleNameException) {
86  
87                  SessionErrors.add(actionRequest, e.getClass().getName());
88  
89                  if (cmd.equals(Constants.DELETE)) {
90                      actionResponse.sendRedirect(
91                          ParamUtil.getString(actionRequest, "redirect"));
92                  }
93              }
94              else {
95                  throw e;
96              }
97          }
98      }
99  
100     public ActionForward render(
101             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
102             RenderRequest renderRequest, RenderResponse renderResponse)
103         throws Exception {
104 
105         try {
106             ActionUtil.getRole(renderRequest);
107         }
108         catch (Exception e) {
109             if (e instanceof NoSuchRoleException ||
110                 e instanceof PrincipalException) {
111 
112                 SessionErrors.add(renderRequest, e.getClass().getName());
113 
114                 return mapping.findForward("portlet.enterprise_admin.error");
115             }
116             else {
117                 throw e;
118             }
119         }
120 
121         return mapping.findForward(
122             getForward(renderRequest, "portlet.enterprise_admin.edit_role"));
123     }
124 
125     protected void deleteRole(ActionRequest actionRequest) throws Exception {
126         long roleId = ParamUtil.getLong(actionRequest, "roleId");
127 
128         RoleServiceUtil.deleteRole(roleId);
129     }
130 
131     protected void updateRole(ActionRequest actionRequest) throws Exception {
132         long roleId = ParamUtil.getLong(actionRequest, "roleId");
133 
134         String name = ParamUtil.getString(actionRequest, "name");
135         Map<Locale, String> localeTitlesMap =
136             LocalizationUtil.getLocalizedParameter(actionRequest, "title");
137         String description = ParamUtil.getString(actionRequest, "description");
138         int type = ParamUtil.getInteger(
139             actionRequest, "type", RoleConstants.TYPE_REGULAR);
140         String subtype = ParamUtil.getString(actionRequest, "subtype");
141 
142         if (roleId <= 0) {
143 
144             // Add role
145 
146             RoleServiceUtil.addRole(name, description, type);
147         }
148         else {
149 
150             // Update role
151 
152             RoleServiceUtil.updateRole(
153                 roleId, name, localeTitlesMap, description, subtype);
154         }
155     }
156 
157 }