1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.service.LayoutLocalServiceUtil;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  
37  import java.util.List;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class ViewAction extends PortletAction {
59  
60      public ActionForward strutsExecute(
61              ActionMapping mapping, ActionForm form, HttpServletRequest request,
62              HttpServletResponse response)
63          throws Exception {
64  
65          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
66              WebKeys.THEME_DISPLAY);
67  
68          long groupId = ParamUtil.getLong(request, "groupId");
69          String privateLayoutParam = request.getParameter("privateLayout");
70  
71          String redirect = getRedirect(
72              themeDisplay, groupId, privateLayoutParam);
73  
74          if (redirect == null) {
75              redirect = ParamUtil.getString(request, "redirect");
76  
77              SessionErrors.add(
78                  request, NoSuchLayoutSetException.class.getName(),
79                  new NoSuchLayoutSetException(
80                      "{groupId=" + groupId + ",privateLayout=" +
81                          privateLayoutParam + "}"));
82          }
83  
84          response.sendRedirect(redirect);
85  
86          return null;
87      }
88  
89      public void processAction(
90              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
91              ActionRequest actionRequest, ActionResponse actionResponse)
92          throws Exception {
93  
94          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
95              WebKeys.THEME_DISPLAY);
96  
97          long groupId = ParamUtil.getLong(actionRequest, "groupId");
98          String privateLayoutParam = actionRequest.getParameter("privateLayout");
99  
100         String redirect = getRedirect(
101             themeDisplay, groupId, privateLayoutParam);
102 
103         if (redirect == null) {
104             redirect = ParamUtil.getString(actionRequest, "redirect");
105 
106             SessionErrors.add(
107                 actionRequest, NoSuchLayoutSetException.class.getName(),
108                 new NoSuchLayoutSetException(
109                     "{groupId=" + groupId + ",privateLayout=" +
110                         privateLayoutParam + "}"));
111         }
112 
113         actionResponse.sendRedirect(redirect);
114     }
115 
116     public ActionForward render(
117             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
118             RenderRequest renderRequest, RenderResponse renderResponse)
119         throws Exception {
120 
121         return mapping.findForward("portlet.my_places.view");
122     }
123 
124     protected List<Layout> getLayouts(long groupId, boolean privateLayout)
125         throws Exception {
126 
127         return LayoutLocalServiceUtil.getLayouts(
128             groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0,
129             1);
130     }
131 
132     protected String getRedirect(
133             ThemeDisplay themeDisplay, long groupId, String privateLayoutParam)
134         throws Exception {
135 
136         List<Layout> layouts = null;
137 
138         if (privateLayoutParam == null) {
139             layouts = getLayouts(groupId, false);
140 
141             if (layouts.size() == 0) {
142                 layouts = getLayouts(groupId, true);
143             }
144         }
145         else {
146             boolean privateLayout = GetterUtil.getBoolean(privateLayoutParam);
147 
148             layouts = getLayouts(groupId, privateLayout);
149         }
150 
151         String redirect = null;
152 
153         if (layouts.size() > 0) {
154             Layout layout = layouts.get(0);
155 
156             redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
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 }