Commit 78576346 authored by Matija Obreza's avatar Matija Obreza

Repository services with `sort` parameter

parent 9d74d5e2
Pipeline #7357 failed with stage
in 2 minutes and 6 seconds
......@@ -48,6 +48,7 @@ import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.filerepository.metadata.BaseMetadata;
import org.genesys.filerepository.service.BytesStorageService;
import org.hibernate.annotations.Type;
import org.springframework.data.domain.Sort;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
......@@ -71,6 +72,8 @@ public class RepositoryFile extends AuditedVersionedModelWithoutId implements Ac
/** The Constant serialVersionUID. */
private static final long serialVersionUID = -4816923593950502695L;
public static final Sort DEFAULT_SORT = new Sort("originalFilename");
/** The id. */
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
......
......@@ -39,6 +39,7 @@ import org.genesys.blocks.security.model.AclObjectIdentity;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.service.impl.PathValidator;
import org.hibernate.annotations.Type;
import org.springframework.data.domain.Sort;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
......@@ -59,6 +60,8 @@ public class RepositoryFolder extends UuidModel implements AclAwareModel {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = -7947000802758739238L;
public static final Sort DEFAULT_SORT = new Sort("name");
/**
* Reference to parent Folder. Root folders have this set to null. This
* establishes hierarchical structure of folders.
......
......@@ -22,6 +22,7 @@ import java.util.UUID;
import org.genesys.filerepository.RepositoryPersistence;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.model.RepositoryImage;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.stereotype.Repository;
......@@ -55,6 +56,6 @@ public interface RepositoryImagePersistence extends RepositoryPersistence<Reposi
* @param repositoryPath the repository folder
* @return the list
*/
List<RepositoryImage> findByFolder(RepositoryFolder folder);
List<RepositoryImage> findByFolder(RepositoryFolder folder, final Sort sort);
}
......@@ -32,10 +32,10 @@ import org.genesys.filerepository.metadata.ImageMetadata;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.model.RepositoryImage;
import org.springframework.data.domain.Sort;
// TODO: Auto-generated Javadoc
/**
* The Interface RepositoryService.
* The File Repository Service.
*/
/**
* @author Matija Obreza
......@@ -127,7 +127,7 @@ public interface RepositoryService {
* @throws InvalidRepositoryPathException if path is not a valid repository path
* @throws NoSuchRepositoryFileException
*/
List<RepositoryFile> getFiles(Path folderPath) throws InvalidRepositoryPathException;
List<RepositoryFile> getFiles(Path folderPath, Sort sort) throws InvalidRepositoryPathException;
/**
* Stream file info at the specified repository path.
......@@ -136,7 +136,7 @@ public interface RepositoryService {
* @return the files
* @throws InvalidRepositoryPathException
*/
Stream<RepositoryFile> streamFiles(Path root) throws InvalidRepositoryPathException;
Stream<RepositoryFile> streamFiles(Path root, Sort sort) throws InvalidRepositoryPathException;
/**
* Update file metadata. The update is based on the record UUID.
......@@ -215,7 +215,7 @@ public interface RepositoryService {
* @param path the path
* @return the list
*/
List<RepositoryImage> listImages(Path path);
List<RepositoryImage> listImages(Path path, Sort sort);
/**
* List all paths within the root Path, including the root folder itself if it
......@@ -225,7 +225,7 @@ public interface RepositoryService {
* @return the list
* @throws InvalidRepositoryPathException if path is not a valid repository path
*/
List<RepositoryFolder> listPaths(Path root) throws InvalidRepositoryPathException;
List<RepositoryFolder> listPathsRecursively(Path root) throws InvalidRepositoryPathException;
/**
* Update image metadata. The update is based on the record UUID.
......@@ -330,6 +330,6 @@ public interface RepositoryService {
* @return
* @throws InvalidRepositoryPathException if path is not a valid repository path
*/
List<RepositoryFolder> getFolders(Path root) throws InvalidRepositoryPathException;
List<RepositoryFolder> getFolders(Path root, Sort sort) throws InvalidRepositoryPathException;
}
......@@ -27,6 +27,7 @@ import org.genesys.blocks.security.SecurityContextUtil;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.QImageGallery;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.model.RepositoryImage;
import org.genesys.filerepository.persistence.ImageGalleryPersistence;
......@@ -146,7 +147,7 @@ public class ImageGalleryServiceImpl implements ImageGalleryService {
imageGallery.setTitle(title);
imageGallery.setDescription(description);
final List<RepositoryImage> images = new ArrayList<>(repositoryService.listImages(Paths.get(imageGallery.getPath())));
final List<RepositoryImage> images = new ArrayList<>(repositoryService.listImages(Paths.get(imageGallery.getPath()), RepositoryFile.DEFAULT_SORT));
imageGallery.setImages(images);
......@@ -374,7 +375,7 @@ public class ImageGalleryServiceImpl implements ImageGalleryService {
@Override
public Page<ImageGallery> listImageGalleries(final Path root, final Pageable pageable) throws InvalidRepositoryPathException {
PathValidator.checkValidPath(root);
List<RepositoryFolder> folders = repositoryService.listPaths(root);
List<RepositoryFolder> folders = repositoryService.listPathsRecursively(root);
// Remove folders user can't read
folders.removeIf(folder -> !SecurityContextUtil.hasRole("ADMINISTRATOR") && !SecurityContextUtil.hasPermission(folder, "READ"));
......
......@@ -55,6 +55,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -362,12 +363,12 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* Path)
*/
@Override
public List<RepositoryFile> getFiles(Path folderPath) throws InvalidRepositoryPathException {
public List<RepositoryFile> getFiles(Path folderPath, final Sort sort) throws InvalidRepositoryPathException {
RepositoryFolder folder = getFolder(folderPath);
if (folder == null) {
return Collections.emptyList();
}
return (List<RepositoryFile>) repositoryFilePersistence.findAll(QRepositoryFile.repositoryFile.folder.eq(folder));
return (List<RepositoryFile>) repositoryFilePersistence.findAll(QRepositoryFile.repositoryFile.folder.eq(folder), sort);
}
/*
......@@ -377,15 +378,15 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* .Path)
*/
@Override
public List<RepositoryFolder> getFolders(Path root) throws InvalidRepositoryPathException {
public List<RepositoryFolder> getFolders(Path root, final Sort sort) throws InvalidRepositoryPathException {
PathValidator.checkValidPath(root);
if (root == null || "/".equals(root.normalize().toAbsolutePath().toString())) {
return (List<RepositoryFolder>) folderRepository.findAll(QRepositoryFolder.repositoryFolder.parent.isNull());
return (List<RepositoryFolder>) folderRepository.findAll(QRepositoryFolder.repositoryFolder.parent.isNull(), sort);
} else {
RepositoryFolder parent = getFolder(root);
return parent == null ? Collections.emptyList() :
// Load child folders
(List<RepositoryFolder>) folderRepository.findAll(QRepositoryFolder.repositoryFolder.parent.eq(parent));
(List<RepositoryFolder>) folderRepository.findAll(QRepositoryFolder.repositoryFolder.parent.eq(parent), sort);
}
}
......@@ -422,10 +423,10 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* file.Path)
*/
@Override
public Stream<RepositoryFile> streamFiles(Path root) throws InvalidRepositoryPathException {
public Stream<RepositoryFile> streamFiles(Path root, final Sort sort) throws InvalidRepositoryPathException {
PathValidator.checkValidPath(root);
BooleanExpression q = QRepositoryFile.repositoryFile.folder.in(listPaths(root));
return StreamSupport.stream(repositoryFilePersistence.findAll(q).spliterator(), false);
BooleanExpression q = QRepositoryFile.repositoryFile.folder.in(listPathsRecursively(root));
return StreamSupport.stream(repositoryFilePersistence.findAll(q, sort).spliterator(), false);
}
/*
......@@ -718,8 +719,8 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* String)
*/
@Override
public List<RepositoryImage> listImages(final Path repositoryPath) {
return repositoryImagePersistence.findByFolder(folderRepository.findByPath(repositoryPath.normalize().toAbsolutePath().toString()));
public List<RepositoryImage> listImages(final Path repositoryPath, final Sort sort) {
return repositoryImagePersistence.findByFolder(folderRepository.findByPath(repositoryPath.normalize().toAbsolutePath().toString()), sort);
}
/*
......@@ -744,7 +745,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* String)
*/
@Override
public List<RepositoryFolder> listPaths(final Path prefix) throws InvalidRepositoryPathException {
public List<RepositoryFolder> listPathsRecursively(final Path prefix) throws InvalidRepositoryPathException {
PathValidator.checkValidPath(prefix);
ArrayList<RepositoryFolder> folders = new ArrayList<>();
......@@ -793,7 +794,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
throw new InvalidRepositoryPathException("Folder with this name already exists. Path was " + nPath);
}
for (final RepositoryFolder folder : Lists.reverse(listPaths(currentPath))) {
for (final RepositoryFolder folder : Lists.reverse(listPathsRecursively(currentPath))) {
final Path folderPath = folder.getFolderPath();
final Path relative = cPath.relativize(folderPath);
......
......@@ -92,7 +92,7 @@ public class FileRepositoryDirectoryTest extends RepositoryServiceTest {
repoFiles.add(fileRepoService.addFile(PATH.resolve("bb/cc/ee"), 2 + originalFilename, contentType, SOME_BYTES, null));
repoFiles.add(fileRepoService.addFile(PATH.resolve("bb/cc/ee/ff"), originalFilename, contentType, SOME_BYTES, null));
List<Path> paths = fileRepoService.listPaths(PATH.resolve("aa")).stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList());
List<Path> paths = fileRepoService.listPathsRecursively(PATH.resolve("aa")).stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList());
assertThat("Paths definitely exist", paths, notNullValue());
// paths.stream().forEach(path -> {
// System.err.println("Distinct path=" + path);
......@@ -100,7 +100,7 @@ public class FileRepositoryDirectoryTest extends RepositoryServiceTest {
assertThat("Paths definitely exist", paths, hasSize(4));
assertThat("Paths not in correct order", paths, contains(PATH.resolve("aa"), PATH.resolve("aa/dd"), PATH.resolve("aa/dd/ee"), PATH.resolve("aa/dd/ee/gg")));
paths = fileRepoService.listPaths(PATH.resolve("bb")).stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList());
paths = fileRepoService.listPathsRecursively(PATH.resolve("bb")).stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList());
assertThat("Paths definitely exist", paths, notNullValue());
// paths.stream().forEach(path -> {
// System.err.println("Distinct path=" + path);
......@@ -135,10 +135,10 @@ public class FileRepositoryDirectoryTest extends RepositoryServiceTest {
repoFiles.add(fileRepoService.addFile(PATH.resolve("aaaa/bbb"), 1 + originalFilename, contentType, SOME_BYTES, null));
repoFiles.add(fileRepoService.addFile(PATH.resolve("aaaa/cc"), 1 + originalFilename, contentType, SOME_BYTES, null));
assertThat(fileRepoService.listPaths(PATH.resolve("aa")), hasSize(1)); // self
assertThat(fileRepoService.listPaths(PATH.resolve("aaa")), hasSize(1)); // self
assertThat(fileRepoService.listPaths(PATH.resolve("aaaa")), hasSize(4)); // self + 3
assertThat(fileRepoService.listPaths(PATH.resolve("aaaa/bb")), hasSize(1)); // self
assertThat(fileRepoService.listPathsRecursively(PATH.resolve("aa")), hasSize(1)); // self
assertThat(fileRepoService.listPathsRecursively(PATH.resolve("aaa")), hasSize(1)); // self
assertThat(fileRepoService.listPathsRecursively(PATH.resolve("aaaa")), hasSize(4)); // self + 3
assertThat(fileRepoService.listPathsRecursively(PATH.resolve("aaaa/bb")), hasSize(1)); // self
for (final RepositoryFile repoFile : repoFiles) {
fileRepoService.removeFile(repoFile);
......@@ -170,18 +170,18 @@ public class FileRepositoryDirectoryTest extends RepositoryServiceTest {
repoFiles.add(fileRepoService.addFile(PATH.resolve("aa/dd/ee/gg"), 1 + originalFilename, contentType, SOME_BYTES, null));
repoFiles.add(fileRepoService.addFile(PATH.resolve("aa/dd/ee/gg"), 2 + originalFilename, contentType, SOME_BYTES, null));
List<RepositoryFolder> paths = fileRepoService.listPaths(PATH.resolve("aa"));
List<RepositoryFolder> paths = fileRepoService.listPathsRecursively(PATH.resolve("aa"));
assertThat("Paths definitely exist", paths, notNullValue());
assertThat("Paths definitely exist", paths, hasSize(4));
assertThat("Paths not in correct order", paths.stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList()), contains(PATH.resolve("aa"), PATH.resolve("aa/dd"), PATH.resolve("aa/dd/ee"), PATH.resolve("aa/dd/ee/gg")));
fileRepoService.renamePath(PATH.resolve("aa/dd/ee/gg"), PATH.resolve("aa/dd/ee/zz"));
assertThat("Paths not renamed correctly", fileRepoService.listPaths(PATH.resolve("aa/dd/ee")).stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList()), contains(PATH.resolve("aa/dd/ee"), PATH.resolve("aa/dd/ee/zz")));
assertThat("Paths not renamed correctly", fileRepoService.listPathsRecursively(PATH.resolve("aa/dd/ee")).stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList()), contains(PATH.resolve("aa/dd/ee"), PATH.resolve("aa/dd/ee/zz")));
fileRepoService.renamePath(PATH.resolve("aa/dd"), PATH.resolve("aa/zz"));
assertThat("Paths not renamed correctly", fileRepoService.listPaths(PATH.resolve("aa/zz")).stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList()), contains(PATH.resolve("aa/zz"), PATH.resolve("aa/zz/ee"), PATH.resolve("aa/zz/ee/zz")));
assertThat("Paths not renamed correctly", fileRepoService.listPathsRecursively(PATH.resolve("aa/zz")).stream().map(rf -> rf.getFolderPath()).collect(Collectors.toList()), contains(PATH.resolve("aa/zz"), PATH.resolve("aa/zz/ee"), PATH.resolve("aa/zz/ee/zz")));
for (final RepositoryFolder repositoryFolder : fileRepoService.listPaths(PATH.resolve("aa"))) {
for (final RepositoryFile repoFile : fileRepoService.getFiles(repositoryFolder.getFolderPath())) {
for (final RepositoryFolder repositoryFolder : fileRepoService.listPathsRecursively(PATH.resolve("aa"))) {
for (final RepositoryFile repoFile : fileRepoService.getFiles(repositoryFolder.getFolderPath(), RepositoryFile.DEFAULT_SORT)) {
fileRepoService.removeFile(repoFile);
}
}
......
......@@ -101,7 +101,7 @@ public class FileRepositoryUpdateTest extends RepositoryServiceTest {
*/
@Test
public void fetchFilesByPath() throws NoSuchRepositoryFileException, InvalidRepositoryPathException {
final List<RepositoryFile> repoFiles = repositoryService.getFiles(initialPath);
final List<RepositoryFile> repoFiles = repositoryService.getFiles(initialPath, RepositoryFile.DEFAULT_SORT);
assertThat("List should have one element", repoFiles.size(), is(1));
......
......@@ -84,7 +84,7 @@ public class RepositoryFolderTest extends RepositoryServiceTest {
RepositoryFolder folder = repositoryService.ensureFolder(path);
assertThat(folder, notNullValue());
List<RepositoryFolder> folders = repositoryService.listPaths(Paths.get("/bbbb"));
List<RepositoryFolder> folders = repositoryService.listPathsRecursively(Paths.get("/bbbb"));
assertThat(folders, hasSize(4));
}
......@@ -115,7 +115,7 @@ public class RepositoryFolderTest extends RepositoryServiceTest {
Path path = Paths.get("/bbbb/cccc/dddd/eeee");
RepositoryFolder folder = repositoryService.ensureFolder(path);
assertThat(folder, notNullValue());
repositoryService.listPaths(ROOT).forEach(f -> System.err.println("0 " + f));
repositoryService.listPathsRecursively(ROOT).forEach(f -> System.err.println("0 " + f));
Path newPath = path.getRoot().resolve("bbbb");
repositoryService.renamePath(path, newPath);
......@@ -126,7 +126,7 @@ public class RepositoryFolderTest extends RepositoryServiceTest {
Path path = Paths.get("/bbbb/cccc/dddd/eeee");
RepositoryFolder folder = repositoryService.ensureFolder(path);
assertThat(folder, notNullValue());
repositoryService.listPaths(ROOT).forEach(f -> System.err.println("0 " + f));
repositoryService.listPathsRecursively(ROOT).forEach(f -> System.err.println("0 " + f));
Path newPath = path.getParent().getParent().resolve("dddd");
repositoryService.renamePath(path, newPath);
......
......@@ -36,11 +36,13 @@ import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.service.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
......@@ -194,11 +196,11 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
LOG.debug("Listing files in path={}", root);
final ArrayList<FtpFile> all = new ArrayList<>();
all.addAll(repositoryService.getFolders(root).stream().peek(rf -> {
all.addAll(repositoryService.getFolders(root, RepositoryFolder.DEFAULT_SORT).stream().peek(rf -> {
// System.err.println("repoFolder " + rf.getPath());
}).map(rf -> directory(rf.getFolderPath(), session)).collect(Collectors.toList()));
all.addAll(repositoryService.getFiles(root).stream().peek(rf -> {
all.addAll(repositoryService.getFiles(root, RepositoryFile.DEFAULT_SORT).stream().peek(rf -> {
// System.err.println("repoFile " + rf.getStorageFullPath());
}).map(rf -> file(rf, session)).collect(Collectors.toList()));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment