update from master
[nikiroo-utils.git] / data / MetaData.java
1 package be.nikiroo.fanfix.data;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import be.nikiroo.utils.Image;
8 import be.nikiroo.utils.StringUtils;
9
10 /**
11 * The meta data associated to a {@link Story} object.
12 * <p>
13 * Note that some earlier version of the program did not save the resume as an
14 * external file; for those stories, the resume is not fetched until the story
15 * is.
16 * <p>
17 * The cover is never fetched until the story is.
18 *
19 * @author niki
20 */
21 public class MetaData implements Cloneable, Comparable<MetaData>, Serializable {
22 private static final long serialVersionUID = 1L;
23
24 private String title;
25 private String author;
26 private String date;
27 private Chapter resume;
28 private List<String> tags;
29 private Image cover;
30 private String subject;
31 private String source;
32 private String url;
33 private String uuid;
34 private String luid;
35 private String lang;
36 private String publisher;
37 private String type;
38 private boolean imageDocument;
39 private long words;
40 private String creationDate;
41 private boolean fakeCover;
42
43 /**
44 * Create an empty {@link MetaData}.
45 */
46 public MetaData() {
47 }
48
49 /**
50 * The title of the story.
51 *
52 * @return the title
53 */
54 public String getTitle() {
55 return title;
56 }
57
58 /**
59 * The title of the story.
60 *
61 * @param title
62 * the title to set
63 */
64 public void setTitle(String title) {
65 this.title = title;
66 }
67
68 /**
69 * The author of the story.
70 *
71 * @return the author
72 */
73 public String getAuthor() {
74 return author;
75 }
76
77 /**
78 * The author of the story.
79 *
80 * @param author
81 * the author to set
82 */
83 public void setAuthor(String author) {
84 this.author = author;
85 }
86
87 /**
88 * The story publication date.
89 *
90 * @return the date
91 */
92 public String getDate() {
93 return date;
94 }
95
96 /**
97 * The story publication date.
98 *
99 * @param date
100 * the date to set
101 */
102 public void setDate(String date) {
103 this.date = date;
104 }
105
106 /**
107 * The tags associated with this story.
108 *
109 * @return the tags
110 */
111 public List<String> getTags() {
112 return tags;
113 }
114
115 /**
116 * The tags associated with this story.
117 *
118 * @param tags
119 * the tags to set
120 */
121 public void setTags(List<String> tags) {
122 this.tags = tags;
123 }
124
125 /**
126 * The story resume (a.k.a. description).
127 * <p>
128 * This can be NULL if we don't have a resume for this {@link Story}.
129 * <p>
130 * Note that some earlier version of the program did not save the resume as
131 * an external file; for those stories, the resume is not fetched until the
132 * story is.
133 *
134 * @return the resume
135 */
136 public Chapter getResume() {
137 return resume;
138 }
139
140 /**
141 * The story resume (a.k.a. description).
142 * <p>
143 * Note that some earlier version of the program did not save the resume as
144 * an external file; for those stories, the resume is not fetched until the
145 * story is.
146 *
147 * @param resume
148 * the resume to set
149 */
150 public void setResume(Chapter resume) {
151 this.resume = resume;
152 }
153
154 /**
155 * The cover image of the story if any (can be NULL).
156 * <p>
157 * The cover is not fetched until the story is.
158 *
159 * @return the cover
160 */
161 public Image getCover() {
162 return cover;
163 }
164
165 /**
166 * The cover image of the story if any (can be NULL).
167 * <p>
168 * The cover is not fetched until the story is.
169 *
170 * @param cover
171 * the cover to set
172 */
173 public void setCover(Image cover) {
174 this.cover = cover;
175 }
176
177 /**
178 * The subject of the story (or instance, if it is a fanfiction, what is the
179 * original work; if it is a technical text, what is the technical
180 * subject...).
181 *
182 * @return the subject
183 */
184 public String getSubject() {
185 return subject;
186 }
187
188 /**
189 * The subject of the story (for instance, if it is a fanfiction, what is
190 * the original work; if it is a technical text, what is the technical
191 * subject...).
192 *
193 * @param subject
194 * the subject to set
195 */
196 public void setSubject(String subject) {
197 this.subject = subject;
198 }
199
200 /**
201 * The source of this story (which online library it was downloaded from).
202 *
203 * @return the source
204 */
205 public String getSource() {
206 return source;
207 }
208
209 /**
210 * The source of this story (which online library it was downloaded from).
211 *
212 * @param source
213 * the source to set
214 */
215 public void setSource(String source) {
216 this.source = source;
217 }
218
219 /**
220 * The original URL from which this {@link Story} was imported.
221 *
222 * @return the url
223 */
224 public String getUrl() {
225 return url;
226 }
227
228 /**
229 * The original URL from which this {@link Story} was imported.
230 *
231 * @param url
232 * the new url to set
233 */
234 public void setUrl(String url) {
235 this.url = url;
236 }
237
238 /**
239 * A unique value representing the story (it is often a URL).
240 *
241 * @return the uuid
242 */
243 public String getUuid() {
244 return uuid;
245 }
246
247 /**
248 * A unique value representing the story (it is often a URL).
249 *
250 * @param uuid
251 * the uuid to set
252 */
253 public void setUuid(String uuid) {
254 this.uuid = uuid;
255 }
256
257 /**
258 * A unique value representing the story in the local library.
259 *
260 * @return the luid
261 */
262 public String getLuid() {
263 return luid;
264 }
265
266 /**
267 * A unique value representing the story in the local library.
268 *
269 * @param luid
270 * the luid to set
271 */
272 public void setLuid(String luid) {
273 this.luid = luid;
274 }
275
276 /**
277 * The 2-letter code language of this story.
278 *
279 * @return the lang
280 */
281 public String getLang() {
282 return lang;
283 }
284
285 /**
286 * The 2-letter code language of this story.
287 *
288 * @param lang
289 * the lang to set
290 */
291 public void setLang(String lang) {
292 this.lang = lang;
293 }
294
295 /**
296 * The story publisher (other the same as the source).
297 *
298 * @return the publisher
299 */
300 public String getPublisher() {
301 return publisher;
302 }
303
304 /**
305 * The story publisher (other the same as the source).
306 *
307 * @param publisher
308 * the publisher to set
309 */
310 public void setPublisher(String publisher) {
311 this.publisher = publisher;
312 }
313
314 /**
315 * The output type this {@link Story} is in.
316 *
317 * @return the type the type
318 */
319 public String getType() {
320 return type;
321 }
322
323 /**
324 * The output type this {@link Story} is in.
325 *
326 * @param type
327 * the new type to set
328 */
329 public void setType(String type) {
330 this.type = type;
331 }
332
333 /**
334 * Document catering mostly to image files.
335 *
336 * @return the imageDocument state
337 */
338 public boolean isImageDocument() {
339 return imageDocument;
340 }
341
342 /**
343 * Document catering mostly to image files.
344 *
345 * @param imageDocument
346 * the imageDocument state to set
347 */
348 public void setImageDocument(boolean imageDocument) {
349 this.imageDocument = imageDocument;
350 }
351
352 /**
353 * The number of words in the related {@link Story}.
354 *
355 * @return the number of words
356 */
357 public long getWords() {
358 return words;
359 }
360
361 /**
362 * The number of words in the related {@link Story}.
363 *
364 * @param words
365 * the number of words to set
366 */
367 public void setWords(long words) {
368 this.words = words;
369 }
370
371 /**
372 * The (Fanfix) {@link Story} creation date.
373 *
374 * @return the creationDate
375 */
376 public String getCreationDate() {
377 return creationDate;
378 }
379
380 /**
381 * The (Fanfix) {@link Story} creation date.
382 *
383 * @param creationDate
384 * the creationDate to set
385 */
386 public void setCreationDate(String creationDate) {
387 this.creationDate = creationDate;
388 }
389
390 /**
391 * The cover in this {@link MetaData} object is "fake", in the sens that it
392 * comes from the actual content images.
393 *
394 * @return TRUE for a fake cover
395 */
396 public boolean isFakeCover() {
397 return fakeCover;
398 }
399
400 /**
401 * The cover in this {@link MetaData} object is "fake", in the sens that it
402 * comes from the actual content images
403 *
404 * @param fakeCover
405 * TRUE for a fake cover
406 */
407 public void setFakeCover(boolean fakeCover) {
408 this.fakeCover = fakeCover;
409 }
410
411 @Override
412 public int compareTo(MetaData o) {
413 if (o == null) {
414 return 1;
415 }
416
417 String id = (getTitle() == null ? "" : getTitle())
418 + (getUuid() == null ? "" : getUuid())
419 + (getLuid() == null ? "" : getLuid());
420 String oId = (getTitle() == null ? "" : o.getTitle())
421 + (getUuid() == null ? "" : o.getUuid())
422 + (o.getLuid() == null ? "" : o.getLuid());
423
424 return id.compareToIgnoreCase(oId);
425 }
426
427 @Override
428 public boolean equals(Object obj) {
429 if (!(obj instanceof MetaData)) {
430 return false;
431 }
432
433 return compareTo((MetaData) obj) == 0;
434 }
435
436 @Override
437 public int hashCode() {
438 String uuid = getUuid();
439 if (uuid == null) {
440 uuid = "" + title + author + source;
441 }
442
443 return uuid.hashCode();
444 }
445
446 @Override
447 public MetaData clone() {
448 MetaData meta = null;
449 try {
450 meta = (MetaData) super.clone();
451 } catch (CloneNotSupportedException e) {
452 // Did the clones rebel?
453 System.err.println(e);
454 }
455
456 if (tags != null) {
457 meta.tags = new ArrayList<String>(tags);
458 }
459
460 if (resume != null) {
461 meta.resume = resume.clone();
462 }
463
464 return meta;
465 }
466
467 /**
468 * Display a DEBUG {@link String} representation of this object.
469 * <p>
470 * This is not efficient, nor intended to be.
471 */
472 @Override
473 public String toString() {
474 String title = "";
475 if (getTitle() != null) {
476 title = getTitle();
477 }
478
479 StringBuilder tags = new StringBuilder();
480 if (getTags() != null) {
481 for (String tag : getTags()) {
482 if (tags.length() > 0) {
483 tags.append(", ");
484 }
485 tags.append(tag);
486 }
487 }
488
489 String resume = "";
490 if (getResume() != null) {
491 for (Paragraph para : getResume()) {
492 resume += "\n\t";
493 resume += para.toString().substring(0,
494 Math.min(para.toString().length(), 120));
495 }
496 resume += "\n";
497 }
498
499 String cover = "none";
500 if (getCover() != null) {
501 cover = StringUtils.formatNumber(getCover().getSize())
502 + "bytes";
503 }
504
505 return String.format(
506 "Meta %s:\n\tTitle: [%s]\n\tAuthor: [%s]\n\tDate: [%s]\n\tTags: [%s]\n\tWord count: [%s]"
507 + "\n\tResume: [%s]\n\tCover: [%s]",
508 luid, title, getAuthor(), getDate(), tags.toString(),
509 "" + words, resume, cover);
510 }
511 }