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.Role;
027    import com.liferay.portal.model.RoleConstants;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.service.RoleServiceUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.ServiceContextFactory;
032    import com.liferay.portal.struts.PortletAction;
033    import com.liferay.portal.util.PortalUtil;
034    
035    import java.util.Locale;
036    import java.util.Map;
037    
038    import javax.portlet.ActionRequest;
039    import javax.portlet.ActionResponse;
040    import javax.portlet.PortletConfig;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionForward;
046    import org.apache.struts.action.ActionMapping;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     */
051    public class EditRoleAction extends PortletAction {
052    
053            @Override
054            public void processAction(
055                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
056                            ActionRequest actionRequest, ActionResponse actionResponse)
057                    throws Exception {
058    
059                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
060    
061                    try {
062                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
063                                    updateRole(actionRequest);
064                            }
065                            else if (cmd.equals(Constants.DELETE)) {
066                                    deleteRole(actionRequest);
067                            }
068    
069                            sendRedirect(actionRequest, actionResponse);
070                    }
071                    catch (Exception e) {
072                            if (e instanceof PrincipalException) {
073                                    SessionErrors.add(actionRequest, e.getClass());
074    
075                                    setForward(actionRequest, "portlet.roles_admin.error");
076                            }
077                            else if (e instanceof DuplicateRoleException ||
078                                             e instanceof NoSuchRoleException ||
079                                             e instanceof RequiredRoleException ||
080                                             e instanceof RoleNameException) {
081    
082                                    SessionErrors.add(actionRequest, e.getClass());
083    
084                                    if (cmd.equals(Constants.DELETE)) {
085                                            String redirect = PortalUtil.escapeRedirect(
086                                                    ParamUtil.getString(actionRequest, "redirect"));
087    
088                                            if (Validator.isNotNull(redirect)) {
089                                                    actionResponse.sendRedirect(redirect);
090                                            }
091                                    }
092                            }
093                            else {
094                                    throw e;
095                            }
096                    }
097            }
098    
099            @Override
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());
113    
114                                    return mapping.findForward("portlet.roles_admin.error");
115                            }
116                            else {
117                                    throw e;
118                            }
119                    }
120    
121                    return mapping.findForward(
122                            getForward(renderRequest, "portlet.roles_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> titleMap = LocalizationUtil.getLocalizationMap(
136                            actionRequest, "title");
137                    Map<Locale, String> descriptionMap =
138                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
139                    int type = ParamUtil.getInteger(
140                            actionRequest, "type", RoleConstants.TYPE_REGULAR);
141                    String subtype = ParamUtil.getString(actionRequest, "subtype");
142                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
143                            Role.class.getName(), actionRequest);
144    
145                    if (roleId <= 0) {
146    
147                            // Add role
148    
149                            RoleServiceUtil.addRole(
150                                    null, 0, name, titleMap, descriptionMap, type, subtype,
151                                    serviceContext);
152                    }
153                    else {
154    
155                            // Update role
156    
157                            RoleServiceUtil.updateRole(
158                                    roleId, name, titleMap, descriptionMap, subtype,
159                                    serviceContext);
160                    }
161            }
162    
163    }