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