new LoginResult helper class
[fanfix.git] / src / be / nikiroo / fanfix / library / BasicLibrary.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.library;
2
3import java.io.File;
4import java.io.IOException;
5import java.net.URL;
6import java.net.UnknownHostException;
7import java.util.LinkedHashMap;
8import java.util.List;
9import java.util.Map;
10
11import be.nikiroo.fanfix.Instance;
12import be.nikiroo.fanfix.data.MetaData;
13import be.nikiroo.fanfix.data.Story;
14import be.nikiroo.fanfix.output.BasicOutput;
15import be.nikiroo.fanfix.output.BasicOutput.OutputType;
16import be.nikiroo.fanfix.supported.BasicSupport;
17import be.nikiroo.fanfix.supported.SupportType;
18import be.nikiroo.utils.Image;
19import be.nikiroo.utils.Progress;
20import be.nikiroo.utils.StringUtils;
21
22/**
23 * Manage a library of Stories: import, export, list, modify.
24 * <p>
25 * Each {@link Story} object will be associated with a (local to the library)
26 * unique ID, the LUID, which will be used to identify the {@link Story}.
27 * <p>
28 * Most of the {@link BasicLibrary} functions work on a partial (cover
29 * <b>MAY</b> not be included) {@link MetaData} object.
30 *
31 * @author niki
32 */
33abstract public class BasicLibrary {
34 /**
35 * A {@link BasicLibrary} status.
36 *
37 * @author niki
38 */
39 public enum Status {
40 /** The library is ready and r/w. */
41 READ_WRITE,
42 /** The library is ready, but read-only. */
43 READ_ONLY,
44 /** The library is invalid (not correctly set up). */
45 INVALID,
46 /** You are not allowed to access this library. */
47 UNAUTHORIZED,
48 /** The library is currently out of commission. */
49 UNAVAILABLE;
50
51 /**
52 * The library is available (you can query it).
53 * <p>
54 * It does <b>not</b> specify if it is read-only or not.
55 *
56 * @return TRUE if it is
57 */
58 public boolean isReady() {
59 return (this == READ_WRITE || this == READ_ONLY);
60 }
61
62 /**
63 * This library can be modified (= you are allowed to modify it).
64 *
65 * @return TRUE if it is
66 */
67 public boolean isWritable() {
68 return (this == READ_WRITE);
69 }
70 }
71
72 /**
73 * Return a name for this library (the UI may display this).
74 * <p>
75 * Must not be NULL.
76 *
77 * @return the name, or an empty {@link String} if none
78 */
79 public String getLibraryName() {
80 return "";
81 }
82
83 /**
84 * The library status.
85 *
86 * @return the current status
87 */
88 public Status getStatus() {
89 return Status.READ_WRITE;
90 }
91
92 /**
93 * Retrieve the main {@link File} corresponding to the given {@link Story},
94 * which can be passed to an external reader or instance.
95 * <p>
96 * Do <b>NOT</b> alter this file.
97 *
98 * @param luid
99 * the Library UID of the story, can be NULL
100 * @param pg
101 * the optional {@link Progress}
102 *
103 * @return the corresponding {@link Story}
104 *
105 * @throws IOException
106 * in case of IOException
107 */
108 public abstract File getFile(String luid, Progress pg) throws IOException;
109
110 /**
111 * Return the cover image associated to this story.
112 *
113 * @param luid
114 * the Library UID of the story
115 *
116 * @return the cover image
117 *
118 * @throws IOException
119 * in case of IOException
120 */
121 public abstract Image getCover(String luid) throws IOException;
122
123 /**
124 * Retrieve the list of {@link MetaData} known by this {@link BasicLibrary}
125 * in a easy-to-filter version.
126 *
127 * @param pg
128 * the optional {@link Progress}
129 * @return the list of {@link MetaData} as a {@link MetaResultList} you can
130 * query
131 * @throws IOException
132 * in case of I/O eror
133 */
134 public MetaResultList getList(Progress pg) throws IOException {
135 // TODO: ensure it is the main used interface
136
137 return new MetaResultList(getMetas(pg));
138 }
139
140 // TODO: make something for (normal and custom) non-story covers
141
142 /**
143 * Return the cover image associated to this source.
144 * <p>
145 * By default, return the custom cover if any, and if not, return the cover
146 * of the first story with this source.
147 *
148 * @param source
149 * the source
150 *
151 * @return the cover image or NULL
152 *
153 * @throws IOException
154 * in case of IOException
155 */
156 public Image getSourceCover(String source) throws IOException {
157 Image custom = getCustomSourceCover(source);
158 if (custom != null) {
159 return custom;
160 }
161
162 List<MetaData> metas = getList().filter(source, null, null);
163 if (metas.size() > 0) {
164 return getCover(metas.get(0).getLuid());
165 }
166
167 return null;
168 }
169
170 /**
171 * Return the cover image associated to this author.
172 * <p>
173 * By default, return the custom cover if any, and if not, return the cover
174 * of the first story with this author.
175 *
176 * @param author
177 * the author
178 *
179 * @return the cover image or NULL
180 *
181 * @throws IOException
182 * in case of IOException
183 */
184 public Image getAuthorCover(String author) throws IOException {
185 Image custom = getCustomAuthorCover(author);
186 if (custom != null) {
187 return custom;
188 }
189
190 List<MetaData> metas = getList().filter(null, author, null);
191 if (metas.size() > 0) {
192 return getCover(metas.get(0).getLuid());
193 }
194
195 return null;
196 }
197
198 /**
199 * Return the custom cover image associated to this source.
200 * <p>
201 * By default, return NULL.
202 *
203 * @param source
204 * the source to look for
205 *
206 * @return the custom cover or NULL if none
207 *
208 * @throws IOException
209 * in case of IOException
210 */
211 @SuppressWarnings("unused")
212 public Image getCustomSourceCover(String source) throws IOException {
213 return null;
214 }
215
216 /**
217 * Return the custom cover image associated to this author.
218 * <p>
219 * By default, return NULL.
220 *
221 * @param author
222 * the author to look for
223 *
224 * @return the custom cover or NULL if none
225 *
226 * @throws IOException
227 * in case of IOException
228 */
229 @SuppressWarnings("unused")
230 public Image getCustomAuthorCover(String author) throws IOException {
231 return null;
232 }
233
234 /**
235 * Set the source cover to the given story cover.
236 *
237 * @param source
238 * the source to change
239 * @param luid
240 * the story LUID
241 *
242 * @throws IOException
243 * in case of IOException
244 */
245 public abstract void setSourceCover(String source, String luid)
246 throws IOException;
247
248 /**
249 * Set the author cover to the given story cover.
250 *
251 * @param author
252 * the author to change
253 * @param luid
254 * the story LUID
255 *
256 * @throws IOException
257 * in case of IOException
258 */
259 public abstract void setAuthorCover(String author, String luid)
260 throws IOException;
261
262 /**
263 * Return the list of stories (represented by their {@link MetaData}, which
264 * <b>MAY</b> not have the cover included).
265 * <p>
266 * The returned list <b>MUST</b> be a copy, not the original one.
267 *
268 * @param pg
269 * the optional {@link Progress}
270 *
271 * @return the list (can be empty but not NULL)
272 *
273 * @throws IOException
274 * in case of IOException
275 */
276 protected abstract List<MetaData> getMetas(Progress pg) throws IOException;
277
278 /**
279 * Invalidate the {@link Story} cache (when the content should be re-read
280 * because it was changed).
281 */
282 protected void invalidateInfo() {
283 invalidateInfo(null);
284 }
285
286 /**
287 * Invalidate the {@link Story} cache (when the content is removed).
288 * <p>
289 * All the cache can be deleted if NULL is passed as meta.
290 *
291 * @param luid
292 * the LUID of the {@link Story} to clear from the cache, or NULL
293 * for all stories
294 */
295 protected abstract void invalidateInfo(String luid);
296
297 /**
298 * Invalidate the {@link Story} cache (when the content has changed, but we
299 * already have it) with the new given meta.
300 *
301 * @param meta
302 * the {@link Story} to clear from the cache
303 *
304 * @throws IOException
305 * in case of IOException
306 */
307 protected abstract void updateInfo(MetaData meta) throws IOException;
308
309 /**
310 * Return the next LUID that can be used.
311 *
312 * @return the next luid
313 */
314 protected abstract int getNextId();
315
316 /**
317 * Delete the target {@link Story}.
318 *
319 * @param luid
320 * the LUID of the {@link Story}
321 *
322 * @throws IOException
323 * in case of I/O error or if the {@link Story} wa not found
324 */
325 protected abstract void doDelete(String luid) throws IOException;
326
327 /**
328 * Actually save the story to the back-end.
329 *
330 * @param story
331 * the {@link Story} to save
332 * @param pg
333 * the optional {@link Progress}
334 *
335 * @return the saved {@link Story} (which may have changed, especially
336 * regarding the {@link MetaData})
337 *
338 * @throws IOException
339 * in case of I/O error
340 */
341 protected abstract Story doSave(Story story, Progress pg)
342 throws IOException;
343
344 /**
345 * Refresh the {@link BasicLibrary}, that is, make sure all metas are
346 * loaded.
347 *
348 * @param pg
349 * the optional progress reporter
350 */
351 public void refresh(Progress pg) {
352 try {
353 getMetas(pg);
354 } catch (IOException e) {
355 // We will let it fail later
356 }
357 }
358
359 /**
360 * Check if the {@link Story} denoted by this Library UID is present in the
361 * cache (if we have no cache, we default to </tt>true</tt>).
362 *
363 * @param luid
364 * the Library UID
365 *
366 * @return TRUE if it is
367 */
368 public boolean isCached(@SuppressWarnings("unused") String luid) {
369 // By default, everything is cached
370 return true;
371 }
372
373 /**
374 * Clear the {@link Story} from the cache, if needed.
375 * <p>
376 * The next time we try to retrieve the {@link Story}, it may be required to
377 * cache it again.
378 *
379 * @param luid
380 * the story to clear
381 *
382 * @throws IOException
383 * in case of I/O error
384 */
385 @SuppressWarnings("unused")
386 public void clearFromCache(String luid) throws IOException {
387 // By default, this is a noop.
388 }
389
390 /**
391 * @return the same as getList()
392 * @throws IOException
393 * in case of I/O error
394 * @deprecated please use {@link BasicLibrary#getList()} and
395 * {@link MetaResultList#getSources()} instead.
396 */
397 @Deprecated
398 public List<String> getSources() throws IOException {
399 return getList().getSources();
400 }
401
402 /**
403 * @return the same as getList()
404 * @throws IOException
405 * in case of I/O error
406 * @deprecated please use {@link BasicLibrary#getList()} and
407 * {@link MetaResultList#getSourcesGrouped()} instead.
408 */
409 @Deprecated
410 public Map<String, List<String>> getSourcesGrouped() throws IOException {
411 return getList().getSourcesGrouped();
412 }
413
414 /**
415 * @return the same as getList()
416 * @throws IOException
417 * in case of I/O error
418 * @deprecated please use {@link BasicLibrary#getList()} and
419 * {@link MetaResultList#getAuthors()} instead.
420 */
421 @Deprecated
422 public List<String> getAuthors() throws IOException {
423 return getList().getAuthors();
424 }
425
426 /**
427 * @return the same as getList()
428 * @throws IOException
429 * in case of I/O error
430 * @deprecated please use {@link BasicLibrary#getList()} and
431 * {@link MetaResultList#getAuthorsGrouped()} instead.
432 */
433 @Deprecated
434 public Map<String, List<String>> getAuthorsGrouped() throws IOException {
435 return getList().getAuthorsGrouped();
436 }
437
438 /**
439 * List all the stories in the {@link BasicLibrary}.
440 * <p>
441 * Cover images <b>MAYBE</b> not included.
442 *
443 * @return the stories
444 *
445 * @throws IOException
446 * in case of IOException
447 */
448 public MetaResultList getList() throws IOException {
449 return getList(null);
450 }
451
452 /**
453 * Retrieve a {@link MetaData} corresponding to the given {@link Story},
454 * cover image <b>MAY</b> not be included.
455 *
456 * @param luid
457 * the Library UID of the story, can be NULL
458 *
459 * @return the corresponding {@link Story} or NULL if not found
460 *
461 * @throws IOException
462 * in case of IOException
463 */
464 public MetaData getInfo(String luid) throws IOException {
465 if (luid != null) {
466 for (MetaData meta : getMetas(null)) {
467 if (luid.equals(meta.getLuid())) {
468 return meta;
469 }
470 }
471 }
472
473 return null;
474 }
475
476 /**
477 * Retrieve a specific {@link Story}.
478 *
479 * @param luid
480 * the Library UID of the story
481 * @param pg
482 * the optional progress reporter
483 *
484 * @return the corresponding {@link Story} or NULL if not found
485 *
486 * @throws IOException
487 * in case of IOException
488 */
489 public Story getStory(String luid, Progress pg) throws IOException {
490 Progress pgMetas = new Progress();
491 Progress pgStory = new Progress();
492 if (pg != null) {
493 pg.setMinMax(0, 100);
494 pg.addProgress(pgMetas, 10);
495 pg.addProgress(pgStory, 90);
496 }
497
498 MetaData meta = null;
499 for (MetaData oneMeta : getMetas(pgMetas)) {
500 if (oneMeta.getLuid().equals(luid)) {
501 meta = oneMeta;
502 break;
503 }
504 }
505
506 pgMetas.done();
507
508 Story story = getStory(luid, meta, pgStory);
509 pgStory.done();
510
511 return story;
512 }
513
514 /**
515 * Retrieve a specific {@link Story}.
516 *
517 * @param luid
518 * the LUID of the story
519 * @param meta
520 * the meta of the story
521 * @param pg
522 * the optional progress reporter
523 *
524 * @return the corresponding {@link Story} or NULL if not found
525 *
526 * @throws IOException
527 * in case of IOException
528 */
529 public synchronized Story getStory(String luid, MetaData meta, Progress pg)
530 throws IOException {
531
532 if (pg == null) {
533 pg = new Progress();
534 }
535
536 Progress pgGet = new Progress();
537 Progress pgProcess = new Progress();
538
539 pg.setMinMax(0, 2);
540 pg.addProgress(pgGet, 1);
541 pg.addProgress(pgProcess, 1);
542
543 Story story = null;
544 File file = null;
545
546 if (luid != null && meta != null) {
547 file = getFile(luid, pgGet);
548 }
549
550 pgGet.done();
551 try {
552 if (file != null) {
553 SupportType type = SupportType.valueOfAllOkUC(meta.getType());
554 if (type == null) {
555 throw new IOException("Unknown type: " + meta.getType());
556 }
557
558 URL url = file.toURI().toURL();
559 story = BasicSupport.getSupport(type, url) //
560 .process(pgProcess);
561
562 // Because we do not want to clear the meta cache:
563 meta.setCover(story.getMeta().getCover());
564 meta.setResume(story.getMeta().getResume());
565 story.setMeta(meta);
566 }
567 } catch (IOException e) {
568 // We should not have not-supported files in the library
569 Instance.getInstance().getTraceHandler()
570 .error(new IOException(String.format(
571 "Cannot load file of type '%s' from library: %s",
572 meta.getType(), file), e));
573 } finally {
574 pgProcess.done();
575 pg.done();
576 }
577
578 return story;
579 }
580
581 /**
582 * Import the {@link Story} at the given {@link URL} into the
583 * {@link BasicLibrary}.
584 *
585 * @param url
586 * the {@link URL} to import
587 * @param pg
588 * the optional progress reporter
589 *
590 * @return the imported Story {@link MetaData}
591 *
592 * @throws UnknownHostException
593 * if the host is not supported
594 * @throws IOException
595 * in case of I/O error
596 */
597 public MetaData imprt(URL url, Progress pg) throws IOException {
598 if (pg == null)
599 pg = new Progress();
600
601 pg.setMinMax(0, 1000);
602 Progress pgProcess = new Progress();
603 Progress pgSave = new Progress();
604 pg.addProgress(pgProcess, 800);
605 pg.addProgress(pgSave, 200);
606
607 BasicSupport support = BasicSupport.getSupport(url);
608 if (support == null) {
609 throw new UnknownHostException("" + url);
610 }
611
612 Story story = save(support.process(pgProcess), pgSave);
613 pg.done();
614
615 return story.getMeta();
616 }
617
618 /**
619 * Import the story from one library to another, and keep the same LUID.
620 *
621 * @param other
622 * the other library to import from
623 * @param luid
624 * the Library UID
625 * @param pg
626 * the optional progress reporter
627 *
628 * @throws IOException
629 * in case of I/O error
630 */
631 public void imprt(BasicLibrary other, String luid, Progress pg)
632 throws IOException {
633 Progress pgGetStory = new Progress();
634 Progress pgSave = new Progress();
635 if (pg == null) {
636 pg = new Progress();
637 }
638
639 pg.setMinMax(0, 2);
640 pg.addProgress(pgGetStory, 1);
641 pg.addProgress(pgSave, 1);
642
643 Story story = other.getStory(luid, pgGetStory);
644 if (story != null) {
645 story = this.save(story, luid, pgSave);
646 pg.done();
647 } else {
648 pg.done();
649 throw new IOException("Cannot find story in Library: " + luid);
650 }
651 }
652
653 /**
654 * Export the {@link Story} to the given target in the given format.
655 *
656 * @param luid
657 * the {@link Story} ID
658 * @param type
659 * the {@link OutputType} to transform it to
660 * @param target
661 * the target to save to
662 * @param pg
663 * the optional progress reporter
664 *
665 * @return the saved resource (the main saved {@link File})
666 *
667 * @throws IOException
668 * in case of I/O error
669 */
670 public File export(String luid, OutputType type, String target, Progress pg)
671 throws IOException {
672 Progress pgGetStory = new Progress();
673 Progress pgOut = new Progress();
674 if (pg != null) {
675 pg.setMax(2);
676 pg.addProgress(pgGetStory, 1);
677 pg.addProgress(pgOut, 1);
678 }
679
680 BasicOutput out = BasicOutput.getOutput(type, false, false);
681 if (out == null) {
682 throw new IOException("Output type not supported: " + type);
683 }
684
685 Story story = getStory(luid, pgGetStory);
686 if (story == null) {
687 throw new IOException("Cannot find story to export: " + luid);
688 }
689
690 return out.process(story, target, pgOut);
691 }
692
693 /**
694 * Save a {@link Story} to the {@link BasicLibrary}.
695 *
696 * @param story
697 * the {@link Story} to save
698 * @param pg
699 * the optional progress reporter
700 *
701 * @return the same {@link Story}, whose LUID may have changed
702 *
703 * @throws IOException
704 * in case of I/O error
705 */
706 public Story save(Story story, Progress pg) throws IOException {
707 return save(story, null, pg);
708 }
709
710 /**
711 * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b>
712 * be correct, or NULL to get the next free one.
713 * <p>
714 * Will override any previous {@link Story} with the same LUID.
715 *
716 * @param story
717 * the {@link Story} to save
718 * @param luid
719 * the <b>correct</b> LUID or NULL to get the next free one
720 * @param pg
721 * the optional progress reporter
722 *
723 * @return the same {@link Story}, whose LUID may have changed
724 *
725 * @throws IOException
726 * in case of I/O error
727 */
728 public synchronized Story save(Story story, String luid, Progress pg)
729 throws IOException {
730 if (pg == null) {
731 pg = new Progress();
732 }
733
734 Instance.getInstance().getTraceHandler().trace(
735 this.getClass().getSimpleName() + ": saving story " + luid);
736
737 // Do not change the original metadata, but change the original story
738 MetaData meta = story.getMeta().clone();
739 story.setMeta(meta);
740
741 pg.setName("Saving story");
742
743 if (luid == null || luid.isEmpty()) {
744 meta.setLuid(String.format("%03d", getNextId()));
745 } else {
746 meta.setLuid(luid);
747 }
748
749 if (luid != null && getInfo(luid) != null) {
750 delete(luid);
751 }
752
753 story = doSave(story, pg);
754
755 updateInfo(story.getMeta());
756
757 Instance.getInstance().getTraceHandler()
758 .trace(this.getClass().getSimpleName() + ": story saved ("
759 + luid + ")");
760
761 pg.setName(meta.getTitle());
762 pg.done();
763 return story;
764 }
765
766 /**
767 * Delete the given {@link Story} from this {@link BasicLibrary}.
768 *
769 * @param luid
770 * the LUID of the target {@link Story}
771 *
772 * @throws IOException
773 * in case of I/O error
774 */
775 public synchronized void delete(String luid) throws IOException {
776 Instance.getInstance().getTraceHandler().trace(
777 this.getClass().getSimpleName() + ": deleting story " + luid);
778
779 doDelete(luid);
780 invalidateInfo(luid);
781
782 Instance.getInstance().getTraceHandler()
783 .trace(this.getClass().getSimpleName() + ": story deleted ("
784 + luid + ")");
785 }
786
787 /**
788 * Change the type (source) of the given {@link Story}.
789 *
790 * @param luid
791 * the {@link Story} LUID
792 * @param newSource
793 * the new source
794 * @param pg
795 * the optional progress reporter
796 *
797 * @throws IOException
798 * in case of I/O error or if the {@link Story} was not found
799 */
800 public synchronized void changeSource(String luid, String newSource,
801 Progress pg) throws IOException {
802 MetaData meta = getInfo(luid);
803 if (meta == null) {
804 throw new IOException("Story not found: " + luid);
805 }
806
807 changeSTA(luid, newSource, meta.getTitle(), meta.getAuthor(), pg);
808 }
809
810 /**
811 * Change the title (name) of the given {@link Story}.
812 *
813 * @param luid
814 * the {@link Story} LUID
815 * @param newTitle
816 * the new title
817 * @param pg
818 * the optional progress reporter
819 *
820 * @throws IOException
821 * in case of I/O error or if the {@link Story} was not found
822 */
823 public synchronized void changeTitle(String luid, String newTitle,
824 Progress pg) throws IOException {
825 MetaData meta = getInfo(luid);
826 if (meta == null) {
827 throw new IOException("Story not found: " + luid);
828 }
829
830 changeSTA(luid, meta.getSource(), newTitle, meta.getAuthor(), pg);
831 }
832
833 /**
834 * Change the author of the given {@link Story}.
835 *
836 * @param luid
837 * the {@link Story} LUID
838 * @param newAuthor
839 * the new author
840 * @param pg
841 * the optional progress reporter
842 *
843 * @throws IOException
844 * in case of I/O error or if the {@link Story} was not found
845 */
846 public synchronized void changeAuthor(String luid, String newAuthor,
847 Progress pg) throws IOException {
848 MetaData meta = getInfo(luid);
849 if (meta == null) {
850 throw new IOException("Story not found: " + luid);
851 }
852
853 changeSTA(luid, meta.getSource(), meta.getTitle(), newAuthor, pg);
854 }
855
856 /**
857 * Change the Source, Title and Author of the {@link Story} in one single
858 * go.
859 *
860 * @param luid
861 * the {@link Story} LUID
862 * @param newSource
863 * the new source
864 * @param newTitle
865 * the new title
866 * @param newAuthor
867 * the new author
868 * @param pg
869 * the optional progress reporter
870 *
871 * @throws IOException
872 * in case of I/O error or if the {@link Story} was not found
873 */
874 protected synchronized void changeSTA(String luid, String newSource,
875 String newTitle, String newAuthor, Progress pg) throws IOException {
876 MetaData meta = getInfo(luid);
877 if (meta == null) {
878 throw new IOException("Story not found: " + luid);
879 }
880
881 meta.setSource(newSource);
882 meta.setTitle(newTitle);
883 meta.setAuthor(newAuthor);
884 saveMeta(meta, pg);
885 }
886
887 /**
888 * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b>
889 * change) for this {@link Story}.
890 * <p>
891 * By default, delete the old {@link Story} then recreate a new
892 * {@link Story}.
893 * <p>
894 * Note that this behaviour can lead to data loss in case of problems!
895 *
896 * @param meta
897 * the new {@link MetaData} (LUID <b>MUST NOT</b> change)
898 * @param pg
899 * the optional {@link Progress}
900 *
901 * @throws IOException
902 * in case of I/O error or if the {@link Story} was not found
903 */
904 protected synchronized void saveMeta(MetaData meta, Progress pg)
905 throws IOException {
906 if (pg == null) {
907 pg = new Progress();
908 }
909
910 Progress pgGet = new Progress();
911 Progress pgSet = new Progress();
912 pg.addProgress(pgGet, 50);
913 pg.addProgress(pgSet, 50);
914
915 Story story = getStory(meta.getLuid(), pgGet);
916 if (story == null) {
917 throw new IOException("Story not found: " + meta.getLuid());
918 }
919
920 // TODO: this is not safe!
921 delete(meta.getLuid());
922 story.setMeta(meta);
923 save(story, meta.getLuid(), pgSet);
924
925 pg.done();
926 }
927
928 /**
929 * Describe a {@link Story} from its {@link MetaData} and return a list of
930 * title/value that represent this {@link Story}.
931 *
932 * @param meta
933 * the {@link MetaData} to represent
934 *
935 * @return the information, translated and sorted
936 */
937 static public Map<String, String> getMetaDesc(MetaData meta) {
938 Map<String, String> metaDesc = new LinkedHashMap<String, String>();
939
940 // TODO: i18n
941
942 StringBuilder tags = new StringBuilder();
943 for (String tag : meta.getTags()) {
944 if (tags.length() > 0) {
945 tags.append(", ");
946 }
947 tags.append(tag);
948 }
949
950 // TODO: i18n
951 metaDesc.put("Author", meta.getAuthor());
952 metaDesc.put("Published on", meta.getPublisher());
953 metaDesc.put("Publication date", meta.getDate());
954 metaDesc.put("Creation date", meta.getCreationDate());
955 String count = "";
956 if (meta.getWords() > 0) {
957 count = StringUtils.formatNumber(meta.getWords());
958 }
959 if (meta.isImageDocument()) {
960 metaDesc.put("Number of images", count);
961 } else {
962 metaDesc.put("Number of words", count);
963 }
964 metaDesc.put("Source", meta.getSource());
965 metaDesc.put("Subject", meta.getSubject());
966 metaDesc.put("Language", meta.getLang());
967 metaDesc.put("Tags", tags.toString());
968 metaDesc.put("URL", meta.getUrl());
969
970 return metaDesc;
971 }
972}