Search in sources :

Example 6 with VersionedRook

use of com.orgzly.android.repos.VersionedRook in project orgzly-android by orgzly.

the class BookNamesake method getAll.

/**
 * Create links between each local book and each remote book with the same name.
 */
public static Map<String, BookNamesake> getAll(Context context, List<Book> books, List<VersionedRook> versionedRooks) {
    Map<String, BookNamesake> namesakes = new HashMap<>();
    /* Create links from all local books first. */
    for (Book book : books) {
        BookNamesake pair = new BookNamesake(book.getName());
        namesakes.put(book.getName(), pair);
        pair.setBook(book);
    }
    /* Set repo books. */
    for (VersionedRook book : versionedRooks) {
        String fileName = BookName.getFileName(context, book.getUri());
        String name = BookName.fromFileName(fileName).getName();
        BookNamesake pair = namesakes.get(name);
        if (pair == null) {
            /* Local file doesn't exists, create new pair. */
            pair = new BookNamesake(name);
            namesakes.put(name, pair);
        }
        /* Add remote book. */
        pair.addRook(book);
    }
    return namesakes;
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) HashMap(java.util.HashMap) Book(com.orgzly.android.Book)

Example 7 with VersionedRook

use of com.orgzly.android.repos.VersionedRook in project orgzly-android by orgzly.

the class BookNamesake method updateStatus.

/**
 * States to consider:
 *
 * - Book exists
 *   - Book is dummy
 *   - Book has a link
 *     - Linked remote book exists
 *   - Book has a last-synced-with remote book
 * - Remote book exists
 */
/* TODO: Case: Remote book deleted? */
public void updateStatus(int reposCount) {
    /* Sanity check. Group's name must come from somewhere - local or remote books. */
    if (book == null && versionedRooks.isEmpty()) {
        throw new IllegalStateException("BookNameGroup does not contain any books");
    }
    if (book == null) {
        if (versionedRooks.size() == 1) {
            status = BookSyncStatus.NO_BOOK_ONE_ROOK;
        } else {
            status = BookSyncStatus.NO_BOOK_MULTIPLE_ROOKS;
        }
        return;
    } else if (versionedRooks.isEmpty()) {
        if (book.isDummy()) {
            /* Only dummy exists. */
            status = BookSyncStatus.ONLY_DUMMY;
        } else {
            if (book.hasLink()) {
                /* Only local book with a link. */
                status = BookSyncStatus.ONLY_BOOK_WITH_LINK;
            } else {
                /* Only local book without link. */
                if (reposCount > 1) {
                    status = BookSyncStatus.ONLY_BOOK_WITHOUT_LINK_AND_MULTIPLE_REPOS;
                } else {
                    status = BookSyncStatus.ONLY_BOOK_WITHOUT_LINK_AND_ONE_REPO;
                }
            // TODO: What about no repos?
            }
        }
        return;
    }
    if (book.hasLink()) {
        // Book has link set.
        VersionedRook latestLinkedRook = getLatestLinkedRookVersion(book, versionedRooks);
        if (latestLinkedRook == null) {
            /* Both local and remote book exist with the same name.
                 * Book has a link, however that link is not pointing to an existing remote book.
                 */
            status = BookSyncStatus.BOOK_WITH_LINK_AND_ROOK_EXISTS_BUT_LINK_POINTING_TO_DIFFERENT_ROOK;
            // TODO: So what's the problem? Just save it then? But can we just overwrite whatever is link pointing too?
            return;
        }
        setLatestLinkedRook(latestLinkedRook);
        if (book.isDummy()) {
            status = BookSyncStatus.DUMMY_WITH_LINK;
            return;
        }
        if (book.getLastSyncedToRook() == null) {
            status = BookSyncStatus.CONFLICT_BOOK_WITH_LINK_AND_ROOK_BUT_NEVER_SYNCED_BEFORE;
            return;
        }
        if (!book.getLastSyncedToRook().getUri().equals(latestLinkedRook.getUri())) {
            status = BookSyncStatus.CONFLICT_LAST_SYNCED_ROOK_AND_LATEST_ROOK_ARE_DIFFERENT;
            return;
        }
        // TODO: We get difference even if the file content is identical - if mtimes (revisions) are different - do compare content too in that case. Size first for speed.
        if (book.getLastSyncedToRook().getRevision().equals(latestLinkedRook.getRevision())) {
            if (book.isModifiedAfterLastSync()) {
                // Local change.
                status = BookSyncStatus.BOOK_WITH_LINK_LOCAL_MODIFIED;
            } else {
                status = BookSyncStatus.NO_CHANGE;
            }
        } else {
            /* Remote book has been modified. */
            if (book.isModifiedAfterLastSync()) {
                /* Uh oh. Both local and remote modified. */
                status = BookSyncStatus.CONFLICT_BOTH_BOOK_AND_ROOK_MODIFIED;
            } else {
                status = BookSyncStatus.BOOK_WITH_LINK_AND_ROOK_MODIFIED;
            }
        }
    } else {
        if (!book.isDummy()) {
            status = BookSyncStatus.BOOK_WITHOUT_LINK_AND_ONE_OR_MORE_ROOKS_EXIST;
        } else {
            if (versionedRooks.size() == 1) {
                status = BookSyncStatus.DUMMY_WITHOUT_LINK_AND_ONE_ROOK;
            } else {
                status = BookSyncStatus.DUMMY_WITHOUT_LINK_AND_MULTIPLE_ROOKS;
            }
        }
    }
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook)

