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.dao.orm.Session;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.TreePathUtil;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
025    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
026    import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionModelImpl;
027    import com.liferay.portlet.documentlibrary.model.impl.DLFolderModelImpl;
028    import com.liferay.portlet.documentlibrary.service.base.DLFileVersionLocalServiceBaseImpl;
029    import com.liferay.portlet.documentlibrary.util.comparator.FileVersionVersionComparator;
030    
031    import java.util.Collections;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class DLFileVersionLocalServiceImpl
038            extends DLFileVersionLocalServiceBaseImpl {
039    
040            @Override
041            public DLFileVersion getFileVersion(long fileVersionId)
042                    throws PortalException, SystemException {
043    
044                    return dlFileVersionPersistence.findByPrimaryKey(fileVersionId);
045            }
046    
047            @Override
048            public DLFileVersion getFileVersion(long fileEntryId, String version)
049                    throws PortalException, SystemException {
050    
051                    return dlFileVersionPersistence.findByF_V(fileEntryId, version);
052            }
053    
054            @Override
055            public DLFileVersion getFileVersionByUuidAndGroupId(
056                            String uuid, long groupId)
057                    throws SystemException {
058    
059                    return dlFileVersionPersistence.fetchByUUID_G(uuid, groupId);
060            }
061    
062            @Override
063            public List<DLFileVersion> getFileVersions(long fileEntryId, int status)
064                    throws SystemException {
065    
066                    List<DLFileVersion> dlFileVersions = null;
067    
068                    if (status == WorkflowConstants.STATUS_ANY) {
069                            dlFileVersions = dlFileVersionPersistence.findByFileEntryId(
070                                    fileEntryId);
071                    }
072                    else {
073                            dlFileVersions = dlFileVersionPersistence.findByF_S(
074                                    fileEntryId, status);
075                    }
076    
077                    dlFileVersions = ListUtil.copy(dlFileVersions);
078    
079                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
080    
081                    return dlFileVersions;
082            }
083    
084            @Override
085            public int getFileVersionsCount(long fileEntryId, int status)
086                    throws SystemException {
087    
088                    return dlFileVersionPersistence.countByF_S(fileEntryId, status);
089            }
090    
091            @Override
092            public DLFileVersion getLatestFileVersion(
093                            long fileEntryId, boolean excludeWorkingCopy)
094                    throws PortalException, SystemException {
095    
096                    List<DLFileVersion> dlFileVersions =
097                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
098    
099                    if (dlFileVersions.isEmpty()) {
100                            throw new NoSuchFileVersionException(
101                                    "No file versions found for fileEntryId " + fileEntryId);
102                    }
103    
104                    dlFileVersions = ListUtil.copy(dlFileVersions);
105    
106                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
107    
108                    DLFileVersion dlFileVersion = dlFileVersions.get(0);
109    
110                    String version = dlFileVersion.getVersion();
111    
112                    if (excludeWorkingCopy &&
113                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
114    
115                            return dlFileVersions.get(1);
116                    }
117    
118                    return dlFileVersion;
119            }
120    
121            @Override
122            public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
123                    throws PortalException, SystemException {
124    
125                    boolean excludeWorkingCopy = true;
126    
127                    if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
128                            excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
129                                    userId, fileEntryId);
130                    }
131    
132                    return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
133            }
134    
135            @Override
136            public void rebuildTree(long companyId) throws SystemException {
137                    dlFolderLocalService.rebuildTree(companyId);
138    
139                    Session session = dlFileVersionPersistence.openSession();
140    
141                    try {
142                            TreePathUtil.rebuildTree(
143                                    session, companyId, DLFileVersionModelImpl.TABLE_NAME,
144                                    DLFolderModelImpl.TABLE_NAME, "folderId", true);
145                    }
146                    finally {
147                            dlFileVersionPersistence.closeSession(session);
148    
149                            dlFileVersionPersistence.clearCache();
150                    }
151            }
152    
153    }