001    /**
002     * Copyright (c) 2000-2010 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.model.Layout;
022    import com.liferay.portal.model.LayoutConstants;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.service.permission.LayoutPermissionUtil;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.WebKeys;
031    
032    import java.util.List;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    
043    import org.apache.struts.action.ActionForm;
044    import org.apache.struts.action.ActionForward;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     */
050    public class ViewAction extends PortletAction {
051    
052            public ActionForward strutsExecute(
053                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
054                            HttpServletResponse response)
055                    throws Exception {
056    
057                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
058                            WebKeys.THEME_DISPLAY);
059    
060                    long groupId = ParamUtil.getLong(request, "groupId");
061                    String privateLayoutParam = request.getParameter("privateLayout");
062    
063                    String redirect = getRedirect(
064                            themeDisplay, groupId, privateLayoutParam);
065    
066                    if (redirect == null) {
067                            redirect = ParamUtil.getString(request, "redirect");
068    
069                            SessionErrors.add(
070                                    request, NoSuchLayoutSetException.class.getName(),
071                                    new NoSuchLayoutSetException(
072                                            "{groupId=" + groupId + ",privateLayout=" +
073                                                    privateLayoutParam + "}"));
074                    }
075    
076                    response.sendRedirect(redirect);
077    
078                    return null;
079            }
080    
081            public void processAction(
082                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
083                            ActionRequest actionRequest, ActionResponse actionResponse)
084                    throws Exception {
085    
086                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
087                            WebKeys.THEME_DISPLAY);
088    
089                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
090                    String privateLayoutParam = actionRequest.getParameter("privateLayout");
091    
092                    String redirect = getRedirect(
093                            themeDisplay, groupId, privateLayoutParam);
094    
095                    if (redirect == null) {
096                            redirect = ParamUtil.getString(actionRequest, "redirect");
097    
098                            SessionErrors.add(
099                                    actionRequest, NoSuchLayoutSetException.class.getName(),
100                                    new NoSuchLayoutSetException(
101                                            "{groupId=" + groupId + ",privateLayout=" +
102                                                    privateLayoutParam + "}"));
103                    }
104    
105                    actionResponse.sendRedirect(redirect);
106            }
107    
108            public ActionForward render(
109                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
110                            RenderRequest renderRequest, RenderResponse renderResponse)
111                    throws Exception {
112    
113                    return mapping.findForward("portlet.my_places.view");
114            }
115    
116            protected List<Layout> getLayouts(long groupId, boolean privateLayout)
117                    throws Exception {
118    
119                    return LayoutLocalServiceUtil.getLayouts(
120                            groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
121            }
122    
123            protected String getRedirect(
124                            ThemeDisplay themeDisplay, long groupId, String privateLayoutParam)
125                    throws Exception {
126    
127                    List<Layout> layouts = null;
128    
129                    if (privateLayoutParam == null) {
130                            layouts = getLayouts(groupId, false);
131    
132                            if (layouts.size() == 0) {
133                                    layouts = getLayouts(groupId, true);
134                            }
135                    }
136                    else {
137                            boolean privateLayout = GetterUtil.getBoolean(privateLayoutParam);
138    
139                            layouts = getLayouts(groupId, privateLayout);
140                    }
141    
142                    String redirect = null;
143    
144                    for (Layout layout : layouts) {
145                            PermissionChecker permissionChecker =
146                                    themeDisplay.getPermissionChecker();
147    
148                            if (LayoutPermissionUtil.contains(
149                                            permissionChecker, layout, ActionKeys.VIEW)){
150    
151                                    redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
152    
153                                    break;
154                            }
155                    }
156    
157                    return redirect;
158            }
159    
160            protected boolean isCheckMethodOnProcessAction() {
161                    return _CHECK_METHOD_ON_PROCESS_ACTION;
162            }
163    
164            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
165    
166    }