001    /**
002     * Copyright (c) 2000-present 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.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
026    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
027    import com.liferay.portlet.documentlibrary.service.base.DLFileVersionLocalServiceBaseImpl;
028    import com.liferay.portlet.documentlibrary.util.comparator.FileVersionVersionComparator;
029    
030    import java.util.Collections;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class DLFileVersionLocalServiceImpl
037            extends DLFileVersionLocalServiceBaseImpl {
038    
039            @Override
040            public DLFileVersion getFileVersion(long fileVersionId)
041                    throws PortalException {
042    
043                    return dlFileVersionPersistence.findByPrimaryKey(fileVersionId);
044            }
045    
046            @Override
047            public DLFileVersion getFileVersion(long fileEntryId, String version)
048                    throws PortalException {
049    
050                    return dlFileVersionPersistence.findByF_V(fileEntryId, version);
051            }
052    
053            @Override
054            public DLFileVersion getFileVersionByUuidAndGroupId(
055                    String uuid, long groupId) {
056    
057                    return dlFileVersionPersistence.fetchByUUID_G(uuid, groupId);
058            }
059    
060            @Override
061            public List<DLFileVersion> getFileVersions(long fileEntryId, int status) {
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                    return dlFileVersionPersistence.countByF_S(fileEntryId, status);
083            }
084    
085            @Override
086            public DLFileVersion getLatestFileVersion(
087                            long fileEntryId, boolean excludeWorkingCopy)
088                    throws PortalException {
089    
090                    List<DLFileVersion> dlFileVersions =
091                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
092    
093                    if (dlFileVersions.isEmpty()) {
094                            throw new NoSuchFileVersionException(
095                                    "No file versions found for fileEntryId " + fileEntryId);
096                    }
097    
098                    dlFileVersions = ListUtil.copy(dlFileVersions);
099    
100                    Collections.sort(dlFileVersions, new FileVersionVersionComparator());
101    
102                    DLFileVersion dlFileVersion = dlFileVersions.get(0);
103    
104                    String version = dlFileVersion.getVersion();
105    
106                    if (excludeWorkingCopy &&
107                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
108    
109                            return dlFileVersions.get(1);
110                    }
111    
112                    return dlFileVersion;
113            }
114    
115            @Override
116            public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
117                    throws PortalException {
118    
119                    boolean excludeWorkingCopy = true;
120    
121                    if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
122                            excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
123                                    userId, fileEntryId);
124                    }
125    
126                    return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
127            }
128    
129            @Override
130            public void rebuildTree(long companyId) throws PortalException {
131                    dlFolderLocalService.rebuildTree(companyId);
132            }
133    
134            @Override
135            public void setTreePaths(final long folderId, final String treePath)
136                    throws PortalException {
137    
138                    ActionableDynamicQuery actionableDynamicQuery =
139                            getActionableDynamicQuery();
140    
141                    actionableDynamicQuery.setAddCriteriaMethod(
142                            new ActionableDynamicQuery.AddCriteriaMethod() {
143    
144                                    @Override
145                                    public void addCriteria(DynamicQuery dynamicQuery) {
146                                            Property folderIdProperty = PropertyFactoryUtil.forName(
147                                                    "folderId");
148    
149                                            dynamicQuery.add(folderIdProperty.eq(folderId));
150    
151                                            Property treePathProperty = PropertyFactoryUtil.forName(
152                                                    "treePath");
153    
154                                            dynamicQuery.add(treePathProperty.ne(treePath));
155                                    }
156    
157                            });
158    
159                    actionableDynamicQuery.setPerformActionMethod(
160                            new ActionableDynamicQuery.PerformActionMethod() {
161    
162                                    @Override
163                                    public void performAction(Object object) {
164                                            DLFileVersion dlFileVersion = (DLFileVersion)object;
165    
166                                            dlFileVersion.setTreePath(treePath);
167    
168                                            updateDLFileVersion(dlFileVersion);
169                                    }
170    
171                            });
172    
173                    actionableDynamicQuery.performActions();
174            }
175    
176    }