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.DuplicateUserGroupException;
024: import com.liferay.portal.NoSuchUserGroupException;
025: import com.liferay.portal.RequiredUserGroupException;
026: import com.liferay.portal.UserGroupNameException;
027: import com.liferay.portal.kernel.util.Constants;
028: import com.liferay.portal.kernel.util.ParamUtil;
029: import com.liferay.portal.kernel.util.StringUtil;
030: import com.liferay.portal.security.auth.PrincipalException;
031: import com.liferay.portal.service.UserGroupServiceUtil;
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="EditUserGroupAction.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Charles May
049: *
050: */
051: public class EditUserGroupAction 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: updateUserGroup(req);
063: } else if (cmd.equals(Constants.DELETE)) {
064: deleteUserGroups(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 DuplicateUserGroupException
074: || e instanceof NoSuchUserGroupException
075: || e instanceof RequiredUserGroupException
076: || e instanceof UserGroupNameException) {
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.getUserGroup(req);
096: } catch (Exception e) {
097: if (e instanceof NoSuchUserGroupException
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_user_group"));
111: }
112:
113: protected void deleteUserGroups(ActionRequest req) throws Exception {
114: long[] deleteUserGroupIds = StringUtil.split(ParamUtil
115: .getString(req, "deleteUserGroupIds"), 0L);
116:
117: for (int i = 0; i < deleteUserGroupIds.length; i++) {
118: UserGroupServiceUtil.deleteUserGroup(deleteUserGroupIds[i]);
119: }
120: }
121:
122: protected void updateUserGroup(ActionRequest req) throws Exception {
123: long userGroupId = ParamUtil.getLong(req, "userGroupId");
124:
125: String name = ParamUtil.getString(req, "name");
126: String description = ParamUtil.getString(req, "description");
127:
128: if (userGroupId <= 0) {
129:
130: // Add user group
131:
132: UserGroupServiceUtil.addUserGroup(name, description);
133: } else {
134:
135: // Update user group
136:
137: UserGroupServiceUtil.updateUserGroup(userGroupId, name,
138: description);
139: }
140: }
141:
142: }
|