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;
}
Aggregations