X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FBasicLibrary.java;h=f77d0edcef5ae64b186942be2a6048fa7eca7745;hb=8d45985b29359a84eaf3b0465374acbec3474faf;hp=099859dcfaffed075f30b3d716b790f644a43a09;hpb=d46b7b96f94e88a776bcd2dfd756549ffb300cc9;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/library/BasicLibrary.java b/src/be/nikiroo/fanfix/library/BasicLibrary.java index 099859d..f77d0ed 100644 --- a/src/be/nikiroo/fanfix/library/BasicLibrary.java +++ b/src/be/nikiroo/fanfix/library/BasicLibrary.java @@ -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; /** @@ -98,7 +96,7 @@ abstract public class BasicLibrary { * Do NOT alter this file. * * @param luid - * the Library UID of the story + * the Library UID of the story, can be NULL * @param pg * the optional {@link Progress} * @@ -122,6 +120,25 @@ abstract public class BasicLibrary { */ public abstract Image getCover(String luid) throws IOException; + /** + * 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) non-story covers + /** * Return the cover image associated to this source. *
@@ -142,7 +159,7 @@ abstract public class BasicLibrary {
return custom;
}
- List
+ * The returned list MUST be a copy, not the original one.
*
* @param pg
* the optional {@link Progress}
@@ -292,7 +311,7 @@ abstract public class BasicLibrary {
*
* @return the next luid
*/
- protected abstract int getNextId();
+ protected abstract String getNextId();
/**
* Delete the target {@link Story}.
@@ -338,284 +357,96 @@ abstract public class BasicLibrary {
}
/**
- * List all the known types (sources) of stories.
+ * Check if the {@link Story} denoted by this Library UID is present in the
+ * cache (if we have no cache, we default to true).
*
- * @return the sources
+ * @param luid
+ * the Library UID
*
- * @throws IOException
- * in case of IOException
+ * @return TRUE if it is
*/
- public synchronized List
- * Note that an empty item in the list means a non-grouped source (type) --
- * e.g., you could have for Source_1:
- *
- * If the number of author is not too high, only one group with an empty
- * name and all the authors will be returned.
- *
- * If not, the authors will be separated into groups:
- *
- * Cover images MAYBE not included.
- *
- * @return the stories
- *
+ * @return the same as getList()
* @throws IOException
- * in case of IOException
+ * in case of I/O error
+ * @deprecated please use {@link BasicLibrary#getList()} and
+ * {@link MetaResultList#getAuthors()} instead.
*/
- public synchronized List
- * Cover images not included.
- *
- * @param type
- * the type of story to retrieve, or NULL for all
- *
- * @return the stories
- *
+ * @return the same as getList()
* @throws IOException
- * in case of IOException
+ * in case of I/O error
+ * @deprecated please use {@link BasicLibrary#getList()} and
+ * {@link MetaResultList#getAuthorsGrouped()} instead.
*/
- public synchronized List
- * Cover images not included.
- *
- * @param author
- * the author of the stories to retrieve, or NULL for all
+ * Cover images MAYBE not included.
*
* @return the stories
*
* @throws IOException
* in case of IOException
*/
- public synchronized List
+ * Note that it will update both the cover and the resume in meta.
*
* @param luid
* the Library UID of the story
@@ -655,8 +488,7 @@ abstract public class BasicLibrary {
* @throws IOException
* in case of IOException
*/
- public synchronized Story getStory(String luid, Progress pg)
- throws IOException {
+ public Story getStory(String luid, Progress pg) throws IOException {
Progress pgMetas = new Progress();
Progress pgStory = new Progress();
if (pg != null) {
@@ -683,8 +515,12 @@ abstract public class BasicLibrary {
/**
* Retrieve a specific {@link Story}.
+ *
+ * Note that it will update both the cover and the resume in meta.
*
* @param luid
+ * the LUID of the story
+ * @param meta
* the meta of the story
* @param pg
* the optional progress reporter
@@ -694,8 +530,7 @@ abstract public class BasicLibrary {
* @throws IOException
* in case of IOException
*/
- public synchronized Story getStory(String luid,
- @SuppressWarnings("javadoc") MetaData meta, Progress pg)
+ public synchronized Story getStory(String luid, MetaData meta, Progress pg)
throws IOException {
if (pg == null) {
@@ -710,12 +545,21 @@ abstract public class BasicLibrary {
pg.addProgress(pgProcess, 1);
Story story = null;
- File file = getFile(luid, pgGet);
+ File file = null;
+
+ if (luid != null && meta != null) {
+ file = getFile(luid, pgGet);
+ }
+
pgGet.done();
try {
- SupportType type = SupportType.valueOfAllOkUC(meta.getType());
- URL url = file.toURI().toURL();
- if (type != null) {
+ if (file != null) {
+ SupportType type = SupportType.valueOfAllOkUC(meta.getType());
+ if (type == null) {
+ throw new IOException("Unknown type: " + meta.getType());
+ }
+
+ URL url = file.toURI().toURL();
story = BasicSupport.getSupport(type, url) //
.process(pgProcess);
@@ -723,15 +567,11 @@ abstract public class BasicLibrary {
meta.setCover(story.getMeta().getCover());
meta.setResume(story.getMeta().getResume());
story.setMeta(meta);
- //
- } else {
- throw new IOException("Unknown type: " + meta.getType());
}
} catch (IOException e) {
- // We should not have not-supported files in the
- // library
- Instance.getTraceHandler().error(
- new IOException(String.format(
+ // We should not have not-supported files in the library
+ Instance.getInstance().getTraceHandler()
+ .error(new IOException(String.format(
"Cannot load file of type '%s' from library: %s",
meta.getType(), file), e));
} finally {
@@ -759,6 +599,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();
@@ -773,7 +635,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();
@@ -891,16 +753,21 @@ abstract public class BasicLibrary {
*/
public synchronized Story save(Story story, String luid, Progress pg)
throws IOException {
+ if (pg == null) {
+ pg = new Progress();
+ }
- Instance.getTraceHandler().trace(
+ Instance.getInstance().getTraceHandler().trace(
this.getClass().getSimpleName() + ": saving story " + luid);
// Do not change the original metadata, but change the original story
MetaData meta = story.getMeta().clone();
story.setMeta(meta);
+ pg.setName("Saving story");
+
if (luid == null || luid.isEmpty()) {
- meta.setLuid(String.format("%03d", getNextId()));
+ meta.setLuid(getNextId());
} else {
meta.setLuid(luid);
}
@@ -913,10 +780,12 @@ abstract public class BasicLibrary {
updateInfo(story.getMeta());
- Instance.getTraceHandler().trace(
- this.getClass().getSimpleName() + ": story saved (" + luid
- + ")");
+ Instance.getInstance().getTraceHandler()
+ .trace(this.getClass().getSimpleName() + ": story saved ("
+ + luid + ")");
+ pg.setName(meta.getTitle());
+ pg.done();
return story;
}
@@ -930,15 +799,15 @@ abstract public class BasicLibrary {
* in case of I/O error
*/
public synchronized void delete(String luid) throws IOException {
- Instance.getTraceHandler().trace(
+ Instance.getInstance().getTraceHandler().trace(
this.getClass().getSimpleName() + ": deleting story " + luid);
doDelete(luid);
invalidateInfo(luid);
- Instance.getTraceHandler().trace(
- this.getClass().getSimpleName() + ": story deleted (" + luid
- + ")");
+ Instance.getInstance().getTraceHandler()
+ .trace(this.getClass().getSimpleName() + ": story deleted ("
+ + luid + ")");
}
/**
@@ -1039,8 +908,6 @@ abstract public class BasicLibrary {
meta.setTitle(newTitle);
meta.setAuthor(newAuthor);
saveMeta(meta, pg);
-
- invalidateInfo(luid);
}
/**
@@ -1083,4 +950,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
- *
+ * The next time we try to retrieve the {@link Story}, it may be required to
+ * cache it again.
*
- * @return the grouped list
+ * @param luid
+ * the story to clear
*
* @throws IOException
- * in case of IOException
+ * in case of I/O error
*/
- public synchronized Map
- *
- * Note that the letters used in the groups can vary (except * and
- * 0-9, which may only be present or not).
- *
- * @return the authors' names, grouped by letter(s)
- *
+ * @return the same as getList()
* @throws IOException
- * in case of IOException
- */
- public Map
- *
- *
- * @param authors
- * the full list of authors
- * @param car
- * the starting character, *, 0 or a capital
- * letter
- *
- * @return the authors that fulfil the starting letter
+ * in case of I/O error
+ * @deprecated please use {@link BasicLibrary#getList()} and
+ * {@link MetaResultList#getSourcesGrouped()} instead.
*/
- private List