Example 8 with VersionedRook

use of com.orgzly.android.repos.VersionedRook in project orgzly-android by orgzly.

the class ProviderTest method testSaveBook.

/**
 * Saving book to repo.
 */
@Test
public void testSaveBook() throws IOException {
    shelfTestUtils.setupBook("book-1", "* Note 1\n** Note 2");
    shelfTestUtils.setupRepo("mock://repo");
    long now = System.currentTimeMillis();
    Book savedBook = shelf.saveBookToRepo("mock://repo", "book-1.org", shelf.getBook(1), BookName.Format.ORG);
    VersionedRook vrook = savedBook.getLastSyncedToRook();
    assertEquals("book-1", BookName.getInstance(context, vrook).getName());
    assertEquals("mock://repo", vrook.getRepoUri().toString());
    assertEquals("mock://repo/book-1.org", vrook.getUri().toString());
    assertTrue(vrook.getMtime() >= now);
    Book book = shelf.getBook(1);
    vrook = book.getLastSyncedToRook();
    assertEquals("book-1", BookName.getInstance(context, vrook).getName());
    assertEquals("mock://repo", vrook.getRepoUri().toString());
    assertEquals("mock://repo/book-1.org", vrook.getUri().toString());
    assertTrue(vrook.getMtime() >= now);
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) Book(com.orgzly.android.Book) OrgzlyTest(com.orgzly.android.OrgzlyTest) Test(org.junit.Test)

Example 9 with VersionedRook

use of com.orgzly.android.repos.VersionedRook in project orgzly-android by orgzly.

the class Shelf method saveBookToRepo.

/**
 * Exports {@code Book}, uploads it to repo and link it to newly created
 * {@link com.orgzly.android.repos.VersionedRook}.
 *
 * @return {@link Book}
 * @throws IOException
 */
public Book saveBookToRepo(String repoUrl, String fileName, Book book, BookName.Format format) throws IOException {
    Repo repo = RepoFactory.getFromUri(mContext, repoUrl);
    if (repo == null) {
        throw new IOException("Unsupported repository URL \"" + repoUrl + "\"");
    }
    VersionedRook uploadedBook;
    File tmpFile = getTempBookFile();
    try {
        /* Write to temporary file. */
        writeBookToFile(book, format, tmpFile);
        /* Upload to repo. */
        uploadedBook = repo.storeBook(tmpFile, fileName);
    } finally {
        /* Delete temporary file. */
        tmpFile.delete();
    }
    book.setLastSyncedToRook(uploadedBook);
    BooksClient.saved(mContext, book.getId(), uploadedBook);
    return book;
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) Repo(com.orgzly.android.repos.Repo) IOException(java.io.IOException) File(java.io.File) OrgParsedFile(com.orgzly.org.parser.OrgParsedFile)

Example 10 with VersionedRook

use of com.orgzly.android.repos.VersionedRook in project orgzly-android by orgzly.

the class Shelf method getBooksFromAllRepos.

/**
 * Goes through each repository and collects all books from each one.
 */
public List<VersionedRook> getBooksFromAllRepos(Map<String, Repo> repos) throws IOException {
    List<VersionedRook> result = new ArrayList<>();
    if (repos == null) {
        repos = getAllRepos();
    }
    for (Repo repo : repos.values()) {
        /* Each repository. */
        List<VersionedRook> libBooks = repo.getBooks();
        for (VersionedRook vrook : libBooks) {
            /* Each book in repository. */
            result.add(vrook);
        }
    }
    CurrentRooksClient.set(mContext, result);
    return result;
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) Repo(com.orgzly.android.repos.Repo) CircularArrayList(com.orgzly.android.util.CircularArrayList) ArrayList(java.util.ArrayList)

Aggregations

VersionedRook (com.orgzly.android.repos.VersionedRook)15 Repo (com.orgzly.android.repos.Repo)5 IOException (java.io.IOException)5 Cursor (android.database.Cursor)3 Uri (android.net.Uri)3 Book (com.orgzly.android.Book)3 ArrayList (java.util.ArrayList)3 OrgParsedFile (com.orgzly.org.parser.OrgParsedFile)2 File (java.io.File)2 HashMap (java.util.HashMap)2 ContentProviderOperation (android.content.ContentProviderOperation)1 ContentValues (android.content.ContentValues)1 OperationApplicationException (android.content.OperationApplicationException)1 RemoteException (android.os.RemoteException)1 BookAction (com.orgzly.android.BookAction)1 OrgzlyTest (com.orgzly.android.OrgzlyTest)1 BookNamesake (com.orgzly.android.sync.BookNamesake)1 CircularArrayList (com.orgzly.android.util.CircularArrayList)1 Test (org.junit.Test)1