cf17a2bef49fce8591150c6e93eb50eb9b41a30b
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 * Retrieve the main {@link File} corresponding to the given {@link Story},
34 * which can be passed to an external reader or instance.
36 * Do <b>NOT</b> alter this file.
39 * the Library UID of the story
41 * @return the corresponding {@link Story}
43 public abstract File
getFile(String luid
);
46 * Return the cover image associated to this story.
49 * the Library UID of the story
51 * @return the cover image
53 public abstract BufferedImage
getCover(String luid
);
56 * Return the cover image associated to this source.
58 * By default, return the cover of the first story with this source.
63 * @return the cover image or NULL
65 public BufferedImage
getSourceCover(String source
) {
66 List
<MetaData
> metas
= getListBySource(source
);
67 if (metas
.size() > 0) {
68 return getCover(metas
.get(0).getLuid());
75 * Fix the source cover to the given story cover.
78 * the source to change
82 public abstract void setSourceCover(String source
, String luid
);
85 * Return the list of stories (represented by their {@link MetaData}, which
86 * <b>MAY</b> not have the cover included).
89 * the optional {@link Progress}
91 * @return the list (can be empty but not NULL)
93 protected abstract List
<MetaData
> getMetas(Progress pg
);
96 * Invalidate the {@link Story} cache (when the content should be re-read
97 * because it was changed).
99 protected abstract void clearCache();
102 * Return the next LUID that can be used.
104 * @return the next luid
106 protected abstract int getNextId();
109 * Delete the target {@link Story}.
112 * the LUID of the {@link Story}
114 * @throws IOException
115 * in case of I/O error or if the {@link Story} wa not found
117 protected abstract void doDelete(String luid
) throws IOException
;
120 * Actually save the story to the back-end.
123 * the {@link Story} to save
125 * the optional {@link Progress}
127 * @return the saved {@link Story} (which may have changed, especially
128 * regarding the {@link MetaData})
130 * @throws IOException
131 * in case of I/O error
133 protected abstract Story
doSave(Story story
, Progress pg
)
137 * Refresh the {@link BasicLibrary}, that is, make sure all stories are
141 * force the full content of the stories to be loaded, not just
142 * the {@link MetaData}
145 * the optional progress reporter
147 public void refresh(boolean full
, Progress pg
) {
150 List
<MetaData
> metas
= getMetas(pg
);
151 for (MetaData meta
: metas
) {
152 getStory(meta
.getLuid(), null);
160 * List all the known types (sources) of stories.
162 * @return the sources
164 public synchronized List
<String
> getSources() {
165 List
<String
> list
= new ArrayList
<String
>();
166 for (MetaData meta
: getMetas(null)) {
167 String storySource
= meta
.getSource();
168 if (!list
.contains(storySource
)) {
169 list
.add(storySource
);
173 Collections
.sort(list
);
178 * List all the known authors of stories.
180 * @return the authors
182 public synchronized List
<String
> getAuthors() {
183 List
<String
> list
= new ArrayList
<String
>();
184 for (MetaData meta
: getMetas(null)) {
185 String storyAuthor
= meta
.getAuthor();
186 if (!list
.contains(storyAuthor
)) {
187 list
.add(storyAuthor
);
191 Collections
.sort(list
);
196 * List all the stories in the {@link BasicLibrary}.
198 * Cover images not included.
200 * @return the stories
202 public synchronized List
<MetaData
> getList() {
203 return getMetas(null);
207 * List all the stories of the given source type in the {@link BasicLibrary}
208 * , or all the stories if NULL is passed as a type.
210 * Cover images not included.
213 * the type of story to retrieve, or NULL for all
215 * @return the stories
217 public synchronized List
<MetaData
> getListBySource(String type
) {
218 List
<MetaData
> list
= new ArrayList
<MetaData
>();
219 for (MetaData meta
: getMetas(null)) {
220 String storyType
= meta
.getSource();
221 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
226 Collections
.sort(list
);
231 * List all the stories of the given author in the {@link BasicLibrary}, or
232 * all the stories if NULL is passed as an author.
234 * Cover images not included.
237 * the author of the stories to retrieve, or NULL for all
239 * @return the stories
241 public synchronized List
<MetaData
> getListByAuthor(String author
) {
242 List
<MetaData
> list
= new ArrayList
<MetaData
>();
243 for (MetaData meta
: getMetas(null)) {
244 String storyAuthor
= meta
.getAuthor();
245 if (author
== null || author
.equalsIgnoreCase(storyAuthor
)) {
250 Collections
.sort(list
);
255 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
256 * cover image <b>MAY</b> not be included.
259 * the Library UID of the story
261 * @return the corresponding {@link Story}
263 public synchronized MetaData
getInfo(String luid
) {
265 for (MetaData meta
: getMetas(null)) {
266 if (luid
.equals(meta
.getLuid())) {
276 * Retrieve a specific {@link Story}.
279 * the Library UID of the story
281 * the optional progress reporter
283 * @return the corresponding {@link Story} or NULL if not found
285 public synchronized Story
getStory(String luid
, Progress pg
) {
292 for (MetaData meta
: getMetas(null)) {
293 if (meta
.getLuid().equals(luid
)) {
294 File file
= getFile(luid
);
296 SupportType type
= SupportType
.valueOfAllOkUC(meta
298 URL url
= file
.toURI().toURL();
300 story
= BasicSupport
.getSupport(type
).process(url
, pg
);
302 throw new IOException("Unknown type: " + meta
.getType());
304 } catch (IOException e
) {
305 // We should not have not-supported files in the
307 Instance
.syserr(new IOException(
308 "Cannot load file from library: " + file
, e
));
321 * Import the {@link Story} at the given {@link URL} into the
322 * {@link BasicLibrary}.
325 * the {@link URL} to import
327 * the optional progress reporter
329 * @return the imported {@link Story}
331 * @throws IOException
332 * in case of I/O error
334 public Story
imprt(URL url
, Progress pg
) throws IOException
{
335 BasicSupport support
= BasicSupport
.getSupport(url
);
336 if (support
== null) {
337 throw new IOException("URL not supported: " + url
.toString());
340 return save(support
.process(url
, pg
), null);
344 * Import the story from one library to another, and keep the same LUID.
347 * the other library to import from
351 * the optional progress reporter
353 * @throws IOException
354 * in case of I/O error
356 public void imprt(BasicLibrary other
, String luid
, Progress pg
)
358 Progress pgGetStory
= new Progress();
359 Progress pgSave
= new Progress();
365 pg
.addProgress(pgGetStory
, 1);
366 pg
.addProgress(pgSave
, 1);
368 Story story
= other
.getStory(luid
, pgGetStory
);
370 story
= this.save(story
, luid
, pgSave
);
374 throw new IOException("Cannot find story in Library: " + luid
);
379 * Export the {@link Story} to the given target in the given format.
382 * the {@link Story} ID
384 * the {@link OutputType} to transform it to
386 * the target to save to
388 * the optional progress reporter
390 * @return the saved resource (the main saved {@link File})
392 * @throws IOException
393 * in case of I/O error
395 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
397 Progress pgGetStory
= new Progress();
398 Progress pgOut
= new Progress();
401 pg
.addProgress(pgGetStory
, 1);
402 pg
.addProgress(pgOut
, 1);
405 BasicOutput out
= BasicOutput
.getOutput(type
, false);
407 throw new IOException("Output type not supported: " + type
);
410 Story story
= getStory(luid
, pgGetStory
);
412 throw new IOException("Cannot find story to export: " + luid
);
415 return out
.process(story
, target
, pgOut
);
419 * Save a {@link Story} to the {@link BasicLibrary}.
422 * the {@link Story} to save
424 * the optional progress reporter
426 * @return the same {@link Story}, whose LUID may have changed
428 * @throws IOException
429 * in case of I/O error
431 public Story
save(Story story
, Progress pg
) throws IOException
{
432 return save(story
, null, pg
);
436 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
437 * be correct, or NULL to get the next free one.
439 * Will override any previous {@link Story} with the same LUID.
442 * the {@link Story} to save
444 * the <b>correct</b> LUID or NULL to get the next free one
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 synchronized Story
save(Story story
, String luid
, Progress pg
)
455 // Do not change the original metadata, but change the original story
456 MetaData meta
= story
.getMeta().clone();
459 if (luid
== null || luid
.isEmpty()) {
460 meta
.setLuid(String
.format("%03d", getNextId()));
465 if (getInfo(luid
) != null) {
477 * Delete the given {@link Story} from this {@link BasicLibrary}.
480 * the LUID of the target {@link Story}
482 * @throws IOException
483 * in case of I/O error
485 public synchronized void delete(String luid
) throws IOException
{
491 * Change the type (source) of the given {@link Story}.
494 * the {@link Story} LUID
498 * the optional progress reporter
500 * @throws IOException
501 * in case of I/O error or if the {@link Story} was not found
503 public synchronized void changeSource(String luid
, String newSource
,
504 Progress pg
) throws IOException
{
505 MetaData meta
= getInfo(luid
);
507 throw new IOException("Story not found: " + luid
);
510 meta
.setSource(newSource
);
515 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
516 * change) for this {@link Story}.
518 * By default, delete the old {@link Story} then recreate a new
521 * Note that this behaviour can lead to data loss.
524 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
526 * the optional {@link Progress}
528 * @throws IOException
529 * in case of I/O error or if the {@link Story} was not found
531 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
537 Progress pgGet
= new Progress();
538 Progress pgSet
= new Progress();
539 pg
.addProgress(pgGet
, 50);
540 pg
.addProgress(pgSet
, 50);
542 Story story
= getStory(meta
.getLuid(), pgGet
);
544 throw new IOException("Story not found: " + meta
.getLuid());
547 delete(meta
.getLuid());
550 save(story
, meta
.getLuid(), pgSet
);