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.HttpUtil;
024    import com.liferay.portal.kernel.util.LocalizationUtil;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Role;
028    import com.liferay.portal.model.RoleConstants;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.service.RoleServiceUtil;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.service.ServiceContextFactory;
033    import com.liferay.portal.struts.PortletAction;
034    import com.liferay.portal.util.PortalUtil;
035    
036    import java.util.Locale;
037    import java.util.Map;
038    
039    import javax.portlet.ActionRequest;
040    import javax.portlet.ActionResponse;
041    import javax.portlet.PortletConfig;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionForward;
047    import org.apache.struts.action.ActionMapping;
048    
049    /**
050     * @author Brian Wing Shun Chan
051     */
052    public class EditRoleAction extends PortletAction {
053    
054            @Override
055            public void processAction(
056                            ActionMapping actionMapping, ActionForm actionForm,
057                            PortletConfig portletConfig, ActionRequest actionRequest,
058                            ActionResponse actionResponse)
059                    throws Exception {
060    
061                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
062    
063                    try {
064                            Role role = null;
065    
066                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
067                                    role = updateRole(actionRequest);
068                            }
069                            else if (cmd.equals(Constants.DELETE)) {
070                                    deleteRole(actionRequest);
071                            }
072    
073                            String redirect = ParamUtil.getString(actionRequest, "redirect");
074    
075                            if (role != null) {
076                                    redirect = HttpUtil.setParameter(
077                                            redirect, actionResponse.getNamespace() + "roleId",
078                                            role.getRoleId());
079                            }
080    
081                            sendRedirect(actionRequest, actionResponse, redirect);
082                    }
083                    catch (Exception e) {
084                            if (e instanceof PrincipalException) {
085                                    SessionErrors.add(actionRequest, e.getClass());
086    
087                                    setForward(actionRequest, "portlet.roles_admin.error");
088                            }
089                            else if (e instanceof DuplicateRoleException ||
090                                             e instanceof NoSuchRoleException ||
091                                             e instanceof RequiredRoleException ||
092                                             e instanceof RoleNameException) {
093    
094                                    SessionErrors.add(actionRequest, e.getClass());
095    
096                                    if (cmd.equals(Constants.DELETE)) {
097                                            String redirect = PortalUtil.escapeRedirect(
098                                                    ParamUtil.getString(actionRequest, "redirect"));
099    
100                                            if (Validator.isNotNull(redirect)) {
101                                                    actionResponse.sendRedirect(redirect);
102                                            }
103                                    }
104                            }
105                            else {
106                                    throw e;
107                            }
108                    }
109            }
110    
111            @Override
112            public ActionForward render(
113                            ActionMapping actionMapping, ActionForm actionForm,
114                            PortletConfig portletConfig, RenderRequest renderRequest,
115                            RenderResponse renderResponse)
116                    throws Exception {
117    
118                    try {
119                            ActionUtil.getRole(renderRequest);
120                    }
121                    catch (Exception e) {
122                            if (e instanceof NoSuchRoleException ||
123                                    e instanceof PrincipalException) {
124    
125                                    SessionErrors.add(renderRequest, e.getClass());
126    
127                                    return actionMapping.findForward("portlet.roles_admin.error");
128                            }
129                            else {
130                                    throw e;
131                            }
132                    }
133    
134                    return actionMapping.findForward(
135                            getForward(renderRequest, "portlet.roles_admin.edit_role"));
136            }
137    
138            protected void deleteRole(ActionRequest actionRequest) throws Exception {
139                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
140    
141                    RoleServiceUtil.deleteRole(roleId);
142            }
143    
144            protected Role updateRole(ActionRequest actionRequest) throws Exception {
145                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
146    
147                    String name = ParamUtil.getString(actionRequest, "name");
148                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
149                            actionRequest, "title");
150                    Map<Locale, String> descriptionMap =
151                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
152                    int type = ParamUtil.getInteger(
153                            actionRequest, "type", RoleConstants.TYPE_REGULAR);
154                    String subtype = ParamUtil.getString(actionRequest, "subtype");
155                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
156                            Role.class.getName(), actionRequest);
157    
158                    if (roleId <= 0) {
159    
160                            // Add role
161    
162                            return RoleServiceUtil.addRole(
163                                    null, 0, name, titleMap, descriptionMap, type, subtype,
164                                    serviceContext);
165                    }
166                    else {
167    
168                            // Update role
169    
170                            return RoleServiceUtil.updateRole(
171                                    roleId, name, titleMap, descriptionMap, subtype,
172                                    serviceContext);
173                    }
174            }
175    
176    }