use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.
the class FolderRepository method getHierarchy.
/**
* Get the folder hierarchy (list of its parents)
*
* @param folder the folder.
* @return A {@link Iterable} of {@link Folder} containing all its parents starting from home.
*/
default List<Folder> getHierarchy(final Folder folder) {
final List<Folder> hierarchy = new ArrayList<>();
String nextParentId = folder.getParentId();
while (nextParentId != null) {
try {
final Folder parent = this.getFolderById(nextParentId);
hierarchy.add(0, parent);
nextParentId = parent.getParentId();
} catch (final TDPException e) {
// as the user's home child
if (e.getCode().getHttpStatus() == 403) {
hierarchy.add(0, this.getHome());
break;
}
throw e;
}
}
return hierarchy;
}
use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.
the class FileSystemFolderRepository method renameFolder.
@Override
public Folder renameFolder(String folderId, String newName) {
final Folder folder = getFolderById(folderId);
if (folder == null) {
throw new IllegalArgumentException("Cannot rename a folder that cannot be found");
}
FolderPath folderToMovePath = FolderPath.deserializeFromString(folder.getPath());
if (folderToMovePath.isRoot()) {
throw new IllegalArgumentException("Cannot rename home folder");
}
if (newName.contains(PATH_SEPARATOR.toString())) {
throw new TDPException(ILLEGAL_FOLDER_NAME, build().put("name", newName));
}
FolderPath targetFolderPath = new FolderPath(folderToMovePath.getParent(), newName);
Path folderPath = pathsConverter.toPath(folderToMovePath);
Path newFolderPath = pathsConverter.toPath(targetFolderPath);
try {
FileUtils.moveDirectory(folderPath.toFile(), newFolderPath.toFile());
} catch (IOException e) {
throw new TDPException(UNABLE_TO_RENAME_FOLDER, e, build().put("path", folder.getPath()));
}
return getFolderById(toId(pathsConverter.toFolderPath(newFolderPath)));
}
use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.
the class FolderAPITest method should_rename_folder.
@Test
public void should_rename_folder() throws IOException {
// given
createFolder("beer", home.getId());
final Folder beer = getFolder(home.getId(), "beer");
final String name = "new beer";
// when
final Response response = //
given().body(//
name).when().put("/api/folders/{id}/name", beer.getId());
// then
assertThat(response.getStatusCode(), is(200));
final Folder updatedBeer = getFolder(home.getId(), name);
assertNotNull(updatedBeer);
}
use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.
the class FolderAPITest method shouldFolderMetadataWithHierarchy.
@Test
public void shouldFolderMetadataWithHierarchy() throws Exception {
// given
// HOME
// ___________|____________
// | |
// first second
// ____|____ |
// | | |
// first child 1 first child 2 second child
// |
// |
// second child child
createFolder("first", home.getId());
createFolder("second", home.getId());
final Folder firstFolder = getFolder(home.getId(), "first");
final Folder secondFolder = getFolder(home.getId(), "second");
createFolder("first child one", firstFolder.getId());
createFolder("first child two", firstFolder.getId());
createFolder("second child", secondFolder.getId());
final Folder secondChildFolder = getFolder(secondFolder.getId(), "second child");
createFolder("second child child", secondChildFolder.getId());
final Folder firstChildTwo = getFolder(firstFolder.getId(), "first child two");
// when
final Response response = //
given().expect().statusCode(200).log().ifError().when().get("/folders/{id}", firstChildTwo.getId());
// then
final FolderInfo infos = mapper.readValue(response.asString(), new TypeReference<FolderInfo>() {
});
final List<Folder> hierarchy = StreamSupport.stream(infos.getHierarchy().spliterator(), false).collect(toList());
assertThat(infos.getFolder(), equalTo(firstChildTwo));
assertThat(hierarchy, hasSize(2));
assertThat(hierarchy.get(0).getId(), equalTo(home.getId()));
assertThat(hierarchy.get(1).getId(), equalTo(firstFolder.getId()));
}
use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.
the class FolderAPITest method assertOnSearch.
private void assertOnSearch(final String searchQuery, final boolean strict, final int expectedSize) throws Exception {
// when
final Response response = //
given().queryParam("name", //
searchQuery).queryParam("strict", //
strict).when().get("/api/folders/search");
final List<Folder> folders = mapper.readValue(response.asString(), new TypeReference<List<Folder>>() {
});
// then
assertThat(folders, hasSize(expectedSize));
}
Aggregations