c6ce22dd24c1c4fa85f60edfdcea7bd5f8de1b05
1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.IOException
;
6 import java
.net
.UnknownHostException
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Collections
;
11 import java
.util
.TreeMap
;
13 import be
.nikiroo
.fanfix
.Instance
;
14 import be
.nikiroo
.fanfix
.data
.MetaData
;
15 import be
.nikiroo
.fanfix
.data
.Story
;
16 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
17 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
18 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
19 import be
.nikiroo
.fanfix
.supported
.SupportType
;
20 import be
.nikiroo
.utils
.Image
;
21 import be
.nikiroo
.utils
.Progress
;
22 import be
.nikiroo
.utils
.StringUtils
;
25 * Manage a library of Stories: import, export, list, modify.
27 * Each {@link Story} object will be associated with a (local to the library)
28 * unique ID, the LUID, which will be used to identify the {@link Story}.
30 * Most of the {@link BasicLibrary} functions work on a partial (cover
31 * <b>MAY</b> not be included) {@link MetaData} object.
35 abstract public class BasicLibrary
{
37 * A {@link BasicLibrary} status.
42 /** The library is ready and r/w. */
44 /** The library is ready, but read-only. */
46 /** The library is invalid (not correctly set up). */
48 /** You are not allowed to access this library. */
50 /** The library is currently out of commission. */
54 * The library is available (you can query it).
56 * It does <b>not</b> specify if it is read-only or not.
58 * @return TRUE if it is
60 public boolean isReady() {
61 return (this == READ_WRITE
|| this == READ_ONLY
);
65 * This library can be modified (= you are allowed to modify it).
67 * @return TRUE if it is
69 public boolean isWritable() {
70 return (this == READ_WRITE
);
75 * Return a name for this library (the UI may display this).
79 * @return the name, or an empty {@link String} if none
81 public String
getLibraryName() {
88 * @return the current status
90 public Status
getStatus() {
91 return Status
.READ_WRITE
;
95 * Retrieve the main {@link File} corresponding to the given {@link Story},
96 * which can be passed to an external reader or instance.
98 * Do <b>NOT</b> alter this file.
101 * the Library UID of the story
103 * the optional {@link Progress}
105 * @return the corresponding {@link Story}
107 * @throws IOException
108 * in case of IOException
110 public abstract File
getFile(String luid
, Progress pg
) throws IOException
;
113 * Return the cover image associated to this story.
116 * the Library UID of the story
118 * @return the cover image
120 * @throws IOException
121 * in case of IOException
123 public abstract Image
getCover(String luid
) throws IOException
;
125 // TODO: ensure it is the main used interface
126 public synchronized MetaResultList
getList(Progress pg
) throws IOException
{
127 return new MetaResultList(getMetas(pg
));
130 //TODO: make something for (normal and custom) not-story covers
133 * Return the cover image associated to this source.
135 * By default, return the custom cover if any, and if not, return the cover
136 * of the first story with this source.
141 * @return the cover image or NULL
143 * @throws IOException
144 * in case of IOException
146 public Image
getSourceCover(String source
) throws IOException
{
147 Image custom
= getCustomSourceCover(source
);
148 if (custom
!= null) {
152 List
<MetaData
> metas
= getListBySource(source
);
153 if (metas
.size() > 0) {
154 return getCover(metas
.get(0).getLuid());
161 * Return the cover image associated to this author.
163 * By default, return the custom cover if any, and if not, return the cover
164 * of the first story with this author.
169 * @return the cover image or NULL
171 * @throws IOException
172 * in case of IOException
174 public Image
getAuthorCover(String author
) throws IOException
{
175 Image custom
= getCustomAuthorCover(author
);
176 if (custom
!= null) {
180 List
<MetaData
> metas
= getListByAuthor(author
);
181 if (metas
.size() > 0) {
182 return getCover(metas
.get(0).getLuid());
189 * Return the custom cover image associated to this source.
191 * By default, return NULL.
194 * the source to look for
196 * @return the custom cover or NULL if none
198 * @throws IOException
199 * in case of IOException
201 @SuppressWarnings("unused")
202 public Image
getCustomSourceCover(String source
) throws IOException
{
207 * Return the custom cover image associated to this author.
209 * By default, return NULL.
212 * the author to look for
214 * @return the custom cover or NULL if none
216 * @throws IOException
217 * in case of IOException
219 @SuppressWarnings("unused")
220 public Image
getCustomAuthorCover(String author
) throws IOException
{
225 * Set the source cover to the given story cover.
228 * the source to change
232 * @throws IOException
233 * in case of IOException
235 public abstract void setSourceCover(String source
, String luid
)
239 * Set the author cover to the given story cover.
242 * the author to change
246 * @throws IOException
247 * in case of IOException
249 public abstract void setAuthorCover(String author
, String luid
)
253 * Return the list of stories (represented by their {@link MetaData}, which
254 * <b>MAY</b> not have the cover included).
256 * The returned list <b>MUST</b> be a copy, not the original one.
259 * the optional {@link Progress}
261 * @return the list (can be empty but not NULL)
263 * @throws IOException
264 * in case of IOException
266 protected abstract List
<MetaData
> getMetas(Progress pg
) throws IOException
;
269 * Invalidate the {@link Story} cache (when the content should be re-read
270 * because it was changed).
272 protected void invalidateInfo() {
273 invalidateInfo(null);
277 * Invalidate the {@link Story} cache (when the content is removed).
279 * All the cache can be deleted if NULL is passed as meta.
282 * the LUID of the {@link Story} to clear from the cache, or NULL
285 protected abstract void invalidateInfo(String luid
);
288 * Invalidate the {@link Story} cache (when the content has changed, but we
289 * already have it) with the new given meta.
292 * the {@link Story} to clear from the cache
294 * @throws IOException
295 * in case of IOException
297 protected abstract void updateInfo(MetaData meta
) throws IOException
;
300 * Return the next LUID that can be used.
302 * @return the next luid
304 protected abstract int getNextId();
307 * Delete the target {@link Story}.
310 * the LUID of the {@link Story}
312 * @throws IOException
313 * in case of I/O error or if the {@link Story} wa not found
315 protected abstract void doDelete(String luid
) throws IOException
;
318 * Actually save the story to the back-end.
321 * the {@link Story} to save
323 * the optional {@link Progress}
325 * @return the saved {@link Story} (which may have changed, especially
326 * regarding the {@link MetaData})
328 * @throws IOException
329 * in case of I/O error
331 protected abstract Story
doSave(Story story
, Progress pg
)
335 * Refresh the {@link BasicLibrary}, that is, make sure all metas are
339 * the optional progress reporter
341 public void refresh(Progress pg
) {
344 } catch (IOException e
) {
345 // We will let it fail later
350 * Check if the {@link Story} denoted by this Library UID is present in the
351 * cache (if we have no cache, we default to </t>true</tt>).
356 * @return TRUE if it is
358 public boolean isCached(String luid
) {
359 // By default, everything is cached
364 * Clear the {@link Story} from the cache, if needed.
366 * The next time we try to retrieve the {@link Story}, it may be required to
372 * @throws IOException
373 * in case of I/O error
375 public void clearFromCache(String luid
) throws IOException
{
376 // By default, this is a noop.
380 * List all the known types (sources) of stories.
382 * @return the sources
384 * @throws IOException
385 * in case of IOException
387 public synchronized List
<String
> getSources() throws IOException
{
388 List
<String
> list
= new ArrayList
<String
>();
389 for (MetaData meta
: getMetas(null)) {
390 String storySource
= meta
.getSource();
391 if (!list
.contains(storySource
)) {
392 list
.add(storySource
);
396 Collections
.sort(list
);
401 * List all the known types (sources) of stories, grouped by directory
402 * ("Source_1/a" and "Source_1/b" will be grouped into "Source_1").
404 * Note that an empty item in the list means a non-grouped source (type) --
405 * e.g., you could have for Source_1:
407 * <li><tt></tt>: empty, so source is "Source_1"</li>
408 * <li><tt>a</tt>: empty, so source is "Source_1/a"</li>
409 * <li><tt>b</tt>: empty, so source is "Source_1/b"</li>
412 * @return the grouped list
414 * @throws IOException
415 * in case of IOException
417 public synchronized Map
<String
, List
<String
>> getSourcesGrouped()
419 Map
<String
, List
<String
>> map
= new TreeMap
<String
, List
<String
>>();
420 for (String source
: getSources()) {
424 int pos
= source
.indexOf('/');
425 if (pos
> 0 && pos
< source
.length() - 1) {
426 name
= source
.substring(0, pos
);
427 subname
= source
.substring(pos
+ 1);
434 List
<String
> list
= map
.get(name
);
436 list
= new ArrayList
<String
>();
446 * List all the known authors of stories.
448 * @return the authors
450 * @throws IOException
451 * in case of IOException
453 public synchronized List
<String
> getAuthors() throws IOException
{
454 List
<String
> list
= new ArrayList
<String
>();
455 for (MetaData meta
: getMetas(null)) {
456 String storyAuthor
= meta
.getAuthor();
457 if (!list
.contains(storyAuthor
)) {
458 list
.add(storyAuthor
);
462 Collections
.sort(list
);
467 * Return the list of authors, grouped by starting letter(s) if needed.
469 * If the number of author is not too high, only one group with an empty
470 * name and all the authors will be returned.
472 * If not, the authors will be separated into groups:
474 * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
476 * <li><tt>0-9</tt>: any authors whose name starts with a number</li>
477 * <li><tt>A-C</tt> (for instance): any author whose name starts with
478 * <tt>A</tt>, <tt>B</tt> or <tt>C</tt></li>
480 * Note that the letters used in the groups can vary (except <tt>*</tt> and
481 * <tt>0-9</tt>, which may only be present or not).
483 * @return the authors' names, grouped by letter(s)
485 * @throws IOException
486 * in case of IOException
488 public Map
<String
, List
<String
>> getAuthorsGrouped() throws IOException
{
491 Map
<String
, List
<String
>> groups
= new TreeMap
<String
, List
<String
>>();
492 List
<String
> authors
= getAuthors();
494 // If all authors fit the max, just report them as is
495 if (authors
.size() <= MAX
) {
496 groups
.put("", authors
);
500 // Create groups A to Z, which can be empty here
501 for (char car
= 'A'; car
<= 'Z'; car
++) {
502 groups
.put(Character
.toString(car
), getAuthorsGroup(authors
, car
));
506 List
<String
> keys
= new ArrayList
<String
>(groups
.keySet());
507 for (int i
= 0; i
+ 1 < keys
.size(); i
++) {
508 String keyNow
= keys
.get(i
);
509 String keyNext
= keys
.get(i
+ 1);
511 List
<String
> now
= groups
.get(keyNow
);
512 List
<String
> next
= groups
.get(keyNext
);
514 int currentTotal
= now
.size() + next
.size();
515 if (currentTotal
<= MAX
) {
516 String key
= keyNow
.charAt(0) + "-"
517 + keyNext
.charAt(keyNext
.length() - 1);
519 List
<String
> all
= new ArrayList
<String
>();
523 groups
.remove(keyNow
);
524 groups
.remove(keyNext
);
525 groups
.put(key
, all
);
527 keys
.set(i
, key
); // set the new key instead of key(i)
528 keys
.remove(i
+ 1); // remove the next, consumed key
529 i
--; // restart at key(i)
533 // Add "special" groups
534 groups
.put("*", getAuthorsGroup(authors
, '*'));
535 groups
.put("0-9", getAuthorsGroup(authors
, '0'));
537 // Prune empty groups
538 keys
= new ArrayList
<String
>(groups
.keySet());
539 for (String key
: keys
) {
540 if (groups
.get(key
).isEmpty()) {
549 * Get all the authors that start with the given character:
551 * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
553 * <li><tt>0</tt>: any authors whose name starts with a number</li>
554 * <li><tt>A</tt> (any capital latin letter): any author whose name starts
555 * with <tt>A</tt></li>
559 * the full list of authors
561 * the starting character, <tt>*</tt>, <tt>0</tt> or a capital
564 * @return the authors that fulfil the starting letter
566 private List
<String
> getAuthorsGroup(List
<String
> authors
, char car
) {
567 List
<String
> accepted
= new ArrayList
<String
>();
568 for (String author
: authors
) {
570 for (int i
= 0; first
== '*' && i
< author
.length(); i
++) {
571 String san
= StringUtils
.sanitize(author
, true, true);
572 char c
= san
.charAt(i
);
573 if (c
>= '0' && c
<= '9') {
575 } else if (c
>= 'a' && c
<= 'z') {
576 first
= (char) (c
- 'a' + 'A');
577 } else if (c
>= 'A' && c
<= 'Z') {
583 accepted
.add(author
);
591 * List all the stories in the {@link BasicLibrary}.
593 * Cover images <b>MAYBE</b> not included.
595 * @return the stories
597 * @throws IOException
598 * in case of IOException
600 public synchronized List
<MetaData
> getList() throws IOException
{
601 return getMetas(null);
605 * List all the stories of the given source type in the {@link BasicLibrary} ,
606 * or all the stories if NULL is passed as a type.
608 * Cover images not included.
610 * @param source the type of story to retrieve, or NULL for all
612 * @return the stories
614 * @throws IOException in case of IOException
616 * @deprecated use {@link BasicLibrary#getList(Progress)} and
617 * {@link MetaResultList#filter(String, String, String)}
620 public synchronized List
<MetaData
> getListBySource(String source
) throws IOException
{
621 return getList(null).filter(source
, null, null);
625 * List all the stories of the given author in the {@link BasicLibrary}, or
626 * all the stories if NULL is passed as an author.
628 * Cover images not included.
631 * the author of the stories to retrieve, or NULL for all
633 * @return the stories
635 * @throws IOException
636 * in case of IOException
638 * @deprecated use {@link BasicLibrary#getList(Progress)} and
639 * {@link MetaResultList#filter(String, String, String)}
641 public synchronized List
<MetaData
> getListByAuthor(String author
) throws IOException
{
642 return getList(null).filter(null, author
, null);
646 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
647 * cover image <b>MAY</b> not be included.
650 * the Library UID of the story
652 * @return the corresponding {@link Story}
654 * @throws IOException
655 * in case of IOException
657 public synchronized MetaData
getInfo(String luid
) throws IOException
{
659 for (MetaData meta
: getMetas(null)) {
660 if (luid
.equals(meta
.getLuid())) {
670 * Retrieve a specific {@link Story}.
673 * the Library UID of the story
675 * the optional progress reporter
677 * @return the corresponding {@link Story} or NULL if not found
679 * @throws IOException
680 * in case of IOException
682 public synchronized Story
getStory(String luid
, Progress pg
)
684 Progress pgMetas
= new Progress();
685 Progress pgStory
= new Progress();
687 pg
.setMinMax(0, 100);
688 pg
.addProgress(pgMetas
, 10);
689 pg
.addProgress(pgStory
, 90);
692 MetaData meta
= null;
693 for (MetaData oneMeta
: getMetas(pgMetas
)) {
694 if (oneMeta
.getLuid().equals(luid
)) {
702 Story story
= getStory(luid
, meta
, pgStory
);
709 * Retrieve a specific {@link Story}.
712 * the meta of the story
714 * the optional progress reporter
716 * @return the corresponding {@link Story} or NULL if not found
718 * @throws IOException
719 * in case of IOException
721 public synchronized Story
getStory(String luid
,
722 @SuppressWarnings("javadoc") MetaData meta
, Progress pg
)
729 Progress pgGet
= new Progress();
730 Progress pgProcess
= new Progress();
733 pg
.addProgress(pgGet
, 1);
734 pg
.addProgress(pgProcess
, 1);
737 File file
= getFile(luid
, pgGet
);
740 SupportType type
= SupportType
.valueOfAllOkUC(meta
.getType());
741 URL url
= file
.toURI().toURL();
743 story
= BasicSupport
.getSupport(type
, url
) //
746 // Because we do not want to clear the meta cache:
747 meta
.setCover(story
.getMeta().getCover());
748 meta
.setResume(story
.getMeta().getResume());
752 throw new IOException("Unknown type: " + meta
.getType());
754 } catch (IOException e
) {
755 // We should not have not-supported files in the
757 Instance
.getInstance().getTraceHandler().error(new IOException(
758 String
.format("Cannot load file of type '%s' from library: %s", meta
.getType(), file
), e
));
768 * Import the {@link Story} at the given {@link URL} into the
769 * {@link BasicLibrary}.
772 * the {@link URL} to import
774 * the optional progress reporter
776 * @return the imported Story {@link MetaData}
778 * @throws UnknownHostException
779 * if the host is not supported
780 * @throws IOException
781 * in case of I/O error
783 public MetaData
imprt(URL url
, Progress pg
) throws IOException
{
787 pg
.setMinMax(0, 1000);
788 Progress pgProcess
= new Progress();
789 Progress pgSave
= new Progress();
790 pg
.addProgress(pgProcess
, 800);
791 pg
.addProgress(pgSave
, 200);
793 BasicSupport support
= BasicSupport
.getSupport(url
);
794 if (support
== null) {
795 throw new UnknownHostException("" + url
);
798 Story story
= save(support
.process(pgProcess
), pgSave
);
801 return story
.getMeta();
805 * Import the story from one library to another, and keep the same LUID.
808 * the other library to import from
812 * the optional progress reporter
814 * @throws IOException
815 * in case of I/O error
817 public void imprt(BasicLibrary other
, String luid
, Progress pg
)
819 Progress pgGetStory
= new Progress();
820 Progress pgSave
= new Progress();
826 pg
.addProgress(pgGetStory
, 1);
827 pg
.addProgress(pgSave
, 1);
829 Story story
= other
.getStory(luid
, pgGetStory
);
831 story
= this.save(story
, luid
, pgSave
);
835 throw new IOException("Cannot find story in Library: " + luid
);
840 * Export the {@link Story} to the given target in the given format.
843 * the {@link Story} ID
845 * the {@link OutputType} to transform it to
847 * the target to save to
849 * the optional progress reporter
851 * @return the saved resource (the main saved {@link File})
853 * @throws IOException
854 * in case of I/O error
856 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
858 Progress pgGetStory
= new Progress();
859 Progress pgOut
= new Progress();
862 pg
.addProgress(pgGetStory
, 1);
863 pg
.addProgress(pgOut
, 1);
866 BasicOutput out
= BasicOutput
.getOutput(type
, false, false);
868 throw new IOException("Output type not supported: " + type
);
871 Story story
= getStory(luid
, pgGetStory
);
873 throw new IOException("Cannot find story to export: " + luid
);
876 return out
.process(story
, target
, pgOut
);
880 * Save a {@link Story} to the {@link BasicLibrary}.
883 * the {@link Story} to save
885 * the optional progress reporter
887 * @return the same {@link Story}, whose LUID may have changed
889 * @throws IOException
890 * in case of I/O error
892 public Story
save(Story story
, Progress pg
) throws IOException
{
893 return save(story
, null, pg
);
897 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
898 * be correct, or NULL to get the next free one.
900 * Will override any previous {@link Story} with the same LUID.
903 * the {@link Story} to save
905 * the <b>correct</b> LUID or NULL to get the next free one
907 * the optional progress reporter
909 * @return the same {@link Story}, whose LUID may have changed
911 * @throws IOException
912 * in case of I/O error
914 public synchronized Story
save(Story story
, String luid
, Progress pg
)
917 Instance
.getInstance().getTraceHandler().trace(this.getClass().getSimpleName() + ": saving story " + luid
);
919 // Do not change the original metadata, but change the original story
920 MetaData meta
= story
.getMeta().clone();
923 if (luid
== null || luid
.isEmpty()) {
924 meta
.setLuid(String
.format("%03d", getNextId()));
929 if (luid
!= null && getInfo(luid
) != null) {
933 story
= doSave(story
, pg
);
935 updateInfo(story
.getMeta());
937 Instance
.getInstance().getTraceHandler()
938 .trace(this.getClass().getSimpleName() + ": story saved (" + luid
+ ")");
944 * Delete the given {@link Story} from this {@link BasicLibrary}.
947 * the LUID of the target {@link Story}
949 * @throws IOException
950 * in case of I/O error
952 public synchronized void delete(String luid
) throws IOException
{
953 Instance
.getInstance().getTraceHandler().trace(this.getClass().getSimpleName() + ": deleting story " + luid
);
956 invalidateInfo(luid
);
958 Instance
.getInstance().getTraceHandler()
959 .trace(this.getClass().getSimpleName() + ": story deleted (" + luid
964 * Change the type (source) of the given {@link Story}.
967 * the {@link Story} LUID
971 * the optional progress reporter
973 * @throws IOException
974 * in case of I/O error or if the {@link Story} was not found
976 public synchronized void changeSource(String luid
, String newSource
,
977 Progress pg
) throws IOException
{
978 MetaData meta
= getInfo(luid
);
980 throw new IOException("Story not found: " + luid
);
983 changeSTA(luid
, newSource
, meta
.getTitle(), meta
.getAuthor(), pg
);
987 * Change the title (name) of the given {@link Story}.
990 * the {@link Story} LUID
994 * the optional progress reporter
996 * @throws IOException
997 * in case of I/O error or if the {@link Story} was not found
999 public synchronized void changeTitle(String luid
, String newTitle
,
1000 Progress pg
) throws IOException
{
1001 MetaData meta
= getInfo(luid
);
1003 throw new IOException("Story not found: " + luid
);
1006 changeSTA(luid
, meta
.getSource(), newTitle
, meta
.getAuthor(), pg
);
1010 * Change the author of the given {@link Story}.
1013 * the {@link Story} LUID
1017 * the optional progress reporter
1019 * @throws IOException
1020 * in case of I/O error or if the {@link Story} was not found
1022 public synchronized void changeAuthor(String luid
, String newAuthor
,
1023 Progress pg
) throws IOException
{
1024 MetaData meta
= getInfo(luid
);
1026 throw new IOException("Story not found: " + luid
);
1029 changeSTA(luid
, meta
.getSource(), meta
.getTitle(), newAuthor
, pg
);
1033 * Change the Source, Title and Author of the {@link Story} in one single
1037 * the {@link Story} LUID
1045 * the optional progress reporter
1047 * @throws IOException
1048 * in case of I/O error or if the {@link Story} was not found
1050 protected synchronized void changeSTA(String luid
, String newSource
,
1051 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
1052 MetaData meta
= getInfo(luid
);
1054 throw new IOException("Story not found: " + luid
);
1057 meta
.setSource(newSource
);
1058 meta
.setTitle(newTitle
);
1059 meta
.setAuthor(newAuthor
);
1062 invalidateInfo(luid
);
1066 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
1067 * change) for this {@link Story}.
1069 * By default, delete the old {@link Story} then recreate a new
1072 * Note that this behaviour can lead to data loss in case of problems!
1075 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
1077 * the optional {@link Progress}
1079 * @throws IOException
1080 * in case of I/O error or if the {@link Story} was not found
1082 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
1083 throws IOException
{
1085 pg
= new Progress();
1088 Progress pgGet
= new Progress();
1089 Progress pgSet
= new Progress();
1090 pg
.addProgress(pgGet
, 50);
1091 pg
.addProgress(pgSet
, 50);
1093 Story story
= getStory(meta
.getLuid(), pgGet
);
1094 if (story
== null) {
1095 throw new IOException("Story not found: " + meta
.getLuid());
1098 // TODO: this is not safe!
1099 delete(meta
.getLuid());
1100 story
.setMeta(meta
);
1101 save(story
, meta
.getLuid(), pgSet
);