weblib: fix delete and imprt
[fanfix.git] / src / be / nikiroo / fanfix / library / BasicLibrary.java
index d435f8d7ec7fd3e2f09242339eefb9ff6fa74d93..8b72f19b3a92ad1c1d7ada09d915440596330ee2 100644 (file)
@@ -4,11 +4,9 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Collections;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.TreeMap;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
@@ -43,11 +41,11 @@ abstract public class BasicLibrary {
                READ_WRITE,
                /** The library is ready, but read-only. */
                READ_ONLY,
-               /** The library is invalid (not correctly set up). */
-               INVALID,
                /** You are not allowed to access this library. */
                UNAUTHORIZED,
-               /** The library is currently out of commission. */
+               /** The library is invalid, and will never work as is. */
+               INVALID,
+               /** The library is currently out of commission, but may work later. */
                UNAVAILABLE;
 
                /**
@@ -122,12 +120,24 @@ abstract public class BasicLibrary {
         */
        public abstract Image getCover(String luid) throws IOException;
 
-       // TODO: ensure it is the main used interface
+       /**
+        * Retrieve the list of {@link MetaData} known by this {@link BasicLibrary}
+        * in a easy-to-filter version.
+        * 
+        * @param pg
+        *            the optional {@link Progress}
+        * @return the list of {@link MetaData} as a {@link MetaResultList} you can
+        *         query
+        * @throws IOException
+        *             in case of I/O eror
+        */
        public MetaResultList getList(Progress pg) throws IOException {
+               // TODO: ensure it is the main used interface
+
                return new MetaResultList(getMetas(pg));
        }
 
-       // TODO: make something for (normal and custom) not-story covers
+       // TODO: make something for (normal and custom) non-story covers
 
        /**
         * Return the cover image associated to this source.
@@ -301,7 +311,7 @@ abstract public class BasicLibrary {
         * 
         * @return the next luid
         */
-       protected abstract int getNextId();
+       protected abstract String getNextId();
 
        /**
         * Delete the target {@link Story}.
@@ -378,6 +388,9 @@ abstract public class BasicLibrary {
        }
 
        /**
+        * @return the same as getList()
+        * @throws IOException
+        *             in case of I/O error
         * @deprecated please use {@link BasicLibrary#getList()} and
         *             {@link MetaResultList#getSources()} instead.
         */
@@ -387,6 +400,9 @@ abstract public class BasicLibrary {
        }
 
        /**
+        * @return the same as getList()
+        * @throws IOException
+        *             in case of I/O error
         * @deprecated please use {@link BasicLibrary#getList()} and
         *             {@link MetaResultList#getSourcesGrouped()} instead.
         */
@@ -396,6 +412,9 @@ abstract public class BasicLibrary {
        }
 
        /**
+        * @return the same as getList()
+        * @throws IOException
+        *             in case of I/O error
         * @deprecated please use {@link BasicLibrary#getList()} and
         *             {@link MetaResultList#getAuthors()} instead.
         */
@@ -405,9 +424,13 @@ abstract public class BasicLibrary {
        }
 
        /**
+        * @return the same as getList()
+        * @throws IOException
+        *             in case of I/O error
         * @deprecated please use {@link BasicLibrary#getList()} and
         *             {@link MetaResultList#getAuthorsGrouped()} instead.
         */
+       @Deprecated
        public Map<String, List<String>> getAuthorsGrouped() throws IOException {
                return getList().getAuthorsGrouped();
        }
@@ -572,6 +595,28 @@ abstract public class BasicLibrary {
         *             in case of I/O error
         */
        public MetaData imprt(URL url, Progress pg) throws IOException {
+               return imprt(url, null, pg);
+       }
+
+       /**
+        * Import the {@link Story} at the given {@link URL} into the
+        * {@link BasicLibrary}.
+        * 
+        * @param url
+        *            the {@link URL} to import
+        * @param luid
+        *            the LUID to use
+        * @param pg
+        *            the optional progress reporter
+        * 
+        * @return the imported Story {@link MetaData}
+        * 
+        * @throws UnknownHostException
+        *             if the host is not supported
+        * @throws IOException
+        *             in case of I/O error
+        */
+       MetaData imprt(URL url, String luid, Progress pg) throws IOException {
                if (pg == null)
                        pg = new Progress();
 
@@ -586,7 +631,7 @@ abstract public class BasicLibrary {
                        throw new UnknownHostException("" + url);
                }
 
-               Story story = save(support.process(pgProcess), pgSave);
+               Story story = save(support.process(pgProcess), luid, pgSave);
                pg.done();
 
                return story.getMeta();
@@ -718,7 +763,7 @@ abstract public class BasicLibrary {
                pg.setName("Saving story");
 
                if (luid == null || luid.isEmpty()) {
-                       meta.setLuid(String.format("%03d", getNextId()));
+                       meta.setLuid(getNextId());
                } else {
                        meta.setLuid(luid);
                }
@@ -901,4 +946,49 @@ abstract public class BasicLibrary {
 
                pg.done();
        }
+
+       /**
+        * Describe a {@link Story} from its {@link MetaData} and return a list of
+        * title/value that represent this {@link Story}.
+        * 
+        * @param meta
+        *            the {@link MetaData} to represent
+        * 
+        * @return the information, translated and sorted
+        */
+       static public Map<String, String> getMetaDesc(MetaData meta) {
+               Map<String, String> metaDesc = new LinkedHashMap<String, String>();
+
+               // TODO: i18n
+
+               StringBuilder tags = new StringBuilder();
+               for (String tag : meta.getTags()) {
+                       if (tags.length() > 0) {
+                               tags.append(", ");
+                       }
+                       tags.append(tag);
+               }
+
+               // TODO: i18n
+               metaDesc.put("Author", meta.getAuthor());
+               metaDesc.put("Published on", meta.getPublisher());
+               metaDesc.put("Publication date", meta.getDate());
+               metaDesc.put("Creation date", meta.getCreationDate());
+               String count = "";
+               if (meta.getWords() > 0) {
+                       count = StringUtils.formatNumber(meta.getWords());
+               }
+               if (meta.isImageDocument()) {
+                       metaDesc.put("Number of images", count);
+               } else {
+                       metaDesc.put("Number of words", count);
+               }
+               metaDesc.put("Source", meta.getSource());
+               metaDesc.put("Subject", meta.getSubject());
+               metaDesc.put("Language", meta.getLang());
+               metaDesc.put("Tags", tags.toString());
+               metaDesc.put("URL", meta.getUrl());
+
+               return metaDesc;
+       }
 }