| 1 | package be.nikiroo.fanfix.library; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.net.URL; |
| 6 | import java.net.UnknownHostException; |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.Collections; |
| 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | import java.util.TreeMap; |
| 12 | |
| 13 | import be.nikiroo.fanfix.Instance; |
| 14 | import be.nikiroo.fanfix.data.MetaData; |
| 15 | import be.nikiroo.fanfix.data.Story; |
| 16 | import be.nikiroo.fanfix.output.BasicOutput; |
| 17 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
| 18 | import be.nikiroo.fanfix.supported.BasicSupport; |
| 19 | import be.nikiroo.fanfix.supported.SupportType; |
| 20 | import be.nikiroo.utils.Image; |
| 21 | import be.nikiroo.utils.Progress; |
| 22 | import be.nikiroo.utils.StringUtils; |
| 23 | |
| 24 | /** |
| 25 | * Manage a library of Stories: import, export, list, modify. |
| 26 | * <p> |
| 27 | * Each {@link Story} object will be associated with a (local to the library) |
| 28 | * unique ID, the LUID, which will be used to identify the {@link Story}. |
| 29 | * <p> |
| 30 | * Most of the {@link BasicLibrary} functions work on a partial (cover |
| 31 | * <b>MAY</b> not be included) {@link MetaData} object. |
| 32 | * |
| 33 | * @author niki |
| 34 | */ |
| 35 | abstract public class BasicLibrary { |
| 36 | /** |
| 37 | * A {@link BasicLibrary} status. |
| 38 | * |
| 39 | * @author niki |
| 40 | */ |
| 41 | public enum Status { |
| 42 | /** The library is ready and r/w. */ |
| 43 | READ_WRITE, |
| 44 | /** The library is ready, but read-only. */ |
| 45 | READ_ONLY, |
| 46 | /** The library is invalid (not correctly set up). */ |
| 47 | INVALID, |
| 48 | /** You are not allowed to access this library. */ |
| 49 | UNAUTHORIZED, |
| 50 | /** The library is currently out of commission. */ |
| 51 | UNAVAILABLE; |
| 52 | |
| 53 | /** |
| 54 | * The library is available (you can query it). |
| 55 | * <p> |
| 56 | * It does <b>not</b> specify if it is read-only or not. |
| 57 | * |
| 58 | * @return TRUE if it is |
| 59 | */ |
| 60 | public boolean isReady() { |
| 61 | return (this == READ_WRITE || this == READ_ONLY); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * This library can be modified (= you are allowed to modify it). |
| 66 | * |
| 67 | * @return TRUE if it is |
| 68 | */ |
| 69 | public boolean isWritable() { |
| 70 | return (this == READ_WRITE); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Return a name for this library (the UI may display this). |
| 76 | * <p> |
| 77 | * Must not be NULL. |
| 78 | * |
| 79 | * @return the name, or an empty {@link String} if none |
| 80 | */ |
| 81 | public String getLibraryName() { |
| 82 | return ""; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * The library status. |
| 87 | * |
| 88 | * @return the current status |
| 89 | */ |
| 90 | public Status getStatus() { |
| 91 | return Status.READ_WRITE; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Retrieve the main {@link File} corresponding to the given {@link Story}, |
| 96 | * which can be passed to an external reader or instance. |
| 97 | * <p> |
| 98 | * Do <b>NOT</b> alter this file. |
| 99 | * |
| 100 | * @param luid |
| 101 | * the Library UID of the story |
| 102 | * @param pg |
| 103 | * the optional {@link Progress} |
| 104 | * |
| 105 | * @return the corresponding {@link Story} |
| 106 | * |
| 107 | * @throws IOException |
| 108 | * in case of IOException |
| 109 | */ |
| 110 | public abstract File getFile(String luid, Progress pg) throws IOException; |
| 111 | |
| 112 | /** |
| 113 | * Return the cover image associated to this story. |
| 114 | * |
| 115 | * @param luid |
| 116 | * the Library UID of the story |
| 117 | * |
| 118 | * @return the cover image |
| 119 | * |
| 120 | * @throws IOException |
| 121 | * in case of IOException |
| 122 | */ |
| 123 | public abstract Image getCover(String luid) throws IOException; |
| 124 | |
| 125 | /** |
| 126 | * Return the cover image associated to this source. |
| 127 | * <p> |
| 128 | * By default, return the custom cover if any, and if not, return the cover |
| 129 | * of the first story with this source. |
| 130 | * |
| 131 | * @param source |
| 132 | * the source |
| 133 | * |
| 134 | * @return the cover image or NULL |
| 135 | * |
| 136 | * @throws IOException |
| 137 | * in case of IOException |
| 138 | */ |
| 139 | public Image getSourceCover(String source) throws IOException { |
| 140 | Image custom = getCustomSourceCover(source); |
| 141 | if (custom != null) { |
| 142 | return custom; |
| 143 | } |
| 144 | |
| 145 | List<MetaData> metas = getListBySource(source); |
| 146 | if (metas.size() > 0) { |
| 147 | return getCover(metas.get(0).getLuid()); |
| 148 | } |
| 149 | |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Return the cover image associated to this author. |
| 155 | * <p> |
| 156 | * By default, return the custom cover if any, and if not, return the cover |
| 157 | * of the first story with this author. |
| 158 | * |
| 159 | * @param author |
| 160 | * the author |
| 161 | * |
| 162 | * @return the cover image or NULL |
| 163 | * |
| 164 | * @throws IOException |
| 165 | * in case of IOException |
| 166 | */ |
| 167 | public Image getAuthorCover(String author) throws IOException { |
| 168 | Image custom = getCustomAuthorCover(author); |
| 169 | if (custom != null) { |
| 170 | return custom; |
| 171 | } |
| 172 | |
| 173 | List<MetaData> metas = getListByAuthor(author); |
| 174 | if (metas.size() > 0) { |
| 175 | return getCover(metas.get(0).getLuid()); |
| 176 | } |
| 177 | |
| 178 | return null; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Return the custom cover image associated to this source. |
| 183 | * <p> |
| 184 | * By default, return NULL. |
| 185 | * |
| 186 | * @param source |
| 187 | * the source to look for |
| 188 | * |
| 189 | * @return the custom cover or NULL if none |
| 190 | * |
| 191 | * @throws IOException |
| 192 | * in case of IOException |
| 193 | */ |
| 194 | @SuppressWarnings("unused") |
| 195 | public Image getCustomSourceCover(String source) throws IOException { |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Return the custom cover image associated to this author. |
| 201 | * <p> |
| 202 | * By default, return NULL. |
| 203 | * |
| 204 | * @param author |
| 205 | * the author to look for |
| 206 | * |
| 207 | * @return the custom cover or NULL if none |
| 208 | * |
| 209 | * @throws IOException |
| 210 | * in case of IOException |
| 211 | */ |
| 212 | @SuppressWarnings("unused") |
| 213 | public Image getCustomAuthorCover(String author) throws IOException { |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Set the source cover to the given story cover. |
| 219 | * |
| 220 | * @param source |
| 221 | * the source to change |
| 222 | * @param luid |
| 223 | * the story LUID |
| 224 | * |
| 225 | * @throws IOException |
| 226 | * in case of IOException |
| 227 | */ |
| 228 | public abstract void setSourceCover(String source, String luid) |
| 229 | throws IOException; |
| 230 | |
| 231 | /** |
| 232 | * Set the author cover to the given story cover. |
| 233 | * |
| 234 | * @param author |
| 235 | * the author to change |
| 236 | * @param luid |
| 237 | * the story LUID |
| 238 | * |
| 239 | * @throws IOException |
| 240 | * in case of IOException |
| 241 | */ |
| 242 | public abstract void setAuthorCover(String author, String luid) |
| 243 | throws IOException; |
| 244 | |
| 245 | /** |
| 246 | * Return the list of stories (represented by their {@link MetaData}, which |
| 247 | * <b>MAY</b> not have the cover included). |
| 248 | * |
| 249 | * @param pg |
| 250 | * the optional {@link Progress} |
| 251 | * |
| 252 | * @return the list (can be empty but not NULL) |
| 253 | * |
| 254 | * @throws IOException |
| 255 | * in case of IOException |
| 256 | */ |
| 257 | protected abstract List<MetaData> getMetas(Progress pg) throws IOException; |
| 258 | |
| 259 | /** |
| 260 | * Invalidate the {@link Story} cache (when the content should be re-read |
| 261 | * because it was changed). |
| 262 | */ |
| 263 | protected void invalidateInfo() { |
| 264 | invalidateInfo(null); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Invalidate the {@link Story} cache (when the content is removed). |
| 269 | * <p> |
| 270 | * All the cache can be deleted if NULL is passed as meta. |
| 271 | * |
| 272 | * @param luid |
| 273 | * the LUID of the {@link Story} to clear from the cache, or NULL |
| 274 | * for all stories |
| 275 | */ |
| 276 | protected abstract void invalidateInfo(String luid); |
| 277 | |
| 278 | /** |
| 279 | * Invalidate the {@link Story} cache (when the content has changed, but we |
| 280 | * already have it) with the new given meta. |
| 281 | * |
| 282 | * @param meta |
| 283 | * the {@link Story} to clear from the cache |
| 284 | * |
| 285 | * @throws IOException |
| 286 | * in case of IOException |
| 287 | */ |
| 288 | protected abstract void updateInfo(MetaData meta) throws IOException; |
| 289 | |
| 290 | /** |
| 291 | * Return the next LUID that can be used. |
| 292 | * |
| 293 | * @return the next luid |
| 294 | */ |
| 295 | protected abstract int getNextId(); |
| 296 | |
| 297 | /** |
| 298 | * Delete the target {@link Story}. |
| 299 | * |
| 300 | * @param luid |
| 301 | * the LUID of the {@link Story} |
| 302 | * |
| 303 | * @throws IOException |
| 304 | * in case of I/O error or if the {@link Story} wa not found |
| 305 | */ |
| 306 | protected abstract void doDelete(String luid) throws IOException; |
| 307 | |
| 308 | /** |
| 309 | * Actually save the story to the back-end. |
| 310 | * |
| 311 | * @param story |
| 312 | * the {@link Story} to save |
| 313 | * @param pg |
| 314 | * the optional {@link Progress} |
| 315 | * |
| 316 | * @return the saved {@link Story} (which may have changed, especially |
| 317 | * regarding the {@link MetaData}) |
| 318 | * |
| 319 | * @throws IOException |
| 320 | * in case of I/O error |
| 321 | */ |
| 322 | protected abstract Story doSave(Story story, Progress pg) |
| 323 | throws IOException; |
| 324 | |
| 325 | /** |
| 326 | * Refresh the {@link BasicLibrary}, that is, make sure all metas are |
| 327 | * loaded. |
| 328 | * |
| 329 | * @param pg |
| 330 | * the optional progress reporter |
| 331 | */ |
| 332 | public void refresh(Progress pg) { |
| 333 | try { |
| 334 | getMetas(pg); |
| 335 | } catch (IOException e) { |
| 336 | // We will let it fail later |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * List all the known types (sources) of stories. |
| 342 | * |
| 343 | * @return the sources |
| 344 | * |
| 345 | * @throws IOException |
| 346 | * in case of IOException |
| 347 | */ |
| 348 | public synchronized List<String> getSources() throws IOException { |
| 349 | List<String> list = new ArrayList<String>(); |
| 350 | for (MetaData meta : getMetas(null)) { |
| 351 | String storySource = meta.getSource(); |
| 352 | if (!list.contains(storySource)) { |
| 353 | list.add(storySource); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | Collections.sort(list); |
| 358 | return list; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * List all the known types (sources) of stories, grouped by directory |
| 363 | * ("Source_1/a" and "Source_1/b" will be grouped into "Source_1"). |
| 364 | * <p> |
| 365 | * Note that an empty item in the list means a non-grouped source (type) -- |
| 366 | * e.g., you could have for Source_1: |
| 367 | * <ul> |
| 368 | * <li><tt></tt>: empty, so source is "Source_1"</li> |
| 369 | * <li><tt>a</tt>: empty, so source is "Source_1/a"</li> |
| 370 | * <li><tt>b</tt>: empty, so source is "Source_1/b"</li> |
| 371 | * </ul> |
| 372 | * |
| 373 | * @return the grouped list |
| 374 | * |
| 375 | * @throws IOException |
| 376 | * in case of IOException |
| 377 | */ |
| 378 | public synchronized Map<String, List<String>> getSourcesGrouped() |
| 379 | throws IOException { |
| 380 | Map<String, List<String>> map = new TreeMap<String, List<String>>(); |
| 381 | for (String source : getSources()) { |
| 382 | String name; |
| 383 | String subname; |
| 384 | |
| 385 | int pos = source.indexOf('/'); |
| 386 | if (pos > 0 && pos < source.length() - 1) { |
| 387 | name = source.substring(0, pos); |
| 388 | subname = source.substring(pos + 1); |
| 389 | |
| 390 | } else { |
| 391 | name = source; |
| 392 | subname = ""; |
| 393 | } |
| 394 | |
| 395 | List<String> list = map.get(name); |
| 396 | if (list == null) { |
| 397 | list = new ArrayList<String>(); |
| 398 | map.put(name, list); |
| 399 | } |
| 400 | list.add(subname); |
| 401 | } |
| 402 | |
| 403 | return map; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * List all the known authors of stories. |
| 408 | * |
| 409 | * @return the authors |
| 410 | * |
| 411 | * @throws IOException |
| 412 | * in case of IOException |
| 413 | */ |
| 414 | public synchronized List<String> getAuthors() throws IOException { |
| 415 | List<String> list = new ArrayList<String>(); |
| 416 | for (MetaData meta : getMetas(null)) { |
| 417 | String storyAuthor = meta.getAuthor(); |
| 418 | if (!list.contains(storyAuthor)) { |
| 419 | list.add(storyAuthor); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | Collections.sort(list); |
| 424 | return list; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Return the list of authors, grouped by starting letter(s) if needed. |
| 429 | * <p> |
| 430 | * If the number of author is not too high, only one group with an empty |
| 431 | * name and all the authors will be returned. |
| 432 | * <p> |
| 433 | * If not, the authors will be separated into groups: |
| 434 | * <ul> |
| 435 | * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers |
| 436 | * </li> |
| 437 | * <li><tt>0-9</tt>: any authors whose name starts with a number</li> |
| 438 | * <li><tt>A-C</tt> (for instance): any author whose name starts with |
| 439 | * <tt>A</tt>, <tt>B</tt> or <tt>C</tt></li> |
| 440 | * </ul> |
| 441 | * Note that the letters used in the groups can vary (except <tt>*</tt> and |
| 442 | * <tt>0-9</tt>, which may only be present or not). |
| 443 | * |
| 444 | * @return the authors' names, grouped by letter(s) |
| 445 | * |
| 446 | * @throws IOException |
| 447 | * in case of IOException |
| 448 | */ |
| 449 | public Map<String, List<String>> getAuthorsGrouped() throws IOException { |
| 450 | int MAX = 20; |
| 451 | |
| 452 | Map<String, List<String>> groups = new TreeMap<String, List<String>>(); |
| 453 | List<String> authors = getAuthors(); |
| 454 | |
| 455 | // If all authors fit the max, just report them as is |
| 456 | if (authors.size() <= MAX) { |
| 457 | groups.put("", authors); |
| 458 | return groups; |
| 459 | } |
| 460 | |
| 461 | // Create groups A to Z, which can be empty here |
| 462 | for (char car = 'A'; car <= 'Z'; car++) { |
| 463 | groups.put(Character.toString(car), getAuthorsGroup(authors, car)); |
| 464 | } |
| 465 | |
| 466 | // Collapse them |
| 467 | List<String> keys = new ArrayList<String>(groups.keySet()); |
| 468 | for (int i = 0; i + 1 < keys.size(); i++) { |
| 469 | String keyNow = keys.get(i); |
| 470 | String keyNext = keys.get(i + 1); |
| 471 | |
| 472 | List<String> now = groups.get(keyNow); |
| 473 | List<String> next = groups.get(keyNext); |
| 474 | |
| 475 | int currentTotal = now.size() + next.size(); |
| 476 | if (currentTotal <= MAX) { |
| 477 | String key = keyNow.charAt(0) + "-" |
| 478 | + keyNext.charAt(keyNext.length() - 1); |
| 479 | |
| 480 | List<String> all = new ArrayList<String>(); |
| 481 | all.addAll(now); |
| 482 | all.addAll(next); |
| 483 | |
| 484 | groups.remove(keyNow); |
| 485 | groups.remove(keyNext); |
| 486 | groups.put(key, all); |
| 487 | |
| 488 | keys.set(i, key); // set the new key instead of key(i) |
| 489 | keys.remove(i + 1); // remove the next, consumed key |
| 490 | i--; // restart at key(i) |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // Add "special" groups |
| 495 | groups.put("*", getAuthorsGroup(authors, '*')); |
| 496 | groups.put("0-9", getAuthorsGroup(authors, '0')); |
| 497 | |
| 498 | // Prune empty groups |
| 499 | keys = new ArrayList<String>(groups.keySet()); |
| 500 | for (String key : keys) { |
| 501 | if (groups.get(key).isEmpty()) { |
| 502 | groups.remove(key); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | return groups; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Get all the authors that start with the given character: |
| 511 | * <ul> |
| 512 | * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers |
| 513 | * </li> |
| 514 | * <li><tt>0</tt>: any authors whose name starts with a number</li> |
| 515 | * <li><tt>A</tt> (any capital latin letter): any author whose name starts |
| 516 | * with <tt>A</tt></li> |
| 517 | * </ul> |
| 518 | * |
| 519 | * @param authors |
| 520 | * the full list of authors |
| 521 | * @param car |
| 522 | * the starting character, <tt>*</tt>, <tt>0</tt> or a capital |
| 523 | * letter |
| 524 | * |
| 525 | * @return the authors that fulfil the starting letter |
| 526 | */ |
| 527 | private List<String> getAuthorsGroup(List<String> authors, char car) { |
| 528 | List<String> accepted = new ArrayList<String>(); |
| 529 | for (String author : authors) { |
| 530 | char first = '*'; |
| 531 | for (int i = 0; first == '*' && i < author.length(); i++) { |
| 532 | String san = StringUtils.sanitize(author, true, true); |
| 533 | char c = san.charAt(i); |
| 534 | if (c >= '0' && c <= '9') { |
| 535 | first = '0'; |
| 536 | } else if (c >= 'a' && c <= 'z') { |
| 537 | first = (char) (c - 'a' + 'A'); |
| 538 | } else if (c >= 'A' && c <= 'Z') { |
| 539 | first = c; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (first == car) { |
| 544 | accepted.add(author); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | return accepted; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * List all the stories in the {@link BasicLibrary}. |
| 553 | * <p> |
| 554 | * Cover images <b>MAYBE</b> not included. |
| 555 | * |
| 556 | * @return the stories |
| 557 | * |
| 558 | * @throws IOException |
| 559 | * in case of IOException |
| 560 | */ |
| 561 | public synchronized List<MetaData> getList() throws IOException { |
| 562 | return getMetas(null); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * List all the stories of the given source type in the {@link BasicLibrary} |
| 567 | * , or all the stories if NULL is passed as a type. |
| 568 | * <p> |
| 569 | * Cover images not included. |
| 570 | * |
| 571 | * @param type |
| 572 | * the type of story to retrieve, or NULL for all |
| 573 | * |
| 574 | * @return the stories |
| 575 | * |
| 576 | * @throws IOException |
| 577 | * in case of IOException |
| 578 | */ |
| 579 | public synchronized List<MetaData> getListBySource(String type) |
| 580 | throws IOException { |
| 581 | List<MetaData> list = new ArrayList<MetaData>(); |
| 582 | for (MetaData meta : getMetas(null)) { |
| 583 | String storyType = meta.getSource(); |
| 584 | if (type == null || type.equalsIgnoreCase(storyType)) { |
| 585 | list.add(meta); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | Collections.sort(list); |
| 590 | return list; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * List all the stories of the given author in the {@link BasicLibrary}, or |
| 595 | * all the stories if NULL is passed as an author. |
| 596 | * <p> |
| 597 | * Cover images not included. |
| 598 | * |
| 599 | * @param author |
| 600 | * the author of the stories to retrieve, or NULL for all |
| 601 | * |
| 602 | * @return the stories |
| 603 | * |
| 604 | * @throws IOException |
| 605 | * in case of IOException |
| 606 | */ |
| 607 | public synchronized List<MetaData> getListByAuthor(String author) |
| 608 | throws IOException { |
| 609 | List<MetaData> list = new ArrayList<MetaData>(); |
| 610 | for (MetaData meta : getMetas(null)) { |
| 611 | String storyAuthor = meta.getAuthor(); |
| 612 | if (author == null || author.equalsIgnoreCase(storyAuthor)) { |
| 613 | list.add(meta); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | Collections.sort(list); |
| 618 | return list; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Retrieve a {@link MetaData} corresponding to the given {@link Story}, |
| 623 | * cover image <b>MAY</b> not be included. |
| 624 | * |
| 625 | * @param luid |
| 626 | * the Library UID of the story |
| 627 | * |
| 628 | * @return the corresponding {@link Story} |
| 629 | * |
| 630 | * @throws IOException |
| 631 | * in case of IOException |
| 632 | */ |
| 633 | public synchronized MetaData getInfo(String luid) throws IOException { |
| 634 | if (luid != null) { |
| 635 | for (MetaData meta : getMetas(null)) { |
| 636 | if (luid.equals(meta.getLuid())) { |
| 637 | return meta; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | return null; |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Retrieve a specific {@link Story}. |
| 647 | * |
| 648 | * @param luid |
| 649 | * the Library UID of the story |
| 650 | * @param pg |
| 651 | * the optional progress reporter |
| 652 | * |
| 653 | * @return the corresponding {@link Story} or NULL if not found |
| 654 | * |
| 655 | * @throws IOException |
| 656 | * in case of IOException |
| 657 | */ |
| 658 | public synchronized Story getStory(String luid, Progress pg) |
| 659 | throws IOException { |
| 660 | Progress pgMetas = new Progress(); |
| 661 | Progress pgStory = new Progress(); |
| 662 | if (pg != null) { |
| 663 | pg.setMinMax(0, 100); |
| 664 | pg.addProgress(pgMetas, 10); |
| 665 | pg.addProgress(pgStory, 90); |
| 666 | } |
| 667 | |
| 668 | MetaData meta = null; |
| 669 | for (MetaData oneMeta : getMetas(pgMetas)) { |
| 670 | if (oneMeta.getLuid().equals(luid)) { |
| 671 | meta = oneMeta; |
| 672 | break; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | pgMetas.done(); |
| 677 | |
| 678 | Story story = getStory(luid, meta, pgStory); |
| 679 | pgStory.done(); |
| 680 | |
| 681 | return story; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Retrieve a specific {@link Story}. |
| 686 | * |
| 687 | * @param luid |
| 688 | * the meta of the story |
| 689 | * @param pg |
| 690 | * the optional progress reporter |
| 691 | * |
| 692 | * @return the corresponding {@link Story} or NULL if not found |
| 693 | * |
| 694 | * @throws IOException |
| 695 | * in case of IOException |
| 696 | */ |
| 697 | public synchronized Story getStory(String luid, |
| 698 | @SuppressWarnings("javadoc") MetaData meta, Progress pg) |
| 699 | throws IOException { |
| 700 | |
| 701 | if (pg == null) { |
| 702 | pg = new Progress(); |
| 703 | } |
| 704 | |
| 705 | Progress pgGet = new Progress(); |
| 706 | Progress pgProcess = new Progress(); |
| 707 | |
| 708 | pg.setMinMax(0, 2); |
| 709 | pg.addProgress(pgGet, 1); |
| 710 | pg.addProgress(pgProcess, 1); |
| 711 | |
| 712 | Story story = null; |
| 713 | File file = getFile(luid, pgGet); |
| 714 | pgGet.done(); |
| 715 | try { |
| 716 | SupportType type = SupportType.valueOfAllOkUC(meta.getType()); |
| 717 | URL url = file.toURI().toURL(); |
| 718 | if (type != null) { |
| 719 | story = BasicSupport.getSupport(type, url) // |
| 720 | .process(pgProcess); |
| 721 | |
| 722 | // Because we do not want to clear the meta cache: |
| 723 | meta.setCover(story.getMeta().getCover()); |
| 724 | meta.setResume(story.getMeta().getResume()); |
| 725 | story.setMeta(meta); |
| 726 | // |
| 727 | } else { |
| 728 | throw new IOException("Unknown type: " + meta.getType()); |
| 729 | } |
| 730 | } catch (IOException e) { |
| 731 | // We should not have not-supported files in the |
| 732 | // library |
| 733 | Instance.getTraceHandler().error( |
| 734 | new IOException(String.format( |
| 735 | "Cannot load file of type '%s' from library: %s", |
| 736 | meta.getType(), file), e)); |
| 737 | } finally { |
| 738 | pgProcess.done(); |
| 739 | pg.done(); |
| 740 | } |
| 741 | |
| 742 | return story; |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Import the {@link Story} at the given {@link URL} into the |
| 747 | * {@link BasicLibrary}. |
| 748 | * |
| 749 | * @param url |
| 750 | * the {@link URL} to import |
| 751 | * @param pg |
| 752 | * the optional progress reporter |
| 753 | * |
| 754 | * @return the imported {@link Story} |
| 755 | * |
| 756 | * @throws UnknownHostException |
| 757 | * if the host is not supported |
| 758 | * @throws IOException |
| 759 | * in case of I/O error |
| 760 | */ |
| 761 | public Story imprt(URL url, Progress pg) throws IOException { |
| 762 | if (pg == null) |
| 763 | pg = new Progress(); |
| 764 | |
| 765 | pg.setMinMax(0, 1000); |
| 766 | Progress pgProcess = new Progress(); |
| 767 | Progress pgSave = new Progress(); |
| 768 | pg.addProgress(pgProcess, 800); |
| 769 | pg.addProgress(pgSave, 200); |
| 770 | |
| 771 | BasicSupport support = BasicSupport.getSupport(url); |
| 772 | if (support == null) { |
| 773 | throw new UnknownHostException("" + url); |
| 774 | } |
| 775 | |
| 776 | Story story = save(support.process(pgProcess), pgSave); |
| 777 | pg.done(); |
| 778 | |
| 779 | return story; |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * Import the story from one library to another, and keep the same LUID. |
| 784 | * |
| 785 | * @param other |
| 786 | * the other library to import from |
| 787 | * @param luid |
| 788 | * the Library UID |
| 789 | * @param pg |
| 790 | * the optional progress reporter |
| 791 | * |
| 792 | * @throws IOException |
| 793 | * in case of I/O error |
| 794 | */ |
| 795 | public void imprt(BasicLibrary other, String luid, Progress pg) |
| 796 | throws IOException { |
| 797 | Progress pgGetStory = new Progress(); |
| 798 | Progress pgSave = new Progress(); |
| 799 | if (pg == null) { |
| 800 | pg = new Progress(); |
| 801 | } |
| 802 | |
| 803 | pg.setMinMax(0, 2); |
| 804 | pg.addProgress(pgGetStory, 1); |
| 805 | pg.addProgress(pgSave, 1); |
| 806 | |
| 807 | Story story = other.getStory(luid, pgGetStory); |
| 808 | if (story != null) { |
| 809 | story = this.save(story, luid, pgSave); |
| 810 | pg.done(); |
| 811 | } else { |
| 812 | pg.done(); |
| 813 | throw new IOException("Cannot find story in Library: " + luid); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * Export the {@link Story} to the given target in the given format. |
| 819 | * |
| 820 | * @param luid |
| 821 | * the {@link Story} ID |
| 822 | * @param type |
| 823 | * the {@link OutputType} to transform it to |
| 824 | * @param target |
| 825 | * the target to save to |
| 826 | * @param pg |
| 827 | * the optional progress reporter |
| 828 | * |
| 829 | * @return the saved resource (the main saved {@link File}) |
| 830 | * |
| 831 | * @throws IOException |
| 832 | * in case of I/O error |
| 833 | */ |
| 834 | public File export(String luid, OutputType type, String target, Progress pg) |
| 835 | throws IOException { |
| 836 | Progress pgGetStory = new Progress(); |
| 837 | Progress pgOut = new Progress(); |
| 838 | if (pg != null) { |
| 839 | pg.setMax(2); |
| 840 | pg.addProgress(pgGetStory, 1); |
| 841 | pg.addProgress(pgOut, 1); |
| 842 | } |
| 843 | |
| 844 | BasicOutput out = BasicOutput.getOutput(type, false, false); |
| 845 | if (out == null) { |
| 846 | throw new IOException("Output type not supported: " + type); |
| 847 | } |
| 848 | |
| 849 | Story story = getStory(luid, pgGetStory); |
| 850 | if (story == null) { |
| 851 | throw new IOException("Cannot find story to export: " + luid); |
| 852 | } |
| 853 | |
| 854 | return out.process(story, target, pgOut); |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Save a {@link Story} to the {@link BasicLibrary}. |
| 859 | * |
| 860 | * @param story |
| 861 | * the {@link Story} to save |
| 862 | * @param pg |
| 863 | * the optional progress reporter |
| 864 | * |
| 865 | * @return the same {@link Story}, whose LUID may have changed |
| 866 | * |
| 867 | * @throws IOException |
| 868 | * in case of I/O error |
| 869 | */ |
| 870 | public Story save(Story story, Progress pg) throws IOException { |
| 871 | return save(story, null, pg); |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Save a {@link Story} to the {@link BasicLibrary} -- the LUID <b>must</b> |
| 876 | * be correct, or NULL to get the next free one. |
| 877 | * <p> |
| 878 | * Will override any previous {@link Story} with the same LUID. |
| 879 | * |
| 880 | * @param story |
| 881 | * the {@link Story} to save |
| 882 | * @param luid |
| 883 | * the <b>correct</b> LUID or NULL to get the next free one |
| 884 | * @param pg |
| 885 | * the optional progress reporter |
| 886 | * |
| 887 | * @return the same {@link Story}, whose LUID may have changed |
| 888 | * |
| 889 | * @throws IOException |
| 890 | * in case of I/O error |
| 891 | */ |
| 892 | public synchronized Story save(Story story, String luid, Progress pg) |
| 893 | throws IOException { |
| 894 | |
| 895 | Instance.getTraceHandler().trace( |
| 896 | this.getClass().getSimpleName() + ": saving story " + luid); |
| 897 | |
| 898 | // Do not change the original metadata, but change the original story |
| 899 | MetaData meta = story.getMeta().clone(); |
| 900 | story.setMeta(meta); |
| 901 | |
| 902 | if (luid == null || luid.isEmpty()) { |
| 903 | meta.setLuid(String.format("%03d", getNextId())); |
| 904 | } else { |
| 905 | meta.setLuid(luid); |
| 906 | } |
| 907 | |
| 908 | if (luid != null && getInfo(luid) != null) { |
| 909 | delete(luid); |
| 910 | } |
| 911 | |
| 912 | story = doSave(story, pg); |
| 913 | |
| 914 | updateInfo(story.getMeta()); |
| 915 | |
| 916 | Instance.getTraceHandler().trace( |
| 917 | this.getClass().getSimpleName() + ": story saved (" + luid |
| 918 | + ")"); |
| 919 | |
| 920 | return story; |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Delete the given {@link Story} from this {@link BasicLibrary}. |
| 925 | * |
| 926 | * @param luid |
| 927 | * the LUID of the target {@link Story} |
| 928 | * |
| 929 | * @throws IOException |
| 930 | * in case of I/O error |
| 931 | */ |
| 932 | public synchronized void delete(String luid) throws IOException { |
| 933 | Instance.getTraceHandler().trace( |
| 934 | this.getClass().getSimpleName() + ": deleting story " + luid); |
| 935 | |
| 936 | doDelete(luid); |
| 937 | invalidateInfo(luid); |
| 938 | |
| 939 | Instance.getTraceHandler().trace( |
| 940 | this.getClass().getSimpleName() + ": story deleted (" + luid |
| 941 | + ")"); |
| 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Change the type (source) of the given {@link Story}. |
| 946 | * |
| 947 | * @param luid |
| 948 | * the {@link Story} LUID |
| 949 | * @param newSource |
| 950 | * the new source |
| 951 | * @param pg |
| 952 | * the optional progress reporter |
| 953 | * |
| 954 | * @throws IOException |
| 955 | * in case of I/O error or if the {@link Story} was not found |
| 956 | */ |
| 957 | public synchronized void changeSource(String luid, String newSource, |
| 958 | Progress pg) throws IOException { |
| 959 | MetaData meta = getInfo(luid); |
| 960 | if (meta == null) { |
| 961 | throw new IOException("Story not found: " + luid); |
| 962 | } |
| 963 | |
| 964 | changeSTA(luid, newSource, meta.getTitle(), meta.getAuthor(), pg); |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Change the title (name) of the given {@link Story}. |
| 969 | * |
| 970 | * @param luid |
| 971 | * the {@link Story} LUID |
| 972 | * @param newTitle |
| 973 | * the new title |
| 974 | * @param pg |
| 975 | * the optional progress reporter |
| 976 | * |
| 977 | * @throws IOException |
| 978 | * in case of I/O error or if the {@link Story} was not found |
| 979 | */ |
| 980 | public synchronized void changeTitle(String luid, String newTitle, |
| 981 | Progress pg) throws IOException { |
| 982 | MetaData meta = getInfo(luid); |
| 983 | if (meta == null) { |
| 984 | throw new IOException("Story not found: " + luid); |
| 985 | } |
| 986 | |
| 987 | changeSTA(luid, meta.getSource(), newTitle, meta.getAuthor(), pg); |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * Change the author of the given {@link Story}. |
| 992 | * |
| 993 | * @param luid |
| 994 | * the {@link Story} LUID |
| 995 | * @param newAuthor |
| 996 | * the new author |
| 997 | * @param pg |
| 998 | * the optional progress reporter |
| 999 | * |
| 1000 | * @throws IOException |
| 1001 | * in case of I/O error or if the {@link Story} was not found |
| 1002 | */ |
| 1003 | public synchronized void changeAuthor(String luid, String newAuthor, |
| 1004 | Progress pg) throws IOException { |
| 1005 | MetaData meta = getInfo(luid); |
| 1006 | if (meta == null) { |
| 1007 | throw new IOException("Story not found: " + luid); |
| 1008 | } |
| 1009 | |
| 1010 | changeSTA(luid, meta.getSource(), meta.getTitle(), newAuthor, pg); |
| 1011 | } |
| 1012 | |
| 1013 | /** |
| 1014 | * Change the Source, Title and Author of the {@link Story} in one single |
| 1015 | * go. |
| 1016 | * |
| 1017 | * @param luid |
| 1018 | * the {@link Story} LUID |
| 1019 | * @param newSource |
| 1020 | * the new source |
| 1021 | * @param newTitle |
| 1022 | * the new title |
| 1023 | * @param newAuthor |
| 1024 | * the new author |
| 1025 | * @param pg |
| 1026 | * the optional progress reporter |
| 1027 | * |
| 1028 | * @throws IOException |
| 1029 | * in case of I/O error or if the {@link Story} was not found |
| 1030 | */ |
| 1031 | protected synchronized void changeSTA(String luid, String newSource, |
| 1032 | String newTitle, String newAuthor, Progress pg) throws IOException { |
| 1033 | MetaData meta = getInfo(luid); |
| 1034 | if (meta == null) { |
| 1035 | throw new IOException("Story not found: " + luid); |
| 1036 | } |
| 1037 | |
| 1038 | meta.setSource(newSource); |
| 1039 | meta.setTitle(newTitle); |
| 1040 | meta.setAuthor(newAuthor); |
| 1041 | saveMeta(meta, pg); |
| 1042 | |
| 1043 | invalidateInfo(luid); |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * Save back the current state of the {@link MetaData} (LUID <b>MUST NOT</b> |
| 1048 | * change) for this {@link Story}. |
| 1049 | * <p> |
| 1050 | * By default, delete the old {@link Story} then recreate a new |
| 1051 | * {@link Story}. |
| 1052 | * <p> |
| 1053 | * Note that this behaviour can lead to data loss in case of problems! |
| 1054 | * |
| 1055 | * @param meta |
| 1056 | * the new {@link MetaData} (LUID <b>MUST NOT</b> change) |
| 1057 | * @param pg |
| 1058 | * the optional {@link Progress} |
| 1059 | * |
| 1060 | * @throws IOException |
| 1061 | * in case of I/O error or if the {@link Story} was not found |
| 1062 | */ |
| 1063 | protected synchronized void saveMeta(MetaData meta, Progress pg) |
| 1064 | throws IOException { |
| 1065 | if (pg == null) { |
| 1066 | pg = new Progress(); |
| 1067 | } |
| 1068 | |
| 1069 | Progress pgGet = new Progress(); |
| 1070 | Progress pgSet = new Progress(); |
| 1071 | pg.addProgress(pgGet, 50); |
| 1072 | pg.addProgress(pgSet, 50); |
| 1073 | |
| 1074 | Story story = getStory(meta.getLuid(), pgGet); |
| 1075 | if (story == null) { |
| 1076 | throw new IOException("Story not found: " + meta.getLuid()); |
| 1077 | } |
| 1078 | |
| 1079 | // TODO: this is not safe! |
| 1080 | delete(meta.getLuid()); |
| 1081 | story.setMeta(meta); |
| 1082 | save(story, meta.getLuid(), pgSet); |
| 1083 | |
| 1084 | pg.done(); |
| 1085 | } |
| 1086 | } |