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  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  public class EditRoleAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
64  
65          try {
66              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
67                  updateRole(actionRequest);
68              }
69              else if (cmd.equals(Constants.DELETE)) {
70                  deleteRole(actionRequest);
71              }
72  
73              sendRedirect(actionRequest, actionResponse);
74          }
75          catch (Exception e) {
76              if (e instanceof PrincipalException) {
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78  
79                  setForward(actionRequest, "portlet.enterprise_admin.error");
80              }
81              else if (e instanceof DuplicateRoleException ||
82                       e instanceof NoSuchRoleException ||
83                       e instanceof RequiredRoleException ||
84                       e instanceof RoleNameException) {
85  
86                  SessionErrors.add(actionRequest, e.getClass().getName());
87  
88                  if (cmd.equals(Constants.DELETE)) {
89                      actionResponse.sendRedirect(
90                          ParamUtil.getString(actionRequest, "redirect"));
91                  }
92              }
93              else {
94                  throw e;
95              }
96          }
97      }
98  
99      public ActionForward render(
100             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101             RenderRequest renderRequest, RenderResponse renderResponse)
102         throws Exception {
103 
104         try {
105             ActionUtil.getRole(renderRequest);
106         }
107         catch (Exception e) {
108             if (e instanceof NoSuchRoleException ||
109                 e instanceof PrincipalException) {
110 
111                 SessionErrors.add(renderRequest, e.getClass().getName());
112 
113                 return mapping.findForward("portlet.enterprise_admin.error");
114             }
115             else {
116                 throw e;
117             }
118         }
119 
120         return mapping.findForward(
121             getForward(renderRequest, "portlet.enterprise_admin.edit_role"));
122     }
123 
124     protected void deleteRole(ActionRequest actionRequest) throws Exception {
125         long roleId = ParamUtil.getLong(actionRequest, "roleId");
126 
127         RoleServiceUtil.deleteRole(roleId);
128     }
129 
130     protected void updateRole(ActionRequest actionRequest) throws Exception {
131         long roleId = ParamUtil.getLong(actionRequest, "roleId");
132 
133         String name = ParamUtil.getString(actionRequest, "name");
134         Map<Locale, String> localeTitlesMap =
135             LocalizationUtil.getLocalizationMap(actionRequest, "title");
136         String description = ParamUtil.getString(actionRequest, "description");
137         int type = ParamUtil.getInteger(
138             actionRequest, "type", RoleConstants.TYPE_REGULAR);
139         String subtype = ParamUtil.getString(actionRequest, "subtype");
140 
141         if (roleId <= 0) {
142 
143             // Add role
144 
145             RoleServiceUtil.addRole(name, description, type);
146         }
147         else {
148 
149             // Update role
150 
151             RoleServiceUtil.updateRole(
152                 roleId, name, localeTitlesMap, description, subtype);
153         }
154     }
155 
156 }