Search in sources :

Example 1 with OrgParsedFile

use of com.orgzly.org.parser.OrgParsedFile in project orgzly-android by orgzly.

the class Shelf method reParseNotesStateAndTitles.

/**
 * Using current states configuration, update states and titles for all notes.
 * Keywords that were part of the title can become states and vice versa.
 */
public void reParseNotesStateAndTitles() throws IOException {
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    /* Get all notes. */
    Cursor cursor = mContext.getContentResolver().query(ProviderContract.Notes.ContentUri.notes(), null, null, null, null);
    try {
        OrgParser.Builder parserBuilder = new OrgParser.Builder().setTodoKeywords(AppPreferences.todoKeywordsSet(mContext)).setDoneKeywords(AppPreferences.doneKeywordsSet(mContext));
        OrgParserWriter parserWriter = new OrgParserWriter();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            /* Get current heading string. */
            Note note = NotesClient.fromCursor(cursor);
            /* Skip root node. */
            if (note.getPosition().getLevel() == 0) {
                continue;
            }
            OrgHead head = note.getHead();
            String headString = parserWriter.whiteSpacedHead(head, note.getPosition().getLevel(), false);
            /* Re-parse heading using current setting of keywords. */
            OrgParsedFile file = parserBuilder.setInput(headString).build().parse();
            if (BuildConfig.LOG_DEBUG)
                LogUtils.d(TAG, "Note " + note + " parsed has " + file.getHeadsInList().size() + " notes");
            if (file.getHeadsInList().size() != 1) {
                throw new IOException("Got " + file.getHeadsInList().size() + " notes after parsing \"" + headString + "\"");
            }
            OrgHead newHead = file.getHeadsInList().get(0).getHead();
            ContentValues values = new ContentValues();
            /* Update if state, title or priority are different. */
            if (!TextUtils.equals(newHead.getState(), head.getState()) || !TextUtils.equals(newHead.getTitle(), head.getTitle()) || !TextUtils.equals(newHead.getPriority(), head.getPriority())) {
                values.put(ProviderContract.Notes.UpdateParam.TITLE, newHead.getTitle());
                values.put(ProviderContract.Notes.UpdateParam.STATE, newHead.getState());
                values.put(ProviderContract.Notes.UpdateParam.PRIORITY, newHead.getPriority());
            }
            if (values.size() > 0) {
                ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(ProviderContract.Notes.ContentUri.notes(), cursor.getLong(0))).withValues(values).build());
            }
        }
    } finally {
        cursor.close();
    }
    /*
         * Apply batch.
         */
    try {
        mContext.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    notifyDataChanged(mContext);
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) OrgHead(com.orgzly.org.OrgHead) CircularArrayList(com.orgzly.android.util.CircularArrayList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(android.database.Cursor) OrgParserWriter(com.orgzly.org.parser.OrgParserWriter) OrgParsedFile(com.orgzly.org.parser.OrgParsedFile) OrgParser(com.orgzly.org.parser.OrgParser) DbNote(com.orgzly.android.provider.models.DbNote) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Aggregations

ContentProviderOperation (android.content.ContentProviderOperation)1 ContentValues (android.content.ContentValues)1 OperationApplicationException (android.content.OperationApplicationException)1 Cursor (android.database.Cursor)1 RemoteException (android.os.RemoteException)1 DbNote (com.orgzly.android.provider.models.DbNote)1 CircularArrayList (com.orgzly.android.util.CircularArrayList)1 OrgHead (com.orgzly.org.OrgHead)1 OrgParsedFile (com.orgzly.org.parser.OrgParsedFile)1 OrgParser (com.orgzly.org.parser.OrgParser)1 OrgParserWriter (com.orgzly.org.parser.OrgParserWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1