Add methods to delete a temporary image.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 6a90c7a..09b0cbf 100644 (file)
@@ -39,6 +39,7 @@ import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityListener;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
@@ -173,6 +174,9 @@ public class Core implements IdentityListener, UpdateListener {
        /** All known images. */
        private Map<String, Image> images = new HashMap<String, Image>();
 
+       /** All temporary images. */
+       private Map<String, TemporaryImage> temporaryImages = new HashMap<String, TemporaryImage>();
+
        /**
         * Creates a new core.
         *
@@ -799,6 +803,20 @@ public class Core implements IdentityListener, UpdateListener {
                }
        }
 
+       /**
+        * Returns the temporary image with the given ID.
+        *
+        * @param imageId
+        *            The ID of the temporary image
+        * @return The temporary image, or {@code null} if there is no temporary
+        *         image with the given ID
+        */
+       public TemporaryImage getTemporaryImage(String imageId) {
+               synchronized (temporaryImages) {
+                       return temporaryImages.get(imageId);
+               }
+       }
+
        //
        // ACTIONS
        //
@@ -1320,7 +1338,6 @@ public class Core implements IdentityListener, UpdateListener {
                }
 
                /* load albums. */
-               Map<String, Album> albums = new HashMap<String, Album>();
                List<Album> topLevelAlbums = new ArrayList<Album>();
                while (true) {
                        String albumPrefix = sonePrefix + "/Albums/" + albums.size();
@@ -1328,17 +1345,16 @@ public class Core implements IdentityListener, UpdateListener {
                        if (albumId == null) {
                                break;
                        }
-                       String albumName = configuration.getStringValue(albumPrefix + "/Name").getValue(null);
+                       String albumTitle = configuration.getStringValue(albumPrefix + "/Title").getValue(null);
                        String albumDescription = configuration.getStringValue(albumPrefix + "/Description").getValue(null);
                        String albumParentId = configuration.getStringValue(albumPrefix + "/Parent").getValue(null);
-                       if ((albumName == null) || (albumDescription == null)) {
+                       if ((albumTitle == null) || (albumDescription == null)) {
                                logger.log(Level.WARNING, "Invalid album found, aborting load!");
                                return;
                        }
-                       Album album = new Album(albumId).setSone(sone).setName(albumName).setDescription(albumDescription);
-                       albums.put(albumId, album);
+                       Album album = getAlbum(albumId).setSone(sone).setTitle(albumTitle).setDescription(albumDescription);
                        if (albumParentId != null) {
-                               Album parentAlbum = albums.get(albumParentId);
+                               Album parentAlbum = getAlbum(albumParentId, false);
                                if (parentAlbum == null) {
                                        logger.log(Level.WARNING, "Invalid parent album ID: " + albumParentId);
                                        return;
@@ -1485,7 +1501,7 @@ public class Core implements IdentityListener, UpdateListener {
                        for (Album album : albums) {
                                String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
                                configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
-                               configuration.getStringValue(albumPrefix + "/Name").setValue(album.getName());
+                               configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
                                configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
                                configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent() == null ? null : album.getParent().getId());
                        }
@@ -1777,6 +1793,48 @@ public class Core implements IdentityListener, UpdateListener {
        }
 
        /**
+        * Creates a new temporary image.
+        *
+        * @param mimeType
+        *            The MIME type of the temporary image
+        * @param imageData
+        *            The encoded data of the image
+        * @return The temporary image
+        */
+       public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData) {
+               TemporaryImage temporaryImage = new TemporaryImage();
+               temporaryImage.setMimeType(mimeType).setImageData(imageData);
+               synchronized (temporaryImages) {
+                       temporaryImages.put(temporaryImage.getId(), temporaryImage);
+               }
+               return temporaryImage;
+       }
+
+       /**
+        * Deletes the given temporary image.
+        *
+        * @param temporaryImage
+        *            The temporary image to delete
+        */
+       public void deteleTemporaryImage(TemporaryImage temporaryImage) {
+               Validation.begin().isNotNull("Temporary Image", temporaryImage).check();
+               deleteTemporaryImage(temporaryImage.getId());
+       }
+
+       /**
+        * Deletes the temporary image with the given ID.
+        *
+        * @param imageId
+        *            The ID of the temporary image to delete
+        */
+       public void deleteTemporaryImage(String imageId) {
+               Validation.begin().isNotNull("Temporary Image ID", imageId).check();
+               synchronized (temporaryImages) {
+                       temporaryImages.remove(imageId);
+               }
+       }
+
+       /**
         * Starts the core.
         */
        public void start() {