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