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