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