use of com.orgzly.android.repos.Repo in project orgzly-android by orgzly.
the class Shelf method renameBook.
public void renameBook(Book book, String name) throws IOException {
String oldName = book.getName();
/* Make sure there is no notebook with this name. */
if (getBook(name) != null) {
throw new IOException("Notebook with that name already exists");
}
/* Make sure link's repo is the same as sync book repo. */
if (book.hasLink() && book.getLastSyncedToRook() != null) {
if (!book.getLinkRepo().equals(book.getLastSyncedToRook().getRepoUri())) {
String s = BookSyncStatus.ROOK_AND_VROOK_HAVE_DIFFERENT_REPOS.toString();
setBookStatus(book, s, new BookAction(BookAction.Type.ERROR, s));
return;
}
}
/* Do not rename if there are local changes. */
if (book.getLastSyncedToRook() != null) {
if (book.isModifiedAfterLastSync()) {
throw new IOException("Notebook is not synced");
}
}
/* Prefer link. */
if (book.getLastSyncedToRook() != null) {
VersionedRook vrook = book.getLastSyncedToRook();
Repo repo = RepoFactory.getFromUri(mContext, vrook.getRepoUri());
VersionedRook movedVrook = repo.renameBook(vrook.getUri(), name);
book.setLastSyncedToRook(movedVrook);
BooksClient.saved(mContext, book.getId(), movedVrook);
}
if (BooksClient.updateName(mContext, book.getId(), name) != 1) {
String msg = mContext.getString(R.string.failed_renaming_book);
setBookStatus(book, null, new BookAction(BookAction.Type.ERROR, msg));
throw new IOException(msg);
}
setBookStatus(book, null, new BookAction(BookAction.Type.INFO, mContext.getString(R.string.renamed_book_from, oldName)));
}
Aggregations