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