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 * @return the corresponding {@link Story}
54 public abstract File
getFile(String luid
);
57 * Return the cover image associated to this story.
60 * the Library UID of the story
62 * @return the cover image
64 public abstract BufferedImage
getCover(String luid
);
67 * Return the cover image associated to this source.
69 * By default, return the cover of the first story with this source.
74 * @return the cover image or NULL
76 public BufferedImage
getSourceCover(String source
) {
77 List
<MetaData
> metas
= getListBySource(source
);
78 if (metas
.size() > 0) {
79 return getCover(metas
.get(0).getLuid());
86 * Fix the source cover to the given story cover.
89 * the source to change
93 public abstract void setSourceCover(String source
, String luid
);
96 * Return the list of stories (represented by their {@link MetaData}, which
97 * <b>MAY</b> not have the cover included).
100 * the optional {@link Progress}
102 * @return the list (can be empty but not NULL)
104 protected abstract List
<MetaData
> getMetas(Progress pg
);
107 * Invalidate the {@link Story} cache (when the content should be re-read
108 * because it was changed).
110 protected abstract void clearCache();
113 * Return the next LUID that can be used.
115 * @return the next luid
117 protected abstract int getNextId();
120 * Delete the target {@link Story}.
123 * the LUID of the {@link Story}
125 * @throws IOException
126 * in case of I/O error or if the {@link Story} wa not found
128 protected abstract void doDelete(String luid
) throws IOException
;
131 * Actually save the story to the back-end.
134 * the {@link Story} to save
136 * the optional {@link Progress}
138 * @return the saved {@link Story} (which may have changed, especially
139 * regarding the {@link MetaData})
141 * @throws IOException
142 * in case of I/O error
144 protected abstract Story
doSave(Story story
, Progress pg
)
148 * Refresh the {@link BasicLibrary}, that is, make sure all stories are
152 * force the full content of the stories to be loaded, not just
153 * the {@link MetaData}
156 * the optional progress reporter
158 public void refresh(boolean full
, Progress pg
) {
161 List
<MetaData
> metas
= getMetas(pg
);
162 for (MetaData meta
: metas
) {
163 getStory(meta
.getLuid(), null);
171 * List all the known types (sources) of stories.
173 * @return the sources
175 public synchronized List
<String
> getSources() {
176 List
<String
> list
= new ArrayList
<String
>();
177 for (MetaData meta
: getMetas(null)) {
178 String storySource
= meta
.getSource();
179 if (!list
.contains(storySource
)) {
180 list
.add(storySource
);
184 Collections
.sort(list
);
189 * List all the known authors of stories.
191 * @return the authors
193 public synchronized List
<String
> getAuthors() {
194 List
<String
> list
= new ArrayList
<String
>();
195 for (MetaData meta
: getMetas(null)) {
196 String storyAuthor
= meta
.getAuthor();
197 if (!list
.contains(storyAuthor
)) {
198 list
.add(storyAuthor
);
202 Collections
.sort(list
);
207 * List all the stories in the {@link BasicLibrary}.
209 * Cover images not included.
211 * @return the stories
213 public synchronized List
<MetaData
> getList() {
214 return getMetas(null);
218 * List all the stories of the given source type in the {@link BasicLibrary}
219 * , or all the stories if NULL is passed as a type.
221 * Cover images not included.
224 * the type of story to retrieve, or NULL for all
226 * @return the stories
228 public synchronized List
<MetaData
> getListBySource(String type
) {
229 List
<MetaData
> list
= new ArrayList
<MetaData
>();
230 for (MetaData meta
: getMetas(null)) {
231 String storyType
= meta
.getSource();
232 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
237 Collections
.sort(list
);
242 * List all the stories of the given author in the {@link BasicLibrary}, or
243 * all the stories if NULL is passed as an author.
245 * Cover images not included.
248 * the author of the stories to retrieve, or NULL for all
250 * @return the stories
252 public synchronized List
<MetaData
> getListByAuthor(String author
) {
253 List
<MetaData
> list
= new ArrayList
<MetaData
>();
254 for (MetaData meta
: getMetas(null)) {
255 String storyAuthor
= meta
.getAuthor();
256 if (author
== null || author
.equalsIgnoreCase(storyAuthor
)) {
261 Collections
.sort(list
);
266 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
267 * cover image <b>MAY</b> not be included.
270 * the Library UID of the story
272 * @return the corresponding {@link Story}
274 public synchronized MetaData
getInfo(String luid
) {
276 for (MetaData meta
: getMetas(null)) {
277 if (luid
.equals(meta
.getLuid())) {
287 * Retrieve a specific {@link Story}.
290 * the Library UID of the story
292 * the optional progress reporter
294 * @return the corresponding {@link Story} or NULL if not found
296 public synchronized Story
getStory(String luid
, Progress pg
) {
303 for (MetaData meta
: getMetas(null)) {
304 if (meta
.getLuid().equals(luid
)) {
305 File file
= getFile(luid
);
307 SupportType type
= SupportType
.valueOfAllOkUC(meta
309 URL url
= file
.toURI().toURL();
311 story
= BasicSupport
.getSupport(type
).process(url
, pg
);
313 throw new IOException("Unknown type: " + meta
.getType());
315 } catch (IOException e
) {
316 // We should not have not-supported files in the
318 Instance
.syserr(new IOException(
319 "Cannot load file from library: " + file
, e
));
332 * Import the {@link Story} at the given {@link URL} into the
333 * {@link BasicLibrary}.
336 * the {@link URL} to import
338 * the optional progress reporter
340 * @return the imported {@link Story}
342 * @throws IOException
343 * in case of I/O error
345 public Story
imprt(URL url
, Progress pg
) throws IOException
{
346 BasicSupport support
= BasicSupport
.getSupport(url
);
347 if (support
== null) {
348 throw new IOException("URL not supported: " + url
.toString());
351 return save(support
.process(url
, pg
), null);
355 * Import the story from one library to another, and keep the same LUID.
358 * the other library to import from
362 * the optional progress reporter
364 * @throws IOException
365 * in case of I/O error
367 public void imprt(BasicLibrary other
, String luid
, Progress pg
)
369 Progress pgGetStory
= new Progress();
370 Progress pgSave
= new Progress();
376 pg
.addProgress(pgGetStory
, 1);
377 pg
.addProgress(pgSave
, 1);
379 Story story
= other
.getStory(luid
, pgGetStory
);
381 story
= this.save(story
, luid
, pgSave
);
385 throw new IOException("Cannot find story in Library: " + luid
);
390 * Export the {@link Story} to the given target in the given format.
393 * the {@link Story} ID
395 * the {@link OutputType} to transform it to
397 * the target to save to
399 * the optional progress reporter
401 * @return the saved resource (the main saved {@link File})
403 * @throws IOException
404 * in case of I/O error
406 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
408 Progress pgGetStory
= new Progress();
409 Progress pgOut
= new Progress();
412 pg
.addProgress(pgGetStory
, 1);
413 pg
.addProgress(pgOut
, 1);
416 BasicOutput out
= BasicOutput
.getOutput(type
, false);
418 throw new IOException("Output type not supported: " + type
);
421 Story story
= getStory(luid
, pgGetStory
);
423 throw new IOException("Cannot find story to export: " + luid
);
426 return out
.process(story
, target
, pgOut
);
430 * Save a {@link Story} to the {@link BasicLibrary}.
433 * the {@link Story} to save
435 * the optional progress reporter
437 * @return the same {@link Story}, whose LUID may have changed
439 * @throws IOException
440 * in case of I/O error
442 public Story
save(Story story
, Progress pg
) throws IOException
{
443 return save(story
, null, pg
);
447 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
448 * be correct, or NULL to get the next free one.
450 * Will override any previous {@link Story} with the same LUID.
453 * the {@link Story} to save
455 * the <b>correct</b> LUID or NULL to get the next free one
457 * the optional progress reporter
459 * @return the same {@link Story}, whose LUID may have changed
461 * @throws IOException
462 * in case of I/O error
464 public synchronized Story
save(Story story
, String luid
, Progress pg
)
466 // Do not change the original metadata, but change the original story
467 MetaData meta
= story
.getMeta().clone();
470 if (luid
== null || luid
.isEmpty()) {
471 meta
.setLuid(String
.format("%03d", getNextId()));
476 if (getInfo(luid
) != null) {
488 * Delete the given {@link Story} from this {@link BasicLibrary}.
491 * the LUID of the target {@link Story}
493 * @throws IOException
494 * in case of I/O error
496 public synchronized void delete(String luid
) throws IOException
{
502 * Change the type (source) of the given {@link Story}.
505 * the {@link Story} LUID
509 * the optional progress reporter
511 * @throws IOException
512 * in case of I/O error or if the {@link Story} was not found
514 public synchronized void changeSource(String luid
, String newSource
,
515 Progress pg
) throws IOException
{
516 MetaData meta
= getInfo(luid
);
518 throw new IOException("Story not found: " + luid
);
521 meta
.setSource(newSource
);
526 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
527 * change) for this {@link Story}.
529 * By default, delete the old {@link Story} then recreate a new
532 * Note that this behaviour can lead to data loss.
535 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
537 * the optional {@link Progress}
539 * @throws IOException
540 * in case of I/O error or if the {@link Story} was not found
542 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
548 Progress pgGet
= new Progress();
549 Progress pgSet
= new Progress();
550 pg
.addProgress(pgGet
, 50);
551 pg
.addProgress(pgSet
, 50);
553 Story story
= getStory(meta
.getLuid(), pgGet
);
555 throw new IOException("Story not found: " + meta
.getLuid());
558 delete(meta
.getLuid());
561 save(story
, meta
.getLuid(), pgSet
);