Search in sources :

Example 1 with NOT_FOUND_NOTE_TYPE

use of com.ichi2.libanki.Models.NOT_FOUND_NOTE_TYPE in project Anki-Android by ankidroid.

the class CardContentProvider method bulkInsertNotes.

/**
 * This implementation optimizes for when the notes are grouped according to model.
 */
private int bulkInsertNotes(ContentValues[] valuesArr, long deckId) {
    if (valuesArr == null || valuesArr.length == 0) {
        return 0;
    }
    Collection col = CollectionHelper.getInstance().getCol(mContext);
    if (col == null) {
        throw new IllegalStateException(COL_NULL_ERROR_MSG);
    }
    if (col.getDecks().isDyn(deckId)) {
        throw new IllegalArgumentException("A filtered deck cannot be specified as the deck in bulkInsertNotes");
    }
    col.log(String.format(Locale.US, "bulkInsertNotes: %d items.\n%s", valuesArr.length, getLogMessage("bulkInsert", null)));
    // for caching model information (so we don't have to query for each note)
    long modelId = NOT_FOUND_NOTE_TYPE;
    Model model = null;
    // is it okay to move this outside the for-loop? Is it needed at all?
    col.getDecks().flush();
    SupportSQLiteDatabase sqldb = col.getDb().getDatabase();
    try {
        int result = 0;
        sqldb.beginTransaction();
        for (int i = 0; i < valuesArr.length; i++) {
            ContentValues values = valuesArr[i];
            if (values == null) {
                continue;
            }
            String flds = values.getAsString(FlashCardsContract.Note.FLDS);
            if (flds == null) {
                continue;
            }
            Models.AllowEmpty allowEmpty = Models.AllowEmpty.fromBoolean(values.getAsBoolean(FlashCardsContract.Note.ALLOW_EMPTY));
            Long thisModelId = values.getAsLong(FlashCardsContract.Note.MID);
            if (thisModelId == null || thisModelId < 0) {
                Timber.d("Unable to get model at index: %d", i);
                continue;
            }
            String[] fldsArray = Utils.splitFields(flds);
            if (model == null || thisModelId != modelId) {
                // new modelId so need to recalculate model, modelId and invalidate duplicateChecker (which is based on previous model)
                model = col.getModels().get(thisModelId);
                modelId = thisModelId;
            }
            // Create empty note
            // for some reason we cannot pass modelId in here
            com.ichi2.libanki.Note newNote = new com.ichi2.libanki.Note(col, model);
            // Check that correct number of flds specified
            if (fldsArray.length != newNote.getFields().length) {
                throw new IllegalArgumentException("Incorrect flds argument : " + flds);
            }
            for (int idx = 0; idx < fldsArray.length; idx++) {
                newNote.setField(idx, fldsArray[idx]);
            }
            // Set tags
            String tags = values.getAsString(FlashCardsContract.Note.TAGS);
            if (tags != null) {
                newNote.setTagsFromStr(tags);
            }
            // Add to collection
            col.addNote(newNote, allowEmpty);
            for (Card card : newNote.cards()) {
                card.setDid(deckId);
                card.flush();
            }
            result++;
        }
        col.save();
        sqldb.setTransactionSuccessful();
        return result;
    } finally {
        DB.safeEndInTransaction(sqldb);
    }
}
Also used : ContentValues(android.content.ContentValues) Note(com.ichi2.libanki.Note) Card(com.ichi2.libanki.Card) SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) Models(com.ichi2.libanki.Models)

Aggregations

ContentValues (android.content.ContentValues)1 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)1 Card (com.ichi2.libanki.Card)1 Collection (com.ichi2.libanki.Collection)1 Model (com.ichi2.libanki.Model)1 Models (com.ichi2.libanki.Models)1 Note (com.ichi2.libanki.Note)1