001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.model.Lock;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
025    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
026    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
027    import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
028    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
029    
030    import java.io.InputStream;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class DLFileEntryResourceImpl extends BaseResourceImpl {
036    
037            public DLFileEntryResourceImpl(
038                    WebDAVRequest webDavRequest, DLFileEntry fileEntry, String parentPath,
039                    String name) {
040    
041                    super(
042                            parentPath, name, fileEntry.getTitle(), fileEntry.getCreateDate(),
043                            fileEntry.getModifiedDate(), fileEntry.getSize());
044    
045                    setModel(fileEntry);
046                    setClassName(DLFileEntry.class.getName());
047                    setPrimaryKey(fileEntry.getPrimaryKey());
048    
049                    _webDavRequest = webDavRequest;
050                    _fileEntry = fileEntry;
051            }
052    
053            public boolean isCollection() {
054                    return false;
055            }
056    
057            public Lock getLock() {
058                    try {
059                            return DLFileEntryServiceUtil.getFileEntryLock(
060                                    _fileEntry.getGroupId(), _fileEntry.getFolderId(),
061                                    _fileEntry.getName());
062                    }
063                    catch (Exception e) {
064                    }
065    
066                    return null;
067            }
068    
069            public boolean isLocked() {
070                    try {
071                            return DLFileEntryServiceUtil.hasFileEntryLock(
072                                    _fileEntry.getGroupId(), _fileEntry.getFolderId(),
073                                    _fileEntry.getName());
074                    }
075                    catch (Exception e) {
076                    }
077    
078                    return false;
079            }
080    
081            public String getContentType() {
082                    return MimeTypesUtil.getContentType(_fileEntry.getTitle());
083            }
084    
085            public InputStream getContentAsStream() throws WebDAVException {
086                    try {
087                            String version = StringPool.BLANK;
088    
089                            if (PropsValues.DL_WEBDAV_HOLD_LOCK) {
090    
091                                    // Get last version regardless of status
092    
093                                    DLFileVersion fileVersion =
094                                            DLFileVersionLocalServiceUtil.getLatestFileVersion(
095                                                    _fileEntry.getGroupId(), _fileEntry.getFolderId(),
096                                                    _fileEntry.getName());
097    
098                                    version = fileVersion.getVersion();
099                            }
100    
101                            return DLFileEntryLocalServiceUtil.getFileAsStream(
102                                    _webDavRequest.getCompanyId(), _webDavRequest.getUserId(),
103                                    _fileEntry.getGroupId(), _fileEntry.getFolderId(),
104                                    _fileEntry.getName(), version);
105                    }
106                    catch (Exception e) {
107                            throw new WebDAVException(e);
108                    }
109            }
110    
111            private WebDAVRequest _webDavRequest;
112            private DLFileEntry _fileEntry;
113    
114    }