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. */
44 /** The library is invalid (not correctly set up). */
46 /** You are not allowed to access this library. */
48 /** The library is currently out of commission. */
53 * Return a name for this library (the UI may display this).
57 * @return the name, or an empty {@link String} if none
59 public String
getLibraryName() {
66 * @return the current status
68 public Status
getStatus() {
73 * Retrieve the main {@link File} corresponding to the given {@link Story},
74 * which can be passed to an external reader or instance.
76 * Do <b>NOT</b> alter this file.
79 * the Library UID of the story
81 * the optional {@link Progress}
83 * @return the corresponding {@link Story}
85 public abstract File
getFile(String luid
, Progress pg
);
88 * Return the cover image associated to this story.
91 * the Library UID of the story
93 * @return the cover image
95 public abstract Image
getCover(String luid
);
98 * Return the cover image associated to this source.
100 * By default, return the custom cover if any, and if not, return the cover
101 * of the first story with this source.
106 * @return the cover image or NULL
108 public Image
getSourceCover(String source
) {
109 Image custom
= getCustomSourceCover(source
);
110 if (custom
!= null) {
114 List
<MetaData
> metas
= getListBySource(source
);
115 if (metas
.size() > 0) {
116 return getCover(metas
.get(0).getLuid());
123 * Return the custom cover image associated to this source.
125 * By default, return NULL.
128 * the source to look for
130 * @return the custom cover or NULL if none
132 public Image
getCustomSourceCover(@SuppressWarnings("unused") String source
) {
137 * Fix the source cover to the given story cover.
140 * the source to change
144 public abstract void setSourceCover(String source
, String luid
);
147 * Return the list of stories (represented by their {@link MetaData}, which
148 * <b>MAY</b> not have the cover included).
151 * the optional {@link Progress}
153 * @return the list (can be empty but not NULL)
155 protected abstract List
<MetaData
> getMetas(Progress pg
);
158 * Invalidate the {@link Story} cache (when the content should be re-read
159 * because it was changed).
161 protected void invalidateInfo() {
162 invalidateInfo(null);
166 * Invalidate the {@link Story} cache (when the content is removed).
168 * All the cache can be deleted if NULL is passed as meta.
171 * the LUID of the {@link Story} to clear from the cache, or NULL
174 protected abstract void invalidateInfo(String luid
);
177 * Invalidate the {@link Story} cache (when the content has changed, but we
178 * already have it) with the new given meta.
181 * the {@link Story} to clear from the cache
183 protected abstract void updateInfo(MetaData meta
);
186 * Return the next LUID that can be used.
188 * @return the next luid
190 protected abstract int getNextId();
193 * Delete the target {@link Story}.
196 * the LUID of the {@link Story}
198 * @throws IOException
199 * in case of I/O error or if the {@link Story} wa not found
201 protected abstract void doDelete(String luid
) throws IOException
;
204 * Actually save the story to the back-end.
207 * the {@link Story} to save
209 * the optional {@link Progress}
211 * @return the saved {@link Story} (which may have changed, especially
212 * regarding the {@link MetaData})
214 * @throws IOException
215 * in case of I/O error
217 protected abstract Story
doSave(Story story
, Progress pg
)
221 * Refresh the {@link BasicLibrary}, that is, make sure all metas are
225 * the optional progress reporter
227 public void refresh(Progress pg
) {
232 * List all the known types (sources) of stories.
234 * @return the sources
236 public synchronized List
<String
> getSources() {
237 List
<String
> list
= new ArrayList
<String
>();
238 for (MetaData meta
: getMetas(null)) {
239 String storySource
= meta
.getSource();
240 if (!list
.contains(storySource
)) {
241 list
.add(storySource
);
245 Collections
.sort(list
);
250 * List all the known types (sources) of stories, grouped by directory
251 * ("Source_1/a" and "Source_1/b" will be grouped into "Source_1").
253 * Note that an empty item in the list means a non-grouped source (type) --
254 * e.g., you could have for Source_1:
256 * <li><tt></tt>: empty, so source is "Source_1"</li>
257 * <li><tt>a</tt>: empty, so source is "Source_1/a"</li>
258 * <li><tt>b</tt>: empty, so source is "Source_1/b"</li>
261 * @return the grouped list
263 public synchronized Map
<String
, List
<String
>> getSourcesGrouped() {
264 Map
<String
, List
<String
>> map
= new TreeMap
<String
, List
<String
>>();
265 for (String source
: getSources()) {
269 int pos
= source
.indexOf('/');
270 if (pos
> 0 && pos
< source
.length() - 1) {
271 name
= source
.substring(0, pos
);
272 subname
= source
.substring(pos
+ 1);
279 List
<String
> list
= map
.get(name
);
281 list
= new ArrayList
<String
>();
291 * List all the known authors of stories.
293 * @return the authors
295 public synchronized List
<String
> getAuthors() {
296 List
<String
> list
= new ArrayList
<String
>();
297 for (MetaData meta
: getMetas(null)) {
298 String storyAuthor
= meta
.getAuthor();
299 if (!list
.contains(storyAuthor
)) {
300 list
.add(storyAuthor
);
304 Collections
.sort(list
);
309 * Return the list of authors, grouped by starting letter(s) if needed.
311 * If the number of author is not too high, only one group with an empty
312 * name and all the authors will be returned.
314 * If not, the authors will be separated into groups:
316 * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
318 * <li><tt>0-9</tt>: any authors whose name starts with a number</li>
319 * <li><tt>A-C</tt> (for instance): any author whose name starts with
320 * <tt>A</tt>, <tt>B</tt> or <tt>C</tt></li>
322 * Note that the letters used in the groups can vary (except <tt>*</tt> and
323 * <tt>0-9</tt>, which may only be present or not).
325 * @return the authors' names, grouped by letter(s)
327 public Map
<String
, List
<String
>> getAuthorsGrouped() {
330 Map
<String
, List
<String
>> groups
= new TreeMap
<String
, List
<String
>>();
331 List
<String
> authors
= getAuthors();
333 // If all authors fit the max, just report them as is
334 if (authors
.size() <= MAX
) {
335 groups
.put("", authors
);
339 // Create groups A to Z, which can be empty here
340 for (char car
= 'A'; car
<= 'Z'; car
++) {
341 groups
.put(Character
.toString(car
), getAuthorsGroup(authors
, car
));
345 List
<String
> keys
= new ArrayList
<String
>(groups
.keySet());
346 for (int i
= 0; i
+ 1 < keys
.size(); i
++) {
347 String keyNow
= keys
.get(i
);
348 String keyNext
= keys
.get(i
+ 1);
350 List
<String
> now
= groups
.get(keyNow
);
351 List
<String
> next
= groups
.get(keyNext
);
353 int currentTotal
= now
.size() + next
.size();
354 if (currentTotal
<= MAX
) {
355 String key
= keyNow
.charAt(0) + "-"
356 + keyNext
.charAt(keyNext
.length() - 1);
358 List
<String
> all
= new ArrayList
<String
>();
362 groups
.remove(keyNow
);
363 groups
.remove(keyNext
);
364 groups
.put(key
, all
);
366 keys
.set(i
, key
); // set the new key instead of key(i)
367 keys
.remove(i
+ 1); // remove the next, consumed key
368 i
--; // restart at key(i)
372 // Add "special" groups
373 groups
.put("*", getAuthorsGroup(authors
, '*'));
374 groups
.put("0-9", getAuthorsGroup(authors
, '0'));
376 // Prune empty groups
377 keys
= new ArrayList
<String
>(groups
.keySet());
378 for (String key
: keys
) {
379 if (groups
.get(key
).isEmpty()) {
388 * Get all the authors that start with the given character:
390 * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
392 * <li><tt>0</tt>: any authors whose name starts with a number</li>
393 * <li><tt>A</tt> (any capital latin letter): any author whose name starts
394 * with <tt>A</tt></li>
398 * the full list of authors
400 * the starting character, <tt>*</tt>, <tt>0</tt> or a capital
402 * @return the authors that fulfill the starting letter
404 private List
<String
> getAuthorsGroup(List
<String
> authors
, char car
) {
405 List
<String
> accepted
= new ArrayList
<String
>();
406 for (String author
: authors
) {
408 for (int i
= 0; first
== '*' && i
< author
.length(); i
++) {
409 String san
= StringUtils
.sanitize(author
, true, true);
410 char c
= san
.charAt(i
);
411 if (c
>= '0' && c
<= '9') {
413 } else if (c
>= 'a' && c
<= 'z') {
414 first
= (char) (c
- 'a' + 'A');
415 } else if (c
>= 'A' && c
<= 'Z') {
421 accepted
.add(author
);
429 * List all the stories in the {@link BasicLibrary}.
431 * Cover images <b>MAYBE</b> not included.
433 * @return the stories
435 public synchronized List
<MetaData
> getList() {
436 return getMetas(null);
440 * List all the stories of the given source type in the {@link BasicLibrary}
441 * , or all the stories if NULL is passed as a type.
443 * Cover images not included.
446 * the type of story to retrieve, or NULL for all
448 * @return the stories
450 public synchronized List
<MetaData
> getListBySource(String type
) {
451 List
<MetaData
> list
= new ArrayList
<MetaData
>();
452 for (MetaData meta
: getMetas(null)) {
453 String storyType
= meta
.getSource();
454 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
459 Collections
.sort(list
);
464 * List all the stories of the given author in the {@link BasicLibrary}, or
465 * all the stories if NULL is passed as an author.
467 * Cover images not included.
470 * the author of the stories to retrieve, or NULL for all
472 * @return the stories
474 public synchronized List
<MetaData
> getListByAuthor(String author
) {
475 List
<MetaData
> list
= new ArrayList
<MetaData
>();
476 for (MetaData meta
: getMetas(null)) {
477 String storyAuthor
= meta
.getAuthor();
478 if (author
== null || author
.equalsIgnoreCase(storyAuthor
)) {
483 Collections
.sort(list
);
488 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
489 * cover image <b>MAY</b> not be included.
492 * the Library UID of the story
494 * @return the corresponding {@link Story}
496 public synchronized MetaData
getInfo(String luid
) {
498 for (MetaData meta
: getMetas(null)) {
499 if (luid
.equals(meta
.getLuid())) {
509 * Retrieve a specific {@link Story}.
512 * the Library UID of the story
514 * the optional progress reporter
516 * @return the corresponding {@link Story} or NULL if not found
518 public synchronized Story
getStory(String luid
, Progress pg
) {
523 Progress pgGet
= new Progress();
524 Progress pgProcess
= new Progress();
527 pg
.addProgress(pgGet
, 1);
528 pg
.addProgress(pgProcess
, 1);
531 for (MetaData meta
: getMetas(null)) {
532 if (meta
.getLuid().equals(luid
)) {
533 File file
= getFile(luid
, pgGet
);
536 SupportType type
= SupportType
.valueOfAllOkUC(meta
538 URL url
= file
.toURI().toURL();
540 story
= BasicSupport
.getSupport(type
, url
) //
543 // Because we do not want to clear the meta cache:
544 meta
.setCover(story
.getMeta().getCover());
545 meta
.setResume(story
.getMeta().getResume());
549 throw new IOException("Unknown type: " + meta
.getType());
551 } catch (IOException e
) {
552 // We should not have not-supported files in the
554 Instance
.getTraceHandler().error(
555 new IOException("Cannot load file from library: "
570 * Import the {@link Story} at the given {@link URL} into the
571 * {@link BasicLibrary}.
574 * the {@link URL} to import
576 * the optional progress reporter
578 * @return the imported {@link Story}
580 * @throws UnknownHostException
581 * if the host is not supported
582 * @throws IOException
583 * in case of I/O error
585 public Story
imprt(URL url
, Progress pg
) throws IOException
{
589 pg
.setMinMax(0, 1000);
590 Progress pgProcess
= new Progress();
591 Progress pgSave
= new Progress();
592 pg
.addProgress(pgProcess
, 800);
593 pg
.addProgress(pgSave
, 200);
595 BasicSupport support
= BasicSupport
.getSupport(url
);
596 if (support
== null) {
597 throw new UnknownHostException("" + url
);
600 Story story
= save(support
.process(pgProcess
), pgSave
);
607 * Import the story from one library to another, and keep the same LUID.
610 * the other library to import from
614 * the optional progress reporter
616 * @throws IOException
617 * in case of I/O error
619 public void imprt(BasicLibrary other
, String luid
, Progress pg
)
621 Progress pgGetStory
= new Progress();
622 Progress pgSave
= new Progress();
628 pg
.addProgress(pgGetStory
, 1);
629 pg
.addProgress(pgSave
, 1);
631 Story story
= other
.getStory(luid
, pgGetStory
);
633 story
= this.save(story
, luid
, pgSave
);
637 throw new IOException("Cannot find story in Library: " + luid
);
642 * Export the {@link Story} to the given target in the given format.
645 * the {@link Story} ID
647 * the {@link OutputType} to transform it to
649 * the target to save to
651 * the optional progress reporter
653 * @return the saved resource (the main saved {@link File})
655 * @throws IOException
656 * in case of I/O error
658 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
660 Progress pgGetStory
= new Progress();
661 Progress pgOut
= new Progress();
664 pg
.addProgress(pgGetStory
, 1);
665 pg
.addProgress(pgOut
, 1);
668 BasicOutput out
= BasicOutput
.getOutput(type
, false, false);
670 throw new IOException("Output type not supported: " + type
);
673 Story story
= getStory(luid
, pgGetStory
);
675 throw new IOException("Cannot find story to export: " + luid
);
678 return out
.process(story
, target
, pgOut
);
682 * Save a {@link Story} to the {@link BasicLibrary}.
685 * the {@link Story} to save
687 * the optional progress reporter
689 * @return the same {@link Story}, whose LUID may have changed
691 * @throws IOException
692 * in case of I/O error
694 public Story
save(Story story
, Progress pg
) throws IOException
{
695 return save(story
, null, pg
);
699 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
700 * be correct, or NULL to get the next free one.
702 * Will override any previous {@link Story} with the same LUID.
705 * the {@link Story} to save
707 * the <b>correct</b> LUID or NULL to get the next free one
709 * the optional progress reporter
711 * @return the same {@link Story}, whose LUID may have changed
713 * @throws IOException
714 * in case of I/O error
716 public synchronized Story
save(Story story
, String luid
, Progress pg
)
719 Instance
.getTraceHandler().trace(
720 this.getClass().getSimpleName() + ": saving story " + luid
);
722 // Do not change the original metadata, but change the original story
723 MetaData meta
= story
.getMeta().clone();
726 if (luid
== null || luid
.isEmpty()) {
727 meta
.setLuid(String
.format("%03d", getNextId()));
732 if (luid
!= null && getInfo(luid
) != null) {
736 story
= doSave(story
, pg
);
738 updateInfo(story
.getMeta());
740 Instance
.getTraceHandler().trace(
741 this.getClass().getSimpleName() + ": story saved (" + luid
748 * Delete the given {@link Story} from this {@link BasicLibrary}.
751 * the LUID of the target {@link Story}
753 * @throws IOException
754 * in case of I/O error
756 public synchronized void delete(String luid
) throws IOException
{
757 Instance
.getTraceHandler().trace(
758 this.getClass().getSimpleName() + ": deleting story " + luid
);
761 invalidateInfo(luid
);
763 Instance
.getTraceHandler().trace(
764 this.getClass().getSimpleName() + ": story deleted (" + luid
769 * Change the type (source) of the given {@link Story}.
772 * the {@link Story} LUID
776 * the optional progress reporter
778 * @throws IOException
779 * in case of I/O error or if the {@link Story} was not found
781 public synchronized void changeSource(String luid
, String newSource
,
782 Progress pg
) throws IOException
{
783 MetaData meta
= getInfo(luid
);
785 throw new IOException("Story not found: " + luid
);
788 changeSTA(luid
, newSource
, meta
.getTitle(), meta
.getAuthor(), pg
);
792 * Change the title (name) of the given {@link Story}.
795 * the {@link Story} LUID
799 * the optional progress reporter
801 * @throws IOException
802 * in case of I/O error or if the {@link Story} was not found
804 public synchronized void changeTitle(String luid
, String newTitle
,
805 Progress pg
) throws IOException
{
806 MetaData meta
= getInfo(luid
);
808 throw new IOException("Story not found: " + luid
);
811 changeSTA(luid
, meta
.getSource(), newTitle
, meta
.getAuthor(), pg
);
815 * Change the author of the given {@link Story}.
818 * the {@link Story} LUID
822 * the optional progress reporter
824 * @throws IOException
825 * in case of I/O error or if the {@link Story} was not found
827 public synchronized void changeAuthor(String luid
, String newAuthor
,
828 Progress pg
) throws IOException
{
829 MetaData meta
= getInfo(luid
);
831 throw new IOException("Story not found: " + luid
);
834 changeSTA(luid
, meta
.getSource(), meta
.getTitle(), newAuthor
, pg
);
838 * Change the Source, Title and Author of the {@link Story} in one single
842 * the {@link Story} LUID
850 * the optional progress reporter
852 * @throws IOException
853 * in case of I/O error or if the {@link Story} was not found
855 protected synchronized void changeSTA(String luid
, String newSource
,
856 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
857 MetaData meta
= getInfo(luid
);
859 throw new IOException("Story not found: " + luid
);
862 meta
.setSource(newSource
);
863 meta
.setTitle(newTitle
);
864 meta
.setAuthor(newAuthor
);
869 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
870 * change) for this {@link Story}.
872 * By default, delete the old {@link Story} then recreate a new
875 * Note that this behaviour can lead to data loss in case of problems!
878 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
880 * the optional {@link Progress}
882 * @throws IOException
883 * in case of I/O error or if the {@link Story} was not found
885 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
891 Progress pgGet
= new Progress();
892 Progress pgSet
= new Progress();
893 pg
.addProgress(pgGet
, 50);
894 pg
.addProgress(pgSet
, 50);
896 Story story
= getStory(meta
.getLuid(), pgGet
);
898 throw new IOException("Story not found: " + meta
.getLuid());
901 // TODO: this is not safe!
902 delete(meta
.getLuid());
904 save(story
, meta
.getLuid(), pgSet
);