@ -9,6 +9,7 @@ import android.content.ContentValues;
import android.database.sqlite.SQLiteConstraintException ;
import android.database.sqlite.SQLiteConstraintException ;
import com.todoroo.andlib.data.AbstractModel ;
import com.todoroo.andlib.data.AbstractModel ;
import com.todoroo.andlib.data.DatabaseDao ;
import com.todoroo.andlib.data.Property ;
import com.todoroo.andlib.data.Property ;
import com.todoroo.andlib.data.TodorooCursor ;
import com.todoroo.andlib.data.TodorooCursor ;
import com.todoroo.andlib.sql.Criterion ;
import com.todoroo.andlib.sql.Criterion ;
@ -28,6 +29,9 @@ import org.tasks.R;
import org.tasks.notifications.NotificationManager ;
import org.tasks.notifications.NotificationManager ;
import org.tasks.preferences.Preferences ;
import org.tasks.preferences.Preferences ;
import java.io.ByteArrayOutputStream ;
import java.util.List ;
import javax.inject.Inject ;
import javax.inject.Inject ;
import javax.inject.Singleton ;
import javax.inject.Singleton ;
@ -38,10 +42,12 @@ import javax.inject.Singleton;
*
*
* /
* /
@Singleton
@Singleton
public class TaskDao extends RemoteModelDao < Task > {
public class TaskDao {
private static final Logger log = LoggerFactory . getLogger ( TaskDao . class ) ;
private static final Logger log = LoggerFactory . getLogger ( TaskDao . class ) ;
private final RemoteModelDao < Task > dao ;
private final MetadataDao metadataDao ;
private final MetadataDao metadataDao ;
private final Broadcaster broadcaster ;
private final Broadcaster broadcaster ;
private final ReminderService reminderService ;
private final ReminderService reminderService ;
@ -52,8 +58,7 @@ public class TaskDao extends RemoteModelDao<Task> {
public TaskDao ( Database database , MetadataDao metadataDao , Broadcaster broadcaster ,
public TaskDao ( Database database , MetadataDao metadataDao , Broadcaster broadcaster ,
ReminderService reminderService , NotificationManager notificationManager ,
ReminderService reminderService , NotificationManager notificationManager ,
Preferences preferences ) {
Preferences preferences ) {
super ( Task . class ) ;
dao = new RemoteModelDao < > ( database , Task . class ) ;
setDatabase ( database ) ;
this . preferences = preferences ;
this . preferences = preferences ;
this . metadataDao = metadataDao ;
this . metadataDao = metadataDao ;
this . broadcaster = broadcaster ;
this . broadcaster = broadcaster ;
@ -61,6 +66,42 @@ public class TaskDao extends RemoteModelDao<Task> {
this . notificationManager = notificationManager ;
this . notificationManager = notificationManager ;
}
}
public TodorooCursor < Task > query ( Query query ) {
return dao . query ( query ) ;
}
public Task fetch ( long id , Property < ? > . . . properties ) {
return dao . fetch ( id , properties ) ;
}
public int count ( Query query ) {
return dao . count ( query ) ;
}
public TodorooCursor < Task > rawQuery ( String selection , String [ ] selectionArgs , Property . LongProperty id ) {
return dao . rawQuery ( selection , selectionArgs , id ) ;
}
public int update ( Criterion where , Task template ) {
return dao . update ( where , template ) ;
}
public int deleteWhere ( Criterion criterion ) {
return dao . deleteWhere ( criterion ) ;
}
public void addListener ( DatabaseDao . ModelUpdateListener < Task > modelUpdateListener ) {
dao . addListener ( modelUpdateListener ) ;
}
public List < Task > toList ( Query query ) {
return dao . toList ( query ) ;
}
public void persist ( Task task ) {
dao . persist ( task ) ;
}
// --- SQL clause generators
// --- SQL clause generators
/ * *
/ * *
@ -103,7 +144,7 @@ public class TaskDao extends RemoteModelDao<Task> {
}
}
public String uuidFromLocalId ( long localId ) {
public String uuidFromLocalId ( long localId ) {
TodorooCursor < Task > cursor = query( Query . select ( RemoteModel . UUID_PROPERTY ) . where ( AbstractModel . ID_PROPERTY . eq ( localId ) ) ) ;
TodorooCursor < Task > cursor = dao. query( Query . select ( RemoteModel . UUID_PROPERTY ) . where ( AbstractModel . ID_PROPERTY . eq ( localId ) ) ) ;
try {
try {
if ( cursor . getCount ( ) = = 0 ) {
if ( cursor . getCount ( ) = = 0 ) {
return RemoteModel . NO_UUID ;
return RemoteModel . NO_UUID ;
@ -122,9 +163,8 @@ public class TaskDao extends RemoteModelDao<Task> {
*
*
* @return true if delete was successful
* @return true if delete was successful
* /
* /
@Override
public boolean delete ( long id ) {
public boolean delete ( long id ) {
boolean result = super . delete ( id ) ;
boolean result = dao . delete ( id ) ;
if ( ! result ) {
if ( ! result ) {
return false ;
return false ;
}
}
@ -158,7 +198,7 @@ public class TaskDao extends RemoteModelDao<Task> {
}
}
public void handleSQLiteConstraintException ( Task task ) {
public void handleSQLiteConstraintException ( Task task ) {
TodorooCursor < Task > cursor = query( Query . select ( Task . ID ) . where (
TodorooCursor < Task > cursor = dao. query( Query . select ( Task . ID ) . where (
Task . UUID . eq ( task . getUUID ( ) ) ) ) ;
Task . UUID . eq ( task . getUUID ( ) ) ) ) ;
if ( cursor . getCount ( ) > 0 ) {
if ( cursor . getCount ( ) > 0 ) {
cursor . moveToFirst ( ) ;
cursor . moveToFirst ( ) ;
@ -167,7 +207,6 @@ public class TaskDao extends RemoteModelDao<Task> {
}
}
}
}
@Override
public boolean createNew ( Task item ) {
public boolean createNew ( Task item ) {
if ( ! item . containsValue ( Task . CREATION_DATE ) ) {
if ( ! item . containsValue ( Task . CREATION_DATE ) ) {
item . setCreationDate ( DateUtilities . now ( ) ) ;
item . setCreationDate ( DateUtilities . now ( ) ) ;
@ -189,7 +228,7 @@ public class TaskDao extends RemoteModelDao<Task> {
setDefaultReminders ( preferences , item ) ;
setDefaultReminders ( preferences , item ) ;
ContentValues values = item . getSetValues ( ) ;
ContentValues values = item . getSetValues ( ) ;
boolean result = super . createNew ( item ) ;
boolean result = dao . createNew ( item ) ;
if ( result ) {
if ( result ) {
afterSave ( item , values ) ;
afterSave ( item , values ) ;
}
}
@ -222,7 +261,6 @@ public class TaskDao extends RemoteModelDao<Task> {
}
}
}
}
@Override
public boolean saveExisting ( Task item ) {
public boolean saveExisting ( Task item ) {
ContentValues values = item . getSetValues ( ) ;
ContentValues values = item . getSetValues ( ) ;
if ( values = = null | | values . size ( ) = = 0 ) {
if ( values = = null | | values . size ( ) = = 0 ) {
@ -233,7 +271,7 @@ public class TaskDao extends RemoteModelDao<Task> {
item . setModificationDate ( DateUtilities . now ( ) ) ;
item . setModificationDate ( DateUtilities . now ( ) ) ;
}
}
}
}
boolean result = super . saveExisting ( item ) ;
boolean result = dao . saveExisting ( item ) ;
if ( result ) {
if ( result ) {
afterSave ( item , values ) ;
afterSave ( item , values ) ;
}
}
@ -259,7 +297,7 @@ public class TaskDao extends RemoteModelDao<Task> {
} catch ( SQLiteConstraintException e ) {
} catch ( SQLiteConstraintException e ) {
log . error ( e . getMessage ( ) , e ) ;
log . error ( e . getMessage ( ) , e ) ;
String uuid = item . getUUID ( ) ;
String uuid = item . getUUID ( ) ;
TodorooCursor < Task > tasksWithUUID = query( Query . select (
TodorooCursor < Task > tasksWithUUID = dao. query( Query . select (
SQL_CONSTRAINT_MERGE_PROPERTIES ) . where (
SQL_CONSTRAINT_MERGE_PROPERTIES ) . where (
Task . UUID . eq ( uuid ) ) ) ;
Task . UUID . eq ( uuid ) ) ) ;
try {
try {
@ -270,7 +308,7 @@ public class TaskDao extends RemoteModelDao<Task> {
continue ;
continue ;
}
}
compareAndMergeAfterConflict ( curr , fetch( item . getId ( ) ,
compareAndMergeAfterConflict ( curr , dao. fetch( item . getId ( ) ,
tasksWithUUID . getProperties ( ) ) ) ;
tasksWithUUID . getProperties ( ) ) ) ;
return ;
return ;
}
}