001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.enterpriseadmin.action;
022:
023: import com.liferay.portal.DuplicateRoleException;
024: import com.liferay.portal.NoSuchRoleException;
025: import com.liferay.portal.RequiredRoleException;
026: import com.liferay.portal.RoleNameException;
027: import com.liferay.portal.kernel.util.Constants;
028: import com.liferay.portal.kernel.util.ParamUtil;
029: import com.liferay.portal.model.impl.RoleImpl;
030: import com.liferay.portal.security.auth.PrincipalException;
031: import com.liferay.portal.service.RoleServiceUtil;
032: import com.liferay.portal.struts.PortletAction;
033: import com.liferay.util.servlet.SessionErrors;
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: * <a href="EditRoleAction.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class EditRoleAction extends PortletAction {
052:
053: public void processAction(ActionMapping mapping, ActionForm form,
054: PortletConfig config, ActionRequest req, ActionResponse res)
055: throws Exception {
056:
057: String cmd = ParamUtil.getString(req, Constants.CMD);
058:
059: try {
060: if (cmd.equals(Constants.ADD)
061: || cmd.equals(Constants.UPDATE)) {
062: updateRole(req);
063: } else if (cmd.equals(Constants.DELETE)) {
064: deleteRole(req);
065: }
066:
067: sendRedirect(req, res);
068: } catch (Exception e) {
069: if (e instanceof PrincipalException) {
070: SessionErrors.add(req, e.getClass().getName());
071:
072: setForward(req, "portlet.enterprise_admin.error");
073: } else if (e instanceof DuplicateRoleException
074: || e instanceof NoSuchRoleException
075: || e instanceof RequiredRoleException
076: || e instanceof RoleNameException) {
077:
078: SessionErrors.add(req, e.getClass().getName());
079:
080: if (cmd.equals(Constants.DELETE)) {
081: res.sendRedirect(ParamUtil.getString(req,
082: "redirect"));
083: }
084: } else {
085: throw e;
086: }
087: }
088: }
089:
090: public ActionForward render(ActionMapping mapping, ActionForm form,
091: PortletConfig config, RenderRequest req, RenderResponse res)
092: throws Exception {
093:
094: try {
095: ActionUtil.getRole(req);
096: } catch (Exception e) {
097: if (e instanceof NoSuchRoleException
098: || e instanceof PrincipalException) {
099:
100: SessionErrors.add(req, e.getClass().getName());
101:
102: return mapping
103: .findForward("portlet.enterprise_admin.error");
104: } else {
105: throw e;
106: }
107: }
108:
109: return mapping.findForward(getForward(req,
110: "portlet.enterprise_admin.edit_role"));
111: }
112:
113: protected void deleteRole(ActionRequest req) throws Exception {
114: long roleId = ParamUtil.getLong(req, "roleId");
115:
116: RoleServiceUtil.deleteRole(roleId);
117: }
118:
119: protected void updateRole(ActionRequest req) throws Exception {
120: long roleId = ParamUtil.getLong(req, "roleId");
121:
122: String name = ParamUtil.getString(req, "name");
123: String description = ParamUtil.getString(req, "description");
124: int type = ParamUtil.getInteger(req, "type",
125: RoleImpl.TYPE_REGULAR);
126:
127: if (roleId <= 0) {
128:
129: // Add role
130:
131: RoleServiceUtil.addRole(name, description, type);
132: } else {
133:
134: // Update role
135:
136: RoleServiceUtil.updateRole(roleId, name, description);
137: }
138: }
139:
140: }
|