Change to folder modification semantics: When anything inside a folder is updated...
[pithos] / src / gr / ebs / gss / server / ejb / ExternalAPIBean.java
index 188227d..da10aa0 100644 (file)
@@ -56,6 +56,7 @@ import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Locale;
 import java.util.Random;
 import java.util.Set;
 import java.util.StringTokenizer;
@@ -138,6 +139,17 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
         */
        private static Random random = new Random();
 
+       private void touchParentFolders(Folder folder, User modifiedBy, Date modificationDate) {
+               Folder f = folder;
+               while (f!=null) {
+                       AuditInfo ai = f.getAuditInfo();
+                       ai.setModifiedBy(modifiedBy);
+                       ai.setModificationDate(modificationDate);
+                       f.setAuditInfo(ai);
+                       f = f.getParent();
+               }
+       }
+
        @Override
        public FolderDTO getRootFolder(Long userId) throws ObjectNotFoundException {
                if (userId == null)
@@ -263,7 +275,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
        }
 
        @Override
-       public void createFolder(Long userId, Long parentId, String name)
+       public FolderDTO createFolder(Long userId, Long parentId, String name)
                        throws DuplicateNameException, ObjectNotFoundException, InsufficientPermissionsException {
                // Validate.
                if (userId == null)
@@ -290,7 +302,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                                        " to write to this folder");
 
                // Do the actual work.
-               createFolder(name, parent, creator);
+               return createFolder(name, parent, creator);
        }
 
        /**
@@ -299,8 +311,9 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
         * @param name
         * @param parent
         * @param creator
+        * @return the new folder
         */
