001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.calendar.action;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.LayoutConstants;
022    import com.liferay.portal.model.LayoutTypePortlet;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.PortletURLImpl;
027    import com.liferay.portlet.calendar.model.CalEvent;
028    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
029    
030    import javax.portlet.PortletMode;
031    import javax.portlet.PortletRequest;
032    import javax.portlet.PortletURL;
033    import javax.portlet.WindowState;
034    
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    import org.apache.struts.action.Action;
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class FindEventAction extends Action {
047    
048            public ActionForward execute(
049                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
050                            HttpServletResponse response)
051                    throws Exception {
052    
053                    try {
054                            long plid = ParamUtil.getLong(request, "p_l_id");
055                            String redirect = ParamUtil.getString(request, "redirect");
056                            long eventId = ParamUtil.getLong(request, "eventId");
057    
058                            plid = getPlid(plid, eventId);
059    
060                            PortletURL portletURL = new PortletURLImpl(
061                                    request, PortletKeys.CALENDAR, plid,
062                                    PortletRequest.RENDER_PHASE);
063    
064                            portletURL.setWindowState(WindowState.MAXIMIZED);
065                            portletURL.setPortletMode(PortletMode.VIEW);
066    
067                            portletURL.setParameter("struts_action", "/calendar/view_event");
068    
069                            if (Validator.isNotNull(redirect)) {
070                                    portletURL.setParameter("redirect", redirect);
071                            }
072    
073                            portletURL.setParameter("eventId", String.valueOf(eventId));
074    
075                            response.sendRedirect(portletURL.toString());
076    
077                            return null;
078                    }
079                    catch (Exception e) {
080                            PortalUtil.sendError(e, request, response);
081    
082                            return null;
083                    }
084            }
085    
086            protected long getPlid(long plid, long eventId) throws Exception {
087                    if (plid != LayoutConstants.DEFAULT_PLID) {
088                            try {
089                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
090    
091                                    LayoutTypePortlet layoutTypePortlet =
092                                            (LayoutTypePortlet)layout.getLayoutType();
093    
094                                    if (layoutTypePortlet.hasPortletId(PortletKeys.CALENDAR)) {
095                                            return plid;
096                                    }
097                            }
098                            catch (NoSuchLayoutException nsle) {
099                            }
100                    }
101    
102                    CalEvent event = CalEventLocalServiceUtil.getEvent(eventId);
103    
104                    plid = PortalUtil.getPlidFromPortletId(
105                            event.getGroupId(), PortletKeys.CALENDAR);
106    
107                    if (plid != LayoutConstants.DEFAULT_PLID) {
108                            return plid;
109                    }
110                    else {
111                            throw new NoSuchLayoutException(
112                                    "No page was found with the Calendar portlet.");
113                    }
114            }
115    
116    }