public static SpecialList firstSpecialSafe(final Context ctx) { SpecialList s = SpecialList.firstSpecial(); if (s == null) { s = SpecialList.newSpecialList( ctx.getString(R.string.list_all), new HashMap<String, SpecialListsBaseProperty>(), true, ctx); if (ListMirakel.count() == 0) { ListMirakel.safeFirst(ctx); } s.save(false); } return s; }
public static SpecialList newSpecialList( final String name, final Map<String, SpecialListsBaseProperty> whereQuery, final boolean active, final Context context) { final int listId = ListMirakel.safeFirst(context).getId(); database.beginTransaction(); final ContentValues values = new ContentValues(); values.put(DatabaseHelper.NAME, name); values.put(WHERE_QUERY, serializeWhere(whereQuery)); values.put(ACTIVE, active); values.put(DEFAULT_LIST, listId); final long insertId = database.insert(TABLE, null, values); final Cursor cursor = database.query(TABLE, allColumns, "_id = " + insertId, null, null, null, null); cursor.moveToFirst(); database.execSQL( "update " + TABLE + " SET " + LFT + "=(SELECT MAX(" + RGT + ") from " + TABLE + ")+1, " + RGT + "=(SELECT MAX(" + RGT + ") from " + TABLE + ")+2 where " + DatabaseHelper.ID + "=" + insertId); database.setTransactionSuccessful(); database.endTransaction(); final SpecialList newSList = cursorToSList(cursor); cursor.close(); return newSList; }