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