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.dao.orm.RestrictionsFactoryUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.util.ListUtil;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portlet.documentlibrary.exception.NoSuchFileVersionException;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
027    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
028    import com.liferay.portlet.documentlibrary.service.base.DLFileVersionLocalServiceBaseImpl;
029    import com.liferay.portlet.documentlibrary.util.comparator.DLFileVersionVersionComparator;
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 fetchLatestFileVersion(
042                    long fileEntryId, boolean excludeWorkingCopy) {
043    
044                    List<DLFileVersion> dlFileVersions =
045                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
046    
047                    if (dlFileVersions.isEmpty()) {
048                            return null;
049                    }
050    
051                    dlFileVersions = ListUtil.copy(dlFileVersions);
052    
053                    Collections.sort(dlFileVersions, new DLFileVersionVersionComparator());
054    
055                    DLFileVersion dlFileVersion = dlFileVersions.get(0);
056    
057                    String version = dlFileVersion.getVersion();
058    
059                    if (excludeWorkingCopy &&
060                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
061    
062                            return dlFileVersions.get(1);
063                    }
064    
065                    return dlFileVersion;
066            }
067    
068            @Override
069            public DLFileVersion getFileVersion(long fileVersionId)
070                    throws PortalException {
071    
072                    return dlFileVersionPersistence.findByPrimaryKey(fileVersionId);
073            }
074    
075            @Override
076            public DLFileVersion getFileVersion(long fileEntryId, String version)
077                    throws PortalException {
078    
079                    return dlFileVersionPersistence.findByF_V(fileEntryId, version);
080            }
081    
082            @Override
083            public DLFileVersion getFileVersionByUuidAndGroupId(
084                    String uuid, long groupId) {
085    
086                    return dlFileVersionPersistence.fetchByUUID_G(uuid, groupId);
087            }
088    
089            @Override
090            public List<DLFileVersion> getFileVersions(long fileEntryId, int status) {
091                    List<DLFileVersion> dlFileVersions = null;
092    
093                    if (status == WorkflowConstants.STATUS_ANY) {
094                            dlFileVersions = dlFileVersionPersistence.findByFileEntryId(
095                                    fileEntryId);
096                    }
097                    else {
098                            dlFileVersions = dlFileVersionPersistence.findByF_S(
099                                    fileEntryId, status);
100                    }
101    
102                    dlFileVersions = ListUtil.copy(dlFileVersions);
103    
104                    Collections.sort(dlFileVersions, new DLFileVersionVersionComparator());
105    
106                    return dlFileVersions;
107            }
108    
109            @Override
110            public int getFileVersionsCount(long fileEntryId, int status) {
111                    return dlFileVersionPersistence.countByF_S(fileEntryId, status);
112            }
113    
114            @Override
115            public DLFileVersion getLatestFileVersion(
116                            long fileEntryId, boolean excludeWorkingCopy)
117                    throws PortalException {
118    
119                    DLFileVersion dlFileVersion = fetchLatestFileVersion(
120                            fileEntryId, excludeWorkingCopy);
121    
122                    if (dlFileVersion == null) {
123                            throw new NoSuchFileVersionException(
124                                    "No file versions found for fileEntryId " + fileEntryId);
125                    }
126    
127                    return dlFileVersion;
128            }
129    
130            @Override
131            public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
132                    throws PortalException {
133    
134                    boolean excludeWorkingCopy = true;
135    
136                    if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
137                            excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
138                                    userId, fileEntryId);
139                    }
140    
141                    return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
142            }
143    
144            @Override
145            public void rebuildTree(long companyId) throws PortalException {
146                    dlFolderLocalService.rebuildTree(companyId);
147            }
148    
149            @Override
150            public void setTreePaths(final long folderId, final String treePath)
151                    throws PortalException {
152    
153                    if (treePath == null) {
154                            throw new IllegalArgumentException("Tree path is null");
155                    }
156    
157                    ActionableDynamicQuery actionableDynamicQuery =
158                            getActionableDynamicQuery();
159    
160                    actionableDynamicQuery.setAddCriteriaMethod(
161                            new ActionableDynamicQuery.AddCriteriaMethod() {
162    
163                                    @Override
164                                    public void addCriteria(DynamicQuery dynamicQuery) {
165                                            Property folderIdProperty = PropertyFactoryUtil.forName(
166                                                    "folderId");
167    
168                                            dynamicQuery.add(folderIdProperty.eq(folderId));
169    
170                                            Property treePathProperty = PropertyFactoryUtil.forName(
171                                                    "treePath");
172    
173                                            dynamicQuery.add(
174                                                    RestrictionsFactoryUtil.or(
175                                                            treePathProperty.isNull(),
176                                                            treePathProperty.ne(treePath)));
177                                    }
178    
179                            });
180    
181                    actionableDynamicQuery.setPerformActionMethod(
182                            new ActionableDynamicQuery.PerformActionMethod<DLFileVersion>() {
183    
184                                    @Override
185                                    public void performAction(DLFileVersion dlFileVersion) {
186                                            dlFileVersion.setTreePath(treePath);
187    
188                                            updateDLFileVersion(dlFileVersion);
189                                    }
190    
191                            });
192    
193                    actionableDynamicQuery.performActions();
194            }
195    
196    }