001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.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.model.UserGroup;
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.portlet.communities.util.CommunitiesUtil;
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            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
052    
053                    try {
054                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
055                                    updateUserGroup(actionRequest);
056                            }
057                            else if (cmd.equals(Constants.DELETE)) {
058                                    deleteUserGroups(actionRequest);
059                            }
060    
061                            sendRedirect(actionRequest, actionResponse);
062                    }
063                    catch (Exception e) {
064                            if (e instanceof PrincipalException) {
065                                    SessionErrors.add(actionRequest, e.getClass().getName());
066    
067                                    setForward(actionRequest, "portlet.enterprise_admin.error");
068                            }
069                            else if (e instanceof DuplicateUserGroupException ||
070                                             e instanceof NoSuchUserGroupException ||
071                                             e instanceof RequiredUserGroupException ||
072                                             e instanceof UserGroupNameException) {
073    
074                                    SessionErrors.add(actionRequest, e.getClass().getName());
075    
076                                    if (cmd.equals(Constants.DELETE)) {
077                                            actionResponse.sendRedirect(
078                                                    ParamUtil.getString(actionRequest, "redirect"));
079                                    }
080                            }
081                            else {
082                                    throw e;
083                            }
084                    }
085            }
086    
087            public ActionForward render(
088                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
089                            RenderRequest renderRequest, RenderResponse renderResponse)
090                    throws Exception {
091    
092                    try {
093                            ActionUtil.getUserGroup(renderRequest);
094                    }
095                    catch (Exception e) {
096                            if (e instanceof NoSuchUserGroupException ||
097                                    e instanceof PrincipalException) {
098    
099                                    SessionErrors.add(renderRequest, e.getClass().getName());
100    
101                                    return mapping.findForward("portlet.enterprise_admin.error");
102                            }
103                            else {
104                                    throw e;
105                            }
106                    }
107    
108                    return mapping.findForward(getForward(
109                            renderRequest, "portlet.enterprise_admin.edit_user_group"));
110            }
111    
112            protected void deleteUserGroups(ActionRequest actionRequest)
113                    throws Exception {
114    
115                    long[] deleteUserGroupIds = StringUtil.split(
116                            ParamUtil.getString(actionRequest, "deleteUserGroupIds"), 0L);
117    
118                    for (int i = 0; i < deleteUserGroupIds.length; i++) {
119                            UserGroupServiceUtil.deleteUserGroup(deleteUserGroupIds[i]);
120                    }
121            }
122    
123            protected void updateUserGroup(ActionRequest actionRequest)
124                    throws Exception {
125    
126                    long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
127    
128                    String name = ParamUtil.getString(actionRequest, "name");
129                    String description = ParamUtil.getString(actionRequest, "description");
130    
131                    UserGroup userGroup = null;
132    
133                    if (userGroupId <= 0) {
134    
135                            // Add user group
136    
137                            userGroup = UserGroupServiceUtil.addUserGroup(name, description);
138                    }
139                    else {
140    
141                            // Update user group
142    
143                            userGroup = UserGroupServiceUtil.updateUserGroup(
144                                    userGroupId, name, description);
145                    }
146    
147                    // Layout set prototypes
148    
149                    long publicLayoutSetPrototypeId = ParamUtil.getLong(
150                            actionRequest, "publicLayoutSetPrototypeId");
151                    long privateLayoutSetPrototypeId = ParamUtil.getLong(
152                            actionRequest, "privateLayoutSetPrototypeId");
153    
154                    CommunitiesUtil.applyLayoutSetPrototypes(
155                            userGroup.getGroup(), publicLayoutSetPrototypeId,
156                            privateLayoutSetPrototypeId);
157            }
158    
159    }