1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.DLFolderConstants;
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  public class ActionUtil {
49  
50      public static void getFileEntry(ActionRequest actionRequest)
51          throws Exception {
52  
53          HttpServletRequest request = PortalUtil.getHttpServletRequest(
54              actionRequest);
55  
56          getFileEntry(request);
57      }
58  
59      public static void getFileEntry(RenderRequest renderRequest)
60          throws Exception {
61  
62          HttpServletRequest request = PortalUtil.getHttpServletRequest(
63              renderRequest);
64  
65          getFileEntry(request);
66      }
67  
68      public static void getFileEntry(HttpServletRequest request)
69          throws Exception {
70  
71          long folderId = ParamUtil.getLong(request, "folderId");
72          long newFolderId = ParamUtil.getLong(request, "newFolderId");
73          String name = ParamUtil.getString(request, "name");
74  
75          DLFileEntry fileEntry = null;
76  
77          if ((folderId > 0) && Validator.isNotNull(name)) {
78              try {
79                  fileEntry = DLFileEntryServiceUtil.getFileEntry(folderId, name);
80              }
81              catch (NoSuchFileEntryException nsfe) {
82  
83                  // This only happens when you're moving a file to a different
84                  // folder
85  
86                  fileEntry = DLFileEntryServiceUtil.getFileEntry(
87                      newFolderId, name);
88              }
89          }
90  
91          request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, fileEntry);
92      }
93  
94      public static void getFileShortcut(ActionRequest actionRequest)
95          throws Exception {
96  
97          HttpServletRequest request = PortalUtil.getHttpServletRequest(
98              actionRequest);
99  
100         getFileShortcut(request);
101     }
102 
103     public static void getFileShortcut(RenderRequest renderRequest)
104         throws Exception {
105 
106         HttpServletRequest request = PortalUtil.getHttpServletRequest(
107             renderRequest);
108 
109         getFileShortcut(request);
110     }
111 
112     public static void getFileShortcut(HttpServletRequest request)
113         throws Exception {
114 
115         long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
116 
117         DLFileShortcut fileShortcut = null;
118 
119         if (fileShortcutId > 0) {
120             fileShortcut = DLFileShortcutServiceUtil.getFileShortcut(
121                 fileShortcutId);
122         }
123 
124         request.setAttribute(
125             WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
126     }
127 
128     public static void getFolder(ActionRequest actionRequest) throws Exception {
129         HttpServletRequest request = PortalUtil.getHttpServletRequest(
130             actionRequest);
131 
132         getFolder(request);
133     }
134 
135     public static void getFolder(RenderRequest renderRequest) throws Exception {
136         HttpServletRequest request = PortalUtil.getHttpServletRequest(
137             renderRequest);
138 
139         getFolder(request);
140     }
141 
142     public static void getFolder(HttpServletRequest request) throws Exception {
143         long folderId = ParamUtil.getLong(request, "folderId");
144 
145         DLFolder folder = null;
146 
147         if ((folderId > 0) &&
148             (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
149 
150             folder = DLFolderServiceUtil.getFolder(folderId);
151         }
152 
153         request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
154     }
155 
156 }