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 * Export the {@link Story} to the given target in the given format.
347 * the {@link Story} ID
349 * the {@link OutputType} to transform it to
351 * the target to save to
353 * the optional progress reporter
355 * @return the saved resource (the main saved {@link File})
357 * @throws IOException
358 * in case of I/O error
360 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
362 Progress pgGetStory
= new Progress();
363 Progress pgOut
= new Progress();
366 pg
.addProgress(pgGetStory
, 1);
367 pg
.addProgress(pgOut
, 1);
370 BasicOutput out
= BasicOutput
.getOutput(type
, false);
372 throw new IOException("Output type not supported: " + type
);
375 Story story
= getStory(luid
, pgGetStory
);
377 throw new IOException("Cannot find story to export: " + luid
);
380 return out
.process(story
, target
, pgOut
);
384 * Save a {@link Story} to the {@link BasicLibrary}.
387 * the {@link Story} to save
389 * the optional progress reporter
391 * @return the same {@link Story}, whose LUID may have changed
393 * @throws IOException
394 * in case of I/O error
396 public Story
save(Story story
, Progress pg
) throws IOException
{
397 return save(story
, null, pg
);
401 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
402 * be correct, or NULL to get the next free one.
404 * Will override any previous {@link Story} with the same LUID.
407 * the {@link Story} to save
409 * the <b>correct</b> LUID or NULL to get the next free one
411 * the optional progress reporter
413 * @return the same {@link Story}, whose LUID may have changed
415 * @throws IOException
416 * in case of I/O error
418 public synchronized Story
save(Story story
, String luid
, Progress pg
)
420 // Do not change the original metadata, but change the original story
421 MetaData meta
= story
.getMeta().clone();
424 if (luid
== null || luid
.isEmpty()) {
425 meta
.setLuid(String
.format("%03d", getNextId()));
430 if (getInfo(luid
) != null) {
442 * Delete the given {@link Story} from this {@link BasicLibrary}.
445 * the LUID of the target {@link Story}
447 * @throws IOException
448 * in case of I/O error
450 public synchronized void delete(String luid
) throws IOException
{
456 * Change the type (source) of the given {@link Story}.
459 * the {@link Story} LUID
463 * the optional progress reporter
465 * @throws IOException
466 * in case of I/O error or if the {@link Story} was not found
468 public synchronized void changeSource(String luid
, String newSource
,
469 Progress pg
) throws IOException
{
470 MetaData meta
= getInfo(luid
);
472 throw new IOException("Story not found: " + luid
);
475 meta
.setSource(newSource
);
480 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
481 * change) for this {@link Story}.
483 * By default, delete the old {@link Story} then recreate a new
486 * Note that this behaviour can lead to data loss.
489 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
491 * the optional {@link Progress}
493 * @throws IOException
494 * in case of I/O error or if the {@link Story} was not found
496 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
502 Progress pgGet
= new Progress();
503 Progress pgSet
= new Progress();
504 pg
.addProgress(pgGet
, 50);
505 pg
.addProgress(pgSet
, 50);
507 Story story
= getStory(meta
.getLuid(), pgGet
);
509 throw new IOException("Story not found: " + meta
.getLuid());
512 delete(meta
.getLuid());
515 save(story
, meta
.getLuid(), pgSet
);