Network server and Library + nikiroo-utils update
[fanfix.git] / src / be / nikiroo / fanfix / data / Story.java
1 package be.nikiroo.fanfix.data;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 /**
8 * The main data class, where the whole story resides.
9 *
10 * @author niki
11 */
12 public class Story implements Iterable<Chapter> {
13 private MetaData meta;
14 private List<Chapter> chapters = new ArrayList<Chapter>();
15 private List<Chapter> empty = new ArrayList<Chapter>();
16
17 /**
18 * The metadata about this {@link Story}.
19 *
20 * @return the meta
21 */
22 public MetaData getMeta() {
23 return meta;
24 }
25
26 /**
27 * The metadata about this {@link Story}.
28 *
29 * @param meta
30 * the meta to set
31 */
32 public void setMeta(MetaData meta) {
33 this.meta = meta;
34 }
35
36 /**
37 * The chapters of the story.
38 *
39 * @return the chapters
40 */
41 public List<Chapter> getChapters() {
42 return chapters;
43 }
44
45 /**
46 * The chapters of the story.
47 *
48 * @param chapters
49 * the chapters to set
50 */
51 public void setChapters(List<Chapter> chapters) {
52 this.chapters = chapters;
53 }
54
55 /**
56 * Get an iterator on the {@link Chapter}s.
57 */
58 public Iterator<Chapter> iterator() {
59 return chapters == null ? empty.iterator() : chapters.iterator();
60 }
61
62 /**
63 * Display a DEBUG {@link String} representation of this object.
64 * <p>
65 * This is not efficient, nor intended to be.
66 */
67 @Override
68 public String toString() {
69 String title = "";
70 if (meta != null && meta.getTitle() != null) {
71 title = meta.getTitle();
72 }
73
74 String tags = "";
75 if (meta != null && meta.getTags() != null) {
76 for (String tag : meta.getTags()) {
77 if (!tags.isEmpty()) {
78 tags += ", ";
79 }
80 tags += tag;
81 }
82 }
83
84 String resume = "";
85 if (meta != null && meta.getResume() != null) {
86 for (Paragraph para : meta.getResume()) {
87 resume += "\n\t";
88 resume += para.toString().substring(0,
89 Math.min(para.toString().length(), 120));
90 }
91 resume += "\n";
92 }
93
94 String cover = (meta == null || meta.getCover() == null) ? "none"
95 : meta.getCover().getWidth() + "x"
96 + meta.getCover().getHeight();
97 return String.format(
98 "Title: [%s]\nAuthor: [%s]\nDate: [%s]\nTags: [%s]\n"
99 + "Resume: [%s]\nCover: [%s]", title, meta == null ? ""
100 : meta.getAuthor(), meta == null ? "" : meta.getDate(),
101 tags, resume, cover);
102 }
103 }