move isCache/cleanCache to BasicLibrary, improve filter()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / CacheLibrary.java
index 019acd210d1fb3b2263db468e2c21e6e6bfd1d69..9c83e62f76ceb388125cbc73d8769b296513b211 100644 (file)
@@ -3,12 +3,15 @@ package be.nikiroo.fanfix.library;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.List;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.UiConfig;
+import be.nikiroo.fanfix.bundles.UiConfigBundle;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.Progress;
 
@@ -25,19 +28,18 @@ public class CacheLibrary extends BasicLibrary {
        /**
         * Create a cache library around the given one.
         * <p>
-        * It will return the same result, but those will be saved to disk at the
-        * same time to be fetched quicker the next time.
+        * It will return the same result, but those will be saved to disk at the same
+        * time to be fetched quicker the next time.
         * 
-        * @param cacheDir
-        *            the cache directory where to save the files to disk
-        * @param lib
-        *            the original library to wrap
+        * @param cacheDir the cache directory where to save the files to disk
+        * @param lib      the original library to wrap
+        * @param config   the configuration used to know which kind of default
+        *                 {@link OutputType} to use for images and non-images stories
         */
-       public CacheLibrary(File cacheDir, BasicLibrary lib) {
-               this.cacheLib = new LocalLibrary(cacheDir, Instance.getUiConfig()
-                               .getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance
-                               .getUiConfig().getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE),
-                               true);
+       public CacheLibrary(File cacheDir, BasicLibrary lib, UiConfigBundle config) {
+               this.cacheLib = new LocalLibrary(cacheDir, //
+                               config.getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE),
+                               config.getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), true);
                this.lib = lib;
        }
 
@@ -62,7 +64,7 @@ public class CacheLibrary extends BasicLibrary {
                }
 
                pg.done();
-               return metas;
+               return new ArrayList<MetaData>(metas);
        }
 
        @Override
@@ -95,7 +97,7 @@ public class CacheLibrary extends BasicLibrary {
                                updateInfo(cacheLib.getInfo(luid));
                                pgImport.done();
                        } catch (IOException e) {
-                               Instance.getTraceHandler().error(e);
+                               Instance.getInstance().getTraceHandler().error(e);
                        }
 
                        pgImport.done();
@@ -216,11 +218,17 @@ public class CacheLibrary extends BasicLibrary {
        @Override
        protected void updateInfo(MetaData meta) throws IOException {
                if (meta != null && metas != null) {
+                       boolean changed = false;
                        for (int i = 0; i < metas.size(); i++) {
                                if (metas.get(i).getLuid().equals(meta.getLuid())) {
                                        metas.set(i, meta);
+                                       changed = true;
                                }
                        }
+
+                       if (!changed) {
+                               metas.add(meta);
+                       }
                }
 
                cacheLib.updateInfo(meta);
@@ -309,15 +317,7 @@ public class CacheLibrary extends BasicLibrary {
                invalidateInfo(luid);
        }
 
-       /**
-        * Check if the {@link Story} denoted by this Library UID is present in the
-        * cache.
-        * 
-        * @param luid
-        *            the Library UID
-        * 
-        * @return TRUE if it is
-        */
+       @Override
        public boolean isCached(String luid) {
                try {
                        return cacheLib.getInfo(luid) != null;
@@ -326,18 +326,7 @@ public class CacheLibrary extends BasicLibrary {
                }
        }
 
-       /**
-        * Clear the {@link Story} from the cache.
-        * <p>
-        * The next time we try to retrieve the {@link Story}, it may be required to
-        * cache it again.
-        * 
-        * @param luid
-        *            the story to clear
-        * 
-        * @throws IOException
-        *             in case of I/O error
-        */
+       @Override
        public void clearFromCache(String luid) throws IOException {
                if (isCached(luid)) {
                        cacheLib.delete(luid);
@@ -345,7 +334,7 @@ public class CacheLibrary extends BasicLibrary {
        }
 
        @Override
-       public Story imprt(URL url, Progress pg) throws IOException {
+       public MetaData imprt(URL url, Progress pg) throws IOException {
                if (pg == null) {
                        pg = new Progress();
                }
@@ -356,13 +345,13 @@ public class CacheLibrary extends BasicLibrary {
                pg.addProgress(pgImprt, 7);
                pg.addProgress(pgCache, 3);
 
-               Story story = lib.imprt(url, pgImprt);
-               cacheLib.save(story, story.getMeta().getLuid(), pgCache);
-
-               updateInfo(story.getMeta());
-
+               MetaData meta = lib.imprt(url, pgImprt);
+               updateInfo(meta);
+               
+               clearFromCache(meta.getLuid());
+               
                pg.done();
-               return story;
+               return meta;
        }
 
        // All the following methods are only used by Save and Delete in