X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FBasicLibrary.java;h=586c4ef17e11ff4d8c7eefa1cf6c689dac71ce59;hb=f19b48e27a56ebab18687debd9ef52581a03f06d;hp=099859dcfaffed075f30b3d716b790f644a43a09;hpb=0013f760d69fd7db2b298c3da5d89bc0b102eabf;p=nikiroo-utils.git
diff --git a/src/be/nikiroo/fanfix/library/BasicLibrary.java b/src/be/nikiroo/fanfix/library/BasicLibrary.java
index 099859d..586c4ef 100644
--- a/src/be/nikiroo/fanfix/library/BasicLibrary.java
+++ b/src/be/nikiroo/fanfix/library/BasicLibrary.java
@@ -98,7 +98,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 +122,13 @@ abstract public class BasicLibrary {
*/
public abstract Image getCover(String luid) throws IOException;
+ // TODO: ensure it is the main used interface
+ public MetaResultList getList(Progress pg) throws IOException {
+ return new MetaResultList(getMetas(pg));
+ }
+
+ // TODO: make something for (normal and custom) not-story covers
+
/**
* Return the cover image associated to this source.
*
@@ -142,7 +149,7 @@ abstract public class BasicLibrary {
return custom;
}
- List metas = getListBySource(source);
+ List metas = getList().filter(source, null, null);
if (metas.size() > 0) {
return getCover(metas.get(0).getLuid());
}
@@ -170,7 +177,7 @@ abstract public class BasicLibrary {
return custom;
}
- List metas = getListByAuthor(author);
+ List metas = getList().filter(null, author, null);
if (metas.size() > 0) {
return getCover(metas.get(0).getLuid());
}
@@ -245,6 +252,8 @@ abstract public class BasicLibrary {
/**
* Return the list of stories (represented by their {@link MetaData}, which
* MAY not have the cover included).
+ *
+ * The returned list MUST be a copy, not the original one.
*
* @param pg
* the optional {@link Progress}
@@ -337,6 +346,36 @@ abstract public class BasicLibrary {
}
}
+ /**
+ * Check if the {@link Story} denoted by this Library UID is present in the
+ * cache (if we have no cache, we default to true).
+ *
+ * @param luid
+ * the Library UID
+ *
+ * @return TRUE if it is
+ */
+ public boolean isCached(String luid) {
+ // By default, everything is cached
+ return true;
+ }
+
+ /**
+ * Clear the {@link Story} from the cache, if needed.
+ *
+ * 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
+ */
+ public void clearFromCache(String luid) throws IOException {
+ // By default, this is a noop.
+ }
+
/**
* List all the known types (sources) of stories.
*
@@ -345,7 +384,7 @@ abstract public class BasicLibrary {
* @throws IOException
* in case of IOException
*/
- public synchronized List getSources() throws IOException {
+ public List getSources() throws IOException {
List list = new ArrayList();
for (MetaData meta : getMetas(null)) {
String storySource = meta.getSource();
@@ -375,8 +414,7 @@ abstract public class BasicLibrary {
* @throws IOException
* in case of IOException
*/
- public synchronized Map> getSourcesGrouped()
- throws IOException {
+ public Map> getSourcesGrouped() throws IOException {
Map> map = new TreeMap>();
for (String source : getSources()) {
String name;
@@ -411,7 +449,7 @@ abstract public class BasicLibrary {
* @throws IOException
* in case of IOException
*/
- public synchronized List getAuthors() throws IOException {
+ public List getAuthors() throws IOException {
List list = new ArrayList();
for (MetaData meta : getMetas(null)) {
String storyAuthor = meta.getAuthor();
@@ -558,64 +596,8 @@ abstract public class BasicLibrary {
* @throws IOException
* in case of IOException
*/
- public synchronized List getList() throws IOException {
- return getMetas(null);
- }
-
- /**
- * List all the stories of the given source type in the {@link BasicLibrary}
- * , or all the stories if NULL is passed as a type.
- *
- * Cover images not included.
- *
- * @param type
- * the type of story to retrieve, or NULL for all
- *
- * @return the stories
- *
- * @throws IOException
- * in case of IOException
- */
- public synchronized List getListBySource(String type)
- throws IOException {
- List list = new ArrayList();
- for (MetaData meta : getMetas(null)) {
- String storyType = meta.getSource();
- if (type == null || type.equalsIgnoreCase(storyType)) {
- list.add(meta);
- }
- }
-
- Collections.sort(list);
- return list;
- }
-
- /**
- * List all the stories of the given author in the {@link BasicLibrary}, or
- * all the stories if NULL is passed as an author.
- *
- * Cover images not included.
- *
- * @param author
- * the author of the stories to retrieve, or NULL for all
- *
- * @return the stories
- *
- * @throws IOException
- * in case of IOException
- */
- public synchronized List getListByAuthor(String author)
- throws IOException {
- List list = new ArrayList();
- for (MetaData meta : getMetas(null)) {
- String storyAuthor = meta.getAuthor();
- if (author == null || author.equalsIgnoreCase(storyAuthor)) {
- list.add(meta);
- }
- }
-
- Collections.sort(list);
- return list;
+ public MetaResultList getList() throws IOException {
+ return getList(null);
}
/**
@@ -623,14 +605,14 @@ abstract public class BasicLibrary {
* cover image MAY not be included.
*
* @param luid
- * the Library UID of the story
+ * the Library UID of the story, can be NULL
*
- * @return the corresponding {@link Story}
+ * @return the corresponding {@link Story} or NULL if not found
*
* @throws IOException
* in case of IOException
*/
- public synchronized MetaData getInfo(String luid) throws IOException {
+ public MetaData getInfo(String luid) throws IOException {
if (luid != null) {
for (MetaData meta : getMetas(null)) {
if (luid.equals(meta.getLuid())) {
@@ -685,6 +667,8 @@ abstract public class BasicLibrary {
* Retrieve a specific {@link Story}.
*
* @param luid
+ * the LUID of the story
+ * @param meta
* the meta of the story
* @param pg
* the optional progress reporter
@@ -694,8 +678,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 +693,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 +715,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 {
@@ -774,6 +762,7 @@ abstract public class BasicLibrary {
}
Story story = save(support.process(pgProcess), pgSave);
+ pg.setName(story.getMeta().getTitle());
pg.done();
return story.getMeta();
@@ -891,14 +880,19 @@ 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()));
} else {
@@ -913,10 +907,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 +926,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 +1035,6 @@ abstract public class BasicLibrary {
meta.setTitle(newTitle);
meta.setAuthor(newAuthor);
saveMeta(meta, pg);
-
- invalidateInfo(luid);
}
/**