Creating folders is now working.
[photos] / src / main / java / org / wamblee / photos / model / filesystem / FileSystemAlbum.java
index bfff860137c6ad68fafd4e9d3f0ec67436adc2a7..011381bf04cc04fa5293c12b6b92d0f6d3f0aeb7 100644 (file)
@@ -119,7 +119,7 @@ public class FileSystemAlbum implements Album {
      *
      * @param aDir   Directory where the album is located.
      * @param aPath  Path that this album represents.
-     * @param aCache Cache to use.
+     * @param aCache Cache to use. Note that a cache usedin one album hierarachy should not be used in another.
      * @throws IOException
      */
     public FileSystemAlbum(File aDir, String aPath, Cache<String, ArrayList<PhotoEntry>> aCache) throws IOException {
@@ -129,7 +129,7 @@ public class FileSystemAlbum implements Album {
         }
         _dir = aDir;
         _path = aPath;
-        _entries = new CachedObject<String, ArrayList<PhotoEntry>>(aCache, aPath, new AlbumComputation(this));
+        _entries = new CachedObject<String, ArrayList<PhotoEntry>>(aCache, "fs:" + aPath, new AlbumComputation(this));
     }
 
     /**
@@ -496,28 +496,32 @@ public class FileSystemAlbum implements Album {
      * @see org.wamblee.photos.database.Album#addAlbum(java.lang.String)
      */
     public void addAlbum(String aId) throws IOException {
-        PhotoEntry entry = find(aId);
-        if (entry != null) {
-            throw new IOException("Entry already exists in album " + getId() +
-                    " : " + aId);
-        }
-        // Entry not yet found. Try to create it.
-        File albumDir = new File(_dir, aId);
-        if (!albumDir.mkdir()) {
-            throw new IOException("Could not create album: " + aId);
-        }
-        File photosDir = new File(albumDir, PHOTOS_DIR);
-        if (!photosDir.mkdir()) {
-            throw new IOException("Could  not create photo storage dir: " + photosDir.getPath());
-        }
-        File thumbnailsDir = new File(albumDir, THUMBNAILS_DIR);
-        if (!thumbnailsDir.mkdir()) {
-            throw new IOException("Coul dnot create thumbnails storage dir: " + thumbnailsDir.getPath());
+        try {
+            PhotoEntry entry = find(aId);
+            if (entry != null) {
+                throw new IOException("Entry already exists in album " + getId() +
+                        " : " + aId);
+            }
+            // Entry not yet found. Try to create it.
+            File albumDir = new File(_dir, aId);
+            if (!albumDir.mkdir()) {
+                throw new IOException("Could not create album: " + aId);
+            }
+            File photosDir = new File(albumDir, PHOTOS_DIR);
+            if (!photosDir.mkdir()) {
+                throw new IOException("Could  not create photo storage dir: " + photosDir.getPath());
+            }
+            File thumbnailsDir = new File(albumDir, THUMBNAILS_DIR);
+            if (!thumbnailsDir.mkdir()) {
+                throw new IOException("Coul dnot create thumbnails storage dir: " + thumbnailsDir.getPath());
+            }
+            String newPath = _path + "/" + aId;
+            newPath = newPath.replaceAll("//", "/");
+            FileSystemAlbum album = new FileSystemAlbum(albumDir, newPath, _entries.getCache());
+            addEntry(album);
+        } finally {
+            _entries.invalidate();
         }
-        String newPath = _path + "/" + aId;
-        newPath = newPath.replaceAll("//", "/");
-        FileSystemAlbum album = new FileSystemAlbum(albumDir, newPath, _entries.getCache());
-        addEntry(album);
     }
 
     /*
@@ -526,16 +530,20 @@ public class FileSystemAlbum implements Album {
      * @see org.wamblee.photos.database.Album#removeAlbum(java.lang.String)
      */
     public void removeEntry(String aId) throws IOException {
-        PhotoEntry entry = find(aId);
-        if (entry == null) {
-            throw new IOException("Entry " + aId + " not found.");
-        }
-        if (entry instanceof FileSystemAlbum) {
-            // album.
-            removeAlbum((FileSystemAlbum) entry);
-        } else {
-            // regular photo
-            removePhoto(entry);
+        try {
+            PhotoEntry entry = find(aId);
+            if (entry == null) {
+                throw new IOException("Entry " + aId + " not found.");
+            }
+            if (entry instanceof FileSystemAlbum) {
+                // album.
+                removeAlbum((FileSystemAlbum) entry);
+            } else {
+                // regular photo
+                removePhoto(entry);
+            }
+        } finally {
+            _entries.invalidate();
         }
     }