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