Improve remote, fix bugs, update nikiroo-utils
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / BasicLibrary.java
1 package be.nikiroo.fanfix.library;
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
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;
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 {
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
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
51 * @param pg
52 * the optional {@link Progress}
53 *
54 * @return the corresponding {@link Story}
55 */
56 public abstract File getFile(String luid, Progress pg);
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
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
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) {
299 if (pg == null) {
300 pg = new Progress();
301 }
302
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
310 Story story = null;
311 for (MetaData meta : getMetas(null)) {
312 if (meta.getLuid().equals(luid)) {
313 File file = getFile(luid, pgGet);
314 pgGet.done();
315 try {
316 SupportType type = SupportType.valueOfAllOkUC(meta
317 .getType());
318 URL url = file.toURI().toURL();
319 if (type != null) {
320 story = BasicSupport.getSupport(type).process(url,
321 pgProcess);
322 story.setMeta(meta);
323 } else {
324 throw new IOException("Unknown type: " + meta.getType());
325 }
326 } catch (IOException e) {
327 // We should not have not-supported files in the
328 // library
329 Instance.syserr(new IOException(
330 "Cannot load file from library: " + file, e));
331 } finally {
332 pgProcess.done();
333 pg.done();
334 }
335
336 break;
337 }
338 }
339
340 return story;
341 }
342
343 /**
344 * Import the {@link Story} at the given {@link URL} into the
345 * {@link BasicLibrary}.
346 *
347 * @param url
348 * the {@link URL} to import
349 * @param pg
350 * the optional progress reporter
351 *
352 * @return the imported {@link Story}
353 *
354 * @throws IOException
355 * in case of I/O error
356 */
357 public Story imprt(URL url, Progress pg) throws IOException {
358 BasicSupport support = BasicSupport.getSupport(url);
359 if (support == null) {
360 throw new IOException("URL not supported: " + url.toString());
361 }
362
363 return save(support.process(url, pg), null);
364 }
365
366 /**
367 * Import the story from one library to another, and keep the same LUID.
368 *
369 * @param other
370 * the other library to import from
371 * @param luid
372 * the Library UID
373 * @param pg
374 * the optional progress reporter
375 *
376 * @throws IOException
377 * in case of I/O error
378 */
379 public void imprt(BasicLibrary other, String luid, Progress pg)
380 throws IOException {
381 Progress pgGetStory = new Progress();
382 Progress pgSave = new Progress();
383 if (pg == null) {
384 pg = new Progress();
385 }
386
387 pg.setMinMax(0, 2);
388 pg.addProgress(pgGetStory, 1);
389 pg.addProgress(pgSave, 1);
390
391 Story story = other.getStory(luid, pgGetStory);
392 if (story != null) {
393 story = this.save(story, luid, pgSave);
394 pg.done();
395 } else {
396 pg.done();
397 throw new IOException("Cannot find story in Library: " + luid);
398 }
399 }
400
401 /**
402 * Export the {@link Story} to the given target in the given format.
403 *
404 * @param luid
405 * the {@link Story} ID
406 * @param type
407 * the {@link OutputType} to transform it to
408 * @param target
409 * the target to save to
410 * @param pg
411 * the optional progress reporter
412 *
413 * @return the saved resource (the main saved {@link File})
414 *
415 * @throws IOException
416 * in case of I/O error
417 */
418 public File export(String luid, OutputType type, String target, Progress pg)
419 throws IOException {
420 Progress pgGetStory = new Progress();
421 Progress pgOut = new Progress();
422 if (pg != null) {
423 pg.setMax(2);
424 pg.addProgress(pgGetStory, 1);
425 pg.addProgress(pgOut, 1);
426 }
427
428 BasicOutput out = BasicOutput.getOutput(type, false);
429 if (out == null) {
430 throw new IOException("Output type not supported: " + type);
431 }
432
433 Story story = getStory(luid, pgGetStory);
434 if (story == null) {
435 throw new IOException("Cannot find story to export: " + luid);
436 }
437
438 return out.process(story, target, pgOut);
439 }
440
441 /**
442 * Save a {@link Story} to the {@link BasicLibrary}.
443 *
444 * @param story
445 * the {@link Story} to save
446 * @param pg
447 * the optional progress reporter
448 *
449 * @return the same {@link Story}, whose LUID may have changed
450 *
451 * @throws IOException
452 * in case of I/O error
453 */
454 public Story save(Story story, Progress pg) throws IOException {
455 return save(story, null, pg);
456 }
457
458 /**
459 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
460 * be correct, or NULL to get the next free one.
461 * <p>
462 * Will override any previous {@link Story} with the same LUID.
463 *
464 * @param story
465 * the {@link Story} to save
466 * @param luid
467 * the <b>correct</b> LUID or NULL to get the next free one
468 * @param pg
469 * the optional progress reporter
470 *
471 * @return the same {@link Story}, whose LUID may have changed
472 *
473 * @throws IOException
474 * in case of I/O error
475 */
476 public synchronized Story save(Story story, String luid, Progress pg)
477 throws IOException {
478 // Do not change the original metadata, but change the original story
479 MetaData meta = story.getMeta().clone();
480 story.setMeta(meta);
481
482 if (luid == null || luid.isEmpty()) {
483 meta.setLuid(String.format("%03d", getNextId()));
484 } else {
485 meta.setLuid(luid);
486 }
487
488 if (getInfo(luid) != null) {
489 delete(luid);
490 }
491
492 doSave(story, pg);
493
494 clearCache();
495
496 return story;
497 }
498
499 /**
500 * Delete the given {@link Story} from this {@link BasicLibrary}.
501 *
502 * @param luid
503 * the LUID of the target {@link Story}
504 *
505 * @throws IOException
506 * in case of I/O error
507 */
508 public synchronized void delete(String luid) throws IOException {
509 doDelete(luid);
510 clearCache();
511 }
512
513 /**
514 * Change the type (source) of the given {@link Story}.
515 *
516 * @param luid
517 * the {@link Story} LUID
518 * @param newSource
519 * the new source
520 * @param pg
521 * the optional progress reporter
522 *
523 * @throws IOException
524 * in case of I/O error or if the {@link Story} was not found
525 */
526 public synchronized void changeSource(String luid, String newSource,
527 Progress pg) throws IOException {
528 MetaData meta = getInfo(luid);
529 if (meta == null) {
530 throw new IOException("Story not found: " + luid);
531 }
532
533 meta.setSource(newSource);
534 saveMeta(meta, pg);
535 }
536
537 /**
538 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
539 * change) for this {@link Story}.
540 * <p>
541 * By default, delete the old {@link Story} then recreate a new
542 * {@link Story}.
543 * <p>
544 * Note that this behaviour can lead to data loss.
545 *
546 * @param meta
547 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
548 * @param pg
549 * the optional {@link Progress}
550 *
551 * @throws IOException
552 * in case of I/O error or if the {@link Story} was not found
553 */
554 protected synchronized void saveMeta(MetaData meta, Progress pg)
555 throws IOException {
556 if (pg == null) {
557 pg = new Progress();
558 }
559
560 Progress pgGet = new Progress();
561 Progress pgSet = new Progress();
562 pg.addProgress(pgGet, 50);
563 pg.addProgress(pgSet, 50);
564
565 Story story = getStory(meta.getLuid(), pgGet);
566 if (story == null) {
567 throw new IOException("Story not found: " + meta.getLuid());
568 }
569
570 delete(meta.getLuid());
571
572 story.setMeta(meta);
573 save(story, meta.getLuid(), pgSet);
574
575 pg.done();
576 }
577 }