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