1   /**
2    * Copyright (c) 2000-2007 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.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
30  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
31  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
32  import com.liferay.portlet.documentlibrary.model.DLFolder;
33  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
34  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
35  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
36  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.RenderRequest;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  /**
44   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class ActionUtil {
50  
51      public static void getFileEntry(ActionRequest req) throws Exception {
52          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
53  
54          getFileEntry(httpReq);
55      }
56  
57      public static void getFileEntry(RenderRequest req) throws Exception {
58          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
59  
60          getFileEntry(httpReq);
61      }
62  
63      public static void getFileEntry(HttpServletRequest req) throws Exception {
64          long folderId = ParamUtil.getLong(req, "folderId");
65          long newFolderId = ParamUtil.getLong(req, "newFolderId");
66          String name = ParamUtil.getString(req, "name");
67  
68          DLFileEntry fileEntry = null;
69  
70          if ((folderId > 0) && Validator.isNotNull(name)) {
71              try {
72                  fileEntry = DLFileEntryServiceUtil.getFileEntry(folderId, name);
73              }
74              catch (NoSuchFileEntryException nsfe) {
75  
76                  // This only happens when you're moving a file to a different
77                  // folder
78  
79                  fileEntry = DLFileEntryServiceUtil.getFileEntry(
80                      newFolderId, name);
81              }
82          }
83  
84          req.setAttribute(WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, fileEntry);
85      }
86  
87      public static void getFileShortcut(ActionRequest req) throws Exception {
88          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
89  
90          getFileShortcut(httpReq);
91      }
92  
93      public static void getFileShortcut(RenderRequest req) throws Exception {
94          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
95  
96          getFileShortcut(httpReq);
97      }
98  
99      public static void getFileShortcut(HttpServletRequest req)
100         throws Exception {
101 
102         long fileShortcutId = ParamUtil.getLong(req, "fileShortcutId");
103 
104         DLFileShortcut fileShortcut = null;
105 
106         if (fileShortcutId > 0) {
107             fileShortcut = DLFileShortcutServiceUtil.getFileShortcut(
108                 fileShortcutId);
109         }
110 
111         req.setAttribute(WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
112     }
113 
114     public static void getFolder(ActionRequest req) throws Exception {
115         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
116 
117         getFolder(httpReq);
118     }
119 
120     public static void getFolder(RenderRequest req) throws Exception {
121         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
122 
123         getFolder(httpReq);
124     }
125 
126     public static void getFolder(HttpServletRequest req) throws Exception {
127         long folderId = ParamUtil.getLong(req, "folderId");
128 
129         DLFolder folder = null;
130 
131         if ((folderId > 0) &&
132             (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
133 
134             folder = DLFolderServiceUtil.getFolder(folderId);
135         }
136 
137         req.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
138     }
139 
140 }