Search in sources :

Example 6 with OrgProperties

use of com.orgzly.org.OrgProperties in project orgzly-android by orgzly.

the class NotesClient method create.

/**
 * Insert as last note if position is not specified.
 */
public static Note create(Context context, Note note, NotePlace target, long time) {
    ContentValues values = new ContentValues();
    toContentValues(values, note);
    Uri insertUri;
    if (target != null) {
        /* Create note relative to an existing note. */
        insertUri = ProviderContract.Notes.ContentUri.notesIdTarget(target);
    } else {
        /* Create as last note. */
        insertUri = ProviderContract.Notes.ContentUri.notes();
    }
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    /* Insert note. */
    ops.add(ContentProviderOperation.newInsert(insertUri).withValues(values).build());
    /* Add each of the note's property. */
    int i = 0;
    OrgProperties properties = note.getHead().getProperties();
    for (String name : properties.keySet()) {
        String value = properties.get(name);
        values = new ContentValues();
        values.put(ProviderContract.NoteProperties.Param.NAME, name);
        values.put(ProviderContract.NoteProperties.Param.VALUE, value);
        values.put(ProviderContract.NoteProperties.Param.POSITION, i++);
        ops.add(ContentProviderOperation.newInsert(ProviderContract.NoteProperties.ContentUri.notesProperties()).withValues(values).withValueBackReference(ProviderContract.NoteProperties.Param.NOTE_ID, 0).build());
    }
    // Update book's modification time
    ops.add(ContentProviderOperation.newUpdate(ProviderContract.Books.ContentUri.books()).withValue(DbBook.MTIME, time).withSelection(DbBook._ID + " = " + note.getPosition().getBookId(), null).build());
    ContentProviderResult[] result;
    try {
        result = context.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    long noteId = ContentUris.parseId(result[0].uri);
    /* Update ID of newly inserted note. */
    note.setId(noteId);
    return note;
}
Also used : ContentValues(android.content.ContentValues) ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Uri(android.net.Uri) OrgProperties(com.orgzly.org.OrgProperties) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Aggregations

OrgProperties (com.orgzly.org.OrgProperties)6 ContentProviderOperation (android.content.ContentProviderOperation)3 ContentValues (android.content.ContentValues)3 OperationApplicationException (android.content.OperationApplicationException)3 Cursor (android.database.Cursor)3 Uri (android.net.Uri)3 RemoteException (android.os.RemoteException)3 ArrayList (java.util.ArrayList)3 ContentProviderResult (android.content.ContentProviderResult)2 DbNote (com.orgzly.android.provider.models.DbNote)2 HashSet (java.util.HashSet)2 SuppressLint (android.annotation.SuppressLint)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 Note (com.orgzly.android.Note)1 NotePosition (com.orgzly.android.NotePosition)1 SqliteQueryBuilder (com.orgzly.android.query.sql.SqliteQueryBuilder)1 CircularArrayList (com.orgzly.android.util.CircularArrayList)1 OrgFile (com.orgzly.org.OrgFile)1 OrgHead (com.orgzly.org.OrgHead)1 OrgDateTime (com.orgzly.org.datetime.OrgDateTime)1