public void setBookFavorite(Book book, boolean favorite) {
   if (favorite) {
     myDatabase.addToFavorites(book.getId());
   } else {
     myDatabase.removeFromFavorites(book.getId());
   }
   fireBookEvent(BookEvent.Updated, book);
 }
  public void removeBook(Book book, boolean deleteFromDisk) {
    synchronized (myBooksByFile) {
      myBooksByFile.remove(book.File);
      myBooksById.remove(book.getId());

      final List<Long> ids = myDatabase.loadRecentBookIds();
      if (ids.remove(book.getId())) {
        myDatabase.saveRecentBookIds(ids);
      }
      if (deleteFromDisk) {
        book.File.getPhysicalFile().delete();
      }
    }
    fireBookEvent(BookEvent.Removed, book);
  }
  private void addBook(Book book, boolean force) {
    if (book == null || book.getId() == -1) {
      return;
    }

    synchronized (myBooksByFile) {
      final Book existing = myBooksByFile.get(book.File);
      if (existing == null) {
        myBooksByFile.put(book.File, book);
        myBooksById.put(book.getId(), book);
        fireBookEvent(BookEvent.Added, book);
      } else if (force) {
        existing.updateFrom(book);
        fireBookEvent(BookEvent.Updated, existing);
      }
    }
  }
 public void addBookToRecentList(Book book) {
   final List<Long> ids = myDatabase.loadRecentBookIds();
   final Long bookId = book.getId();
   ids.remove(bookId);
   ids.add(0, bookId);
   if (ids.size() > 12) {
     ids.remove(12);
   }
   myDatabase.saveRecentBookIds(ids);
 }
 @Override
 protected void reloadBook(Book book) {
   final Cursor cursor =
       myDatabase.rawQuery(
           "SELECT title,encoding,language FROM Books WHERE book_id = " + book.getId(), null);
   if (cursor.moveToNext()) {
     book.setTitle(cursor.getString(0));
     book.setEncoding(cursor.getString(1));
     book.setLanguage(cursor.getString(2));
   }
   cursor.close();
 }
 private void createItem() {
   try {
     if (!totalItemsLocked) {
       totalItems = Integer.parseInt(text_totalItems.getText());
       totalItemsLocked = true;
     }
     String bookQuery = text_bookID.getText();
     boolean found = false;
     for (Book book : inventory)
       if (book.getId().equals(bookQuery)) {
         this.item = new Item(book, Integer.parseInt(text_quantity.getText()));
         found = true;
       }
     if (!found) bookNotFoundAlert(bookQuery);
   } catch (Exception e) {
     System.out.println("DEBUG: -----Start-----\n" + e + "DEBUG: ------End------\n");
   }
 }
 @Override
 protected void setExistingFlag(Collection<Book> books, boolean flag) {
   if (books.isEmpty()) {
     return;
   }
   final StringBuilder bookSet = new StringBuilder("(");
   boolean first = true;
   for (Book b : books) {
     if (first) {
       first = false;
     } else {
       bookSet.append(",");
     }
     bookSet.append(b.getId());
   }
   bookSet.append(")");
   myDatabase.execSQL(
       "UPDATE Books SET `exists` = " + (flag ? 1 : 0) + " WHERE book_id IN " + bookSet);
 }
  private void processFilesQueue() {
    synchronized (myFilesToRescan) {
      if (!myStatus.IsCompleted) {
        return;
      }

      final Set<ZLFile> filesToRemove = new HashSet<ZLFile>();
      for (String path : myFilesToRescan) {
        path = new ZLPhysicalFile(new File(path)).getPath();
        synchronized (myBooksByFile) {
          for (ZLFile f : myBooksByFile.keySet()) {
            if (f.getPath().startsWith(path)) {
              filesToRemove.add(f);
            }
          }
        }
      }

      for (ZLFile file : collectPhysicalFiles(myFilesToRescan)) {
        // TODO:
        // collect books from archives
        // rescan files and check book id
        filesToRemove.remove(file);
        final Book book = getBookByFile(file);
        if (book != null) {
          saveBook(book, false);
        }
      }

      for (ZLFile f : filesToRemove) {
        synchronized (myBooksByFile) {
          final Book book = myBooksByFile.remove(f);
          if (book != null) {
            myBooksById.remove(book.getId());
            fireBookEvent(BookEvent.Removed, book);
          }
        }
      }

      myFilesToRescan.clear();
    }
  }
 public List<Bookmark> invisibleBookmarks(Book book) {
   final List<Bookmark> list = myDatabase.loadBookmarks(book.getId(), false);
   Collections.sort(list, new Bookmark.ByTimeComparator());
   return list;
 }
  private void build() {
    // Step 0: get database books marked as "existing"
    final FileInfoSet fileInfos = new FileInfoSet(myDatabase);
    final Map<Long, Book> savedBooksByFileId = myDatabase.loadBooks(fileInfos, true);
    final Map<Long, Book> savedBooksByBookId = new HashMap<Long, Book>();
    for (Book b : savedBooksByFileId.values()) {
      savedBooksByBookId.put(b.getId(), b);
    }

    // Step 1: check if files corresponding to "existing" books really exists;
    //         add books to library if yes (and reload book info if needed);
    //         remove from recent/favorites list if no;
    //         collect newly "orphaned" books
    final Set<Book> orphanedBooks = new HashSet<Book>();
    final Set<ZLPhysicalFile> physicalFiles = new HashSet<ZLPhysicalFile>();
    int count = 0;
    for (Book book : savedBooksByFileId.values()) {
      final ZLPhysicalFile file = book.File.getPhysicalFile();
      if (file != null) {
        physicalFiles.add(file);
      }
      if (file != book.File && file != null && file.getPath().endsWith(".epub")) {
        continue;
      }
      if (book.File.exists()) {
        boolean doAdd = true;
        if (file == null) {
          continue;
        }
        if (!fileInfos.check(file, true)) {
          try {
            book.readMetaInfo();
            saveBook(book, false);
          } catch (BookReadingException e) {
            doAdd = false;
          }
          file.setCached(false);
        }
        if (doAdd) {
          // loaded from db
          addBook(book, false);
        }
      } else {
        orphanedBooks.add(book);
      }
    }
    myDatabase.setExistingFlag(orphanedBooks, false);

    // Step 2: collect books from physical files; add new, update already added,
    //         unmark orphaned as existing again, collect newly added
    final Map<Long, Book> orphanedBooksByFileId = myDatabase.loadBooks(fileInfos, false);
    final Set<Book> newBooks = new HashSet<Book>();

    final List<ZLPhysicalFile> physicalFilesList = collectPhysicalFiles(BookDirectories);
    for (ZLPhysicalFile file : physicalFilesList) {
      if (physicalFiles.contains(file)) {
        continue;
      }
      collectBooks(
          file,
          fileInfos,
          savedBooksByFileId,
          orphanedBooksByFileId,
          newBooks,
          !fileInfos.check(file, true));
      file.setCached(false);
    }

    // Step 3: add help file
    try {
      final ZLFile helpFile = BookUtil.getHelpFile();
      Book helpBook = savedBooksByFileId.get(fileInfos.getId(helpFile));
      if (helpBook == null) {
        helpBook = new Book(helpFile);
      }
      saveBook(helpBook, false);
      // saved
      addBook(helpBook, false);
    } catch (BookReadingException e) {
      // that's impossible
      e.printStackTrace();
    }

    // Step 4: save changes into database
    fileInfos.save();

    myDatabase.executeAsTransaction(
        new Runnable() {
          public void run() {
            for (Book book : newBooks) {
              saveBook(book, false);
            }
          }
        });
    myDatabase.setExistingFlag(newBooks, true);
  }
 public boolean isFavorite(Book book) {
   if (book == null) {
     return false;
   }
   return myDatabase.isFavorite(book.getId());
 }