Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.data; |
2 | ||
8b152e7b | 3 | import java.io.Serializable; |
301791d3 | 4 | import java.util.ArrayList; |
08fe2e33 NR |
5 | import java.util.List; |
6 | ||
16a81ef7 | 7 | import be.nikiroo.utils.Image; |
c289a297 | 8 | import be.nikiroo.utils.StringUtils; |
16a81ef7 | 9 | |
08fe2e33 NR |
10 | /** |
11 | * The meta data associated to a {@link Story} object. | |
34f8780d NR |
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. | |
08fe2e33 NR |
18 | * |
19 | * @author niki | |
20 | */ | |
8b152e7b NR |
21 | public class MetaData implements Cloneable, Comparable<MetaData>, Serializable { |
22 | private static final long serialVersionUID = 1L; | |
23 | ||
08fe2e33 NR |
24 | private String title; |
25 | private String author; | |
26 | private String date; | |
27 | private Chapter resume; | |
28 | private List<String> tags; | |
16a81ef7 | 29 | private Image cover; |
08fe2e33 NR |
30 | private String subject; |
31 | private String source; | |
2206ef66 | 32 | private String url; |
08fe2e33 NR |
33 | private String uuid; |
34 | private String luid; | |
35 | private String lang; | |
36 | private String publisher; | |
fe999aa4 | 37 | private String type; |
08fe2e33 | 38 | private boolean imageDocument; |
793f1071 NR |
39 | private long words; |
40 | private String creationDate; | |
a9eb3f46 | 41 | private boolean fakeCover; |
08fe2e33 | 42 | |
96e252e0 NR |
43 | /** |
44 | * Create an empty {@link MetaData}. | |
45 | */ | |
46 | public MetaData() { | |
47 | } | |
48 | ||
08fe2e33 NR |
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). | |
75919f2d NR |
127 | * <p> |
128 | * This can be NULL if we don't have a resume for this {@link Story}. | |
34f8780d NR |
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. | |
08fe2e33 NR |
133 | * |
134 | * @return the resume | |
135 | */ | |
136 | public Chapter getResume() { | |
137 | return resume; | |
138 | } | |
139 | ||
140 | /** | |
141 | * The story resume (a.k.a. description). | |
34f8780d NR |
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. | |
08fe2e33 NR |
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). | |
34f8780d NR |
156 | * <p> |
157 | * The cover is not fetched until the story is. | |
08fe2e33 NR |
158 | * |
159 | * @return the cover | |
160 | */ | |
16a81ef7 | 161 | public Image getCover() { |
08fe2e33 NR |
162 | return cover; |
163 | } | |
164 | ||
165 | /** | |
166 | * The cover image of the story if any (can be NULL). | |
34f8780d NR |
167 | * <p> |
168 | * The cover is not fetched until the story is. | |
08fe2e33 NR |
169 | * |
170 | * @param cover | |
171 | * the cover to set | |
172 | */ | |
16a81ef7 | 173 | public void setCover(Image cover) { |
08fe2e33 NR |
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 | /** | |
2206ef66 | 201 | * The source of this story (which online library it was downloaded from). |
08fe2e33 NR |
202 | * |
203 | * @return the source | |
204 | */ | |
205 | public String getSource() { | |
206 | return source; | |
207 | } | |
208 | ||
209 | /** | |
2206ef66 | 210 | * The source of this story (which online library it was downloaded from). |
08fe2e33 NR |
211 | * |
212 | * @param source | |
213 | * the source to set | |
214 | */ | |
215 | public void setSource(String source) { | |
216 | this.source = source; | |
217 | } | |
218 | ||
219 | /** | |
2206ef66 NR |
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). | |
08fe2e33 NR |
240 | * |
241 | * @return the uuid | |
242 | */ | |
243 | public String getUuid() { | |
244 | return uuid; | |
245 | } | |
246 | ||
247 | /** | |
2206ef66 | 248 | * A unique value representing the story (it is often a URL). |
08fe2e33 NR |
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 | * | |
0efd25e3 | 269 | * @param luid |
08fe2e33 NR |
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 | ||
fe999aa4 NR |
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 | ||
08fe2e33 NR |
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 | } | |
301791d3 | 351 | |
793f1071 NR |
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 | ||
a9eb3f46 NR |
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 | ||
211f7ddb | 411 | @Override |
22848428 | 412 | public int compareTo(MetaData o) { |
9fe3f177 NR |
413 | if (o == null) { |
414 | return 1; | |
415 | } | |
416 | ||
58701825 NR |
417 | String id = (getTitle() == null ? "" : getTitle()) |
418 | + (getUuid() == null ? "" : getUuid()) | |
f40dbebd | 419 | + (getLuid() == null ? "" : getLuid()); |
58701825 NR |
420 | String oId = (getTitle() == null ? "" : o.getTitle()) |
421 | + (getUuid() == null ? "" : o.getUuid()) | |
f40dbebd | 422 | + (o.getLuid() == null ? "" : o.getLuid()); |
9fe3f177 | 423 | |
58701825 | 424 | return id.compareToIgnoreCase(oId); |
9fe3f177 NR |
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(); | |
22848428 NR |
444 | } |
445 | ||
301791d3 NR |
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) { | |
39cd9738 | 457 | meta.tags = new ArrayList<String>(tags); |
301791d3 | 458 | } |
39cd9738 | 459 | |
301791d3 | 460 | if (resume != null) { |
39cd9738 | 461 | meta.resume = resume.clone(); |
301791d3 NR |
462 | } |
463 | ||
464 | return meta; | |
465 | } | |
b5e9855b NR |
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) { | |
75919f2d | 501 | cover = StringUtils.formatNumber(getCover().getSize()) |
c289a297 | 502 | + "bytes"; |
b5e9855b NR |
503 | } |
504 | ||
505 | return String.format( | |
96e252e0 NR |
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); | |
b5e9855b | 510 | } |
08fe2e33 | 511 | } |