Small optimisation in getResourceAtPath(). Should pay more dividents once DTOs are...
authorDimitris Routsis <droutsis@ebs.gr>
Thu, 16 Dec 2010 10:10:58 +0000 (12:10 +0200)
committerDimitris Routsis <droutsis@ebs.gr>
Thu, 16 Dec 2010 10:10:58 +0000 (12:10 +0200)
src/gr/ebs/gss/server/ejb/ExternalAPIBean.java
src/gr/ebs/gss/server/ejb/GSSDAO.java
src/gr/ebs/gss/server/ejb/GSSDAOBean.java

index 8beefa0..cc4481f 100644 (file)
@@ -153,6 +153,12 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                }
        }
 
+       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)
@@ -818,10 +824,12 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        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");
                }
@@ -829,14 +837,14 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                // 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;
@@ -876,14 +884,14 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
         *             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 {
index 73fd152..2095b57 100644 (file)
@@ -95,6 +95,16 @@ public interface GSSDAO {
        public List<Group> getGroups(Long userId) throws ObjectNotFoundException;
 
        /**
+        * Retrieves the root folder id for the specified user. The caller must ensure
+        * that the userId exists.
+        *
+        * @param userId
+        * @return Long The id
+        * @throws ObjectNotFoundException if no Folder was found
+        */
+       public Long getRootFolderId(final Long userId) throws ObjectNotFoundException;
+       
+       /**
         * Retrieves the root folder for the specified user. The caller must ensure
         * that the userId exists.
         *
index ed1cb63..bf245a4 100644 (file)
@@ -65,6 +65,20 @@ public class GSSDAOBean implements GSSDAO {
        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)