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.webdav;
016    
017    import com.liferay.portal.kernel.util.MimeTypesUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.webdav.BaseResourceImpl;
020    import com.liferay.portal.kernel.webdav.WebDAVException;
021    import com.liferay.portal.kernel.webdav.WebDAVRequest;
022    import com.liferay.portal.util.PropsValues;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
024    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
025    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
026    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
027    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
028    
029    import java.io.InputStream;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class DLFileEntryResourceImpl extends BaseResourceImpl {
035    
036            public DLFileEntryResourceImpl(
037                    WebDAVRequest webDavRequest, DLFileEntry fileEntry, String parentPath,
038                    String name) {
039    
040                    super(
041                            parentPath, name, fileEntry.getTitle(), fileEntry.getCreateDate(),
042                            fileEntry.getModifiedDate(), fileEntry.getSize());
043    
044                    setModel(fileEntry);
045                    setClassName(DLFileEntry.class.getName());
046                    setPrimaryKey(fileEntry.getPrimaryKey());
047    
048                    _webDavRequest = webDavRequest;
049                    _fileEntry = fileEntry;
050            }
051    
052            public boolean isCollection() {
053                    return false;
054            }
055    
056            public boolean isLocked() {
057                    try {
058                            return DLFileEntryServiceUtil.hasFileEntryLock(
059                                    _fileEntry.getGroupId(), _fileEntry.getFolderId(),
060                                    _fileEntry.getName());
061                    }
062                    catch (Exception e) {
063                    }
064    
065                    return false;
066            }
067    
068            public String getContentType() {
069                    return MimeTypesUtil.getContentType(_fileEntry.getTitle());
070            }
071    
072            public InputStream getContentAsStream() throws WebDAVException {
073                    try {
074                            String version = StringPool.BLANK;
075    
076                            if (PropsValues.DL_WEBDAV_HOLD_LOCK) {
077    
078                                    // Get last version regardless of status
079    
080                                    DLFileVersion fileVersion =
081                                            DLFileVersionLocalServiceUtil.getLatestFileVersion(
082                                                    _fileEntry.getGroupId(), _fileEntry.getFolderId(),
083                                                    _fileEntry.getName());
084    
085                                    version = fileVersion.getVersion();
086                            }
087    
088                            return DLFileEntryLocalServiceUtil.getFileAsStream(
089                                    _webDavRequest.getCompanyId(), _webDavRequest.getUserId(),
090                                    _fileEntry.getGroupId(), _fileEntry.getFolderId(),
091                                    _fileEntry.getName(), version);
092                    }
093                    catch (Exception e) {
094                            throw new WebDAVException(e);
095                    }
096            }
097    
098            private WebDAVRequest _webDavRequest;
099            private DLFileEntry _fileEntry;
100    
101    }