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.documentlibrary.action;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.LayoutConstants;
021    import com.liferay.portal.model.LayoutTypePortlet;
022    import com.liferay.portal.service.LayoutLocalServiceUtil;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortletURLImpl;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
027    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
028    
029    import javax.portlet.PortletMode;
030    import javax.portlet.PortletRequest;
031    import javax.portlet.PortletURL;
032    import javax.portlet.WindowState;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    import org.apache.struts.action.Action;
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Ryan Park
044     */
045    public class FindFileEntryAction extends Action {
046    
047            public ActionForward execute(
048                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
049                            HttpServletResponse response)
050                    throws Exception {
051    
052                    try {
053                            long plid = ParamUtil.getLong(request, "p_l_id");
054                            long fileEntryId = ParamUtil.getLong(request, "fileEntryId");
055    
056                            plid = getPlid(plid, fileEntryId);
057    
058                            PortletURL portletURL = new PortletURLImpl(
059                                    request, PortletKeys.DOCUMENT_LIBRARY, plid,
060                                    PortletRequest.RENDER_PHASE);
061    
062                            portletURL.setWindowState(WindowState.NORMAL);
063                            portletURL.setPortletMode(PortletMode.VIEW);
064    
065                            portletURL.setParameter(
066                                    "struts_action", "/document_library/view_file_entry");
067    
068                            DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
069                                    fileEntryId);
070    
071                            portletURL.setParameter(
072                                    "folderId", String.valueOf(fileEntry.getFolderId()));
073                            portletURL.setParameter("name", fileEntry.getName());
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 fileEntryId) 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(
095                                                    PortletKeys.DOCUMENT_LIBRARY)) {
096    
097                                            return plid;
098                                    }
099                            }
100                            catch (NoSuchLayoutException nsle) {
101                            }
102                    }
103    
104                    DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
105                            fileEntryId);
106    
107                    plid = PortalUtil.getPlidFromPortletId(
108                            fileEntry.getGroupId(), PortletKeys.DOCUMENT_LIBRARY);
109    
110                    if (plid != LayoutConstants.DEFAULT_PLID) {
111                            return plid;
112                    }
113                    else {
114                            throw new NoSuchLayoutException(
115                                    "No page was found with the Document Library portlet.");
116                    }
117            }
118    
119    }