Search in sources :

Example 1 with VersionedRook

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

the class BooksClient method fromCursor.

public static Book fromCursor(Cursor cursor) {
    Book book = new Book(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.NAME)), cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.PREFACE)), cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.MTIME)), cursor.getInt(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.IS_DUMMY)) == 1);
    book.getOrgFileSettings().setTitle(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.TITLE)));
    book.getOrgFileSettings().setIndented(cursor.getInt(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.IS_INDENTED)) == 1);
    book.setId(cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param._ID)));
    book.setSyncStatus(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNC_STATUS)));
    book.setDetectedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.DETECTED_ENCODING)));
    book.setSelectedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SELECTED_ENCODING)));
    book.setUsedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.USED_ENCODING)));
    /* Set link. */
    int linkRepoUriColumn = cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LINK_REPO_URL);
    if (!cursor.isNull(linkRepoUriColumn)) {
        Uri uri = Uri.parse(cursor.getString(linkRepoUriColumn));
        book.setLinkRepo(uri);
    }
    /* Set versioned rook. */
    if (!cursor.isNull(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_URL))) {
        Uri syncRookUri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_URL)));
        Uri syncRepoUri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_REPO_URL)));
        String rev = cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_REVISION));
        long mtime = cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_MTIME));
        VersionedRook vrook = new VersionedRook(syncRepoUri, syncRookUri, rev, mtime);
        book.setLastSyncedToRook(vrook);
    }
    /* Set last action. */
    String lastActionMessage = cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION));
    if (!TextUtils.isEmpty(lastActionMessage)) {
        BookAction.Type lastActionType = BookAction.Type.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION_TYPE)));
        long lastActionTimestamp = cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION_TIMESTAMP));
        BookAction lastAction = new BookAction(lastActionType, lastActionMessage, lastActionTimestamp);
        book.setLastAction(lastAction);
    }
    return book;
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) BookAction(com.orgzly.android.BookAction) Book(com.orgzly.android.Book) Uri(android.net.Uri)

Example 2 with VersionedRook

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

the class CurrentRooksClient method getAll.

public static Map<String, VersionedRook> getAll(Context context) {
    Map<String, VersionedRook> result = new HashMap<>();
    Cursor cursor = context.getContentResolver().query(ProviderContract.CurrentRooks.ContentUri.currentRooks(), null, null, null, null);
    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            VersionedRook vrook = CurrentRooksClient.fromCursor(cursor);
            result.put(vrook.getUri().toString(), vrook);
        }
    } finally {
        cursor.close();
    }
    return result;
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) HashMap(java.util.HashMap) Cursor(android.database.Cursor)

Example 3 with VersionedRook

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

the class CurrentRooksClient method set.

public static void set(Context context, List<VersionedRook> books) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    /* Delete all previous. */
    ops.add(ContentProviderOperation.newDelete(ProviderContract.CurrentRooks.ContentUri.currentRooks()).build());
    /* Insert each one. */
    for (VersionedRook book : books) {
        ContentValues values = new ContentValues();
        CurrentRooksClient.toContentValues(values, book);
        ops.add(ContentProviderOperation.newInsert(ProviderContract.CurrentRooks.ContentUri.currentRooks()).withValues(values).build());
    }
    try {
        context.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
    }
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) VersionedRook(com.orgzly.android.repos.VersionedRook) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 4 with VersionedRook

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

the class LocalDbRepoClient method retrieveBook.

public static VersionedRook retrieveBook(Context context, Uri repoUri, Uri uri, File file) throws IOException {
    Cursor cursor = context.getContentResolver().query(ProviderContract.LocalDbRepo.ContentUri.dbRepos(), null, ProviderContract.LocalDbRepo.Param.URL + "=?", new String[] { uri.toString() }, null);
    try {
        if (!cursor.moveToFirst()) {
            throw new IOException("Book " + uri + " not found in repo");
        }
        if (cursor.getCount() != 1) {
            throw new IOException("Found " + cursor.getCount() + " books matching name " + uri);
        }
        /* Get data from Cursor. */
        String content = cursor.getString(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.CONTENT));
        String revision = cursor.getString(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.REVISION));
        long mtime = cursor.getLong(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.MTIME));
        /* Write content to file. */
        MiscUtils.writeStringToFile(content, file);
        /* Return book. */
        return new VersionedRook(repoUri, uri, revision, mtime);
    } finally {
        cursor.close();
    }
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) IOException(java.io.IOException) Cursor(android.database.Cursor)

Example 5 with VersionedRook

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

the class LocalDbRepoClient method fromCursor.

public static VersionedRook fromCursor(Cursor cursor) {
    Uri repoUri = Uri.parse(cursor.getString(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.REPO_URL)));
    Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.URL)));
    String revision = cursor.getString(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.REVISION));
    long mtime = cursor.getLong(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.MTIME));
    return new VersionedRook(repoUri, uri, revision, mtime);
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) Uri(android.net.Uri)

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