Methoden zum Laden eines Folders anhand seines Pfades (Ticket 1404).

git-svn-id: https://svn.libreccm.org/ccm/trunk@2054 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2013-01-29 10:46:17 +00:00
parent fa2421055e
commit c5886fb8da
1 changed files with 66 additions and 0 deletions

View File

@ -802,4 +802,70 @@ public class Folder extends ContentItem {
} }
return folder; return folder;
} }
/**
* Retrieves a folder by its path from a given content section.
*
* @param section The content section from which the folder should be retrieved.
* @param path The path of the folder, relative to the content section.
* @return The folder with the given path from the provided content section. If there is no such folder,
* {@code null} is returned. It is up to the caller to check the returned value for {@code null} and take
* appropriate actions.
*/
public static Folder retrieveFolder(final ContentSection section, final String path) {
if (section == null) {
throw new IllegalArgumentException("No content section provided.");
}
if ((path == null) || path.isEmpty()) {
throw new IllegalArgumentException("No path provided.");
}
if (path.charAt(0) != '/') {
throw new IllegalArgumentException("Provided path is not an absolute path (starting with '/').");
}
final String[] pathTokens = path.split("/");
final Folder rootFolder = section.getRootFolder();
Folder folder = rootFolder;
for(String token : pathTokens) {
if ((token == null) || token.isEmpty() || "/".equals(token)) {
continue;
}
folder = getSubFolder(token, folder);
if (folder == null) {
break;
}
}
return folder;
}
private static Folder getSubFolder(final String name, final Folder fromFolder) {
final ItemCollection items = fromFolder.getItems();
items.addFolderFilter(true);
items.addNameFilter(name);
if (items.next()) {
return (Folder) items.getDomainObject();
} else {
return null;
}
}
/**
* Retrieves a folder of the current content section by its path. The path is given in a UNIX like synatax and must
* be an absolute path starting with '/'.
*
* @param path The path of the folder to retrieve relative to the current content section.
* @return The folder with the given path from the content section. If there is no such folder, {@code null} is
* returned. It is up to the caller to check the returned value for {@code null} and take appropriate actions.
*/
public static Folder retrieveFolder(final String path) {
return retrieveFolder(CMS.getContext().getContentSection(), path);
}
} }