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