1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.documentlibrary.action;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.LayoutConstants;
29  import com.liferay.portal.model.LayoutTypePortlet;
30  import com.liferay.portal.service.LayoutLocalServiceUtil;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.PortletURLImpl;
34  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
35  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
36  
37  import javax.portlet.PortletMode;
38  import javax.portlet.PortletRequest;
39  import javax.portlet.PortletURL;
40  import javax.portlet.WindowState;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.Action;
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="FindFileEntryAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Ryan Park
54   *
55   */
56  public class FindFileEntryAction extends Action {
57  
58      public ActionForward execute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          try {
64              long plid = ParamUtil.getLong(request, "p_l_id");
65              long fileEntryId = ParamUtil.getLong(request, "fileEntryId");
66  
67              plid = getPlid(plid, fileEntryId);
68  
69              PortletURL portletURL = new PortletURLImpl(
70                  request, PortletKeys.DOCUMENT_LIBRARY, plid,
71                  PortletRequest.RENDER_PHASE);
72  
73              portletURL.setWindowState(WindowState.NORMAL);
74              portletURL.setPortletMode(PortletMode.VIEW);
75  
76              portletURL.setParameter(
77                  "struts_action", "/document_library/view_file_entry");
78  
79              DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
80                  fileEntryId);
81  
82              portletURL.setParameter(
83                  "folderId", String.valueOf(fileEntry.getFolderId()));
84              portletURL.setParameter("name", fileEntry.getName());
85  
86              response.sendRedirect(portletURL.toString());
87  
88              return null;
89          }
90          catch (Exception e) {
91              PortalUtil.sendError(e, request, response);
92  
93              return null;
94          }
95      }
96  
97      protected long getPlid(long plid, long fileEntryId) throws Exception {
98          if (plid != LayoutConstants.DEFAULT_PLID) {
99              try {
100                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
101 
102                 LayoutTypePortlet layoutTypePortlet =
103                     (LayoutTypePortlet)layout.getLayoutType();
104 
105                 if (layoutTypePortlet.hasPortletId(
106                         PortletKeys.DOCUMENT_LIBRARY)) {
107 
108                     return plid;
109                 }
110             }
111             catch (NoSuchLayoutException nsle) {
112             }
113         }
114 
115         DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
116             fileEntryId);
117 
118         plid = PortalUtil.getPlidFromPortletId(
119             fileEntry.getGroupId(), PortletKeys.DOCUMENT_LIBRARY);
120 
121         if (plid != LayoutConstants.DEFAULT_PLID) {
122             return plid;
123         }
124         else {
125             throw new NoSuchLayoutException(
126                 "No page was found with the Document Library portlet.");
127         }
128     }
129 
130 }