001    /**
002     * Copyright (c) 2000-2012 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 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 DLFileVersion getLatestFileVersion(
067                            long fileEntryId, boolean excludeWorkingCopy)
068                    throws PortalException, SystemException {
069    
070                    List<DLFileVersion> dlFileVersions =
071                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
072    
073                    if (dlFileVersions.isEmpty()) {
074                            throw new NoSuchFileVersionException(
075                                    "No file versions found for fileEntryId " + fileEntryId);
076                    }
077    
078                    dlFileVersions = ListUtil.copy(dlFileVersions);
079    
080                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
081    
082                    DLFileVersion dlFileVersion = dlFileVersions.get(0);
083    
084                    String version = dlFileVersion.getVersion();
085    
086                    if (excludeWorkingCopy &&
087                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
088    
089                            return dlFileVersions.get(1);
090                    }
091    
092                    return dlFileVersion;
093            }
094    
095            public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
096                    throws PortalException, SystemException {
097    
098                    boolean excludeWorkingCopy = true;
099    
100                    if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
101                            excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
102                                    userId, fileEntryId);
103                    }
104    
105                    return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
106            }
107    
108    }