001    /**
002     * Copyright (c) 2000-2011 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.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    }