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            @Override
037            public DLFileVersion getFileVersion(long fileVersionId)
038                    throws PortalException, SystemException {
039    
040                    return dlFileVersionPersistence.findByPrimaryKey(fileVersionId);
041            }
042    
043            @Override
044            public DLFileVersion getFileVersion(long fileEntryId, String version)
045                    throws PortalException, SystemException {
046    
047                    return dlFileVersionPersistence.findByF_V(fileEntryId, version);
048            }
049    
050            @Override
051            public DLFileVersion getFileVersionByUuidAndGroupId(
052                            String uuid, long groupId)
053                    throws SystemException {
054    
055                    return dlFileVersionPersistence.fetchByUUID_G(uuid, groupId);
056            }
057    
058            @Override
059            public List<DLFileVersion> getFileVersions(long fileEntryId, int status)
060                    throws SystemException {
061    
062                    List<DLFileVersion> dlFileVersions = null;
063    
064                    if (status == WorkflowConstants.STATUS_ANY) {
065                            dlFileVersions = dlFileVersionPersistence.findByFileEntryId(
066                                    fileEntryId);
067                    }
068                    else {
069                            dlFileVersions = dlFileVersionPersistence.findByF_S(
070                                    fileEntryId, status);
071                    }
072    
073                    dlFileVersions = ListUtil.copy(dlFileVersions);
074    
075                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
076    
077                    return dlFileVersions;
078            }
079    
080            @Override
081            public int getFileVersionsCount(long fileEntryId, int status)
082                    throws SystemException {
083    
084                    return dlFileVersionPersistence.countByF_S(fileEntryId, status);
085            }
086    
087            @Override
088            public DLFileVersion getLatestFileVersion(
089                            long fileEntryId, boolean excludeWorkingCopy)
090                    throws PortalException, SystemException {
091    
092                    List<DLFileVersion> dlFileVersions =
093                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
094    
095                    if (dlFileVersions.isEmpty()) {
096                            throw new NoSuchFileVersionException(
097                                    "No file versions found for fileEntryId " + fileEntryId);
098                    }
099    
100                    dlFileVersions = ListUtil.copy(dlFileVersions);
101    
102                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
103    
104                    DLFileVersion dlFileVersion = dlFileVersions.get(0);
105    
106                    String version = dlFileVersion.getVersion();
107    
108                    if (excludeWorkingCopy &&
109                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
110    
111                            return dlFileVersions.get(1);
112                    }
113    
114                    return dlFileVersion;
115            }
116    
117            @Override
118            public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
119                    throws PortalException, SystemException {
120    
121                    boolean excludeWorkingCopy = true;
122    
123                    if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
124                            excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
125                                    userId, fileEntryId);
126                    }
127    
128                    return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
129            }
130    
131    }