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().getName());
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().getName());
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().getName());
108    
109                                    return mapping.findForward("portlet.user_groups_admin.error");
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115    
116                    return mapping.findForward(getForward(
117                            renderRequest, "portlet.user_groups_admin.edit_user_group"));
118            }
119    
120            protected void deleteUserGroups(ActionRequest actionRequest)
121                    throws Exception {
122    
123                    long[] deleteUserGroupIds = StringUtil.split(
124                            ParamUtil.getString(actionRequest, "deleteUserGroupIds"), 0L);
125    
126                    for (long deleteUserGroupId : deleteUserGroupIds) {
127                            UserGroupServiceUtil.deleteUserGroup(deleteUserGroupId);
128                    }
129            }
130    
131            protected void updateUserGroup(ActionRequest actionRequest)
132                    throws Exception {
133    
134                    long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
135    
136                    String name = ParamUtil.getString(actionRequest, "name");
137                    String description = ParamUtil.getString(actionRequest, "description");
138    
139                    UserGroup userGroup = null;
140    
141                    if (userGroupId <= 0) {
142    
143                            // Add user group
144    
145                            userGroup = UserGroupServiceUtil.addUserGroup(name, description);
146                    }
147                    else {
148    
149                            // Update user group
150    
151                            userGroup = UserGroupServiceUtil.updateUserGroup(
152                                    userGroupId, name, description);
153                    }
154    
155                    // Layout set prototypes
156    
157                    long publicLayoutSetPrototypeId = ParamUtil.getLong(
158                            actionRequest, "publicLayoutSetPrototypeId");
159                    long privateLayoutSetPrototypeId = ParamUtil.getLong(
160                            actionRequest, "privateLayoutSetPrototypeId");
161                    boolean publicLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
162                            actionRequest, "publicLayoutSetPrototypeLinkEnabled");
163                    boolean privateLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
164                            actionRequest, "privateLayoutSetPrototypeLinkEnabled");
165    
166                    SitesUtil.updateLayoutSetPrototypesLinks(
167                            userGroup.getGroup(), publicLayoutSetPrototypeId,
168                            privateLayoutSetPrototypeId, publicLayoutSetPrototypeLinkEnabled,
169                            privateLayoutSetPrototypeLinkEnabled);
170            }
171    
172    }