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