001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
060                                    updateRole(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.DELETE)) {
063                                    deleteRole(actionRequest);
064                            }
065    
066                            sendRedirect(actionRequest, actionResponse);
067                    }
068                    catch (Exception e) {
069                            if (e instanceof PrincipalException) {
070                                    SessionErrors.add(actionRequest, e.getClass().getName());
071    
072                                    setForward(actionRequest, "portlet.roles_admin.error");
073                            }
074                            else if (e instanceof DuplicateRoleException ||
075                                             e instanceof NoSuchRoleException ||
076                                             e instanceof RequiredRoleException ||
077                                             e instanceof RoleNameException) {
078    
079                                    SessionErrors.add(actionRequest, e.getClass().getName());
080    
081                                    if (cmd.equals(Constants.DELETE)) {
082                                            String redirect = PortalUtil.escapeRedirect(
083                                                    ParamUtil.getString(actionRequest, "redirect"));
084    
085                                            if (Validator.isNotNull(redirect)) {
086                                                    actionResponse.sendRedirect(redirect);
087                                            }
088                                    }
089                            }
090                            else {
091                                    throw e;
092                            }
093                    }
094            }
095    
096            @Override
097            public ActionForward render(
098                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
099                            RenderRequest renderRequest, RenderResponse renderResponse)
100                    throws Exception {
101    
102                    try {
103                            ActionUtil.getRole(renderRequest);
104                    }
105                    catch (Exception e) {
106                            if (e instanceof NoSuchRoleException ||
107                                    e instanceof PrincipalException) {
108    
109                                    SessionErrors.add(renderRequest, e.getClass().getName());
110    
111                                    return mapping.findForward("portlet.roles_admin.error");
112                            }
113                            else {
114                                    throw e;
115                            }
116                    }
117    
118                    return mapping.findForward(
119                            getForward(renderRequest, "portlet.roles_admin.edit_role"));
120            }
121    
122            protected void deleteRole(ActionRequest actionRequest) throws Exception {
123                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
124    
125                    RoleServiceUtil.deleteRole(roleId);
126            }
127    
128            protected void updateRole(ActionRequest actionRequest) throws Exception {
129                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
130    
131                    String name = ParamUtil.getString(actionRequest, "name");
132                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
133                            actionRequest, "title");
134                    Map<Locale, String> descriptionMap =
135                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
136                    int type = ParamUtil.getInteger(
137                            actionRequest, "type", RoleConstants.TYPE_REGULAR);
138                    String subtype = ParamUtil.getString(actionRequest, "subtype");
139    
140                    if (roleId <= 0) {
141    
142                            // Add role
143    
144                            RoleServiceUtil.addRole(name, titleMap, descriptionMap, type);
145                    }
146                    else {
147    
148                            // Update role
149    
150                            RoleServiceUtil.updateRole(
151                                    roleId, name, titleMap, descriptionMap, subtype);
152                    }
153            }
154    
155    }