001    /**
002     * Copyright (c) 2000-2012 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 DLFileVersion getFileVersionByUuidAndGroupId(
049                            String uuid, long groupId)
050                    throws SystemException {
051    
052                    return dlFileVersionPersistence.fetchByUUID_G(uuid, groupId);
053            }
054    
055            public List<DLFileVersion> getFileVersions(long fileEntryId, int status)
056                    throws SystemException {
057    
058                    if (status == WorkflowConstants.STATUS_ANY) {
059                            return dlFileVersionPersistence.findByFileEntryId(fileEntryId);
060                    }
061                    else {
062                            return dlFileVersionPersistence.findByF_S(fileEntryId, status);
063                    }
064            }
065    
066            public int getFileVersionsCount(long fileEntryId, int status)
067                    throws SystemException {
068    
069                    return dlFileVersionPersistence.countByF_S(fileEntryId, status);
070            }
071    
072            public DLFileVersion getLatestFileVersion(
073                            long fileEntryId, boolean excludeWorkingCopy)
074                    throws PortalException, SystemException {
075    
076                    List<DLFileVersion> dlFileVersions =
077                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
078    
079                    if (dlFileVersions.isEmpty()) {
080                            throw new NoSuchFileVersionException(
081                                    "No file versions found for fileEntryId " + fileEntryId);
082                    }
083    
084                    dlFileVersions = ListUtil.copy(dlFileVersions);
085    
086                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
087    
088                    DLFileVersion dlFileVersion = dlFileVersions.get(0);
089    
090                    String version = dlFileVersion.getVersion();
091    
092                    if (excludeWorkingCopy &&
093                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
094    
095                            return dlFileVersions.get(1);
096                    }
097    
098                    return dlFileVersion;
099            }
100    
101            public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
102                    throws PortalException, SystemException {
103    
104                    boolean excludeWorkingCopy = true;
105    
106                    if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
107                            excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
108                                    userId, fileEntryId);
109                    }
110    
111                    return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
112            }
113    
114    }