X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FBasicLibrary.java;h=d435f8d7ec7fd3e2f09242339eefb9ff6fa74d93;hb=bbeec1a33d25bb3d00ba385099368422cc6ef603;hp=c558384d380526819f36a8dac9553b89d33517ca;hpb=d66deb8d8b30cff6b54db352eef34a3508939f84;p=fanfix.git
diff --git a/src/be/nikiroo/fanfix/library/BasicLibrary.java b/src/be/nikiroo/fanfix/library/BasicLibrary.java
index c558384..d435f8d 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}
@@ -338,284 +347,83 @@ 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 getSources() throws IOException {
- List list = new ArrayList();
- for (MetaData meta : getMetas(null)) {
- String storySource = meta.getSource();
- if (!list.contains(storySource)) {
- list.add(storySource);
- }
- }
-
- Collections.sort(list);
- return list;
+ public boolean isCached(@SuppressWarnings("unused") String luid) {
+ // By default, everything is cached
+ return true;
}
/**
- * List all the known types (sources) of stories, grouped by directory
- * ("Source_1/a" and "Source_1/b" will be grouped into "Source_1").
+ * Clear the {@link Story} from the cache, if needed.
*
- * Note that an empty item in the list means a non-grouped source (type) --
- * e.g., you could have for Source_1:
- *
- * - : empty, so source is "Source_1"
- * - a: empty, so source is "Source_1/a"
- * - b: empty, so source is "Source_1/b"
- *
+ * 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> getSourcesGrouped()
- throws IOException {
- Map> map = new TreeMap>();
- for (String source : getSources()) {
- String name;
- String subname;
-
- int pos = source.indexOf('/');
- if (pos > 0 && pos < source.length() - 1) {
- name = source.substring(0, pos);
- subname = source.substring(pos + 1);
-
- } else {
- name = source;
- subname = "";
- }
-
- List list = map.get(name);
- if (list == null) {
- list = new ArrayList();
- map.put(name, list);
- }
- list.add(subname);
- }
-
- return map;
+ @SuppressWarnings("unused")
+ public void clearFromCache(String luid) throws IOException {
+ // By default, this is a noop.
}
/**
- * List all the known authors of stories.
- *
- * @return the authors
- *
- * @throws IOException
- * in case of IOException
+ * @deprecated please use {@link BasicLibrary#getList()} and
+ * {@link MetaResultList#getSources()} instead.
*/
- public synchronized List getAuthors() throws IOException {
- List list = new ArrayList();
- for (MetaData meta : getMetas(null)) {
- String storyAuthor = meta.getAuthor();
- if (!list.contains(storyAuthor)) {
- list.add(storyAuthor);
- }
- }
-
- Collections.sort(list);
- return list;
+ @Deprecated
+ public List getSources() throws IOException {
+ return getList().getSources();
}
/**
- * Return the list of authors, grouped by starting letter(s) if needed.
- *
- * 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:
- *
- * - *: any author whose name doesn't contain letters nor numbers
- *
- * - 0-9: any authors whose name starts with a number
- * - A-C (for instance): any author whose name starts with
- * A, B or C
- *
- * 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)
- *
- * @throws IOException
- * in case of IOException
+ * @deprecated please use {@link BasicLibrary#getList()} and
+ * {@link MetaResultList#getSourcesGrouped()} instead.
*/
- public Map> getAuthorsGrouped() throws IOException {
- int MAX = 20;
-
- Map> groups = new TreeMap>();
- List authors = getAuthors();
-
- // If all authors fit the max, just report them as is
- if (authors.size() <= MAX) {
- groups.put("", authors);
- return groups;
- }
-
- // Create groups A to Z, which can be empty here
- for (char car = 'A'; car <= 'Z'; car++) {
- groups.put(Character.toString(car), getAuthorsGroup(authors, car));
- }
-
- // Collapse them
- List keys = new ArrayList(groups.keySet());
- for (int i = 0; i + 1 < keys.size(); i++) {
- String keyNow = keys.get(i);
- String keyNext = keys.get(i + 1);
-
- List now = groups.get(keyNow);
- List next = groups.get(keyNext);
-
- int currentTotal = now.size() + next.size();
- if (currentTotal <= MAX) {
- String key = keyNow.charAt(0) + "-"
- + keyNext.charAt(keyNext.length() - 1);
-
- List all = new ArrayList();
- all.addAll(now);
- all.addAll(next);
-
- groups.remove(keyNow);
- groups.remove(keyNext);
- groups.put(key, all);
-
- keys.set(i, key); // set the new key instead of key(i)
- keys.remove(i + 1); // remove the next, consumed key
- i--; // restart at key(i)
- }
- }
-
- // Add "special" groups
- groups.put("*", getAuthorsGroup(authors, '*'));
- groups.put("0-9", getAuthorsGroup(authors, '0'));
-
- // Prune empty groups
- keys = new ArrayList(groups.keySet());
- for (String key : keys) {
- if (groups.get(key).isEmpty()) {
- groups.remove(key);
- }
- }
-
- return groups;
+ @Deprecated
+ public Map> getSourcesGrouped() throws IOException {
+ return getList().getSourcesGrouped();
}
/**
- * Get all the authors that start with the given character:
- *
- * - *: any author whose name doesn't contain letters nor numbers
- *
- * - 0: any authors whose name starts with a number
- * - A (any capital latin letter): any author whose name starts
- * with A
- *
- *
- * @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
+ * @deprecated please use {@link BasicLibrary#getList()} and
+ * {@link MetaResultList#getAuthors()} instead.
*/
- private List getAuthorsGroup(List authors, char car) {
- List accepted = new ArrayList();
- for (String author : authors) {
- char first = '*';
- for (int i = 0; first == '*' && i < author.length(); i++) {
- String san = StringUtils.sanitize(author, true, true);
- char c = san.charAt(i);
- if (c >= '0' && c <= '9') {
- first = '0';
- } else if (c >= 'a' && c <= 'z') {
- first = (char) (c - 'a' + 'A');
- } else if (c >= 'A' && c <= 'Z') {
- first = c;
- }
- }
-
- if (first == car) {
- accepted.add(author);
- }
- }
-
- return accepted;
+ @Deprecated
+ public List getAuthors() throws IOException {
+ return getList().getAuthors();
}
/**
- * List all the stories in the {@link BasicLibrary}.
- *
- * Cover images MAYBE not included.
- *
- * @return the stories
- *
- * @throws IOException
- * in case of IOException
+ * @deprecated please use {@link BasicLibrary#getList()} and
+ * {@link MetaResultList#getAuthorsGrouped()} instead.
*/
- 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;
+ public Map> getAuthorsGrouped() throws IOException {
+ return getList().getAuthorsGrouped();
}
/**
- * List all the stories of the given author in the {@link BasicLibrary}, or
- * all the stories if NULL is passed as an author.
+ * List all the stories in the {@link BasicLibrary}.
*
- * 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 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 +431,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())) {
@@ -655,8 +463,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) {
@@ -685,6 +492,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 +503,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 +518,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 +540,13 @@ 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.getInstance().getTraceHandler().error(new IOException(
- String.format("Cannot load file of type '%s' from library: %s", meta.getType(), file), e));
+ // 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 {
pgProcess.done();
pg.done();
@@ -889,13 +704,19 @@ abstract public class BasicLibrary {
*/
public synchronized Story save(Story story, String luid, Progress pg)
throws IOException {
+ if (pg == null) {
+ pg = new Progress();
+ }
- Instance.getInstance().getTraceHandler().trace(this.getClass().getSimpleName() + ": saving story " + luid);
+ 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 {
@@ -911,8 +732,11 @@ abstract public class BasicLibrary {
updateInfo(story.getMeta());
Instance.getInstance().getTraceHandler()
- .trace(this.getClass().getSimpleName() + ": story saved (" + luid + ")");
+ .trace(this.getClass().getSimpleName() + ": story saved ("
+ + luid + ")");
+ pg.setName(meta.getTitle());
+ pg.done();
return story;
}
@@ -926,14 +750,15 @@ abstract public class BasicLibrary {
* in case of I/O error
*/
public synchronized void delete(String luid) throws IOException {
- Instance.getInstance().getTraceHandler().trace(this.getClass().getSimpleName() + ": deleting story " + luid);
+ Instance.getInstance().getTraceHandler().trace(
+ this.getClass().getSimpleName() + ": deleting story " + luid);
doDelete(luid);
invalidateInfo(luid);
Instance.getInstance().getTraceHandler()
- .trace(this.getClass().getSimpleName() + ": story deleted (" + luid
- + ")");
+ .trace(this.getClass().getSimpleName() + ": story deleted ("
+ + luid + ")");
}
/**
@@ -1034,8 +859,6 @@ abstract public class BasicLibrary {
meta.setTitle(newTitle);
meta.setAuthor(newAuthor);
saveMeta(meta, pg);
-
- invalidateInfo(luid);
}
/**