1 package be
.nikiroo
.fanfix
.library
;
3 import java
.awt
.image
.BufferedImage
;
5 import java
.io
.IOException
;
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
.Progress
;
21 * Manage a library of Stories: import, export, list, modify.
23 * Each {@link Story} object will be associated with a (local to the library)
24 * unique ID, the LUID, which will be used to identify the {@link Story}.
26 * Most of the {@link BasicLibrary} functions work on a partial (cover
27 * <b>MAY</b> not be included) {@link MetaData} object.
31 abstract public class BasicLibrary
{
33 * Return a name for this library (the UI may display this).
37 * @return the name, or an empty {@link String} if none
39 public String
getLibraryName() {
44 * Retrieve the main {@link File} corresponding to the given {@link Story},
45 * which can be passed to an external reader or instance.
47 * Do <b>NOT</b> alter this file.
50 * the Library UID of the story
52 * the optional {@link Progress}
54 * @return the corresponding {@link Story}
56 public abstract File
getFile(String luid
, Progress pg
);
59 * Return the cover image associated to this story.
62 * the Library UID of the story
64 * @return the cover image
66 public abstract BufferedImage
getCover(String luid
);
69 * Return the cover image associated to this source.
71 * By default, return the cover of the first story with this source.
76 * @return the cover image or NULL
78 public BufferedImage
getSourceCover(String source
) {
79 List
<MetaData
> metas
= getListBySource(source
);
80 if (metas
.size() > 0) {
81 return getCover(metas
.get(0).getLuid());
88 * Fix the source cover to the given story cover.
91 * the source to change
95 public abstract void setSourceCover(String source
, String luid
);
98 * Return the list of stories (represented by their {@link MetaData}, which
99 * <b>MAY</b> not have the cover included).
102 * the optional {@link Progress}
104 * @return the list (can be empty but not NULL)
106 protected abstract List
<MetaData
> getMetas(Progress pg
);
109 * Invalidate the {@link Story} cache (when the content should be re-read
110 * because it was changed).
112 protected abstract void clearCache();
115 * Return the next LUID that can be used.
117 * @return the next luid
119 protected abstract int getNextId();
122 * Delete the target {@link Story}.
125 * the LUID of the {@link Story}
127 * @throws IOException
128 * in case of I/O error or if the {@link Story} wa not found
130 protected abstract void doDelete(String luid
) throws IOException
;
133 * Actually save the story to the back-end.
136 * the {@link Story} to save
138 * the optional {@link Progress}
140 * @return the saved {@link Story} (which may have changed, especially
141 * regarding the {@link MetaData})
143 * @throws IOException
144 * in case of I/O error
146 protected abstract Story
doSave(Story story
, Progress pg
)
150 * Refresh the {@link BasicLibrary}, that is, make sure all stories are
154 * force the full content of the stories to be loaded, not just
155 * the {@link MetaData}
158 * the optional progress reporter
160 public void refresh(boolean full
, Progress pg
) {
163 List
<MetaData
> metas
= getMetas(pg
);
164 for (MetaData meta
: metas
) {
165 getStory(meta
.getLuid(), null);
173 * List all the known types (sources) of stories.
175 * @return the sources
177 public synchronized List
<String
> getSources() {
178 List
<String
> list
= new ArrayList
<String
>();
179 for (MetaData meta
: getMetas(null)) {
180 String storySource
= meta
.getSource();
181 if (!list
.contains(storySource
)) {
182 list
.add(storySource
);
186 Collections
.sort(list
);
191 * List all the known authors of stories.
193 * @return the authors
195 public synchronized List
<String
> getAuthors() {
196 List
<String
> list
= new ArrayList
<String
>();
197 for (MetaData meta
: getMetas(null)) {
198 String storyAuthor
= meta
.getAuthor();
199 if (!list
.contains(storyAuthor
)) {
200 list
.add(storyAuthor
);
204 Collections
.sort(list
);
209 * List all the stories in the {@link BasicLibrary}.
211 * Cover images not included.
213 * @return the stories
215 public synchronized List
<MetaData
> getList() {
216 return getMetas(null);
220 * List all the stories of the given source type in the {@link BasicLibrary}
221 * , or all the stories if NULL is passed as a type.
223 * Cover images not included.
226 * the type of story to retrieve, or NULL for all
228 * @return the stories
230 public synchronized List
<MetaData
> getListBySource(String type
) {
231 List
<MetaData
> list
= new ArrayList
<MetaData
>();
232 for (MetaData meta
: getMetas(null)) {
233 String storyType
= meta
.getSource();
234 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
239 Collections
.sort(list
);
244 * List all the stories of the given author in the {@link BasicLibrary}, or
245 * all the stories if NULL is passed as an author.
247 * Cover images not included.
250 * the author of the stories to retrieve, or NULL for all
252 * @return the stories
254 public synchronized List
<MetaData
> getListByAuthor(String author
) {
255 List
<MetaData
> list
= new ArrayList
<MetaData
>();
256 for (MetaData meta
: getMetas(null)) {
257 String storyAuthor
= meta
.getAuthor();
258 if (author
== null || author
.equalsIgnoreCase(storyAuthor
)) {
263 Collections
.sort(list
);
268 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
269 * cover image <b>MAY</b> not be included.
272 * the Library UID of the story
274 * @return the corresponding {@link Story}
276 public synchronized MetaData
getInfo(String luid
) {
278 for (MetaData meta
: getMetas(null)) {
279 if (luid
.equals(meta
.getLuid())) {
289 * Retrieve a specific {@link Story}.
292 * the Library UID of the story
294 * the optional progress reporter
296 * @return the corresponding {@link Story} or NULL if not found
298 public synchronized Story
getStory(String luid
, Progress pg
) {
303 Progress pgGet
= new Progress();
304 Progress pgProcess
= new Progress();
307 pg
.addProgress(pgGet
, 1);
308 pg
.addProgress(pgProcess
, 1);
311 for (MetaData meta
: getMetas(null)) {
312 if (meta
.getLuid().equals(luid
)) {
313 File file
= getFile(luid
, pgGet
);
316 SupportType type
= SupportType
.valueOfAllOkUC(meta
318 URL url
= file
.toURI().toURL();
320 story
= BasicSupport
.getSupport(type
).process(url
,
323 throw new IOException("Unknown type: " + meta
.getType());
325 } catch (IOException e
) {
326 // We should not have not-supported files in the
328 Instance
.syserr(new IOException(
329 "Cannot load file from library: " + file
, e
));
343 * Import the {@link Story} at the given {@link URL} into the
344 * {@link BasicLibrary}.
347 * the {@link URL} to import
349 * the optional progress reporter
351 * @return the imported {@link Story}
353 * @throws IOException
354 * in case of I/O error
356 public Story
imprt(URL url
, Progress pg
) throws IOException
{
357 BasicSupport support
= BasicSupport
.getSupport(url
);
358 if (support
== null) {
359 throw new IOException("URL not supported: " + url
.toString());
362 return save(support
.process(url
, pg
), null);
366 * Import the story from one library to another, and keep the same LUID.
369 * the other library to import from
373 * the optional progress reporter
375 * @throws IOException
376 * in case of I/O error
378 public void imprt(BasicLibrary other
, String luid
, Progress pg
)
380 Progress pgGetStory
= new Progress();
381 Progress pgSave
= new Progress();
387 pg
.addProgress(pgGetStory
, 1);
388 pg
.addProgress(pgSave
, 1);
390 Story story
= other
.getStory(luid
, pgGetStory
);
392 story
= this.save(story
, luid
, pgSave
);
396 throw new IOException("Cannot find story in Library: " + luid
);
401 * Export the {@link Story} to the given target in the given format.
404 * the {@link Story} ID
406 * the {@link OutputType} to transform it to
408 * the target to save to
410 * the optional progress reporter
412 * @return the saved resource (the main saved {@link File})
414 * @throws IOException
415 * in case of I/O error
417 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
419 Progress pgGetStory
= new Progress();
420 Progress pgOut
= new Progress();
423 pg
.addProgress(pgGetStory
, 1);
424 pg
.addProgress(pgOut
, 1);
427 BasicOutput out
= BasicOutput
.getOutput(type
, false);
429 throw new IOException("Output type not supported: " + type
);
432 Story story
= getStory(luid
, pgGetStory
);
434 throw new IOException("Cannot find story to export: " + luid
);
437 return out
.process(story
, target
, pgOut
);
441 * Save a {@link Story} to the {@link BasicLibrary}.
444 * the {@link Story} to save
446 * the optional progress reporter
448 * @return the same {@link Story}, whose LUID may have changed
450 * @throws IOException
451 * in case of I/O error
453 public Story
save(Story story
, Progress pg
) throws IOException
{
454 return save(story
, null, pg
);
458 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
459 * be correct, or NULL to get the next free one.
461 * Will override any previous {@link Story} with the same LUID.
464 * the {@link Story} to save
466 * the <b>correct</b> LUID or NULL to get the next free one
468 * the optional progress reporter
470 * @return the same {@link Story}, whose LUID may have changed
472 * @throws IOException
473 * in case of I/O error
475 public synchronized Story
save(Story story
, String luid
, Progress pg
)
477 // Do not change the original metadata, but change the original story
478 MetaData meta
= story
.getMeta().clone();
481 if (luid
== null || luid
.isEmpty()) {
482 meta
.setLuid(String
.format("%03d", getNextId()));
487 if (getInfo(luid
) != null) {
499 * Delete the given {@link Story} from this {@link BasicLibrary}.
502 * the LUID of the target {@link Story}
504 * @throws IOException
505 * in case of I/O error
507 public synchronized void delete(String luid
) throws IOException
{
513 * Change the type (source) of the given {@link Story}.
516 * the {@link Story} LUID
520 * the optional progress reporter
522 * @throws IOException
523 * in case of I/O error or if the {@link Story} was not found
525 public synchronized void changeSource(String luid
, String newSource
,
526 Progress pg
) throws IOException
{
527 MetaData meta
= getInfo(luid
);
529 throw new IOException("Story not found: " + luid
);
532 meta
.setSource(newSource
);
537 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
538 * change) for this {@link Story}.
540 * By default, delete the old {@link Story} then recreate a new
543 * Note that this behaviour can lead to data loss.
546 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
548 * the optional {@link Progress}
550 * @throws IOException
551 * in case of I/O error or if the {@link Story} was not found
553 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
559 Progress pgGet
= new Progress();
560 Progress pgSet
= new Progress();
561 pg
.addProgress(pgGet
, 50);
562 pg
.addProgress(pgSet
, 50);
564 Story story
= getStory(meta
.getLuid(), pgGet
);
566 throw new IOException("Story not found: " + meta
.getLuid());
569 delete(meta
.getLuid());
572 save(story
, meta
.getLuid(), pgSet
);