1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.action;
21  
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
27  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
28  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
29  import com.liferay.portlet.documentlibrary.model.DLFolder;
30  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
31  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
32  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
33  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.RenderRequest;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  /**
41   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ActionUtil {
47  
48      public static void getFileEntry(ActionRequest actionRequest)
49          throws Exception {
50  
51          HttpServletRequest request = PortalUtil.getHttpServletRequest(
52              actionRequest);
53  
54          getFileEntry(request);
55      }
56  
57      public static void getFileEntry(RenderRequest renderRequest)
58          throws Exception {
59  
60          HttpServletRequest request = PortalUtil.getHttpServletRequest(
61              renderRequest);
62  
63          getFileEntry(request);
64      }
65  
66      public static void getFileEntry(HttpServletRequest request)
67          throws Exception {
68  
69          long folderId = ParamUtil.getLong(request, "folderId");
70          long newFolderId = ParamUtil.getLong(request, "newFolderId");
71          String name = ParamUtil.getString(request, "name");
72  
73          DLFileEntry fileEntry = null;
74  
75          if ((folderId > 0) && Validator.isNotNull(name)) {
76              try {
77                  fileEntry = DLFileEntryServiceUtil.getFileEntry(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                      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 }