58bd873a25175f38a0385bc525db340f226d2db8
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 be
.nikiroo
.fanfix
.Instance
;
12 import be
.nikiroo
.fanfix
.data
.MetaData
;
13 import be
.nikiroo
.fanfix
.data
.Story
;
14 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
16 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
17 import be
.nikiroo
.fanfix
.supported
.SupportType
;
18 import be
.nikiroo
.utils
.Image
;
19 import be
.nikiroo
.utils
.Progress
;
22 * Manage a library of Stories: import, export, list, modify.
24 * Each {@link Story} object will be associated with a (local to the library)
25 * unique ID, the LUID, which will be used to identify the {@link Story}.
27 * Most of the {@link BasicLibrary} functions work on a partial (cover
28 * <b>MAY</b> not be included) {@link MetaData} object.
32 abstract public class BasicLibrary
{
34 * A {@link BasicLibrary} status.
39 /** The library is ready. */
41 /** The library is invalid (not correctly set up). */
43 /** You are not allowed to access this library. */
45 /** The library is currently out of commission. */
50 * Return a name for this library (the UI may display this).
54 * @return the name, or an empty {@link String} if none
56 public String
getLibraryName() {
63 * @return the current status
65 public Status
getStatus() {
70 * Retrieve the main {@link File} corresponding to the given {@link Story},
71 * which can be passed to an external reader or instance.
73 * Do <b>NOT</b> alter this file.
76 * the Library UID of the story
78 * the optional {@link Progress}
80 * @return the corresponding {@link Story}
82 public abstract File
getFile(String luid
, Progress pg
);
85 * Return the cover image associated to this story.
88 * the Library UID of the story
90 * @return the cover image
92 public abstract Image
getCover(String luid
);
95 * Return the cover image associated to this source.
97 * By default, return the cover of the first story with this source.
102 * @return the cover image or NULL
104 public Image
getSourceCover(String source
) {
105 List
<MetaData
> metas
= getListBySource(source
);
106 if (metas
.size() > 0) {
107 return getCover(metas
.get(0).getLuid());
114 * Fix the source cover to the given story cover.
117 * the source to change
121 public abstract void setSourceCover(String source
, String luid
);
124 * Return the list of stories (represented by their {@link MetaData}, which
125 * <b>MAY</b> not have the cover included).
128 * the optional {@link Progress}
130 * @return the list (can be empty but not NULL)
132 protected abstract List
<MetaData
> getMetas(Progress pg
);
135 * Invalidate the {@link Story} cache (when the content should be re-read
136 * because it was changed).
138 protected void deleteInfo() {
143 * Invalidate the {@link Story} cache (when the content is removed).
145 * All the cache can be deleted if NULL is passed as meta.
148 * the LUID of the {@link Story} to clear from the cache, or NULL
151 protected abstract void deleteInfo(String luid
);
154 * Invalidate the {@link Story} cache (when the content has changed, but we
155 * already have it) with the new given meta.
158 * the {@link Story} to clear from the cache
160 protected abstract void updateInfo(MetaData meta
);
163 * Return the next LUID that can be used.
165 * @return the next luid
167 protected abstract int getNextId();
170 * Delete the target {@link Story}.
173 * the LUID of the {@link Story}
175 * @throws IOException
176 * in case of I/O error or if the {@link Story} wa not found
178 protected abstract void doDelete(String luid
) throws IOException
;
181 * Actually save the story to the back-end.
184 * the {@link Story} to save
186 * the optional {@link Progress}
188 * @return the saved {@link Story} (which may have changed, especially
189 * regarding the {@link MetaData})
191 * @throws IOException
192 * in case of I/O error
194 protected abstract Story
doSave(Story story
, Progress pg
)
198 * Refresh the {@link BasicLibrary}, that is, make sure all metas are
202 * the optional progress reporter
204 public void refresh(Progress pg
) {
209 * List all the known types (sources) of stories.
211 * @return the sources
213 public synchronized List
<String
> getSources() {
214 List
<String
> list
= new ArrayList
<String
>();
215 for (MetaData meta
: getMetas(null)) {
216 String storySource
= meta
.getSource();
217 if (!list
.contains(storySource
)) {
218 list
.add(storySource
);
222 Collections
.sort(list
);
227 * List all the known authors of stories.
229 * @return the authors
231 public synchronized List
<String
> getAuthors() {
232 List
<String
> list
= new ArrayList
<String
>();
233 for (MetaData meta
: getMetas(null)) {
234 String storyAuthor
= meta
.getAuthor();
235 if (!list
.contains(storyAuthor
)) {
236 list
.add(storyAuthor
);
240 Collections
.sort(list
);
245 * List all the stories in the {@link BasicLibrary}.
247 * Cover images not included.
249 * @return the stories
251 public synchronized List
<MetaData
> getList() {
252 return getMetas(null);
256 * List all the stories of the given source type in the {@link BasicLibrary}
257 * , or all the stories if NULL is passed as a type.
259 * Cover images not included.
262 * the type of story to retrieve, or NULL for all
264 * @return the stories
266 public synchronized List
<MetaData
> getListBySource(String type
) {
267 List
<MetaData
> list
= new ArrayList
<MetaData
>();
268 for (MetaData meta
: getMetas(null)) {
269 String storyType
= meta
.getSource();
270 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
275 Collections
.sort(list
);
280 * List all the stories of the given author in the {@link BasicLibrary}, or
281 * all the stories if NULL is passed as an author.
283 * Cover images not included.
286 * the author of the stories to retrieve, or NULL for all
288 * @return the stories
290 public synchronized List
<MetaData
> getListByAuthor(String author
) {
291 List
<MetaData
> list
= new ArrayList
<MetaData
>();
292 for (MetaData meta
: getMetas(null)) {
293 String storyAuthor
= meta
.getAuthor();
294 if (author
== null || author
.equalsIgnoreCase(storyAuthor
)) {
299 Collections
.sort(list
);
304 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
305 * cover image <b>MAY</b> not be included.
308 * the Library UID of the story
310 * @return the corresponding {@link Story}
312 public synchronized MetaData
getInfo(String luid
) {
314 for (MetaData meta
: getMetas(null)) {
315 if (luid
.equals(meta
.getLuid())) {
325 * Retrieve a specific {@link Story}.
328 * the Library UID of the story
330 * the optional progress reporter
332 * @return the corresponding {@link Story} or NULL if not found
334 public synchronized Story
getStory(String luid
, Progress pg
) {
339 Progress pgGet
= new Progress();
340 Progress pgProcess
= new Progress();
343 pg
.addProgress(pgGet
, 1);
344 pg
.addProgress(pgProcess
, 1);
347 for (MetaData meta
: getMetas(null)) {
348 if (meta
.getLuid().equals(luid
)) {
349 File file
= getFile(luid
, pgGet
);
352 SupportType type
= SupportType
.valueOfAllOkUC(meta
354 URL url
= file
.toURI().toURL();
356 story
= BasicSupport
.getSupport(type
, url
) //
358 // Because we do not want to clear the meta cache:
359 meta
.setCover(story
.getMeta().getCover());
363 throw new IOException("Unknown type: " + meta
.getType());
365 } catch (IOException e
) {
366 // We should not have not-supported files in the
368 Instance
.getTraceHandler().error(
369 new IOException("Cannot load file from library: "
384 * Import the {@link Story} at the given {@link URL} into the
385 * {@link BasicLibrary}.
388 * the {@link URL} to import
390 * the optional progress reporter
392 * @return the imported {@link Story}
394 * @throws UnknownHostException
395 * if the host is not supported
396 * @throws IOException
397 * in case of I/O error
399 public Story
imprt(URL url
, Progress pg
) throws IOException
{
400 BasicSupport support
= BasicSupport
.getSupport(url
);
401 if (support
== null) {
402 throw new UnknownHostException("" + url
);
405 return save(support
.process(pg
), null);
409 * Import the story from one library to another, and keep the same LUID.
412 * the other library to import from
416 * the optional progress reporter
418 * @throws IOException
419 * in case of I/O error
421 public void imprt(BasicLibrary other
, String luid
, Progress pg
)
423 Progress pgGetStory
= new Progress();
424 Progress pgSave
= new Progress();
430 pg
.addProgress(pgGetStory
, 1);
431 pg
.addProgress(pgSave
, 1);
433 Story story
= other
.getStory(luid
, pgGetStory
);
435 story
= this.save(story
, luid
, pgSave
);
439 throw new IOException("Cannot find story in Library: " + luid
);
444 * Export the {@link Story} to the given target in the given format.
447 * the {@link Story} ID
449 * the {@link OutputType} to transform it to
451 * the target to save to
453 * the optional progress reporter
455 * @return the saved resource (the main saved {@link File})
457 * @throws IOException
458 * in case of I/O error
460 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
462 Progress pgGetStory
= new Progress();
463 Progress pgOut
= new Progress();
466 pg
.addProgress(pgGetStory
, 1);
467 pg
.addProgress(pgOut
, 1);
470 BasicOutput out
= BasicOutput
.getOutput(type
, false, false);
472 throw new IOException("Output type not supported: " + type
);
475 Story story
= getStory(luid
, pgGetStory
);
477 throw new IOException("Cannot find story to export: " + luid
);
480 return out
.process(story
, target
, pgOut
);
484 * Save a {@link Story} to the {@link BasicLibrary}.
487 * the {@link Story} to save
489 * the optional progress reporter
491 * @return the same {@link Story}, whose LUID may have changed
493 * @throws IOException
494 * in case of I/O error
496 public Story
save(Story story
, Progress pg
) throws IOException
{
497 return save(story
, null, pg
);
501 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
502 * be correct, or NULL to get the next free one.
504 * Will override any previous {@link Story} with the same LUID.
507 * the {@link Story} to save
509 * the <b>correct</b> LUID or NULL to get the next free one
511 * the optional progress reporter
513 * @return the same {@link Story}, whose LUID may have changed
515 * @throws IOException
516 * in case of I/O error
518 public synchronized Story
save(Story story
, String luid
, Progress pg
)
521 // Do not change the original metadata, but change the original story
522 MetaData meta
= story
.getMeta().clone();
525 if (luid
== null || luid
.isEmpty()) {
526 meta
.setLuid(String
.format("%03d", getNextId()));
531 if (luid
!= null && getInfo(luid
) != null) {
535 story
= doSave(story
, pg
);
537 updateInfo(story
.getMeta());
543 * Delete the given {@link Story} from this {@link BasicLibrary}.
546 * the LUID of the target {@link Story}
548 * @throws IOException
549 * in case of I/O error
551 public synchronized void delete(String luid
) throws IOException
{
557 * Change the type (source) of the given {@link Story}.
560 * the {@link Story} LUID
564 * the optional progress reporter
566 * @throws IOException
567 * in case of I/O error or if the {@link Story} was not found
569 public synchronized void changeSource(String luid
, String newSource
,
570 Progress pg
) throws IOException
{
571 MetaData meta
= getInfo(luid
);
573 throw new IOException("Story not found: " + luid
);
576 meta
.setSource(newSource
);
581 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
582 * change) for this {@link Story}.
584 * By default, delete the old {@link Story} then recreate a new
587 * Note that this behaviour can lead to data loss.
590 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
592 * the optional {@link Progress}
594 * @throws IOException
595 * in case of I/O error or if the {@link Story} was not found
597 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
603 Progress pgGet
= new Progress();
604 Progress pgSet
= new Progress();
605 pg
.addProgress(pgGet
, 50);
606 pg
.addProgress(pgSet
, 50);
608 Story story
= getStory(meta
.getLuid(), pgGet
);
610 throw new IOException("Story not found: " + meta
.getLuid());
613 delete(meta
.getLuid());
616 save(story
, meta
.getLuid(), pgSet
);