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
.BasicSupport
.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 invalidateInfo() {
139 invalidateInfo(null);
143 * Invalidate the {@link Story} cache (when the content should be re-read
144 * because it was changed).
147 * the luid of the {@link Story} to clear from the cache, or NULL
150 protected abstract void invalidateInfo(String luid
);
153 * Return the next LUID that can be used.
155 * @return the next luid
157 protected abstract int getNextId();
160 * Delete the target {@link Story}.
163 * the LUID of the {@link Story}
165 * @throws IOException
166 * in case of I/O error or if the {@link Story} wa not found
168 protected abstract void doDelete(String luid
) throws IOException
;
171 * Actually save the story to the back-end.
174 * the {@link Story} to save
176 * the optional {@link Progress}
178 * @return the saved {@link Story} (which may have changed, especially
179 * regarding the {@link MetaData})
181 * @throws IOException
182 * in case of I/O error
184 protected abstract Story
doSave(Story story
, Progress pg
)
188 * Refresh the {@link BasicLibrary}, that is, make sure all metas are
192 * the optional progress reporter
194 public void refresh(Progress pg
) {
199 * List all the known types (sources) of stories.
201 * @return the sources
203 public synchronized List
<String
> getSources() {
204 List
<String
> list
= new ArrayList
<String
>();
205 for (MetaData meta
: getMetas(null)) {
206 String storySource
= meta
.getSource();
207 if (!list
.contains(storySource
)) {
208 list
.add(storySource
);
212 Collections
.sort(list
);
217 * List all the known authors of stories.
219 * @return the authors
221 public synchronized List
<String
> getAuthors() {
222 List
<String
> list
= new ArrayList
<String
>();
223 for (MetaData meta
: getMetas(null)) {
224 String storyAuthor
= meta
.getAuthor();
225 if (!list
.contains(storyAuthor
)) {
226 list
.add(storyAuthor
);
230 Collections
.sort(list
);
235 * List all the stories in the {@link BasicLibrary}.
237 * Cover images not included.
239 * @return the stories
241 public synchronized List
<MetaData
> getList() {
242 return getMetas(null);
246 * List all the stories of the given source type in the {@link BasicLibrary}
247 * , or all the stories if NULL is passed as a type.
249 * Cover images not included.
252 * the type of story to retrieve, or NULL for all
254 * @return the stories
256 public synchronized List
<MetaData
> getListBySource(String type
) {
257 List
<MetaData
> list
= new ArrayList
<MetaData
>();
258 for (MetaData meta
: getMetas(null)) {
259 String storyType
= meta
.getSource();
260 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
265 Collections
.sort(list
);
270 * List all the stories of the given author in the {@link BasicLibrary}, or
271 * all the stories if NULL is passed as an author.
273 * Cover images not included.
276 * the author of the stories to retrieve, or NULL for all
278 * @return the stories
280 public synchronized List
<MetaData
> getListByAuthor(String author
) {
281 List
<MetaData
> list
= new ArrayList
<MetaData
>();
282 for (MetaData meta
: getMetas(null)) {
283 String storyAuthor
= meta
.getAuthor();
284 if (author
== null || author
.equalsIgnoreCase(storyAuthor
)) {
289 Collections
.sort(list
);
294 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
295 * cover image <b>MAY</b> not be included.
298 * the Library UID of the story
300 * @return the corresponding {@link Story}
302 public synchronized MetaData
getInfo(String luid
) {
304 for (MetaData meta
: getMetas(null)) {
305 if (luid
.equals(meta
.getLuid())) {
315 * Retrieve a specific {@link Story}.
318 * the Library UID of the story
320 * the optional progress reporter
322 * @return the corresponding {@link Story} or NULL if not found
324 public synchronized Story
getStory(String luid
, Progress pg
) {
329 Progress pgGet
= new Progress();
330 Progress pgProcess
= new Progress();
333 pg
.addProgress(pgGet
, 1);
334 pg
.addProgress(pgProcess
, 1);
337 for (MetaData meta
: getMetas(null)) {
338 if (meta
.getLuid().equals(luid
)) {
339 File file
= getFile(luid
, pgGet
);
342 SupportType type
= SupportType
.valueOfAllOkUC(meta
344 URL url
= file
.toURI().toURL();
346 story
= BasicSupport
.getSupport(type
).process(url
,
348 // Because we do not want to clear the meta cache:
349 meta
.setCover(story
.getMeta().getCover());
353 throw new IOException("Unknown type: " + meta
.getType());
355 } catch (IOException e
) {
356 // We should not have not-supported files in the
358 Instance
.getTraceHandler().error(
359 new IOException("Cannot load file from library: "
374 * Import the {@link Story} at the given {@link URL} into the
375 * {@link BasicLibrary}.
378 * the {@link URL} to import
380 * the optional progress reporter
382 * @return the imported {@link Story}
384 * @throws UnknownHostException
385 * if the host is not supported
386 * @throws IOException
387 * in case of I/O error
389 public Story
imprt(URL url
, Progress pg
) throws IOException
{
390 BasicSupport support
= BasicSupport
.getSupport(url
);
391 if (support
== null) {
392 throw new UnknownHostException("" + url
);
395 return save(support
.process(url
, pg
), null);
399 * Import the story from one library to another, and keep the same LUID.
402 * the other library to import from
406 * the optional progress reporter
408 * @throws IOException
409 * in case of I/O error
411 public void imprt(BasicLibrary other
, String luid
, Progress pg
)
413 Progress pgGetStory
= new Progress();
414 Progress pgSave
= new Progress();
420 pg
.addProgress(pgGetStory
, 1);
421 pg
.addProgress(pgSave
, 1);
423 Story story
= other
.getStory(luid
, pgGetStory
);
425 story
= this.save(story
, luid
, pgSave
);
429 throw new IOException("Cannot find story in Library: " + luid
);
434 * Export the {@link Story} to the given target in the given format.
437 * the {@link Story} ID
439 * the {@link OutputType} to transform it to
441 * the target to save to
443 * the optional progress reporter
445 * @return the saved resource (the main saved {@link File})
447 * @throws IOException
448 * in case of I/O error
450 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
452 Progress pgGetStory
= new Progress();
453 Progress pgOut
= new Progress();
456 pg
.addProgress(pgGetStory
, 1);
457 pg
.addProgress(pgOut
, 1);
460 BasicOutput out
= BasicOutput
.getOutput(type
, false, false);
462 throw new IOException("Output type not supported: " + type
);
465 Story story
= getStory(luid
, pgGetStory
);
467 throw new IOException("Cannot find story to export: " + luid
);
470 return out
.process(story
, target
, pgOut
);
474 * Save a {@link Story} to the {@link BasicLibrary}.
477 * the {@link Story} to save
479 * the optional progress reporter
481 * @return the same {@link Story}, whose LUID may have changed
483 * @throws IOException
484 * in case of I/O error
486 public Story
save(Story story
, Progress pg
) throws IOException
{
487 return save(story
, null, pg
);
491 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
492 * be correct, or NULL to get the next free one.
494 * Will override any previous {@link Story} with the same LUID.
497 * the {@link Story} to save
499 * the <b>correct</b> LUID or NULL to get the next free one
501 * the optional progress reporter
503 * @return the same {@link Story}, whose LUID may have changed
505 * @throws IOException
506 * in case of I/O error
508 public synchronized Story
save(Story story
, String luid
, Progress pg
)
510 // Do not change the original metadata, but change the original story
511 MetaData meta
= story
.getMeta().clone();
514 if (luid
== null || luid
.isEmpty()) {
515 meta
.setLuid(String
.format("%03d", getNextId()));
520 if (luid
!= null && getInfo(luid
) != null) {
526 invalidateInfo(luid
);
532 * Delete the given {@link Story} from this {@link BasicLibrary}.
535 * the LUID of the target {@link Story}
537 * @throws IOException
538 * in case of I/O error
540 public synchronized void delete(String luid
) throws IOException
{
542 invalidateInfo(luid
);
546 * Change the type (source) of the given {@link Story}.
549 * the {@link Story} LUID
553 * the optional progress reporter
555 * @throws IOException
556 * in case of I/O error or if the {@link Story} was not found
558 public synchronized void changeSource(String luid
, String newSource
,
559 Progress pg
) throws IOException
{
560 MetaData meta
= getInfo(luid
);
562 throw new IOException("Story not found: " + luid
);
565 meta
.setSource(newSource
);
570 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
571 * change) for this {@link Story}.
573 * By default, delete the old {@link Story} then recreate a new
576 * Note that this behaviour can lead to data loss.
579 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
581 * the optional {@link Progress}
583 * @throws IOException
584 * in case of I/O error or if the {@link Story} was not found
586 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
592 Progress pgGet
= new Progress();
593 Progress pgSet
= new Progress();
594 pg
.addProgress(pgGet
, 50);
595 pg
.addProgress(pgSet
, 50);
597 Story story
= getStory(meta
.getLuid(), pgGet
);
599 throw new IOException("Story not found: " + meta
.getLuid());
602 delete(meta
.getLuid());
605 save(story
, meta
.getLuid(), pgSet
);