-       private void createFolder(String name, Folder parent, User creator) {
+       private FolderDTO createFolder(String name, Folder parent, User creator) {
                Folder folder = new Folder();
                folder.setName(name);
                if (parent != null) {
@@ -316,6 +329,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                auditInfo.setModifiedBy(creator);
                auditInfo.setModificationDate(now);
                folder.setAuditInfo(auditInfo);
+               touchParentFolders(folder, auditInfo.getModifiedBy(), auditInfo.getModificationDate());
 
                if (parent != null)
                        for (Permission p : parent.getPermissions()) {
@@ -336,6 +350,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        folder.addPermission(permission);
                }
                dao.create(folder);
+               return folder.getDTO();
        }
 
        /*
@@ -365,6 +380,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                removeSubfolderFiles(folder);
                parent.removeSubfolder(folder);
                dao.delete(folder);
+               touchParentFolders(parent, user, new Date());
        }
 
        /**
@@ -405,7 +421,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
        }
 
        @Override
-       public void modifyFolder(Long userId, Long folderId, String folderName)
+       public FolderDTO modifyFolder(Long userId, Long folderId, String folderName)
                        throws InsufficientPermissionsException, ObjectNotFoundException, DuplicateNameException {
 
                // Validate.
@@ -429,6 +445,8 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                // Do the actual modification.
                folder.setName(folderName);
                dao.update(folder);
+               touchParentFolders(folder, user, new Date());
+               return folder.getDTO();
        }
 
        /*
@@ -443,6 +461,8 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        throw new ObjectNotFoundException("No user specified");
                if (StringUtils.isEmpty(name))
                        throw new ObjectNotFoundException("New group name is empty");
+               if (name.indexOf('/')>=0)
+                       throw new IllegalArgumentException("Character '/' is not allowed in group name");
                if (dao.existsGroup(userId, name))
                        throw new DuplicateNameException("A group with the name '" + name + "' already exists");
 
@@ -593,6 +613,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                for (final FileBody body : file.getBodies())
                        deleteActualFile(body.getStoredFilePath());
                dao.delete(file);
+               touchParentFolders(parent, user, new Date());
                indexFile(fileId, true);
        }
 
@@ -620,7 +641,11 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
 
                final User user = dao.getEntityById(User.class, userId);
                final FileHeader fh = dao.getEntityById(FileHeader.class, fileHeaderId);
+               final Folder parent = fh.getFolder();
+               if (parent == null)
+                       throw new ObjectNotFoundException("The specified file has no parent folder");
                user.addTag(fh, tag);
+               touchParentFolders(parent, user, new Date());
        }
 
        /* (non-Javadoc)
@@ -640,6 +665,10 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                if (fileId == null)
                        throw new ObjectNotFoundException("No file specified");
                FileHeader file = dao.getEntityById(FileHeader.class, fileId);
+               final Folder parent = file.getFolder();
+               if (parent == null)
+                       throw new ObjectNotFoundException("The specified file has no parent folder");
+
                User user = dao.getEntityById(User.class, userId);
                if (!file.hasWritePermission(user))
                        throw new InsufficientPermissionsException("User " + user.getId() + " cannot update file " + file.getName() + "(" + file.getId() + ")");
@@ -662,6 +691,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        while (st.hasMoreTokens())
                                new FileTag(user, file, st.nextToken().trim());
                }
+               touchParentFolders(parent, user, new Date());
 
                // Re-index the file if it was modified.
                if (name != null || tagSet != null)
@@ -1077,10 +1107,11 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
 
                file.setDeleted(true);
                dao.update(file);
+               touchParentFolders(parent, user, new Date());
        }
 
        @Override
-       public void moveFileToPath(Long userId, Long ownerId, Long fileId, String dest) throws ObjectNotFoundException, InsufficientPermissionsException, DuplicateNameException, GSSIOException, QuotaExceededException {
+       public void moveFileToPath(Long userId, Long ownerId, Long fileId, String dest) throws ObjectNotFoundException, InsufficientPermissionsException, QuotaExceededException {
                if (userId == null)
                        throw new ObjectNotFoundException("No user specified");
                if (ownerId == null)
@@ -1109,6 +1140,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        throw new ObjectNotFoundException("No destination file name specified");
 
                FileHeader file = dao.getEntityById(FileHeader.class, fileId);
+               Folder source = file.getFolder();
                Folder destination = dao.getEntityById(Folder.class, destId);
 
                User owner = dao.getEntityById(User.class, userId);
@@ -1142,6 +1174,8 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                }
                // move the file to the destination folder
                file.setFolder(destination);
+               touchParentFolders(source, owner, new Date());
+               touchParentFolders(destination, owner, new Date());
        }
 
        @Override
@@ -1206,6 +1240,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
 
                file.setDeleted(false);
                dao.update(file);
+               touchParentFolders(parent, user, new Date());
        }
 
        @Override
@@ -1220,6 +1255,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        throw new InsufficientPermissionsException("You don't have the necessary permissions");
                folder.setDeleted(true);
                dao.update(folder);
+               touchParentFolders(folder, user, new Date());
                for (FileHeader file : folder.getFiles())
                        moveFileToTrash(userId, file.getId());
                for (Folder subFolder : folder.getSubfolders())
@@ -1245,6 +1281,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                for (Folder subFolder : folder.getSubfolders())
                        removeFolderFromTrash(userId, subFolder.getId());
                dao.update(folder);
+               touchParentFolders(folder, user, new Date());
        }
 
        @Override
@@ -1567,6 +1604,8 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        }
 
                        dao.update(file);
+                       Folder parent = file.getFolder();
+                       touchParentFolders(parent, user, new Date());
                }
        }
 
@@ -1797,7 +1836,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
         * @see gr.ebs.gss.server.ejb.ExternalAPI#moveFiles(java.lang.Long, java.util.List, java.lang.Long)
         */
        @Override
-       public void moveFiles(Long userId, List<Long> fileIds, Long destId) throws InsufficientPermissionsException, ObjectNotFoundException, DuplicateNameException, GSSIOException, QuotaExceededException {
+       public void moveFiles(Long userId, List<Long> fileIds, Long destId) throws InsufficientPermissionsException, ObjectNotFoundException, QuotaExceededException {
                for(Long l : fileIds){
                        FileHeader file = dao.getEntityById(FileHeader.class, l);
                        moveFile(userId, l, destId, file.getName());
@@ -1829,6 +1868,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        for (final FileBody body : file.getBodies())
                                filesToRemove.add(body.getStoredFilePath());
                        dao.delete(file);
+                       touchParentFolders(parent, user, new Date());
                }
                //then remove physical files if everything is ok
                for(String physicalFileName : filesToRemove)
@@ -1955,6 +1995,8 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                deleteActualFile(body.getStoredFilePath());
                header.getBodies().remove(body);
 
+               Folder parent = header.getFolder();
+               touchParentFolders(parent, user, new Date());
 
        }
 
@@ -2003,6 +2045,8 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                }
                header.getCurrentBody().setVersion(1);
 
+               Folder parent = header.getFolder();
+               touchParentFolders(parent, user, new Date());
        }
 
        /* (non-Javadoc)
@@ -2022,7 +2066,8 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                        if(header.isVersioned())
                                removeOldVersions(userId, fileId);
                        header.setVersioned(versioned);
-
+                       Folder parent = header.getFolder();
+                       touchParentFolders(parent, user, new Date());
                }
        }
 
@@ -2237,6 +2282,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                } catch (FileNotFoundException e) {
                        throw new GSSIOException(e);
                }
+               touchParentFolders(parent, owner, new Date());
                dao.flush();
                indexFile(file.getId(), false);
 
@@ -2275,6 +2321,8 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                } catch (FileNotFoundException e) {
                        throw new GSSIOException(e);
                }
+               Folder parent = file.getFolder();
+               touchParentFolders(parent, owner, new Date());
 
                indexFile(fileId, false);
                return file.getDTO();
@@ -2288,7 +2336,7 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
         */
        private String identifyMimeType(String filename) {
                if (filename.indexOf('.') != -1) {
-                       String extension = filename.substring(filename.lastIndexOf('.'));
+                       String extension = filename.substring(filename.lastIndexOf('.')).toLowerCase(Locale.ENGLISH);
                        if (".doc".equals(extension))
                                return "application/msword";
                        else if (".xls".equals(extension))
@@ -2297,6 +2345,16 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
                                return "application/vnd.ms-powerpoint";
                        else if (".pdf".equals(extension))
                                return "application/pdf";
+                       else if (".gif".equals(extension))
+                               return "image/gif";
+                       else if (".jpg".equals(extension) || ".jpeg".equals(extension) || ".jpe".equals(extension))
+                               return "image/jpeg";
+                       else if (".tiff".equals(extension) || ".tif".equals(extension))
+                               return "image/tiff";
+                       else if (".png".equals(extension))
+                               return "image/png";
+                       else if (".bmp".equals(extension))
+                               return "image/bmp";
                }
                // when all else fails assign the default mime type
                return DEFAULT_MIME_TYPE;