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