}
}
+ private Long getRootFolderId(Long userId) throws ObjectNotFoundException {
+ if (userId == null)
+ throw new ObjectNotFoundException("No user specified");
+ return dao.getRootFolderId(userId);
+ }
+
@Override
public FolderDTO getRootFolder(Long userId) throws ObjectNotFoundException {
if (userId == null)
return getRootFolder(owner.getId());
// Store the last element, since it requires special handling.
String lastElement = pathElements.remove(pathElements.size() - 1);
- FolderDTO cursor = getRootFolder(owner.getId());
+
+ Folder cursor = null;
+ Long rootFolderId = getRootFolderId(owner.getId());
// Traverse and verify the specified folder path.
for (String pathElement : pathElements) {
- cursor = getFolder(cursor.getId(), pathElement);
+ cursor = getFolder(cursor==null ? rootFolderId : cursor.getId(), pathElement);
if (cursor.isDeleted())
throw new ObjectNotFoundException("Folder " + cursor.getPath() + " not found");
}
// Use the lastElement to retrieve the actual resource.
Object resource = null;
try {
- FileHeaderDTO file = getFile(cursor.getId(), lastElement);
+ FileHeaderDTO file = getFile(cursor==null ? rootFolderId : cursor.getId(), lastElement);
if (ignoreDeleted && file.isDeleted())
throw new ObjectNotFoundException("Resource not found");
resource = file;
} catch (ObjectNotFoundException e) {
// Perhaps the requested resource is not a file, so
// check for folders as well.
- FolderDTO folder = getFolder(cursor.getId(), lastElement);
+ FolderDTO folder = getFolder(cursor==null ? rootFolderId : cursor.getId(), lastElement).getDTO();
if (ignoreDeleted && folder.isDeleted())
throw new ObjectNotFoundException("Resource not found");
resource = folder;
* found, with the exception message mentioning the precise
* problem
*/
- private FolderDTO getFolder(Long parentId, String name) throws ObjectNotFoundException {
+ private Folder getFolder(Long parentId, String name) throws ObjectNotFoundException {
if (parentId == null)
throw new ObjectNotFoundException("No parent folder specified");
if (StringUtils.isEmpty(name))
throw new ObjectNotFoundException("No folder specified");
Folder folder = dao.getFolder(parentId, name);
- return folder.getDTO();
+ return folder;
}
private FileHeaderDTO updateFileContents(Long userId, Long fileId, String mimeType, InputStream resourceInputStream) throws ObjectNotFoundException, GSSIOException, InsufficientPermissionsException, QuotaExceededException {
private EntityManager manager;
@Override
+ public Long getRootFolderId(final Long userId) throws ObjectNotFoundException {
+ try {
+ if (userId == null)
+ throw new ObjectNotFoundException("No user specified");
+ return (Long) manager .createQuery("select f.id from Folder f " +
+ "where f.owner.id=:ownerId and f.parent is null")
+ .setParameter("ownerId", userId)
+ .getSingleResult();
+ } catch (final NoResultException e) {
+ throw new ObjectNotFoundException("Root folder not found for user with id=" + userId);
+ }
+ }
+
+ @Override
public Folder getRootFolder(final Long userId) throws ObjectNotFoundException {
try {
if (userId == null)