de3dbb64890dd53df4bad285c198693e8f061348
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 list of stories (represented by their {@link MetaData}, which
57 * <b>MAY</b> not have the cover included).
60 * the optional {@link Progress}
62 * @return the list (can be empty but not NULL)
64 protected abstract List
<MetaData
> getMetas(Progress pg
);
67 * Invalidate the {@link Story} cache (when the content should be re-read
68 * because it was changed).
70 protected abstract void clearCache();
73 * Return the next LUID that can be used.
75 * @return the next luid
77 protected abstract int getNextId();
80 * Delete the target {@link Story}.
83 * the LUID of the {@link Story}
86 * in case of I/O error or if the {@link Story} wa not found
88 protected abstract void doDelete(String luid
) throws IOException
;
91 * Actually save the story to the back-end.
94 * the {@link Story} to save
96 * the optional {@link Progress}
98 * @return the saved {@link Story} (which may have changed, especially
99 * regarding the {@link MetaData})
101 * @throws IOException
102 * in case of I/O error
104 protected abstract Story
doSave(Story story
, Progress pg
)
108 * Refresh the {@link BasicLibrary}, that is, make sure all stories are
112 * force the full content of the stories to be loaded, not just
113 * the {@link MetaData}
116 * the optional progress reporter
118 public void refresh(boolean full
, Progress pg
) {
121 List
<MetaData
> metas
= getMetas(pg
);
122 for (MetaData meta
: metas
) {
123 getStory(meta
.getLuid(), null);
131 * List all the known types (sources) of stories.
133 * @return the sources
135 public synchronized List
<String
> getSources() {
136 List
<String
> list
= new ArrayList
<String
>();
137 for (MetaData meta
: getMetas(null)) {
138 String storySource
= meta
.getSource();
139 if (!list
.contains(storySource
)) {
140 list
.add(storySource
);
144 Collections
.sort(list
);
149 * List all the known authors of stories.
151 * @return the authors
153 public synchronized List
<String
> getAuthors() {
154 List
<String
> list
= new ArrayList
<String
>();
155 for (MetaData meta
: getMetas(null)) {
156 String storyAuthor
= meta
.getAuthor();
157 if (!list
.contains(storyAuthor
)) {
158 list
.add(storyAuthor
);
162 Collections
.sort(list
);
167 * List all the stories in the {@link BasicLibrary}.
169 * Cover images not included.
171 * @return the stories
173 public synchronized List
<MetaData
> getList() {
174 return getMetas(null);
178 * List all the stories of the given source type in the {@link BasicLibrary}
179 * , or all the stories if NULL is passed as a type.
181 * Cover images not included.
184 * the type of story to retrieve, or NULL for all
186 * @return the stories
188 public synchronized List
<MetaData
> getListBySource(String type
) {
189 List
<MetaData
> list
= new ArrayList
<MetaData
>();
190 for (MetaData meta
: getMetas(null)) {
191 String storyType
= meta
.getSource();
192 if (type
== null || type
.equalsIgnoreCase(storyType
)) {
197 Collections
.sort(list
);
202 * List all the stories of the given author in the {@link BasicLibrary}, or
203 * all the stories if NULL is passed as an author.
205 * Cover images not included.
208 * the author of the stories to retrieve, or NULL for all
210 * @return the stories
212 public synchronized List
<MetaData
> getListByAuthor(String author
) {
213 List
<MetaData
> list
= new ArrayList
<MetaData
>();
214 for (MetaData meta
: getMetas(null)) {
215 String storyAuthor
= meta
.getAuthor();
216 if (author
== null || author
.equalsIgnoreCase(storyAuthor
)) {
221 Collections
.sort(list
);
226 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
227 * cover image <b>MAY</b> not be included.
230 * the Library UID of the story
232 * @return the corresponding {@link Story}
234 public synchronized MetaData
getInfo(String luid
) {
236 for (MetaData meta
: getMetas(null)) {
237 if (luid
.equals(meta
.getLuid())) {
247 * Retrieve a specific {@link Story}.
250 * the Library UID of the story
252 * the optional progress reporter
254 * @return the corresponding {@link Story} or NULL if not found
256 public synchronized Story
getStory(String luid
, Progress pg
) {
263 for (MetaData meta
: getMetas(null)) {
264 if (meta
.getLuid().equals(luid
)) {
265 File file
= getFile(luid
);
267 SupportType type
= SupportType
.valueOfAllOkUC(meta
269 URL url
= file
.toURI().toURL();
271 story
= BasicSupport
.getSupport(type
).process(url
, pg
);
273 throw new IOException("Unknown type: " + meta
.getType());
275 } catch (IOException e
) {
276 // We should not have not-supported files in the
278 Instance
.syserr(new IOException(
279 "Cannot load file from library: " + file
, e
));
292 * Import the {@link Story} at the given {@link URL} into the
293 * {@link BasicLibrary}.
296 * the {@link URL} to import
298 * the optional progress reporter
300 * @return the imported {@link Story}
302 * @throws IOException
303 * in case of I/O error
305 public Story
imprt(URL url
, Progress pg
) throws IOException
{
306 BasicSupport support
= BasicSupport
.getSupport(url
);
307 if (support
== null) {
308 throw new IOException("URL not supported: " + url
.toString());
311 return save(support
.process(url
, pg
), null);
315 * Export the {@link Story} to the given target in the given format.
318 * the {@link Story} ID
320 * the {@link OutputType} to transform it to
322 * the target to save to
324 * the optional progress reporter
326 * @return the saved resource (the main saved {@link File})
328 * @throws IOException
329 * in case of I/O error
331 public File
export(String luid
, OutputType type
, String target
, Progress pg
)
333 Progress pgGetStory
= new Progress();
334 Progress pgOut
= new Progress();
337 pg
.addProgress(pgGetStory
, 1);
338 pg
.addProgress(pgOut
, 1);
341 BasicOutput out
= BasicOutput
.getOutput(type
, false);
343 throw new IOException("Output type not supported: " + type
);
346 Story story
= getStory(luid
, pgGetStory
);
348 throw new IOException("Cannot find story to export: " + luid
);
351 return out
.process(story
, target
, pgOut
);
355 * Save a {@link Story} to the {@link BasicLibrary}.
358 * the {@link Story} to save
360 * the optional progress reporter
362 * @return the same {@link Story}, whose LUID may have changed
364 * @throws IOException
365 * in case of I/O error
367 public Story
save(Story story
, Progress pg
) throws IOException
{
368 return save(story
, null, pg
);
372 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
373 * be correct, or NULL to get the next free one.
375 * Will override any previous {@link Story} with the same LUID.
378 * the {@link Story} to save
380 * the <b>correct</b> LUID or NULL to get the next free one
382 * the optional progress reporter
384 * @return the same {@link Story}, whose LUID may have changed
386 * @throws IOException
387 * in case of I/O error
389 public synchronized Story
save(Story story
, String luid
, Progress pg
)
391 // Do not change the original metadata, but change the original story
392 MetaData meta
= story
.getMeta().clone();
395 if (luid
== null || luid
.isEmpty()) {
396 meta
.setLuid(String
.format("%03d", getNextId()));
401 if (getInfo(luid
) != null) {
413 * Delete the given {@link Story} from this {@link BasicLibrary}.
416 * the LUID of the target {@link Story}
418 * @throws IOException
419 * in case of I/O error
421 public synchronized void delete(String luid
) throws IOException
{
427 * Change the type (source) of the given {@link Story}.
430 * the {@link Story} LUID
434 * the optional progress reporter
436 * @throws IOException
437 * in case of I/O error or if the {@link Story} was not found
439 public synchronized void changeSource(String luid
, String newSource
,
440 Progress pg
) throws IOException
{
441 MetaData meta
= getInfo(luid
);
443 throw new IOException("Story not found: " + luid
);
446 meta
.setSource(newSource
);
451 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
452 * change) for this {@link Story}.
454 * By default, delete the old {@link Story} then recreate a new
457 * Note that this behaviour can lead to data loss.
460 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
462 * the optional {@link Progress}
464 * @throws IOException
465 * in case of I/O error or if the {@link Story} was not found
467 protected synchronized void saveMeta(MetaData meta
, Progress pg
)
473 Progress pgGet
= new Progress();
474 Progress pgSet
= new Progress();
475 pg
.addProgress(pgGet
, 50);
476 pg
.addProgress(pgSet
, 50);
478 Story story
= getStory(meta
.getLuid(), pgGet
);
480 throw new IOException("Story not found: " + meta
.getLuid());
483 delete(meta
.getLuid());
486 save(story
, meta
.getLuid(), pgSet
);