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.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 actionRequest)
52          throws Exception {
53  
54          HttpServletRequest request = PortalUtil.getHttpServletRequest(
55              actionRequest);
56  
57          getFileEntry(request);
58      }
59  
60      public static void getFileEntry(RenderRequest renderRequest)
61          throws Exception {
62  
63          HttpServletRequest request = PortalUtil.getHttpServletRequest(
64              renderRequest);
65  
66          getFileEntry(request);
67      }
68  
69      public static void getFileEntry(HttpServletRequest request)
70          throws Exception {
71  
72          long folderId = ParamUtil.getLong(request, "folderId");
73          long newFolderId = ParamUtil.getLong(request, "newFolderId");
74          String name = ParamUtil.getString(request, "name");
75  
76          DLFileEntry fileEntry = null;
77  
78          if ((folderId > 0) && Validator.isNotNull(name)) {
79              try {
80                  fileEntry = DLFileEntryServiceUtil.getFileEntry(folderId, name);
81              }
82              catch (NoSuchFileEntryException nsfe) {
83  
84                  // This only happens when you're moving a file to a different
85                  // folder
86  
87                  fileEntry = DLFileEntryServiceUtil.getFileEntry(
88                      newFolderId, name);
89              }
90          }
91  
92          request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, fileEntry);
93      }
94  
95      public static void getFileShortcut(ActionRequest actionRequest)
96          throws Exception {
97  
98          HttpServletRequest request = PortalUtil.getHttpServletRequest(
99              actionRequest);
100 
101         getFileShortcut(request);
102     }
103 
104     public static void getFileShortcut(RenderRequest renderRequest)
105         throws Exception {
106 
107         HttpServletRequest request = PortalUtil.getHttpServletRequest(
108             renderRequest);
109 
110         getFileShortcut(request);
111     }
112 
113     public static void getFileShortcut(HttpServletRequest request)
114         throws Exception {
115 
116         long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
117 
118         DLFileShortcut fileShortcut = null;
119 
120         if (fileShortcutId > 0) {
121             fileShortcut = DLFileShortcutServiceUtil.getFileShortcut(
122                 fileShortcutId);
123         }
124 
125         request.setAttribute(
126             WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
127     }
128 
129     public static void getFolder(ActionRequest actionRequest) throws Exception {
130         HttpServletRequest request = PortalUtil.getHttpServletRequest(
131             actionRequest);
132 
133         getFolder(request);
134     }
135 
136     public static void getFolder(RenderRequest renderRequest) throws Exception {
137         HttpServletRequest request = PortalUtil.getHttpServletRequest(
138             renderRequest);
139 
140         getFolder(request);
141     }
142 
143     public static void getFolder(HttpServletRequest request) throws Exception {
144         long folderId = ParamUtil.getLong(request, "folderId");
145 
146         DLFolder folder = null;
147 
148         if ((folderId > 0) &&
149             (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
150 
151             folder = DLFolderServiceUtil.getFolder(folderId);
152         }
153 
154         request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
155     }
156 
157 }