Commit | Line | Data |
---|---|---|
e42573a0 | 1 | package be.nikiroo.fanfix.library; |
68e2c6d2 NR |
2 | |
3 | import java.awt.image.BufferedImage; | |
4 | import java.io.File; | |
5 | import java.io.IOException; | |
6 | import java.net.URL; | |
7 | import java.util.ArrayList; | |
8 | import java.util.Collections; | |
9 | import java.util.List; | |
10 | ||
e42573a0 | 11 | import be.nikiroo.fanfix.Instance; |
68e2c6d2 NR |
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; | |
19 | ||
20 | /** | |
21 | * Manage a library of Stories: import, export, list, modify. | |
22 | * <p> | |
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}. | |
25 | * <p> | |
26 | * Most of the {@link BasicLibrary} functions work on a partial (cover | |
27 | * <b>MAY</b> not be included) {@link MetaData} object. | |
28 | * | |
29 | * @author niki | |
30 | */ | |
31 | abstract public class BasicLibrary { | |
99ccbdf6 NR |
32 | /** |
33 | * Return a name for this library (the UI may display this). | |
34 | * <p> | |
35 | * Must not be NULL. | |
36 | * | |
37 | * @return the name, or an empty {@link String} if none | |
38 | */ | |
39 | public String getLibraryName() { | |
40 | return ""; | |
41 | } | |
42 | ||
68e2c6d2 NR |
43 | /** |
44 | * Retrieve the main {@link File} corresponding to the given {@link Story}, | |
45 | * which can be passed to an external reader or instance. | |
46 | * <p> | |
47 | * Do <b>NOT</b> alter this file. | |
48 | * | |
49 | * @param luid | |
50 | * the Library UID of the story | |
ff05b828 NR |
51 | * @param pg |
52 | * the optional {@link Progress} | |
68e2c6d2 NR |
53 | * |
54 | * @return the corresponding {@link Story} | |
55 | */ | |
ff05b828 | 56 | public abstract File getFile(String luid, Progress pg); |
68e2c6d2 NR |
57 | |
58 | /** | |
59 | * Return the cover image associated to this story. | |
60 | * | |
61 | * @param luid | |
62 | * the Library UID of the story | |
63 | * | |
64 | * @return the cover image | |
65 | */ | |
66 | public abstract BufferedImage getCover(String luid); | |
67 | ||
14b57448 NR |
68 | /** |
69 | * Return the cover image associated to this source. | |
70 | * <p> | |
71 | * By default, return the cover of the first story with this source. | |
72 | * | |
73 | * @param source | |
74 | * the source | |
75 | * | |
76 | * @return the cover image or NULL | |
77 | */ | |
78 | public BufferedImage getSourceCover(String source) { | |
79 | List<MetaData> metas = getListBySource(source); | |
80 | if (metas.size() > 0) { | |
81 | return getCover(metas.get(0).getLuid()); | |
82 | } | |
83 | ||
84 | return null; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Fix the source cover to the given story cover. | |
89 | * | |
90 | * @param source | |
91 | * the source to change | |
92 | * @param luid | |
93 | * the story LUID | |
94 | */ | |
95 | public abstract void setSourceCover(String source, String luid); | |
96 | ||
68e2c6d2 NR |
97 | /** |
98 | * Return the list of stories (represented by their {@link MetaData}, which | |
99 | * <b>MAY</b> not have the cover included). | |
100 | * | |
101 | * @param pg | |
102 | * the optional {@link Progress} | |
103 | * | |
104 | * @return the list (can be empty but not NULL) | |
105 | */ | |
106 | protected abstract List<MetaData> getMetas(Progress pg); | |
107 | ||
108 | /** | |
109 | * Invalidate the {@link Story} cache (when the content should be re-read | |
110 | * because it was changed). | |
111 | */ | |
112 | protected abstract void clearCache(); | |
113 | ||
114 | /** | |
115 | * Return the next LUID that can be used. | |
116 | * | |
117 | * @return the next luid | |
118 | */ | |
119 | protected abstract int getNextId(); | |
120 | ||
121 | /** | |
122 | * Delete the target {@link Story}. | |
123 | * | |
124 | * @param luid | |
125 | * the LUID of the {@link Story} | |
126 | * | |
127 | * @throws IOException | |
128 | * in case of I/O error or if the {@link Story} wa not found | |
129 | */ | |
130 | protected abstract void doDelete(String luid) throws IOException; | |
131 | ||
132 | /** | |
133 | * Actually save the story to the back-end. | |
134 | * | |
135 | * @param story | |
136 | * the {@link Story} to save | |
137 | * @param pg | |
138 | * the optional {@link Progress} | |
139 | * | |
140 | * @return the saved {@link Story} (which may have changed, especially | |
141 | * regarding the {@link MetaData}) | |
142 | * | |
143 | * @throws IOException | |
144 | * in case of I/O error | |
145 | */ | |
146 | protected abstract Story doSave(Story story, Progress pg) | |
147 | throws IOException; | |
148 | ||
149 | /** | |
150 | * Refresh the {@link BasicLibrary}, that is, make sure all stories are | |
151 | * loaded. | |
152 | * | |
153 | * @param full | |
154 | * force the full content of the stories to be loaded, not just | |
155 | * the {@link MetaData} | |
156 | * | |
157 | * @param pg | |
158 | * the optional progress reporter | |
159 | */ | |
160 | public void refresh(boolean full, Progress pg) { | |
161 | if (full) { | |
162 | // TODO: progress | |
163 | List<MetaData> metas = getMetas(pg); | |
164 | for (MetaData meta : metas) { | |
165 | getStory(meta.getLuid(), null); | |
166 | } | |
167 | } else { | |
168 | getMetas(pg); | |
169 | } | |
170 | } | |
171 | ||
172 | /** | |
173 | * List all the known types (sources) of stories. | |
174 | * | |
175 | * @return the sources | |
176 | */ | |
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); | |
183 | } | |
184 | } | |
185 | ||
186 | Collections.sort(list); | |
187 | return list; | |
188 | } | |
189 | ||
190 | /** | |
191 | * List all the known authors of stories. | |
192 | * | |
193 | * @return the authors | |
194 | */ | |
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); | |
201 | } | |
202 | } | |
203 | ||
204 | Collections.sort(list); | |
205 | return list; | |
206 | } | |
207 | ||
208 | /** | |
209 | * List all the stories in the {@link BasicLibrary}. | |
210 | * <p> | |
211 | * Cover images not included. | |
212 | * | |
213 | * @return the stories | |
214 | */ | |
215 | public synchronized List<MetaData> getList() { | |
216 | return getMetas(null); | |
217 | } | |
218 | ||
219 | /** | |
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. | |
222 | * <p> | |
223 | * Cover images not included. | |
224 | * | |
225 | * @param type | |
226 | * the type of story to retrieve, or NULL for all | |
227 | * | |
228 | * @return the stories | |
229 | */ | |
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)) { | |
235 | list.add(meta); | |
236 | } | |
237 | } | |
238 | ||
239 | Collections.sort(list); | |
240 | return list; | |
241 | } | |
242 | ||
243 | /** | |
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. | |
246 | * <p> | |
247 | * Cover images not included. | |
248 | * | |
249 | * @param author | |
250 | * the author of the stories to retrieve, or NULL for all | |
251 | * | |
252 | * @return the stories | |
253 | */ | |
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)) { | |
259 | list.add(meta); | |
260 | } | |
261 | } | |
262 | ||
263 | Collections.sort(list); | |
264 | return list; | |
265 | } | |
266 | ||
267 | /** | |
268 | * Retrieve a {@link MetaData} corresponding to the given {@link Story}, | |
269 | * cover image <b>MAY</b> not be included. | |
270 | * | |
271 | * @param luid | |
272 | * the Library UID of the story | |
273 | * | |
274 | * @return the corresponding {@link Story} | |
275 | */ | |
276 | public synchronized MetaData getInfo(String luid) { | |
277 | if (luid != null) { | |
278 | for (MetaData meta : getMetas(null)) { | |
279 | if (luid.equals(meta.getLuid())) { | |
280 | return meta; | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | return null; | |
286 | } | |
287 | ||
288 | /** | |
289 | * Retrieve a specific {@link Story}. | |
290 | * | |
291 | * @param luid | |
292 | * the Library UID of the story | |
293 | * @param pg | |
294 | * the optional progress reporter | |
295 | * | |
296 | * @return the corresponding {@link Story} or NULL if not found | |
297 | */ | |
298 | public synchronized Story getStory(String luid, Progress pg) { | |
68e2c6d2 NR |
299 | if (pg == null) { |
300 | pg = new Progress(); | |
301 | } | |
302 | ||
ff05b828 NR |
303 | Progress pgGet = new Progress(); |
304 | Progress pgProcess = new Progress(); | |
305 | ||
306 | pg.setMinMax(0, 2); | |
307 | pg.addProgress(pgGet, 1); | |
308 | pg.addProgress(pgProcess, 1); | |
309 | ||
68e2c6d2 NR |
310 | Story story = null; |
311 | for (MetaData meta : getMetas(null)) { | |
312 | if (meta.getLuid().equals(luid)) { | |
ff05b828 NR |
313 | File file = getFile(luid, pgGet); |
314 | pgGet.done(); | |
68e2c6d2 NR |
315 | try { |
316 | SupportType type = SupportType.valueOfAllOkUC(meta | |
317 | .getType()); | |
318 | URL url = file.toURI().toURL(); | |
319 | if (type != null) { | |
ff05b828 NR |
320 | story = BasicSupport.getSupport(type).process(url, |
321 | pgProcess); | |
68e2c6d2 NR |
322 | } else { |
323 | throw new IOException("Unknown type: " + meta.getType()); | |
324 | } | |
325 | } catch (IOException e) { | |
326 | // We should not have not-supported files in the | |
327 | // library | |
328 | Instance.syserr(new IOException( | |
329 | "Cannot load file from library: " + file, e)); | |
330 | } finally { | |
ff05b828 | 331 | pgProcess.done(); |
68e2c6d2 NR |
332 | pg.done(); |
333 | } | |
334 | ||
335 | break; | |
336 | } | |
337 | } | |
338 | ||
339 | return story; | |
340 | } | |
341 | ||
342 | /** | |
343 | * Import the {@link Story} at the given {@link URL} into the | |
344 | * {@link BasicLibrary}. | |
345 | * | |
346 | * @param url | |
347 | * the {@link URL} to import | |
348 | * @param pg | |
349 | * the optional progress reporter | |
350 | * | |
351 | * @return the imported {@link Story} | |
352 | * | |
353 | * @throws IOException | |
354 | * in case of I/O error | |
355 | */ | |
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()); | |
360 | } | |
361 | ||
362 | return save(support.process(url, pg), null); | |
363 | } | |
364 | ||
b89dfb6e NR |
365 | /** |
366 | * Import the story from one library to another, and keep the same LUID. | |
367 | * | |
368 | * @param other | |
369 | * the other library to import from | |
370 | * @param luid | |
371 | * the Library UID | |
372 | * @param pg | |
373 | * the optional progress reporter | |
374 | * | |
375 | * @throws IOException | |
376 | * in case of I/O error | |
377 | */ | |
378 | public void imprt(BasicLibrary other, String luid, Progress pg) | |
379 | throws IOException { | |
380 | Progress pgGetStory = new Progress(); | |
381 | Progress pgSave = new Progress(); | |
382 | if (pg == null) { | |
383 | pg = new Progress(); | |
384 | } | |
385 | ||
386 | pg.setMinMax(0, 2); | |
387 | pg.addProgress(pgGetStory, 1); | |
388 | pg.addProgress(pgSave, 1); | |
389 | ||
390 | Story story = other.getStory(luid, pgGetStory); | |
391 | if (story != null) { | |
392 | story = this.save(story, luid, pgSave); | |
393 | pg.done(); | |
394 | } else { | |
395 | pg.done(); | |
396 | throw new IOException("Cannot find story in Library: " + luid); | |
397 | } | |
398 | } | |
399 | ||
68e2c6d2 NR |
400 | /** |
401 | * Export the {@link Story} to the given target in the given format. | |
402 | * | |
403 | * @param luid | |
404 | * the {@link Story} ID | |
405 | * @param type | |
406 | * the {@link OutputType} to transform it to | |
407 | * @param target | |
408 | * the target to save to | |
409 | * @param pg | |
410 | * the optional progress reporter | |
411 | * | |
412 | * @return the saved resource (the main saved {@link File}) | |
413 | * | |
414 | * @throws IOException | |
415 | * in case of I/O error | |
416 | */ | |
417 | public File export(String luid, OutputType type, String target, Progress pg) | |
418 | throws IOException { | |
419 | Progress pgGetStory = new Progress(); | |
420 | Progress pgOut = new Progress(); | |
421 | if (pg != null) { | |
422 | pg.setMax(2); | |
423 | pg.addProgress(pgGetStory, 1); | |
424 | pg.addProgress(pgOut, 1); | |
425 | } | |
426 | ||
fa4dcafe | 427 | BasicOutput out = BasicOutput.getOutput(type, false); |
68e2c6d2 NR |
428 | if (out == null) { |
429 | throw new IOException("Output type not supported: " + type); | |
430 | } | |
431 | ||
432 | Story story = getStory(luid, pgGetStory); | |
433 | if (story == null) { | |
434 | throw new IOException("Cannot find story to export: " + luid); | |
435 | } | |
436 | ||
437 | return out.process(story, target, pgOut); | |
438 | } | |
439 | ||
440 | /** | |
441 | * Save a {@link Story} to the {@link BasicLibrary}. | |
442 | * | |
443 | * @param story | |
444 | * the {@link Story} to save | |
445 | * @param pg | |
446 | * the optional progress reporter | |
447 | * | |
448 | * @return the same {@link Story}, whose LUID may have changed | |
449 | * | |
450 | * @throws IOException | |
451 | * in case of I/O error | |
452 | */ | |
453 | public Story save(Story story, Progress pg) throws IOException { | |
454 | return save(story, null, pg); | |
455 | } | |
456 | ||
457 | /** | |
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. | |
460 | * <p> | |
461 | * Will override any previous {@link Story} with the same LUID. | |
462 | * | |
463 | * @param story | |
464 | * the {@link Story} to save | |
465 | * @param luid | |
466 | * the <b>correct</b> LUID or NULL to get the next free one | |
467 | * @param pg | |
468 | * the optional progress reporter | |
469 | * | |
470 | * @return the same {@link Story}, whose LUID may have changed | |
471 | * | |
472 | * @throws IOException | |
473 | * in case of I/O error | |
474 | */ | |
475 | public synchronized Story save(Story story, String luid, Progress pg) | |
476 | throws IOException { | |
477 | // Do not change the original metadata, but change the original story | |
478 | MetaData meta = story.getMeta().clone(); | |
479 | story.setMeta(meta); | |
480 | ||
481 | if (luid == null || luid.isEmpty()) { | |
482 | meta.setLuid(String.format("%03d", getNextId())); | |
483 | } else { | |
484 | meta.setLuid(luid); | |
485 | } | |
486 | ||
487 | if (getInfo(luid) != null) { | |
488 | delete(luid); | |
489 | } | |
14b57448 | 490 | |
68e2c6d2 NR |
491 | doSave(story, pg); |
492 | ||
493 | clearCache(); | |
494 | ||
495 | return story; | |
496 | } | |
497 | ||
498 | /** | |
499 | * Delete the given {@link Story} from this {@link BasicLibrary}. | |
500 | * | |
501 | * @param luid | |
502 | * the LUID of the target {@link Story} | |
503 | * | |
504 | * @throws IOException | |
505 | * in case of I/O error | |
506 | */ | |
507 | public synchronized void delete(String luid) throws IOException { | |
508 | doDelete(luid); | |
509 | clearCache(); | |
510 | } | |
511 | ||
512 | /** | |
513 | * Change the type (source) of the given {@link Story}. | |
514 | * | |
515 | * @param luid | |
516 | * the {@link Story} LUID | |
517 | * @param newSource | |
518 | * the new source | |
519 | * @param pg | |
520 | * the optional progress reporter | |
521 | * | |
522 | * @throws IOException | |
523 | * in case of I/O error or if the {@link Story} was not found | |
524 | */ | |
525 | public synchronized void changeSource(String luid, String newSource, | |
526 | Progress pg) throws IOException { | |
527 | MetaData meta = getInfo(luid); | |
528 | if (meta == null) { | |
529 | throw new IOException("Story not found: " + luid); | |
530 | } | |
531 | ||
532 | meta.setSource(newSource); | |
533 | saveMeta(meta, pg); | |
534 | } | |
535 | ||
536 | /** | |
537 | * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b> | |
538 | * change) for this {@link Story}. | |
539 | * <p> | |
540 | * By default, delete the old {@link Story} then recreate a new | |
541 | * {@link Story}. | |
542 | * <p> | |
543 | * Note that this behaviour can lead to data loss. | |
544 | * | |
545 | * @param meta | |
546 | * the new {@link MetaData} (LUID <b>MUST NOT</b> change) | |
547 | * @param pg | |
548 | * the optional {@link Progress} | |
549 | * | |
550 | * @throws IOException | |
551 | * in case of I/O error or if the {@link Story} was not found | |
552 | */ | |
553 | protected synchronized void saveMeta(MetaData meta, Progress pg) | |
554 | throws IOException { | |
555 | if (pg == null) { | |
556 | pg = new Progress(); | |
557 | } | |
558 | ||
559 | Progress pgGet = new Progress(); | |
560 | Progress pgSet = new Progress(); | |
561 | pg.addProgress(pgGet, 50); | |
562 | pg.addProgress(pgSet, 50); | |
563 | ||
564 | Story story = getStory(meta.getLuid(), pgGet); | |
565 | if (story == null) { | |
566 | throw new IOException("Story not found: " + meta.getLuid()); | |
567 | } | |
568 | ||
569 | delete(meta.getLuid()); | |
570 | ||
571 | story.setMeta(meta); | |
572 | save(story, meta.getLuid(), pgSet); | |
573 | ||
574 | pg.done(); | |
575 | } | |
576 | } |