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.sites.action;
016    
017    import com.liferay.portal.kernel.util.Constants;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.LayoutTypePortlet;
022    import com.liferay.portal.model.MembershipRequest;
023    import com.liferay.portal.model.PortletPreferencesIds;
024    import com.liferay.portal.model.Team;
025    import com.liferay.portal.service.GroupLocalServiceUtil;
026    import com.liferay.portal.service.MembershipRequestLocalServiceUtil;
027    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
028    import com.liferay.portal.service.TeamLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.PortletPreferencesFactoryUtil;
034    import com.liferay.portlet.sites.util.SitesUtil;
035    
036    import java.util.List;
037    
038    import javax.portlet.PortletPreferences;
039    import javax.portlet.PortletRequest;
040    
041    import javax.servlet.http.HttpServletRequest;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class ActionUtil
047            extends com.liferay.portlet.rolesadmin.action.ActionUtil {
048    
049            public static void copyPreferences(
050                            HttpServletRequest request, Layout targetLayout,
051                            Layout sourceLayout)
052                    throws Exception {
053    
054                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
055                            WebKeys.THEME_DISPLAY);
056    
057                    LayoutTypePortlet sourceLayoutTypePortlet =
058                            (LayoutTypePortlet)sourceLayout.getLayoutType();
059    
060                    List<String> sourcePortletIds = sourceLayoutTypePortlet.getPortletIds();
061    
062                    for (String sourcePortletId : sourcePortletIds) {
063    
064                            // Copy preference
065    
066                            PortletPreferencesIds portletPreferencesIds =
067                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
068                                            request, targetLayout, sourcePortletId);
069    
070                            PortletPreferencesLocalServiceUtil.getPreferences(
071                                    portletPreferencesIds);
072    
073                            PortletPreferencesIds sourcePortletPreferencesIds =
074                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
075                                            request, sourceLayout, sourcePortletId);
076    
077                            PortletPreferences sourcePreferences =
078                                    PortletPreferencesLocalServiceUtil.getPreferences(
079                                            sourcePortletPreferencesIds);
080    
081                            PortletPreferencesLocalServiceUtil.updatePreferences(
082                                    portletPreferencesIds.getOwnerId(),
083                                    portletPreferencesIds.getOwnerType(),
084                                    portletPreferencesIds.getPlid(),
085                                    portletPreferencesIds.getPortletId(), sourcePreferences);
086    
087                            // Copy portlet setup
088    
089                            PortletPreferences targetPreferences =
090                                    PortletPreferencesLocalServiceUtil.getPreferences(
091                                            themeDisplay.getCompanyId(),
092                                            PortletKeys.PREFS_OWNER_ID_DEFAULT,
093                                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT, targetLayout.getPlid(),
094                                            sourcePortletId);
095    
096                            sourcePreferences =
097                                    PortletPreferencesLocalServiceUtil.getPreferences(
098                                            themeDisplay.getCompanyId(),
099                                            PortletKeys.PREFS_OWNER_ID_DEFAULT,
100                                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT, sourceLayout.getPlid(),
101                                            sourcePortletId);
102    
103                            PortletPreferencesLocalServiceUtil.updatePreferences(
104                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
105                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, targetLayout.getPlid(),
106                                    sourcePortletId, sourcePreferences);
107    
108                            SitesUtil.updateLayoutScopes(
109                                    themeDisplay.getUserId(), sourceLayout, targetLayout,
110                                    sourcePreferences, targetPreferences, sourcePortletId,
111                                    themeDisplay.getLanguageId());
112                    }
113            }
114    
115            public static void copyPreferences(
116                            PortletRequest portletRequest, Layout targetLayout,
117                            Layout sourceLayout)
118                    throws Exception {
119    
120                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
121                            portletRequest);
122    
123                    copyPreferences(request, targetLayout, sourceLayout);
124            }
125    
126            public static Group getGroup(HttpServletRequest request) throws Exception {
127                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
128                            WebKeys.THEME_DISPLAY);
129    
130                    String cmd = ParamUtil.getString(request, Constants.CMD);
131    
132                    long groupId = ParamUtil.getLong(request, "groupId");
133    
134                    Group group = null;
135    
136                    if (groupId > 0) {
137                            group = GroupLocalServiceUtil.getGroup(groupId);
138                    }
139                    else if (!cmd.equals(Constants.ADD)) {
140                            group = themeDisplay.getScopeGroup();
141                    }
142    
143                    request.setAttribute(WebKeys.GROUP, group);
144    
145                    return group;
146            }
147    
148            public static Group getGroup(PortletRequest portletRequest)
149                    throws Exception {
150    
151                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
152                            portletRequest);
153    
154                    return getGroup(request);
155            }
156    
157            public static void getMembershipRequest(HttpServletRequest request)
158                    throws Exception {
159    
160                    long membershipRequestId = ParamUtil.getLong(
161                            request, "membershipRequestId");
162    
163                    MembershipRequest membershipRequest = null;
164    
165                    if (membershipRequestId > 0) {
166                            membershipRequest =
167                                    MembershipRequestLocalServiceUtil.getMembershipRequest(
168                                            membershipRequestId);
169                    }
170    
171                    request.setAttribute(WebKeys.MEMBERSHIP_REQUEST, membershipRequest);
172            }
173    
174            public static void getMembershipRequest(PortletRequest portletRequest)
175                    throws Exception {
176    
177                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
178                            portletRequest);
179    
180                    getMembershipRequest(request);
181            }
182    
183            public static void getTeam(HttpServletRequest request) throws Exception {
184                    long teamId = ParamUtil.getLong(request, "teamId");
185    
186                    Team team = null;
187    
188                    if (teamId > 0) {
189                            team = TeamLocalServiceUtil.getTeam(teamId);
190                    }
191    
192                    request.setAttribute(WebKeys.TEAM, team);
193            }
194    
195            public static void getTeam(PortletRequest portletRequest) throws Exception {
196                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
197                            portletRequest);
198    
199                    getTeam(request);
200            }
201    
202    }