001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.theme.ThemeDisplay;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
024    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
025    import com.liferay.portlet.documentlibrary.model.DLFolder;
026    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
027    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
028    import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
029    import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
030    
031    import javax.portlet.PortletRequest;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class ActionUtil {
039    
040            public static void getFileEntry(HttpServletRequest request)
041                    throws Exception {
042    
043                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
044                            WebKeys.THEME_DISPLAY);
045    
046                    long groupId = ParamUtil.getLong(
047                            request, "groupId", themeDisplay.getScopeGroupId());
048                    long folderId = ParamUtil.getLong(request, "folderId");
049                    long newFolderId = ParamUtil.getLong(request, "newFolderId");
050                    String name = ParamUtil.getString(request, "name");
051    
052                    DLFileEntry fileEntry = null;
053    
054                    if (Validator.isNotNull(name)) {
055                            try {
056                                    fileEntry = DLFileEntryServiceUtil.getFileEntry(
057                                            groupId, folderId, name);
058                            }
059                            catch (NoSuchFileEntryException nsfe) {
060    
061                                    // This only happens when you're moving a file to a different
062                                    // folder
063    
064                                    fileEntry = DLFileEntryServiceUtil.getFileEntry(
065                                            groupId, newFolderId, name);
066                            }
067                    }
068    
069                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, fileEntry);
070            }
071    
072            public static void getFileEntry(PortletRequest portletRequest)
073                    throws Exception {
074    
075                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
076                            portletRequest);
077    
078                    getFileEntry(request);
079            }
080    
081            public static void getFileShortcut(HttpServletRequest request)
082                    throws Exception {
083    
084                    long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
085    
086                    DLFileShortcut fileShortcut = null;
087    
088                    if (fileShortcutId > 0) {
089                            fileShortcut = DLFileShortcutServiceUtil.getFileShortcut(
090                                    fileShortcutId);
091                    }
092    
093                    request.setAttribute(
094                            WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
095            }
096    
097            public static void getFileShortcut(PortletRequest portletRequest)
098                    throws Exception {
099    
100                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
101                            portletRequest);
102    
103                    getFileShortcut(request);
104            }
105    
106            public static void getFolder(HttpServletRequest request) throws Exception {
107                    long folderId = ParamUtil.getLong(request, "folderId");
108    
109                    DLFolder folder = null;
110    
111                    if ((folderId > 0) &&
112                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
113    
114                            folder = DLFolderServiceUtil.getFolder(folderId);
115                    }
116    
117                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
118            }
119    
120            public static void getFolder(PortletRequest portletRequest)
121                    throws Exception {
122    
123                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
124                            portletRequest);
125    
126                    getFolder(request);
127            }
128    
129    }