1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.myplaces.action;
16  
17  import com.liferay.portal.NoSuchLayoutSetException;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.model.LayoutConstants;
23  import com.liferay.portal.security.permission.ActionKeys;
24  import com.liferay.portal.security.permission.PermissionChecker;
25  import com.liferay.portal.service.LayoutLocalServiceUtil;
26  import com.liferay.portal.service.permission.LayoutPermissionUtil;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  
32  import java.util.List;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class ViewAction extends PortletAction {
53  
54      public ActionForward strutsExecute(
55              ActionMapping mapping, ActionForm form, HttpServletRequest request,
56              HttpServletResponse response)
57          throws Exception {
58  
59          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
60              WebKeys.THEME_DISPLAY);
61  
62          long groupId = ParamUtil.getLong(request, "groupId");
63          String privateLayoutParam = request.getParameter("privateLayout");
64  
65          String redirect = getRedirect(
66              themeDisplay, groupId, privateLayoutParam);
67  
68          if (redirect == null) {
69              redirect = ParamUtil.getString(request, "redirect");
70  
71              SessionErrors.add(
72                  request, NoSuchLayoutSetException.class.getName(),
73                  new NoSuchLayoutSetException(
74                      "{groupId=" + groupId + ",privateLayout=" +
75                          privateLayoutParam + "}"));
76          }
77  
78          response.sendRedirect(redirect);
79  
80          return null;
81      }
82  
83      public void processAction(
84              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
85              ActionRequest actionRequest, ActionResponse actionResponse)
86          throws Exception {
87  
88          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
89              WebKeys.THEME_DISPLAY);
90  
91          long groupId = ParamUtil.getLong(actionRequest, "groupId");
92          String privateLayoutParam = actionRequest.getParameter("privateLayout");
93  
94          String redirect = getRedirect(
95              themeDisplay, groupId, privateLayoutParam);
96  
97          if (redirect == null) {
98              redirect = ParamUtil.getString(actionRequest, "redirect");
99  
100             SessionErrors.add(
101                 actionRequest, NoSuchLayoutSetException.class.getName(),
102                 new NoSuchLayoutSetException(
103                     "{groupId=" + groupId + ",privateLayout=" +
104                         privateLayoutParam + "}"));
105         }
106 
107         actionResponse.sendRedirect(redirect);
108     }
109 
110     public ActionForward render(
111             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
112             RenderRequest renderRequest, RenderResponse renderResponse)
113         throws Exception {
114 
115         return mapping.findForward("portlet.my_places.view");
116     }
117 
118     protected List<Layout> getLayouts(long groupId, boolean privateLayout)
119         throws Exception {
120 
121         return LayoutLocalServiceUtil.getLayouts(
122             groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
123     }
124 
125     protected String getRedirect(
126             ThemeDisplay themeDisplay, long groupId, String privateLayoutParam)
127         throws Exception {
128 
129         List<Layout> layouts = null;
130 
131         if (privateLayoutParam == null) {
132             layouts = getLayouts(groupId, false);
133 
134             if (layouts.size() == 0) {
135                 layouts = getLayouts(groupId, true);
136             }
137         }
138         else {
139             boolean privateLayout = GetterUtil.getBoolean(privateLayoutParam);
140 
141             layouts = getLayouts(groupId, privateLayout);
142         }
143 
144         String redirect = null;
145 
146         for (Layout layout : layouts) {
147             PermissionChecker permissionChecker =
148                 themeDisplay.getPermissionChecker();
149 
150             if (LayoutPermissionUtil.contains(
151                     permissionChecker, layout, ActionKeys.VIEW)){
152 
153                 redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
154 
155                 break;
156             }
157         }
158 
159         return redirect;
160     }
161 
162     protected boolean isCheckMethodOnProcessAction() {
163         return _CHECK_METHOD_ON_PROCESS_ACTION;
164     }
165 
166     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
167 
168 }