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
;
126 * Return the cover image associated to this source.
128 * By default, return the custom cover if any, and if not, return the cover
129 * of the first story with this source.
134 * @return the cover image or NULL
136 * @throws IOException
137 * in case of IOException
139 public Image
getSourceCover(String source
) throws IOException
{
140 Image custom
= getCustomSourceCover(source
);
141 if (custom
!= null) {
145 List
<MetaData
> metas
= getListBySource(source
);
146 if (metas
.size() > 0) {
147 return getCover(metas
.get(0).getLuid());
154 * Return the cover image associated to this author.
156 * By default, return the custom cover if any, and if not, return the cover
157 * of the first story with this author.
162 * @return the cover image or NULL
164 * @throws IOException
165 * in case of IOException
167 public Image
getAuthorCover(String author
) throws IOException
{
168 Image custom
= getCustomAuthorCover(author
);
169 if (custom
!= null) {
173 List
<MetaData
> metas
= getListByAuthor(author
);
174 if (metas
.size() > 0) {
175 return getCover(metas
.get(0).getLuid());
182 * Return the custom cover image associated to this source.
184 * By default, return NULL.
187 * the source to look for
189 * @return the custom cover or NULL if none
191 * @throws IOException
192 * in case of IOException
194 @SuppressWarnings("unused")
195 public Image
getCustomSourceCover(String source
) throws IOException
{
200 * Return the custom cover image associated to this author.
202 * By default, return NULL.
205 * the author to look for
207 * @return the custom cover or NULL if none
209 * @throws IOException
210 * in case of IOException
212 @SuppressWarnings("unused")
213 public Image
getCustomAuthorCover(String author
) throws IOException
{
218 * Set the source cover to the given story cover.
221 * the source to change
225 * @throws IOException
226 * in case of IOException
228 public abstract void setSourceCover(String source
, String luid
)
232 * Set the author cover to the given story cover.
235 * the author to change
239 * @throws IOException
240 * in case of IOException
242 public abstract void setAuthorCover(String author
, String luid
)
246 * Return the list of stories (represented by their {@link MetaData}, which
247 * <b>MAY</b> not have the cover included).
250 * the optional {@link Progress}
252 * @return the list (can be empty but not NULL)
254 * @throws IOException
255 * in case of IOException
257 protected abstract List
<MetaData
> getMetas(Progress pg
) throws IOException
;
260 * Invalidate the {@link Story} cache (when the content should be re-read
261 * because it was changed).
263 protected void invalidateInfo() {
264 invalidateInfo(null);
268 * Invalidate the {@link Story} cache (when the content is removed).
270 * All the cache can be deleted if NULL is passed as meta.
273 * the LUID of the {@link Story} to clear from the cache, or NULL
276 protected abstract void invalidateInfo(String luid
);
279 * Invalidate the {@link Story} cache (when the content has changed, but we
280 * already have it) with the new given meta.
283 * the {@link Story} to clear from the cache
285 * @throws IOException
286 * in case of IOException
288 protected abstract void updateInfo(MetaData meta
) throws IOException
;
291 * Return the next LUID that can be used.
293 * @return the next luid
295 protected abstract int getNextId();
298 * Delete the target {@link Story}.
301 * the LUID of the {@link Story}
303 * @throws IOException
304 * in case of I/O error or if the {@link Story} wa not found
306 protected abstract void doDelete(String luid
) throws IOException
;
309 * Actually save the story to the back-end.
312 * the {@link Story} to save
314 * the optional {@link Progress}
316 * @return the saved {@link Story} (which may have changed, especially
317 * regarding the {@link MetaData})
319 * @throws IOException
320 * in case of I/O error
322 protected abstract Story
doSave(Story story
, Progress pg
)
326 * Refresh the {@link BasicLibrary}, that is, make sure all metas are
330 * the optional progress reporter
332 public void refresh(Progress pg
) {
335 } catch (IOException e
) {
336 // We will let it fail later
341 * List all the known types (sources) of stories.
343 * @return the sources
345 * @throws IOException
346 * in case of IOException
348 public synchronized List
<String
> getSources() throws IOException
{
349 List
<String
> list
= new ArrayList
<String
>();
350 for (MetaData meta
: getMetas(null)) {
351 String storySource
= meta
.getSource();
352 if (!list
.contains(storySource
)) {
353 list
.add(storySource
);
357 Collections
.sort(list
);
362 * List all the known types (sources) of stories, grouped by directory
363 * ("Source_1/a" and "Source_1/b" will be grouped into "Source_1").
365 * Note that an empty item in the list means a non-grouped source (type) --
366 * e.g., you could have for Source_1:
368 * <li><tt></tt>: empty, so source is "Source_1"</li>
369 * <li><tt>a</tt>: empty, so source is "Source_1/a"</li>
370 * <li><tt>b</tt>: empty, so source is "Source_1/b"</li>
373 * @return the grouped list
375 * @throws IOException
376 * in case of IOException
378 public synchronized Map
<String
, List
<String
>> getSourcesGrouped()
380 Map
<String
, List
<String
>> map
= new TreeMap
<String
, List
<String
>>();
381 for (String source
: getSources()) {
385 int pos
= source
.indexOf('/');
386 if (pos
> 0 && pos
< source
.length() - 1) {
387 name
= source
.substring(0, pos
);
388 subname
= source
.substring(pos
+ 1);
395 List
<String
> list
= map
.get(name
);
397 list
= new ArrayList
<String
>();
407 * List all the known authors of stories.
409 * @return the authors
411 * @throws IOException
412 * in case of IOException
414 public synchronized List
<String
> getAuthors() throws IOException
{
415 List
<String
> list
= new ArrayList
<String
>();
416 for (MetaData meta
: getMetas(null)) {
417 String storyAuthor
= meta
.getAuthor();
418 if (!list
.contains(storyAuthor
)) {
419 list
.add(storyAuthor
);
423 Collections
.sort(list
);
428 * Return the list of authors, grouped by starting letter(s) if needed.
430 * If the number of author is not too high, only one group with an empty
431 * name and all the authors will be returned.
433 * If not, the authors will be separated into groups:
435 * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
437 * <li><tt>0-9</tt>: any authors whose name starts with a number</li>
438 * <li><tt>A-C</tt> (for instance): any author whose name starts with
439 * <tt>A</tt>, <tt>B</tt> or <tt>C</tt></li>
441 * Note that the letters used in the groups can vary (except <tt>*</tt> and
442 * <tt>0-9</tt>, which may only be present or not).
444 * @return the authors' names, grouped by letter(s)
446 * @throws IOException
447 * in case of IOException
449 public Map
<String
, List
<String
>> getAuthorsGrouped() throws IOException
{
452 Map
<String
, List
<String
>> groups
= new TreeMap
<String
, List
<String
>>();
453 List
<String
> authors
= getAuthors();
455 // If all authors fit the max, just report them as is
456 if (authors
.size() <= MAX
) {
457 groups
.put("", authors
);
461 // Create groups A to Z, which can be empty here
462 for (char car
= 'A'; car
<= 'Z'; car
++) {
463 groups
.put(Character
.toString(car
), getAuthorsGroup(authors
, car
));
467 List
<String
> keys
= new ArrayList
<String
>(groups
.keySet());
468 for (int i
= 0; i
+ 1 < keys
.size(); i
++) {
469 String keyNow
= keys
.get(i
);
470 String keyNext
= keys
.get(i
+ 1);
472 List
<String
> now
= groups
.get(keyNow
);
473 List
<String
> next
= groups
.get(keyNext
);
475 int currentTotal
= now
.size() + next
.size();
476 if (currentTotal
<= MAX
) {
477 String key
= keyNow
.charAt(0) + "-"
478 + keyNext
.charAt(keyNext
.length() - 1);
480 List
<String
> all
= new ArrayList
<String
>();
484 groups
.remove(keyNow
);
485 groups
.remove(keyNext
);
486 groups
.put(key
, all
);
488 keys
.set(i
, key
); // set the new key instead of key(i)
489 keys
.remove(i
+ 1); // remove the next, consumed key
490 i
--; // restart at key(i)
494 // Add "special" groups
495 groups
.put("*", getAuthorsGroup(authors
, '*'));
496 groups
.put("0-9", getAuthorsGroup(authors
, '0'));
498 // Prune empty groups
499 keys
= new ArrayList
<String
>(groups
.keySet());
500 for (String key
: keys
) {
501 if (groups
.get(key
).isEmpty()) {
510 * Get all the authors that start with the given character:
512 * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
514 * <li><tt>0</tt>: any authors whose name starts with a number</li>
515 * <li><tt>A</tt> (any capital latin letter): any author whose name starts
516 * with <tt>A</tt></li>
520 * the full list of authors
522 * the starting character, <tt>*</tt>, <tt>0</tt> or a capital
525 * @return the authors that fulfil the starting letter
527 private List
<String
> getAuthorsGroup(List
<String
> authors
, char car
) {
528 List
<String
> accepted
= new ArrayList
<String
>();
529 for (String author
: authors
) {
531 for (int i
= 0; first
== '*' && i
< author
.length(); i
++) {
532 String san
= StringUtils
.sanitize(author
, true, true);
533 char c
= san
.charAt(i
);
534 if (c
>= '0' && c
<= '9') {
536 } else if (c
>= 'a' && c
<= 'z') {
537 first
= (char) (c
- 'a' + 'A');
538 } else if (c
>= 'A' && c
<= 'Z') {
544 accepted
.add(author
);
552 * List all the stories in the {@link BasicLibrary}.
554 * Cover images <b>MAYBE</b> not included.
556 * @return the stories
558 * @throws IOException
559 * in case of IOException
561 public synchronized List
<MetaData
> getList() throws IOException
{
562 return getMetas(null);
566 * List all the stories of the given source type in the {@link BasicLibrary}
567 * , or all the stories if NULL is passed as a type.
569 * Cover images not included.
572 * the type of story to retrieve, or NULL for all
574 * @return the stories
576 * @throws IOException
577 * in case of IOException
579 public synchronized List
<MetaData
> getListBySource(String type
)
581 List
<MetaData
> list
= new ArrayList
<MetaData
>();
582 for (MetaData meta
: getMetas(null)) {
583 String storyType
= meta
.getSource();
584 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
589 Collections
.sort(list
);
594 * List all the stories of the given author in the {@link BasicLibrary}, or
595 * all the stories if NULL is passed as an author.
597 * Cover images not included.
600 * the author of the stories to retrieve, or NULL for all
602 * @return the stories
604 * @throws IOException
605 * in case of IOException
607 public synchronized List
<MetaData
> getListByAuthor(String author
)
609 List
<MetaData
> list
= new ArrayList
<MetaData
>();
610 for (MetaData meta
: getMetas(null)) {
611 String storyAuthor
= meta
.getAuthor();
612 if (author
== null || author
.equalsIgnoreCase(storyAuthor
)) {
617 Collections
.sort(list
);
622 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
623 * cover image <b>MAY</b> not be included.
626 * the Library UID of the story
628 * @return the corresponding {@link Story}
630 * @throws IOException
631 * in case of IOException
633 public synchronized MetaData
getInfo(String luid
) throws IOException
{
635 for (MetaData meta
: getMetas(null)) {
636 if (luid
.equals(meta
.getLuid())) {
646 * Retrieve a specific {@link Story}.
649 * the Library UID of the story
651 * the optional progress reporter
653 * @return the corresponding {@link Story} or NULL if not found
655 * @throws IOException
656 * in case of IOException
658 public synchronized Story
getStory(String luid
, Progress pg
)
660 Progress pgMetas
= new Progress();
661 Progress pgStory
= new Progress();
663 pg
.setMinMax(0, 100);
664 pg
.addProgress(pgMetas
, 10);
665 pg
.addProgress(pgStory
, 90);
668 MetaData meta
= null;
669 for (MetaData oneMeta
: getMetas(pgMetas
)) {
670 if (oneMeta
.getLuid().equals(luid
)) {
678 Story story
= getStory(luid
, meta
, pgStory
);
685 * Retrieve a specific {@link Story}.
688 * the meta of the story
690 * the optional progress reporter
692 * @return the corresponding {@link Story} or NULL if not found
694 * @throws IOException
695 * in case of IOException
697 public synchronized Story
getStory(String luid
,
698 @SuppressWarnings("javadoc") MetaData meta
, Progress pg
)
705 Progress pgGet
= new Progress();
706 Progress pgProcess
= new Progress();
709 pg
.addProgress(pgGet
, 1);
710 pg
.addProgress(pgProcess
, 1);
713 File file
= getFile(luid
, pgGet
);
716 SupportType type
= SupportType
.valueOfAllOkUC(meta
.getType());
717 URL url
= file
.toURI().toURL();
719 story
= BasicSupport
.getSupport(type
, url
) //
722 // Because we do not want to clear the meta cache:
723 meta
.setCover(story
.getMeta().getCover());
724 meta
.setResume(story
.getMeta().getResume());
728 throw new IOException("Unknown type: " + meta
.getType());
730 } catch (IOException e
) {
731 // We should not have not-supported files in the
733 Instance
.getTraceHandler().error(
734 new IOException(String
.format(
735 "Cannot load file of type '%s' from library: %s",
736 meta
.getType(), file
), e
));
746 * Import the {@link Story} at the given {@link URL} into the
747 * {@link BasicLibrary}.
750 * the {@link URL} to import
752 * the optional progress reporter
754 * @return the imported {@link Story}
756 * @throws UnknownHostException
757 * if the host is not supported
758 * @throws IOException
759 * in case of I/O error
761 public Story
imprt(URL url
, Progress pg
) throws IOException
{
765 pg
.setMinMax(0, 1000);
766 Progress pgProcess
= new Progress();
767 Progress pgSave
= new Progress();
768 pg
.addProgress(pgProcess
, 800);
769 pg
.addProgress(pgSave
, 200);
771 BasicSupport support
= BasicSupport
.getSupport(url
);
772 if (support
== null) {
773 throw new UnknownHostException("" + url
);
776 Story story
= save(support
.process(pgProcess
), pgSave
);
783 * Import the story from one library to another, and keep the same LUID.
786 * the other library to import from
790 * the optional progress reporter
792 * @throws IOException
793 * in case of I/O error
795 public void imprt(BasicLibrary other
, String luid
, Progress pg
)
797 Progress pgGetStory
= new Progress();
798 Progress pgSave
= new Progress();
804 pg
.addProgress(pgGetStory
, 1);
805 pg
.addProgress(pgSave
, 1);
807 Story story
= other
.getStory(luid
, pgGetStory
);
809 story
= this.save(story
, luid
, pgSave
);
813 throw new IOException("Cannot find story in Library: " + luid
);
818 * Export the {@link Story} to the given target in the given format.
821 * the {@link Story} ID
823 * the {@link OutputType} to transform it to
825 * the target to save to
827 * the optional progress reporter
829 * @return the saved resource (the main saved {@link File})
831 * @throws IOException
832 * in case of I/O error
834 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
836 Progress pgGetStory
= new Progress();
837 Progress pgOut
= new Progress();
840 pg
.addProgress(pgGetStory
, 1);
841 pg
.addProgress(pgOut
, 1);
844 BasicOutput out
= BasicOutput
.getOutput(type
, false, false);
846 throw new IOException("Output type not supported: " + type
);
849 Story story
= getStory(luid
, pgGetStory
);
851 throw new IOException("Cannot find story to export: " + luid
);
854 return out
.process(story
, target
, pgOut
);
858 * Save a {@link Story} to the {@link BasicLibrary}.
861 * the {@link Story} to save
863 * the optional progress reporter
865 * @return the same {@link Story}, whose LUID may have changed
867 * @throws IOException
868 * in case of I/O error
870 public Story
save(Story story
, Progress pg
) throws IOException
{
871 return save(story
, null, pg
);
875 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
876 * be correct, or NULL to get the next free one.
878 * Will override any previous {@link Story} with the same LUID.
881 * the {@link Story} to save
883 * the <b>correct</b> LUID or NULL to get the next free one
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 synchronized Story
save(Story story
, String luid
, Progress pg
)
895 Instance
.getTraceHandler().trace(
896 this.getClass().getSimpleName() + ": saving story " + luid
);
898 // Do not change the original metadata, but change the original story
899 MetaData meta
= story
.getMeta().clone();
902 if (luid
== null || luid
.isEmpty()) {
903 meta
.setLuid(String
.format("%03d", getNextId()));
908 if (luid
!= null && getInfo(luid
) != null) {
912 story
= doSave(story
, pg
);
914 updateInfo(story
.getMeta());
916 Instance
.getTraceHandler().trace(
917 this.getClass().getSimpleName() + ": story saved (" + luid
924 * Delete the given {@link Story} from this {@link BasicLibrary}.
927 * the LUID of the target {@link Story}
929 * @throws IOException
930 * in case of I/O error
932 public synchronized void delete(String luid
) throws IOException
{
933 Instance
.getTraceHandler().trace(
934 this.getClass().getSimpleName() + ": deleting story " + luid
);
937 invalidateInfo(luid
);
939 Instance
.getTraceHandler().trace(
940 this.getClass().getSimpleName() + ": story deleted (" + luid
945 * Change the type (source) of the given {@link Story}.
948 * the {@link Story} LUID
952 * the optional progress reporter
954 * @throws IOException
955 * in case of I/O error or if the {@link Story} was not found
957 public synchronized void changeSource(String luid
, String newSource
,
958 Progress pg
) throws IOException
{
959 MetaData meta
= getInfo(luid
);
961 throw new IOException("Story not found: " + luid
);
964 changeSTA(luid
, newSource
, meta
.getTitle(), meta
.getAuthor(), pg
);
968 * Change the title (name) of the given {@link Story}.
971 * the {@link Story} LUID
975 * the optional progress reporter
977 * @throws IOException
978 * in case of I/O error or if the {@link Story} was not found
980 public synchronized void changeTitle(String luid
, String newTitle
,
981 Progress pg
) throws IOException
{
982 MetaData meta
= getInfo(luid
);
984 throw new IOException("Story not found: " + luid
);
987 changeSTA(luid
, meta
.getSource(), newTitle
, meta
.getAuthor(), pg
);
991 * Change the author of the given {@link Story}.
994 * the {@link Story} LUID
998 * the optional progress reporter
1000 * @throws IOException
1001 * in case of I/O error or if the {@link Story} was not found
1003 public synchronized void changeAuthor(String luid
, String newAuthor
,
1004 Progress pg
) throws IOException
{
1005 MetaData meta
= getInfo(luid
);
1007 throw new IOException("Story not found: " + luid
);
1010 changeSTA(luid
, meta
.getSource(), meta
.getTitle(), newAuthor
, pg
);
1014 * Change the Source, Title and Author of the {@link Story} in one single
1018 * the {@link Story} LUID
1026 * the optional progress reporter
1028 * @throws IOException
1029 * in case of I/O error or if the {@link Story} was not found
1031 protected synchronized void changeSTA(String luid
, String newSource
,
1032 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
1033 MetaData meta
= getInfo(luid
);
1035 throw new IOException("Story not found: " + luid
);
1038 meta
.setSource(newSource
);
1039 meta
.setTitle(newTitle
);
1040 meta
.setAuthor(newAuthor
);
1043 invalidateInfo(luid
);
1047 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
1048 * change) for this {@link Story}.
1050 * By default, delete the old {@link Story} then recreate a new
1053 * Note that this behaviour can lead to data loss in case of problems!
1056 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
1058 * the optional {@link Progress}
1060 * @throws IOException
1061 * in case of I/O error or if the {@link Story} was not found
1063 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
1064 throws IOException
{
1066 pg
= new Progress();
1069 Progress pgGet
= new Progress();
1070 Progress pgSet
= new Progress();
1071 pg
.addProgress(pgGet
, 50);
1072 pg
.addProgress(pgSet
, 50);
1074 Story story
= getStory(meta
.getLuid(), pgGet
);
1075 if (story
== null) {
1076 throw new IOException("Story not found: " + meta
.getLuid());
1079 // TODO: this is not safe!
1080 delete(meta
.getLuid());
1081 story
.setMeta(meta
);
1082 save(story
, meta
.getLuid(), pgSet
);