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