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