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 = themeDisplay.getScopeGroupId();
047                    long folderId = ParamUtil.getLong(request, "folderId");
048                    long newFolderId = ParamUtil.getLong(request, "newFolderId");
049                    String name = ParamUtil.getString(request, "name");
050    
051                    DLFileEntry fileEntry = null;
052    
053                    if (Validator.isNotNull(name)) {
054                            try {
055                                    fileEntry = DLFileEntryServiceUtil.getFileEntry(
056                                            groupId, folderId, name);
057                            }
058                            catch (NoSuchFileEntryException nsfe) {
059    
060                                    // This only happens when you're moving a file to a different
061                                    // folder
062    
063                                    fileEntry = DLFileEntryServiceUtil.getFileEntry(
064                                            groupId, newFolderId, name);
065                            }
066                    }
067    
068                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, fileEntry);
069            }
070    
071            public static void getFileEntry(PortletRequest portletRequest)
072                    throws Exception {
073    
074                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
075                            portletRequest);
076    
077                    getFileEntry(request);
078            }
079    
080            public static void getFileShortcut(HttpServletRequest request)
081                    throws Exception {
082    
083                    long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
084    
085                    DLFileShortcut fileShortcut = null;
086    
087                    if (fileShortcutId > 0) {
088                            fileShortcut = DLFileShortcutServiceUtil.getFileShortcut(
089                                    fileShortcutId);
090                    }
091    
092                    request.setAttribute(
093                            WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
094            }
095    
096            public static void getFileShortcut(PortletRequest portletRequest)
097                    throws Exception {
098    
099                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
100                            portletRequest);
101    
102                    getFileShortcut(request);
103            }
104    
105            public static void getFolder(HttpServletRequest request) throws Exception {
106                    long folderId = ParamUtil.getLong(request, "folderId");
107    
108                    DLFolder folder = null;
109    
110                    if ((folderId > 0) &&
111                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
112    
113                            folder = DLFolderServiceUtil.getFolder(folderId);
114                    }
115    
116                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
117            }
118    
119            public static void getFolder(PortletRequest portletRequest)
120                    throws Exception {
121    
122                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
123                            portletRequest);
124    
125                    getFolder(request);
126            }
127    
128    }