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.calendar.action;
16  
17  import com.liferay.portal.NoSuchLayoutException;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.model.LayoutConstants;
22  import com.liferay.portal.model.LayoutTypePortlet;
23  import com.liferay.portal.service.LayoutLocalServiceUtil;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.PortletKeys;
26  import com.liferay.portlet.PortletURLImpl;
27  import com.liferay.portlet.calendar.model.CalEvent;
28  import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
29  
30  import javax.portlet.PortletMode;
31  import javax.portlet.PortletRequest;
32  import javax.portlet.PortletURL;
33  import javax.portlet.WindowState;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import org.apache.struts.action.Action;
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="FindEventAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class FindEventAction extends Action {
49  
50      public ActionForward execute(
51              ActionMapping mapping, ActionForm form, HttpServletRequest request,
52              HttpServletResponse response)
53          throws Exception {
54  
55          try {
56              long plid = ParamUtil.getLong(request, "p_l_id");
57              String redirect = ParamUtil.getString(request, "redirect");
58              long eventId = ParamUtil.getLong(request, "eventId");
59  
60              plid = getPlid(plid, eventId);
61  
62              PortletURL portletURL = new PortletURLImpl(
63                  request, PortletKeys.CALENDAR, plid,
64                  PortletRequest.RENDER_PHASE);
65  
66              portletURL.setWindowState(WindowState.MAXIMIZED);
67              portletURL.setPortletMode(PortletMode.VIEW);
68  
69              portletURL.setParameter("struts_action", "/calendar/view_event");
70  
71              if (Validator.isNotNull(redirect)) {
72                  portletURL.setParameter("redirect", redirect);
73              }
74  
75              portletURL.setParameter("eventId", String.valueOf(eventId));
76  
77              response.sendRedirect(portletURL.toString());
78  
79              return null;
80          }
81          catch (Exception e) {
82              PortalUtil.sendError(e, request, response);
83  
84              return null;
85          }
86      }
87  
88      protected long getPlid(long plid, long eventId) throws Exception {
89          if (plid != LayoutConstants.DEFAULT_PLID) {
90              try {
91                  Layout layout = LayoutLocalServiceUtil.getLayout(plid);
92  
93                  LayoutTypePortlet layoutTypePortlet =
94                      (LayoutTypePortlet)layout.getLayoutType();
95  
96                  if (layoutTypePortlet.hasPortletId(PortletKeys.CALENDAR)) {
97                      return plid;
98                  }
99              }
100             catch (NoSuchLayoutException nsle) {
101             }
102         }
103 
104         CalEvent event = CalEventLocalServiceUtil.getEvent(eventId);
105 
106         plid = PortalUtil.getPlidFromPortletId(
107             event.getGroupId(), PortletKeys.CALENDAR);
108 
109         if (plid != LayoutConstants.DEFAULT_PLID) {
110             return plid;
111         }
112         else {
113             throw new NoSuchLayoutException(
114                 "No page was found with the Calendar portlet.");
115         }
116     }
117 
118 }