001    /**
002     * Copyright (c) 2000-2011 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.myplaces.action;
016    
017    import com.liferay.portal.NoSuchLayoutSetException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutConstants;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.service.LayoutLocalServiceUtil;
029    import com.liferay.portal.service.permission.LayoutPermissionUtil;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.WebKeys;
034    
035    import java.util.List;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import javax.servlet.http.HttpServletRequest;
044    import javax.servlet.http.HttpServletResponse;
045    
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     * @author Hugo Huijser
053     */
054    public class ViewAction extends PortletAction {
055    
056            @Override
057            public ActionForward strutsExecute(
058                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
059                            HttpServletResponse response)
060                    throws Exception {
061    
062                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
063                            WebKeys.THEME_DISPLAY);
064    
065                    long groupId = ParamUtil.getLong(request, "groupId");
066                    String privateLayoutParam = request.getParameter("privateLayout");
067    
068                    List<Layout> layouts = getLayouts(groupId, privateLayoutParam);
069    
070                    if (layouts.isEmpty()) {
071                            SessionErrors.add(
072                                    request, NoSuchLayoutSetException.class.getName(),
073                                    new NoSuchLayoutSetException(
074                                            "{groupId=" + groupId + ",privateLayout=" +
075                                                    privateLayoutParam + "}"));
076                    }
077    
078                    String redirect = getRedirect(
079                            themeDisplay, layouts, groupId, privateLayoutParam);
080    
081                    if (Validator.isNull(redirect)) {
082                            redirect = ParamUtil.getString(request, "redirect");
083                    }
084    
085                    response.sendRedirect(redirect);
086    
087                    return null;
088            }
089    
090            @Override
091            public void processAction(
092                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
093                            ActionRequest actionRequest, ActionResponse actionResponse)
094                    throws Exception {
095    
096                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
097                            WebKeys.THEME_DISPLAY);
098    
099                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
100                    String privateLayoutParam = actionRequest.getParameter("privateLayout");
101    
102                    List<Layout> layouts = getLayouts(groupId, privateLayoutParam);
103    
104                    if (layouts.isEmpty()) {
105                            SessionErrors.add(
106                                    actionRequest, NoSuchLayoutSetException.class.getName(),
107                                    new NoSuchLayoutSetException(
108                                            "{groupId=" + groupId + ",privateLayout=" +
109                                                    privateLayoutParam + "}"));
110                    }
111    
112                    String redirect = getRedirect(
113                            themeDisplay, layouts, groupId, privateLayoutParam);
114    
115                    if (Validator.isNull(redirect)) {
116                            redirect = PortalUtil.escapeRedirect(
117                                    ParamUtil.getString(actionRequest, "redirect"));
118                    }
119    
120                    if (Validator.isNotNull(redirect)) {
121                            actionResponse.sendRedirect(redirect);
122                    }
123            }
124    
125            @Override
126            public ActionForward render(
127                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
128                            RenderRequest renderRequest, RenderResponse renderResponse)
129                    throws Exception {
130    
131                    return mapping.findForward("portlet.my_sites.view");
132            }
133    
134            protected List<Layout> getLayouts(long groupId, String privateLayoutParam)
135                    throws Exception {
136    
137                    List<Layout> layouts = null;
138    
139                    boolean privateLayout = false;
140    
141                    if (Validator.isNull(privateLayoutParam)) {
142                            layouts = getLayouts(groupId, false);
143    
144                            if (layouts.isEmpty()) {
145                                    layouts = getLayouts(groupId, true);
146                            }
147                    }
148                    else {
149                            privateLayout = GetterUtil.getBoolean(privateLayoutParam);
150    
151                            layouts = getLayouts(groupId, privateLayout);
152                    }
153    
154                    return layouts;
155            }
156    
157            protected List<Layout> getLayouts(long groupId, boolean privateLayout)
158                    throws Exception {
159    
160                    return LayoutLocalServiceUtil.getLayouts(
161                            groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
162            }
163    
164            protected String getRedirect(
165                            ThemeDisplay themeDisplay, List<Layout> layouts, long groupId,
166                            String privateLayoutParam)
167                    throws Exception {
168    
169                    PermissionChecker permissionChecker =
170                            themeDisplay.getPermissionChecker();
171    
172                    boolean checkGuest = permissionChecker.isCheckGuest();
173    
174                    try {
175                            for (Layout layout : layouts) {
176                                    if (layout.isPrivateLayout()) {
177                                            permissionChecker.setCheckGuest(false);
178                                    }
179                                    else {
180                                            permissionChecker.setCheckGuest(true);
181                                    }
182    
183                                    if (!layout.isHidden() &&
184                                            LayoutPermissionUtil.contains(
185                                                    permissionChecker, layout, ActionKeys.VIEW)) {
186    
187                                            return PortalUtil.getLayoutURL(layout, themeDisplay);
188                                    }
189                            }
190                    }
191                    finally {
192                            permissionChecker.setCheckGuest(checkGuest);
193                    }
194    
195                    Group group = GroupLocalServiceUtil.getGroup(groupId);
196    
197                    return PortalUtil.getGroupFriendlyURL(
198                            group, GetterUtil.getBoolean(privateLayoutParam), themeDisplay);
199            }
200    
201            @Override
202            protected boolean isCheckMethodOnProcessAction() {
203                    return _CHECK_METHOD_ON_PROCESS_ACTION;
204            }
205    
206            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
207    
208    }