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