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