1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.action;
16  
17  import com.liferay.portal.kernel.util.ParamUtil;
18  import com.liferay.portal.kernel.util.Validator;
19  import com.liferay.portal.theme.ThemeDisplay;
20  import com.liferay.portal.util.PortalUtil;
21  import com.liferay.portal.util.WebKeys;
22  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
23  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
24  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
25  import com.liferay.portlet.documentlibrary.model.DLFolder;
26  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
27  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
28  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
29  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.RenderRequest;
33  
34  import javax.servlet.http.HttpServletRequest;
35  
36  /**
37   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class ActionUtil {
42  
43      public static void getFileEntry(ActionRequest actionRequest)
44          throws Exception {
45  
46          HttpServletRequest request = PortalUtil.getHttpServletRequest(
47              actionRequest);
48  
49          getFileEntry(request);
50      }
51  
52      public static void getFileEntry(RenderRequest renderRequest)
53          throws Exception {
54  
55          HttpServletRequest request = PortalUtil.getHttpServletRequest(
56              renderRequest);
57  
58          getFileEntry(request);
59      }
60  
61      public static void getFileEntry(HttpServletRequest request)
62          throws Exception {
63  
64          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
65              WebKeys.THEME_DISPLAY);
66  
67          long groupId = themeDisplay.getScopeGroupId();
68          long folderId = ParamUtil.getLong(request, "folderId");
69          long newFolderId = ParamUtil.getLong(request, "newFolderId");
70          String name = ParamUtil.getString(request, "name");
71  
72          DLFileEntry fileEntry = null;
73  
74          if (Validator.isNotNull(name)) {
75              try {
76                  fileEntry = DLFileEntryServiceUtil.getFileEntry(
77                      groupId, folderId, name);
78              }
79              catch (NoSuchFileEntryException nsfe) {
80  
81                  // This only happens when you're moving a file to a different
82                  // folder
83  
84                  fileEntry = DLFileEntryServiceUtil.getFileEntry(
85                      groupId, newFolderId, name);
86              }
87          }
88  
89          request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, fileEntry);
90      }
91  
92      public static void getFileShortcut(ActionRequest actionRequest)
93          throws Exception {
94  
95          HttpServletRequest request = PortalUtil.getHttpServletRequest(
96              actionRequest);
97  
98          getFileShortcut(request);
99      }
100 
101     public static void getFileShortcut(RenderRequest renderRequest)
102         throws Exception {
103 
104         HttpServletRequest request = PortalUtil.getHttpServletRequest(
105             renderRequest);
106 
107         getFileShortcut(request);
108     }
109 
110     public static void getFileShortcut(HttpServletRequest request)
111         throws Exception {
112 
113         long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
114 
115         DLFileShortcut fileShortcut = null;
116 
117         if (fileShortcutId > 0) {
118             fileShortcut = DLFileShortcutServiceUtil.getFileShortcut(
119                 fileShortcutId);
120         }
121 
122         request.setAttribute(
123             WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
124     }
125 
126     public static void getFolder(ActionRequest actionRequest) throws Exception {
127         HttpServletRequest request = PortalUtil.getHttpServletRequest(
128             actionRequest);
129 
130         getFolder(request);
131     }
132 
133     public static void getFolder(RenderRequest renderRequest) throws Exception {
134         HttpServletRequest request = PortalUtil.getHttpServletRequest(
135             renderRequest);
136 
137         getFolder(request);
138     }
139 
140     public static void getFolder(HttpServletRequest request) throws Exception {
141         long folderId = ParamUtil.getLong(request, "folderId");
142 
143         DLFolder folder = null;
144 
145         if ((folderId > 0) &&
146             (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
147 
148             folder = DLFolderServiceUtil.getFolder(folderId);
149         }
150 
151         request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
152     }
153 
154 }