001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.enterpriseadmin.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.model.RoleConstants;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.RoleServiceUtil;
028    import com.liferay.portal.struts.PortletAction;
029    
030    import java.util.Locale;
031    import java.util.Map;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class EditRoleAction extends PortletAction {
047    
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
057                                    updateRole(actionRequest);
058                            }
059                            else if (cmd.equals(Constants.DELETE)) {
060                                    deleteRole(actionRequest);
061                            }
062    
063                            sendRedirect(actionRequest, actionResponse);
064                    }
065                    catch (Exception e) {
066                            if (e instanceof PrincipalException) {
067                                    SessionErrors.add(actionRequest, e.getClass().getName());
068    
069                                    setForward(actionRequest, "portlet.enterprise_admin.error");
070                            }
071                            else if (e instanceof DuplicateRoleException ||
072                                             e instanceof NoSuchRoleException ||
073                                             e instanceof RequiredRoleException ||
074                                             e instanceof RoleNameException) {
075    
076                                    SessionErrors.add(actionRequest, e.getClass().getName());
077    
078                                    if (cmd.equals(Constants.DELETE)) {
079                                            actionResponse.sendRedirect(
080                                                    ParamUtil.getString(actionRequest, "redirect"));
081                                    }
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            public ActionForward render(
090                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
091                            RenderRequest renderRequest, RenderResponse renderResponse)
092                    throws Exception {
093    
094                    try {
095                            ActionUtil.getRole(renderRequest);
096                    }
097                    catch (Exception e) {
098                            if (e instanceof NoSuchRoleException ||
099                                    e instanceof PrincipalException) {
100    
101                                    SessionErrors.add(renderRequest, e.getClass().getName());
102    
103                                    return mapping.findForward("portlet.enterprise_admin.error");
104                            }
105                            else {
106                                    throw e;
107                            }
108                    }
109    
110                    return mapping.findForward(
111                            getForward(renderRequest, "portlet.enterprise_admin.edit_role"));
112            }
113    
114            protected void deleteRole(ActionRequest actionRequest) throws Exception {
115                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
116    
117                    RoleServiceUtil.deleteRole(roleId);
118            }
119    
120            protected void updateRole(ActionRequest actionRequest) throws Exception {
121                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
122    
123                    String name = ParamUtil.getString(actionRequest, "name");
124                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
125                            actionRequest, "title");
126                    String description = ParamUtil.getString(actionRequest, "description");
127                    int type = ParamUtil.getInteger(
128                            actionRequest, "type", RoleConstants.TYPE_REGULAR);
129                    String subtype = ParamUtil.getString(actionRequest, "subtype");
130    
131                    if (roleId <= 0) {
132    
133                            // Add role
134    
135                            RoleServiceUtil.addRole(name, titleMap, description, type);
136                    }
137                    else {
138    
139                            // Update role
140    
141                            RoleServiceUtil.updateRole(
142                                    roleId, name, titleMap, description, subtype);
143                    }
144            }
145    
146    }