001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.rolesadmin.action;
016    
017    import com.liferay.portal.DuplicateRoleException;
018    import com.liferay.portal.NoSuchRoleException;
019    import com.liferay.portal.RequiredRoleException;
020    import com.liferay.portal.RoleNameException;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.LocalizationUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.RoleConstants;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.RoleServiceUtil;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.util.PortalUtil;
031    
032    import java.util.Locale;
033    import java.util.Map;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class EditRoleAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058    
059                    try {
060                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
061                                    updateRole(actionRequest);
062                            }
063                            else if (cmd.equals(Constants.DELETE)) {
064                                    deleteRole(actionRequest);
065                            }
066    
067                            sendRedirect(actionRequest, actionResponse);
068                    }
069                    catch (Exception e) {
070                            if (e instanceof PrincipalException) {
071                                    SessionErrors.add(actionRequest, e.getClass());
072    
073                                    setForward(actionRequest, "portlet.roles_admin.error");
074                            }
075                            else if (e instanceof DuplicateRoleException ||
076                                             e instanceof NoSuchRoleException ||
077                                             e instanceof RequiredRoleException ||
078                                             e instanceof RoleNameException) {
079    
080                                    SessionErrors.add(actionRequest, e.getClass());
081    
082                                    if (cmd.equals(Constants.DELETE)) {
083                                            String redirect = PortalUtil.escapeRedirect(
084                                                    ParamUtil.getString(actionRequest, "redirect"));
085    
086                                            if (Validator.isNotNull(redirect)) {
087                                                    actionResponse.sendRedirect(redirect);
088                                            }
089                                    }
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095            }
096    
097            @Override
098            public ActionForward render(
099                            ActionMapping actionMapping, ActionForm actionForm,
100                            PortletConfig portletConfig, RenderRequest renderRequest,
101                            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());
112    
113                                    return actionMapping.findForward("portlet.roles_admin.error");
114                            }
115                            else {
116                                    throw e;
117                            }
118                    }
119    
120                    return actionMapping.findForward(
121                            getForward(renderRequest, "portlet.roles_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> titleMap = LocalizationUtil.getLocalizationMap(
135                            actionRequest, "title");
136                    Map<Locale, String> descriptionMap =
137                            LocalizationUtil.getLocalizationMap(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(
147                                    null, 0, name, titleMap, descriptionMap, type, subtype);
148                    }
149                    else {
150    
151                            // Update role
152    
153                            RoleServiceUtil.updateRole(
154                                    roleId, name, titleMap, descriptionMap, subtype);
155                    }
156            }
157    
158    }