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.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  public class ViewAction extends PortletAction {
58  
59      public ActionForward strutsExecute(
60              ActionMapping mapping, ActionForm form, HttpServletRequest request,
61              HttpServletResponse response)
62          throws Exception {
63  
64          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
65              WebKeys.THEME_DISPLAY);
66  
67          long groupId = ParamUtil.getLong(request, "groupId");
68          String privateLayoutParam = request.getParameter("privateLayout");
69  
70          String redirect = getRedirect(
71              themeDisplay, groupId, privateLayoutParam);
72  
73          if (redirect == null) {
74              redirect = ParamUtil.getString(request, "redirect");
75  
76              SessionErrors.add(
77                  request, NoSuchLayoutSetException.class.getName(),
78                  new NoSuchLayoutSetException(
79                      "{groupId=" + groupId + ",privateLayout=" +
80                          privateLayoutParam + "}"));
81          }
82  
83          response.sendRedirect(redirect);
84  
85          return null;
86      }
87  
88      public void processAction(
89              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
90              ActionRequest actionRequest, ActionResponse actionResponse)
91          throws Exception {
92  
93          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
94              WebKeys.THEME_DISPLAY);
95  
96          long groupId = ParamUtil.getLong(actionRequest, "groupId");
97          String privateLayoutParam = actionRequest.getParameter("privateLayout");
98  
99          String redirect = getRedirect(
100             themeDisplay, groupId, privateLayoutParam);
101 
102         if (redirect == null) {
103             redirect = ParamUtil.getString(actionRequest, "redirect");
104 
105             SessionErrors.add(
106                 actionRequest, NoSuchLayoutSetException.class.getName(),
107                 new NoSuchLayoutSetException(
108                     "{groupId=" + groupId + ",privateLayout=" +
109                         privateLayoutParam + "}"));
110         }
111 
112         actionResponse.sendRedirect(redirect);
113     }
114 
115     public ActionForward render(
116             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
117             RenderRequest renderRequest, RenderResponse renderResponse)
118         throws Exception {
119 
120         return mapping.findForward("portlet.my_places.view");
121     }
122 
123     protected List<Layout> getLayouts(long groupId, boolean privateLayout)
124         throws Exception {
125 
126         return LayoutLocalServiceUtil.getLayouts(
127             groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0,
128             1);
129     }
130 
131     protected String getRedirect(
132             ThemeDisplay themeDisplay, long groupId, String privateLayoutParam)
133         throws Exception {
134 
135         List<Layout> layouts = null;
136 
137         if (privateLayoutParam == null) {
138             layouts = getLayouts(groupId, false);
139 
140             if (layouts.size() == 0) {
141                 layouts = getLayouts(groupId, true);
142             }
143         }
144         else {
145             boolean privateLayout = GetterUtil.getBoolean(privateLayoutParam);
146 
147             layouts = getLayouts(groupId, privateLayout);
148         }
149 
150         String redirect = null;
151 
152         if (layouts.size() > 0) {
153             Layout layout = layouts.get(0);
154 
155             redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
156         }
157 
158         return redirect;
159     }
160 
161     protected boolean isCheckMethodOnProcessAction() {
162         return _CHECK_METHOD_ON_PROCESS_ACTION;
163     }
164 
165     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
166 
167 }