001    /**
002     * Copyright (c) 2000-2011 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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
023    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
024    import com.liferay.portlet.documentlibrary.service.base.DLFileVersionLocalServiceBaseImpl;
025    import com.liferay.portlet.documentlibrary.util.comparator.FileVersionVersionComparator;
026    
027    import java.util.Collections;
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class DLFileVersionLocalServiceImpl
034            extends DLFileVersionLocalServiceBaseImpl {
035    
036            public DLFileVersion getFileVersion(long fileVersionId)
037                    throws PortalException, SystemException {
038    
039                    return dlFileVersionPersistence.findByPrimaryKey(fileVersionId);
040            }
041    
042            public DLFileVersion getFileVersion(long fileEntryId, String version)
043                    throws PortalException, SystemException {
044    
045                    return dlFileVersionPersistence.findByF_V(fileEntryId, version);
046            }
047    
048            public List<DLFileVersion> getFileVersions(long fileEntryId, int status)
049                    throws SystemException {
050    
051                    if (status == WorkflowConstants.STATUS_ANY) {
052                            return dlFileVersionPersistence.findByFileEntryId(fileEntryId);
053                    }
054                    else {
055                            return dlFileVersionPersistence.findByF_S(fileEntryId, status);
056                    }
057            }
058    
059            public DLFileVersion getLatestFileVersion(
060                            long fileEntryId, boolean excludeWorkingCopy)
061                    throws PortalException, SystemException {
062    
063                    List<DLFileVersion> dlFileVersions =
064                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
065    
066                    if (dlFileVersions.isEmpty()) {
067                            throw new NoSuchFileVersionException(
068                                    "No file versions found for fileEntryId " + fileEntryId);
069                    }
070    
071                    dlFileVersions = ListUtil.copy(dlFileVersions);
072    
073                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
074    
075                    DLFileVersion dlFileVersion = dlFileVersions.get(0);
076    
077                    String version = dlFileVersion.getVersion();
078    
079                    if (excludeWorkingCopy &&
080                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
081    
082                            return dlFileVersions.get(1);
083                    }
084    
085                    return dlFileVersion;
086            }
087    
088            public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
089                    throws PortalException, SystemException {
090    
091                    boolean excludeWorkingCopy = true;
092    
093                    if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
094                            excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
095                                    userId, fileEntryId);
096                    }
097    
098                    return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
099            }
100    
101    }