001    /**
002     * Copyright (c) 2000-2011 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.usergroupsadmin.action;
016    
017    import com.liferay.portal.DuplicateUserGroupException;
018    import com.liferay.portal.NoSuchUserGroupException;
019    import com.liferay.portal.RequiredUserGroupException;
020    import com.liferay.portal.UserGroupNameException;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.UserGroupServiceUtil;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.util.PortalUtil;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Charles May
043     */
044    public class EditUserGroupAction extends PortletAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
049                            ActionRequest actionRequest, ActionResponse actionResponse)
050                    throws Exception {
051    
052                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
053    
054                    try {
055                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
056                                    updateUserGroup(actionRequest);
057                            }
058                            else if (cmd.equals(Constants.DELETE)) {
059                                    deleteUserGroups(actionRequest);
060                            }
061    
062                            sendRedirect(actionRequest, actionResponse);
063                    }
064                    catch (Exception e) {
065                            if (e instanceof PrincipalException) {
066                                    SessionErrors.add(actionRequest, e.getClass().getName());
067    
068                                    setForward(actionRequest, "portlet.user_groups_admin.error");
069                            }
070                            else if (e instanceof DuplicateUserGroupException ||
071                                             e instanceof NoSuchUserGroupException ||
072                                             e instanceof RequiredUserGroupException ||
073                                             e instanceof UserGroupNameException) {
074    
075                                    SessionErrors.add(actionRequest, e.getClass().getName());
076    
077                                    if (cmd.equals(Constants.DELETE)) {
078                                            String redirect = PortalUtil.escapeRedirect(
079                                                    ParamUtil.getString(actionRequest, "redirect"));
080    
081                                            if (Validator.isNotNull(redirect)) {
082                                                    actionResponse.sendRedirect(redirect);
083                                            }
084                                    }
085                            }
086                            else {
087                                    throw e;
088                            }
089                    }
090            }
091    
092            @Override
093            public ActionForward render(
094                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
095                            RenderRequest renderRequest, RenderResponse renderResponse)
096                    throws Exception {
097    
098                    try {
099                            ActionUtil.getUserGroup(renderRequest);
100                    }
101                    catch (Exception e) {
102                            if (e instanceof NoSuchUserGroupException ||
103                                    e instanceof PrincipalException) {
104    
105                                    SessionErrors.add(renderRequest, e.getClass().getName());
106    
107                                    return mapping.findForward("portlet.user_groups_admin.error");
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113    
114                    return mapping.findForward(getForward(
115                            renderRequest, "portlet.user_groups_admin.edit_user_group"));
116            }
117    
118            protected void deleteUserGroups(ActionRequest actionRequest)
119                    throws Exception {
120    
121                    long[] deleteUserGroupIds = StringUtil.split(
122                            ParamUtil.getString(actionRequest, "deleteUserGroupIds"), 0L);
123    
124                    for (int i = 0; i < deleteUserGroupIds.length; i++) {
125                            UserGroupServiceUtil.deleteUserGroup(deleteUserGroupIds[i]);
126                    }
127            }
128    
129            protected void updateUserGroup(ActionRequest actionRequest)
130                    throws Exception {
131    
132                    long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
133    
134                    String name = ParamUtil.getString(actionRequest, "name");
135                    String description = ParamUtil.getString(actionRequest, "description");
136                    long publicLayoutSetPrototypeId = ParamUtil.getLong(
137                            actionRequest, "publicLayoutSetPrototypeId");
138                    long privateLayoutSetPrototypeId = ParamUtil.getLong(
139                            actionRequest, "privateLayoutSetPrototypeId");
140    
141                    if (userGroupId <= 0) {
142    
143                            // Add user group
144    
145                            UserGroupServiceUtil.addUserGroup(
146                                    name, description, publicLayoutSetPrototypeId,
147                                    privateLayoutSetPrototypeId);
148                    }
149                    else {
150    
151                            // Update user group
152    
153                            UserGroupServiceUtil.updateUserGroup(
154                                    userGroupId, name, description, publicLayoutSetPrototypeId,
155                                    privateLayoutSetPrototypeId);
156                    }
157    
158            }
159    
160    }