/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portlet.documentlibrary.service.impl;
import com.liferay.document.library.kernel.exception.NoSuchFileVersionException;
import com.liferay.document.library.kernel.model.DLFileEntryConstants;
import com.liferay.document.library.kernel.model.DLFileVersion;
import com.liferay.document.library.kernel.util.comparator.DLFileVersionVersionComparator;
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.Property;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portlet.documentlibrary.service.base.DLFileVersionLocalServiceBaseImpl;
import java.util.Collections;
import java.util.List;
/**
* @author Brian Wing Shun Chan
*/
public class DLFileVersionLocalServiceImpl
extends DLFileVersionLocalServiceBaseImpl {
@Override
public DLFileVersion fetchLatestFileVersion(
long fileEntryId, boolean excludeWorkingCopy) {
List<DLFileVersion> dlFileVersions =
dlFileVersionPersistence.findByFileEntryId(fileEntryId);
if (dlFileVersions.isEmpty()) {
return null;
}
dlFileVersions = ListUtil.copy(dlFileVersions);
Collections.sort(dlFileVersions, new DLFileVersionVersionComparator());
DLFileVersion dlFileVersion = dlFileVersions.get(0);
String version = dlFileVersion.getVersion();
if (excludeWorkingCopy &&
version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
return dlFileVersions.get(1);
}
return dlFileVersion;
}
@Override
public DLFileVersion getFileVersion(long fileVersionId)
throws PortalException {
return dlFileVersionPersistence.findByPrimaryKey(fileVersionId);
}
@Override
public DLFileVersion getFileVersion(long fileEntryId, String version)
throws PortalException {
return dlFileVersionPersistence.findByF_V(fileEntryId, version);
}
@Override
public DLFileVersion getFileVersionByUuidAndGroupId(
String uuid, long groupId) {
return dlFileVersionPersistence.fetchByUUID_G(uuid, groupId);
}
@Override
public List<DLFileVersion> getFileVersions(long fileEntryId, int status) {
List<DLFileVersion> dlFileVersions = null;
if (status == WorkflowConstants.STATUS_ANY) {
dlFileVersions = dlFileVersionPersistence.findByFileEntryId(
fileEntryId);
}
else {
dlFileVersions = dlFileVersionPersistence.findByF_S(
fileEntryId, status);
}
dlFileVersions = ListUtil.copy(dlFileVersions);
Collections.sort(dlFileVersions, new DLFileVersionVersionComparator());
return dlFileVersions;
}
@Override
public int getFileVersionsCount(long fileEntryId, int status) {
return dlFileVersionPersistence.countByF_S(fileEntryId, status);
}
@Override
public DLFileVersion getLatestFileVersion(
long fileEntryId, boolean excludeWorkingCopy)
throws PortalException {
DLFileVersion dlFileVersion = fetchLatestFileVersion(
fileEntryId, excludeWorkingCopy);
if (dlFileVersion == null) {
throw new NoSuchFileVersionException(
"No file versions found for fileEntryId " + fileEntryId);
}
return dlFileVersion;
}
@Override
public DLFileVersion getLatestFileVersion(long userId, long fileEntryId)
throws PortalException {
boolean excludeWorkingCopy = true;
if (dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId)) {
excludeWorkingCopy = !dlFileEntryLocalService.hasFileEntryLock(
userId, fileEntryId);
}
return getLatestFileVersion(fileEntryId, excludeWorkingCopy);
}
@Override
public void rebuildTree(long companyId) throws PortalException {
dlFolderLocalService.rebuildTree(companyId);
}
@Override
public void setTreePaths(final long folderId, final String treePath)
throws PortalException {
if (treePath == null) {
throw new IllegalArgumentException("Tree path is null");
}
ActionableDynamicQuery actionableDynamicQuery =
getActionableDynamicQuery();
actionableDynamicQuery.setAddCriteriaMethod(
new ActionableDynamicQuery.AddCriteriaMethod() {
@Override
public void addCriteria(DynamicQuery dynamicQuery) {
Property folderIdProperty = PropertyFactoryUtil.forName(
"folderId");
dynamicQuery.add(folderIdProperty.eq(folderId));
Property treePathProperty = PropertyFactoryUtil.forName(
"treePath");
dynamicQuery.add(
RestrictionsFactoryUtil.or(
treePathProperty.isNull(),
treePathProperty.ne(treePath)));
}
});
actionableDynamicQuery.setPerformActionMethod(
new ActionableDynamicQuery.PerformActionMethod<DLFileVersion>() {
@Override
public void performAction(DLFileVersion dlFileVersion) {
dlFileVersion.setTreePath(treePath);
updateDLFileVersion(dlFileVersion);
}
});
actionableDynamicQuery.performActions();
}
}