X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FBasicLibrary.java;h=f77d0edcef5ae64b186942be2a6048fa7eca7745;hb=4a464a06db3158e31c64aa798ddce9df1ec7f5f5;hp=f96506d9f43bc42feef62460947543487430861f;hpb=99206a39d3c58140402d2b4903f7436168d1d349;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/library/BasicLibrary.java b/src/be/nikiroo/fanfix/library/BasicLibrary.java index f96506d..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; @@ -39,14 +37,36 @@ abstract public class BasicLibrary { * @author niki */ public enum Status { - /** The library is ready. */ - READY, - /** The library is invalid (not correctly set up). */ - INVALID, + /** The library is ready and r/w. */ + READ_WRITE, + /** The library is ready, but read-only. */ + READ_ONLY, /** You are not allowed to access this library. */ UNAUTHORIZED, - /** The library is currently out of commission. */ - UNAVAILABLE, + /** The library is invalid, and will never work as is. */ + INVALID, + /** The library is currently out of commission, but may work later. */ + UNAVAILABLE; + + /** + * The library is available (you can query it). + *
+ * It does not specify if it is read-only or not.
+ *
+ * @return TRUE if it is
+ */
+ public boolean isReady() {
+ return (this == READ_WRITE || this == READ_ONLY);
+ }
+
+ /**
+ * This library can be modified (= you are allowed to modify it).
+ *
+ * @return TRUE if it is
+ */
+ public boolean isWritable() {
+ return (this == READ_WRITE);
+ }
}
/**
@@ -66,7 +86,7 @@ abstract public class BasicLibrary {
* @return the current status
*/
public Status getStatus() {
- return Status.READY;
+ return Status.READ_WRITE;
}
/**
@@ -76,13 +96,16 @@ 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}
*
* @return the corresponding {@link Story}
+ *
+ * @throws IOException
+ * in case of IOException
*/
- public abstract File getFile(String luid, Progress pg);
+ public abstract File getFile(String luid, Progress pg) throws IOException;
/**
* Return the cover image associated to this story.
@@ -91,8 +114,30 @@ abstract public class BasicLibrary {
* the Library UID of the story
*
* @return the cover image
+ *
+ * @throws IOException
+ * in case of IOException
+ */
+ 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 abstract Image getCover(String luid);
+ 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.
@@ -104,14 +149,17 @@ abstract public class BasicLibrary {
* the source
*
* @return the cover image or NULL
+ *
+ * @throws IOException
+ * in case of IOException
*/
- public Image getSourceCover(String source) {
+ public Image getSourceCover(String source) throws IOException {
Image custom = getCustomSourceCover(source);
if (custom != null) {
return custom;
}
- List
+ * The returned list MUST be a copy, not the original one.
*
* @param pg
* the optional {@link Progress}
*
* @return the list (can be empty but not NULL)
+ *
+ * @throws IOException
+ * in case of IOException
*/
- protected abstract 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 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 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
@@ -563,8 +484,11 @@ abstract public class BasicLibrary {
* the optional progress reporter
*
* @return the corresponding {@link Story} or NULL if not found
+ *
+ * @throws IOException
+ * in case of IOException
*/
- public synchronized Story getStory(String luid, Progress pg) {
+ public Story getStory(String luid, Progress pg) throws IOException {
Progress pgMetas = new Progress();
Progress pgStory = new Progress();
if (pg != null) {
@@ -591,16 +515,23 @@ 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
*
* @return the corresponding {@link Story} or NULL if not found
+ *
+ * @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) {
pg = new Progress();
@@ -614,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);
@@ -627,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 {
@@ -655,14 +591,36 @@ abstract public class BasicLibrary {
* @param pg
* the optional progress reporter
*
- * @return the imported {@link Story}
+ * @return the imported Story {@link MetaData}
+ *
+ * @throws UnknownHostException
+ * if the host is not supported
+ * @throws IOException
+ * 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
*/
- public Story imprt(URL url, Progress pg) throws IOException {
+ MetaData imprt(URL url, String luid, Progress pg) throws IOException {
if (pg == null)
pg = new Progress();
@@ -677,10 +635,10 @@ 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;
+ return story.getMeta();
}
/**
@@ -795,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);
}
@@ -817,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;
}
@@ -834,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 + ")");
}
/**
@@ -943,8 +908,6 @@ abstract public class BasicLibrary {
meta.setTitle(newTitle);
meta.setAuthor(newAuthor);
saveMeta(meta, pg);
-
- invalidateInfo(luid);
}
/**
@@ -987,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
- *
- *
- * @return the grouped list
- */
- 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)
- */
- public Map
- *
- *
- * @param authors
- * the full list of authors
- * @param car
- * the starting character, *, 0 or a capital
- * letter
- * @return the authors that fulfill the starting letter
- */
- private List