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.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 void processAction(
058                            ActionMapping actionMapping, ActionForm actionForm,
059                            PortletConfig portletConfig, ActionRequest actionRequest,
060                            ActionResponse actionResponse)
061                    throws Exception {
062    
063                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
064                            WebKeys.THEME_DISPLAY);
065    
066                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
067                    String privateLayoutParam = actionRequest.getParameter("privateLayout");
068    
069                    List<Layout> layouts = getLayouts(groupId, privateLayoutParam);
070    
071                    if (layouts.isEmpty()) {
072                            SessionErrors.add(
073                                    actionRequest, NoSuchLayoutSetException.class.getName(),
074                                    new NoSuchLayoutSetException(
075                                            "{groupId=" + groupId + ",privateLayout=" +
076                                                    privateLayoutParam + "}"));
077                    }
078    
079                    String redirect = getRedirect(
080                            themeDisplay, layouts, groupId, privateLayoutParam);
081    
082                    if (Validator.isNull(redirect)) {
083                            redirect = PortalUtil.escapeRedirect(
084                                    ParamUtil.getString(actionRequest, "redirect"));
085                    }
086    
087                    if (Validator.isNotNull(redirect)) {
088                            actionResponse.sendRedirect(redirect);
089                    }
090            }
091    
092            @Override
093            public ActionForward render(
094                            ActionMapping actionMapping, ActionForm actionForm,
095                            PortletConfig portletConfig, RenderRequest renderRequest,
096                            RenderResponse renderResponse)
097                    throws Exception {
098    
099                    return actionMapping.findForward("portlet.my_sites.view");
100            }
101    
102            @Override
103            public ActionForward strutsExecute(
104                            ActionMapping actionMapping, ActionForm actionForm,
105                            HttpServletRequest request, HttpServletResponse response)
106                    throws Exception {
107    
108                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
109                            WebKeys.THEME_DISPLAY);
110    
111                    long groupId = ParamUtil.getLong(request, "groupId");
112                    String privateLayoutParam = request.getParameter("privateLayout");
113    
114                    List<Layout> layouts = getLayouts(groupId, privateLayoutParam);
115    
116                    if (layouts.isEmpty()) {
117                            SessionErrors.add(
118                                    request, NoSuchLayoutSetException.class.getName(),
119                                    new NoSuchLayoutSetException(
120                                            "{groupId=" + groupId + ",privateLayout=" +
121                                                    privateLayoutParam + "}"));
122                    }
123    
124                    String redirect = getRedirect(
125                            themeDisplay, layouts, groupId, privateLayoutParam);
126    
127                    if (Validator.isNull(redirect)) {
128                            redirect = ParamUtil.getString(request, "redirect");
129                    }
130    
131                    response.sendRedirect(redirect);
132    
133                    return null;
134            }
135    
136            protected List<Layout> getLayouts(long groupId, boolean privateLayout)
137                    throws Exception {
138    
139                    return LayoutLocalServiceUtil.getLayouts(
140                            groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
141            }
142    
143            protected List<Layout> getLayouts(long groupId, String privateLayoutParam)
144                    throws Exception {
145    
146                    List<Layout> layouts = null;
147    
148                    boolean privateLayout = false;
149    
150                    if (Validator.isNull(privateLayoutParam)) {
151                            layouts = getLayouts(groupId, false);
152    
153                            if (layouts.isEmpty()) {
154                                    layouts = getLayouts(groupId, true);
155                            }
156                    }
157                    else {
158                            privateLayout = GetterUtil.getBoolean(privateLayoutParam);
159    
160                            layouts = getLayouts(groupId, privateLayout);
161                    }
162    
163                    return layouts;
164            }
165    
166            protected String getRedirect(
167                            ThemeDisplay themeDisplay, List<Layout> layouts, long groupId,
168                            String privateLayoutParam)
169                    throws Exception {
170    
171                    PermissionChecker permissionChecker =
172                            themeDisplay.getPermissionChecker();
173    
174                    for (Layout layout : layouts) {
175                            if (!layout.isHidden() &&
176                                    LayoutPermissionUtil.contains(
177                                            permissionChecker, layout, ActionKeys.VIEW)) {
178    
179                                    return PortalUtil.getCanonicalURL(
180                                            null, themeDisplay, layout, true);
181                            }
182                    }
183    
184                    Group group = GroupLocalServiceUtil.getGroup(groupId);
185    
186                    return PortalUtil.getGroupFriendlyURL(
187                            group, GetterUtil.getBoolean(privateLayoutParam), themeDisplay);
188            }
189    
190            @Override
191            protected boolean isCheckMethodOnProcessAction() {
192                    return _CHECK_METHOD_ON_PROCESS_ACTION;
193            }
194    
195            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
196    
197    }