001    /**
002     * Copyright (c) 2000-2012 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.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.model.UserGroup;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.UserGroupServiceUtil;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.sites.util.SitesUtil;
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 Charles May
045     */
046    public class EditUserGroupAction extends PortletAction {
047    
048            @Override
049            public void processAction(
050                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
051                            ActionRequest actionRequest, ActionResponse actionResponse)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055    
056                    try {
057                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
058                                    updateUserGroup(actionRequest);
059                            }
060                            else if (cmd.equals(Constants.DELETE)) {
061                                    deleteUserGroups(actionRequest);
062                            }
063    
064                            sendRedirect(actionRequest, actionResponse);
065                    }
066                    catch (Exception e) {
067                            if (e instanceof PrincipalException) {
068                                    SessionErrors.add(actionRequest, e.getClass());
069    
070                                    setForward(actionRequest, "portlet.user_groups_admin.error");
071                            }
072                            else if (e instanceof DuplicateUserGroupException ||
073                                             e instanceof NoSuchUserGroupException ||
074                                             e instanceof RequiredUserGroupException ||
075                                             e instanceof UserGroupNameException) {
076    
077                                    SessionErrors.add(actionRequest, e.getClass());
078    
079                                    if (cmd.equals(Constants.DELETE)) {
080                                            String redirect = PortalUtil.escapeRedirect(
081                                                    ParamUtil.getString(actionRequest, "redirect"));
082    
083                                            if (Validator.isNotNull(redirect)) {
084                                                    actionResponse.sendRedirect(redirect);
085                                            }
086                                    }
087                            }
088                            else {
089                                    throw e;
090                            }
091                    }
092            }
093    
094            @Override
095            public ActionForward render(
096                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
097                            RenderRequest renderRequest, RenderResponse renderResponse)
098                    throws Exception {
099    
100                    try {
101                            ActionUtil.getUserGroup(renderRequest);
102                    }
103                    catch (Exception e) {
104                            if (e instanceof NoSuchUserGroupException ||
105                                    e instanceof PrincipalException) {
106    
107                                    SessionErrors.add(renderRequest, e.getClass());
108    
109                                    return mapping.findForward("portlet.user_groups_admin.error");
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115    
116                    return mapping.findForward(
117                            getForward(
118                                    renderRequest, "portlet.user_groups_admin.edit_user_group"));
119            }
120    
121            protected void deleteUserGroups(ActionRequest actionRequest)
122                    throws Exception {
123    
124                    long[] deleteUserGroupIds = StringUtil.split(
125                            ParamUtil.getString(actionRequest, "deleteUserGroupIds"), 0L);
126    
127                    for (long deleteUserGroupId : deleteUserGroupIds) {
128                            UserGroupServiceUtil.deleteUserGroup(deleteUserGroupId);
129                    }
130            }
131    
132            protected void updateUserGroup(ActionRequest actionRequest)
133                    throws Exception {
134    
135                    long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
136    
137                    String name = ParamUtil.getString(actionRequest, "name");
138                    String description = ParamUtil.getString(actionRequest, "description");
139    
140                    UserGroup userGroup = null;
141    
142                    if (userGroupId <= 0) {
143    
144                            // Add user group
145    
146                            userGroup = UserGroupServiceUtil.addUserGroup(name, description);
147                    }
148                    else {
149    
150                            // Update user group
151    
152                            userGroup = UserGroupServiceUtil.updateUserGroup(
153                                    userGroupId, name, description);
154                    }
155    
156                    // Layout set prototypes
157    
158                    long publicLayoutSetPrototypeId = ParamUtil.getLong(
159                            actionRequest, "publicLayoutSetPrototypeId");
160                    long privateLayoutSetPrototypeId = ParamUtil.getLong(
161                            actionRequest, "privateLayoutSetPrototypeId");
162                    boolean publicLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
163                            actionRequest, "publicLayoutSetPrototypeLinkEnabled");
164                    boolean privateLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
165                            actionRequest, "privateLayoutSetPrototypeLinkEnabled");
166    
167                    SitesUtil.updateLayoutSetPrototypesLinks(
168                            userGroup.getGroup(), publicLayoutSetPrototypeId,
169                            privateLayoutSetPrototypeId, publicLayoutSetPrototypeLinkEnabled,
170                            privateLayoutSetPrototypeLinkEnabled);
171            }
172    
173    }