001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutPrototype;
024    import com.liferay.portal.model.LayoutTypePortlet;
025    import com.liferay.portal.model.MembershipRequest;
026    import com.liferay.portal.model.PortletConstants;
027    import com.liferay.portal.model.PortletPreferencesIds;
028    import com.liferay.portal.model.ResourceConstants;
029    import com.liferay.portal.model.Role;
030    import com.liferay.portal.model.RoleConstants;
031    import com.liferay.portal.model.Team;
032    import com.liferay.portal.security.permission.ResourceActionsUtil;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.LayoutLocalServiceUtil;
035    import com.liferay.portal.service.LayoutServiceUtil;
036    import com.liferay.portal.service.MembershipRequestLocalServiceUtil;
037    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
038    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
039    import com.liferay.portal.service.RoleLocalServiceUtil;
040    import com.liferay.portal.service.TeamLocalServiceUtil;
041    import com.liferay.portal.service.permission.PortletPermissionUtil;
042    import com.liferay.portal.theme.ThemeDisplay;
043    import com.liferay.portal.util.PortalUtil;
044    import com.liferay.portal.util.PortletKeys;
045    import com.liferay.portal.util.WebKeys;
046    import com.liferay.portlet.PortletPreferencesFactoryUtil;
047    
048    import java.util.List;
049    
050    import javax.portlet.PortletPreferences;
051    import javax.portlet.PortletRequest;
052    
053    import javax.servlet.http.HttpServletRequest;
054    
055    /**
056     * @author Brian Wing Shun Chan
057     */
058    public class ActionUtil
059            extends com.liferay.portlet.rolesadmin.action.ActionUtil {
060    
061            public static void copyLayoutPrototypePermissions(
062                            HttpServletRequest request, Layout targetLayout,
063                            LayoutPrototype sourceLayoutPrototype)
064                    throws Exception {
065    
066                    List<Role> roles = RoleLocalServiceUtil.getRoles(
067                            targetLayout.getCompanyId());
068    
069                    for (Role role : roles) {
070                            String roleName = role.getName();
071    
072                            if (roleName.equals(RoleConstants.ADMINISTRATOR)) {
073                                    continue;
074                            }
075    
076                            List<String> actionIds = ResourceActionsUtil.getResourceActions(
077                                    LayoutPrototype.class.getName());
078    
079                            List<String> actions =
080                                    ResourcePermissionLocalServiceUtil.
081                                            getAvailableResourcePermissionActionIds(
082                                                    targetLayout.getCompanyId(),
083                                                    LayoutPrototype.class.getName(),
084                                                    ResourceConstants.SCOPE_INDIVIDUAL,
085                                                    String.valueOf(
086                                                            sourceLayoutPrototype.getLayoutPrototypeId()),
087                                                    role.getRoleId(), actionIds);
088    
089                            ResourcePermissionLocalServiceUtil.setResourcePermissions(
090                                    targetLayout.getCompanyId(), Layout.class.getName(),
091                                    ResourceConstants.SCOPE_INDIVIDUAL,
092                                    String.valueOf(targetLayout.getPlid()), role.getRoleId(),
093                                    actions.toArray(new String[actions.size()]));
094                    }
095            }
096    
097            public static void copyLayoutPrototypePermissions(
098                            PortletRequest portletRequest, Layout targetLayout,
099                            LayoutPrototype sourceLayoutPrototype)
100                    throws Exception {
101    
102                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
103                            portletRequest);
104    
105                    copyLayoutPrototypePermissions(
106                            request, targetLayout, sourceLayoutPrototype);
107            }
108    
109            public static void copyLookAndFeel(
110                            Layout targetLayout, Layout sourceLayout)
111                    throws Exception {
112    
113                    LayoutServiceUtil.updateLookAndFeel(
114                            targetLayout.getGroupId(), targetLayout.isPrivateLayout(),
115                            targetLayout.getLayoutId(), sourceLayout.getThemeId(),
116                            sourceLayout.getColorSchemeId(), sourceLayout.getCss(), false);
117    
118                    LayoutServiceUtil.updateLookAndFeel(
119                            targetLayout.getGroupId(), targetLayout.isPrivateLayout(),
120                            targetLayout.getLayoutId(), sourceLayout.getWapThemeId(),
121                            sourceLayout.getWapColorSchemeId(), sourceLayout.getCss(), true);
122            }
123    
124            public static void copyPortletPermissions(
125                            HttpServletRequest request, Layout targetLayout,
126                            Layout sourceLayout)
127                    throws Exception {
128    
129                    long companyId = targetLayout.getCompanyId();
130    
131                    List<Role> roles = RoleLocalServiceUtil.getRoles(companyId);
132    
133                    LayoutTypePortlet sourceLayoutTypePortlet =
134                            (LayoutTypePortlet)sourceLayout.getLayoutType();
135    
136                    List<String> sourcePortletIds = sourceLayoutTypePortlet.getPortletIds();
137    
138                    for (String sourcePortletId : sourcePortletIds) {
139                            String resourceName = PortletConstants.getRootPortletId(
140                                    sourcePortletId);
141    
142                            String sourceResourcePrimKey = PortletPermissionUtil.getPrimaryKey(
143                                    sourceLayout.getPlid(), sourcePortletId);
144    
145                            String targetResourcePrimKey = PortletPermissionUtil.getPrimaryKey(
146                                    targetLayout.getPlid(), sourcePortletId);
147    
148                            List<String> actionIds =
149                                    ResourceActionsUtil.getPortletResourceActions(resourceName);
150    
151                            for (Role role : roles) {
152                                    String roleName = role.getName();
153    
154                                    if (roleName.equals(RoleConstants.ADMINISTRATOR)) {
155                                            continue;
156                                    }
157    
158                                    List<String> actions =
159                                            ResourcePermissionLocalServiceUtil.
160                                                    getAvailableResourcePermissionActionIds(
161                                                            companyId, resourceName,
162                                                            ResourceConstants.SCOPE_INDIVIDUAL,
163                                                            sourceResourcePrimKey, role.getRoleId(), actionIds);
164    
165                                     ResourcePermissionLocalServiceUtil.setResourcePermissions(
166                                            companyId, resourceName, ResourceConstants.SCOPE_INDIVIDUAL,
167                                            targetResourcePrimKey, role.getRoleId(),
168                                            actions.toArray(new String[actions.size()]));
169                             }
170                    }
171            }
172    
173            public static void copyPortletPermissions(
174                            PortletRequest portletRequest, Layout targetLayout,
175                            Layout sourceLayout)
176                    throws Exception {
177    
178                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
179                            portletRequest);
180    
181                    copyPortletPermissions(request, targetLayout, sourceLayout);
182            }
183    
184            public static void copyPreferences(
185                            HttpServletRequest request, Layout targetLayout,
186                            Layout sourceLayout)
187                    throws Exception {
188    
189                    long companyId = targetLayout.getCompanyId();
190    
191                    LayoutTypePortlet sourceLayoutTypePortlet =
192                            (LayoutTypePortlet)sourceLayout.getLayoutType();
193    
194                    List<String> sourcePortletIds = sourceLayoutTypePortlet.getPortletIds();
195    
196                    for (String sourcePortletId : sourcePortletIds) {
197    
198                            // Copy preference
199    
200                            PortletPreferencesIds portletPreferencesIds =
201                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
202                                            request, targetLayout, sourcePortletId);
203    
204                            PortletPreferencesLocalServiceUtil.getPreferences(
205                                    portletPreferencesIds);
206    
207                            PortletPreferencesIds sourcePortletPreferencesIds =
208                                    PortletPreferencesFactoryUtil.getPortletPreferencesIds(
209                                            request, sourceLayout, sourcePortletId);
210    
211                            PortletPreferences sourcePreferences =
212                                    PortletPreferencesLocalServiceUtil.getPreferences(
213                                            sourcePortletPreferencesIds);
214    
215                            PortletPreferencesLocalServiceUtil.updatePreferences(
216                                    portletPreferencesIds.getOwnerId(),
217                                    portletPreferencesIds.getOwnerType(),
218                                    portletPreferencesIds.getPlid(),
219                                    portletPreferencesIds.getPortletId(), sourcePreferences);
220    
221                            // Copy portlet setup
222    
223                            PortletPreferences targetPreferences =
224                                    PortletPreferencesLocalServiceUtil.getPreferences(
225                                            companyId, PortletKeys.PREFS_OWNER_ID_DEFAULT,
226                                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT, targetLayout.getPlid(),
227                                            sourcePortletId);
228    
229                            sourcePreferences =
230                                    PortletPreferencesLocalServiceUtil.getPreferences(
231                                            companyId, PortletKeys.PREFS_OWNER_ID_DEFAULT,
232                                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT, sourceLayout.getPlid(),
233                                            sourcePortletId);
234    
235                            PortletPreferencesLocalServiceUtil.updatePreferences(
236                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
237                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, targetLayout.getPlid(),
238                                    sourcePortletId, sourcePreferences);
239    
240                            String scopeType = GetterUtil.getString(
241                                    sourcePreferences.getValue("lfrScopeType", null));
242    
243                            if (Validator.isNotNull(scopeType) && scopeType.equals("layout")) {
244                                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
245                                            WebKeys.THEME_DISPLAY);
246    
247                                    Layout targetScopeLayout =
248                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
249                                                    targetLayout.getUuid(), targetLayout.getGroupId());
250    
251                                    String languageId = themeDisplay.getLanguageId();
252    
253                                    if (!targetScopeLayout.hasScopeGroup()) {
254                                            GroupLocalServiceUtil.addGroup(
255                                                    themeDisplay.getUserId(), Layout.class.getName(),
256                                                    targetLayout.getPlid(),
257                                                    targetLayout.getName(languageId), null, 0, null, false,
258                                                    true, null);
259                                    }
260    
261                                    String portletTitle = PortalUtil.getPortletTitle(
262                                            sourcePortletId, languageId);
263    
264                                    String newPortletTitle = PortalUtil.getNewPortletTitle(
265                                            portletTitle, String.valueOf(sourceLayout.getLayoutId()),
266                                            targetLayout.getName(languageId));
267    
268                                    targetPreferences.setValue(
269                                            "groupId", String.valueOf(targetLayout.getGroupId()));
270                                    targetPreferences.setValue("lfrScopeType", "layout");
271                                    targetPreferences.setValue(
272                                            "lfrScopeLayoutUuid", targetLayout.getUuid());
273                                    targetPreferences.setValue(
274                                            "portletSetupTitle_" + languageId, newPortletTitle);
275                                    targetPreferences.setValue(
276                                            "portletSetupUseCustomTitle", Boolean.TRUE.toString());
277    
278                                    targetPreferences.store();
279                            }
280                    }
281            }
282    
283            public static void copyPreferences(
284                            PortletRequest portletRequest, Layout targetLayout,
285                            Layout sourceLayout)
286                    throws Exception {
287    
288                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
289                            portletRequest);
290    
291                    copyPreferences(request, targetLayout, sourceLayout);
292            }
293    
294            public static Group getGroup(HttpServletRequest request) throws Exception {
295                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
296                            WebKeys.THEME_DISPLAY);
297    
298                    String cmd = ParamUtil.getString(request, Constants.CMD);
299    
300                    long groupId = ParamUtil.getLong(request, "groupId");
301    
302                    Group group = null;
303    
304                    if (groupId > 0) {
305                            group = GroupLocalServiceUtil.getGroup(groupId);
306                    }
307                    else if (!cmd.equals(Constants.ADD)) {
308                            group = themeDisplay.getScopeGroup();
309                    }
310    
311                    request.setAttribute(WebKeys.GROUP, group);
312    
313                    return group;
314            }
315    
316            public static Group getGroup(PortletRequest portletRequest)
317                    throws Exception {
318    
319                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
320                            portletRequest);
321    
322                    return getGroup(request);
323            }
324    
325            public static void getMembershipRequest(HttpServletRequest request)
326                    throws Exception {
327    
328                    long membershipRequestId = ParamUtil.getLong(
329                            request, "membershipRequestId");
330    
331                    MembershipRequest membershipRequest = null;
332    
333                    if (membershipRequestId > 0) {
334                            membershipRequest =
335                                    MembershipRequestLocalServiceUtil.getMembershipRequest(
336                                            membershipRequestId);
337                    }
338    
339                    request.setAttribute(WebKeys.MEMBERSHIP_REQUEST, membershipRequest);
340            }
341    
342            public static void getMembershipRequest(PortletRequest portletRequest)
343                    throws Exception {
344    
345                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
346                            portletRequest);
347    
348                    getMembershipRequest(request);
349            }
350    
351            public static void getTeam(HttpServletRequest request)
352                    throws Exception {
353    
354                    long teamId = ParamUtil.getLong(request, "teamId");
355    
356                    Team team = null;
357    
358                    if (teamId > 0) {
359                            team = TeamLocalServiceUtil.getTeam(teamId);
360                    }
361    
362                    request.setAttribute(WebKeys.TEAM, team);
363            }
364    
365            public static void getTeam(PortletRequest portletRequest)
366                    throws Exception {
367    
368                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
369                            portletRequest);
370    
371                    getTeam(request);
372            }
373    
374    }