Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 505e9640 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

continue to implement operationDoneObject, create an abstract DAO class to be...

continue to implement operationDoneObject, create an abstract DAO class to be extended by concrete DAO class. work hard on local file change détection, and find an unwanted behaviour in method to find change on remote server
parent eb391238
Loading
Loading
Loading
Loading
Loading

android-nc-lib @ b0ff4872

Original line number Original line Diff line number Diff line
Subproject commit a86f6d38600b74dd697bcd8ad410a93a7c4637f1
Subproject commit b0ff4872a08bd5eab75708c8acf3518abe6fd374
+31 −0
Original line number Original line Diff line number Diff line
package io.eelo.drive.database;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public abstract class AbstractDAO {

    protected SQLiteDatabase mDB;
    protected final DbHelper mHelper;


    protected AbstractDAO(Context context){
        this.mHelper = new DbHelper(context);
    }
    /**
     * Open Database connexion
     * @param writeMod Connexion mod. If set to true, database is open in writable mod, else it is opened in read mod.
     * @throws SQLException SqlException
     */
    protected void open(Boolean writeMod) throws SQLException {
        mDB = (writeMod)? mHelper.getWritableDatabase() : mHelper.getReadableDatabase();
    }

    /**
     * Close Database connexion
     */
    protected void close() {
        mHelper.close();
    }
}
+3 −1
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@ import android.util.Log;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;


import io.eelo.drive.models.OperationDone;
import io.eelo.drive.models.SyncedFolder;
import io.eelo.drive.models.SyncedFolder;
import io.eelo.drive.models.SyncedFileState;
import io.eelo.drive.models.SyncedFileState;


@@ -29,7 +30,7 @@ import io.eelo.drive.models.SyncedFileState;


public final class DbHelper extends SQLiteOpenHelper {
public final class DbHelper extends SQLiteOpenHelper {
    final private static String TAG = DbHelper.class.getSimpleName(); //Tag for log
    final private static String TAG = DbHelper.class.getSimpleName(); //Tag for log
    private static final int DATABASE_VERSION = 19; //20/09/2018
    private static final int DATABASE_VERSION = 20; //20/09/2018
    public static final String DATABASE_NAME = "eelo_drive.db";
    public static final String DATABASE_NAME = "eelo_drive.db";


    /**
    /**
@@ -47,6 +48,7 @@ public final class DbHelper extends SQLiteOpenHelper {
    public void onCreate(SQLiteDatabase db) {
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SyncedFolderContract.SQL_CREATE_TABLE_SYNCEDFOLDER);
        db.execSQL(SyncedFolderContract.SQL_CREATE_TABLE_SYNCEDFOLDER);
        db.execSQL(SyncedFileStateContract.SQL_CREATE_TABLE_SYNCEDFILESTATE);
        db.execSQL(SyncedFileStateContract.SQL_CREATE_TABLE_SYNCEDFILESTATE);
        db.execSQL(OperationDoneContract.SQL_CREATE_TABLE_OPERATIONDONE);
    }
    }


    /**
    /**
+35 −0
Original line number Original line Diff line number Diff line
package io.eelo.drive.database;

import android.provider.BaseColumns;

public class OperationDoneContract implements BaseColumns {

    /** Table structure **/
    static final String TABLE_NAME ="operation_done";
    static final String TARGET_PATH ="target_path";
    static final String SOURCE_PATH ="source_path";
    static final String OPERATION_CODE = "operation_code";
    static final String RESULT_CODE = "result_code";
    static final String IS_SUCCESS = "is_success";
    static final String NEW_ETAG = "new_etag";


    static final String SQL_CREATE_TABLE_OPERATIONDONE=
            "CREATE TABLE "+TABLE_NAME+" ( "
                    +_ID+" INTEGER PRIMARY KEY, "
                    +TARGET_PATH+" TEXT, "
                    +SOURCE_PATH+" TEXT, "
                    +OPERATION_CODE+" INTEGER, "
                    +RESULT_CODE+" TEXT, "
                    +IS_SUCCESS+" INTEGER, "
                    + NEW_ETAG +" TEXT, "
                    +"CONSTRAINT synced_unicity_constraint UNIQUE ("
                    +TARGET_PATH+", "
                    +SOURCE_PATH
                    +"))";

    static final String SQL_DELETE_TABLE_OPERATIONDONE = " DROP TABLE IF EXISTS "
            + TABLE_NAME;


}
+140 −0
Original line number Original line Diff line number Diff line
package io.eelo.drive.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import io.eelo.drive.models.OperationDone;

import static io.eelo.drive.database.OperationDoneContract.IS_SUCCESS;
import static io.eelo.drive.database.OperationDoneContract.NEW_ETAG;
import static io.eelo.drive.database.OperationDoneContract.OPERATION_CODE;
import static io.eelo.drive.database.OperationDoneContract.RESULT_CODE;
import static io.eelo.drive.database.OperationDoneContract.SOURCE_PATH;
import static io.eelo.drive.database.OperationDoneContract.TABLE_NAME;
import static io.eelo.drive.database.OperationDoneContract.TARGET_PATH;

public class OperationDoneDAO extends AbstractDAO {
    final private static String TAG = OperationDoneDAO.class.getSimpleName();

    private final String[] allColumns = {
        OperationDoneContract._ID,
            TARGET_PATH,
            SOURCE_PATH,
            OPERATION_CODE,
            RESULT_CODE,
            IS_SUCCESS,
            NEW_ETAG
    };


    OperationDoneDAO(Context context){
        super(context);
    }


    /**
     * Remove all entry from DB
     * @return
     */
    public int clearTable(){
        return mDB.delete(TABLE_NAME, "1", null);
    }

    /**
     * Get list of OperationDone
     * @param selection  A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
     * @param args  You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
     * @return List of all OperationDone
     */
    List<OperationDone> getOperationDoneList(String selection, String[] args) {
        List<OperationDone> result = new ArrayList<OperationDone>();

        Cursor cursor = mDB.query(TABLE_NAME, allColumns, selection,args, null, null,
                null);
        cursor.moveToFirst();

        while ( !cursor.isAfterLast() ) {
            OperationDone operationDone = cursorToOperationDone(cursor);
            if(operationDone != null ){
                result.add(operationDone);
            }
            cursor.moveToNext();
        }
        cursor.close();
        return result;
    }


    /**
     * Insert new OperationDone
     * @param operationDone element to register
     * @return the row ID of the newly inserted row OR -1
     */
    long insert(OperationDone operationDone){
        return mDB.insertWithOnConflict(TABLE_NAME,
                null,
                toContentValues(operationDone),
                SQLiteDatabase.CONFLICT_IGNORE);
    }


    /**
     * Update a specific OperationDone
     * @param operationDone OperationDone to update
     * @return Number of row affected
     */
    public int update(OperationDone operationDone){
        int result = 0;
        try{
            result = mDB.update(TABLE_NAME,
                    toContentValues(operationDone),
                    OperationDoneContract._ID+" = "+ operationDone.getId(),
                    null);
        }catch(Exception e){
            Log.e(TAG, e.toString());
        }
        return result;
    }


    /**
     * Convert a operationDone into a ContentValues object
     * @param operationDone syncedFileState to convert
     * @return a ContentValues object
     */
    private ContentValues toContentValues(OperationDone operationDone){
        ContentValues values = new ContentValues();
        values.put( TARGET_PATH, operationDone.getTargetPath() );
        values.put( SOURCE_PATH, operationDone.getSourcePath() );
        values.put( OPERATION_CODE, operationDone.getOperationCode() );
        values.put( RESULT_CODE, operationDone.getResultCode() );
        values.put( IS_SUCCESS, operationDone.isSuccess() ? 1 : 0 );
        values.put( NEW_ETAG, operationDone.getNewEtag() );
        return values;
    }


    /**
     * Create SyncedFileState from cursor
     * @param cursor Cursor containing data to build SyncedFileState
     * @return SyncedFileState instance
     */
    private OperationDone cursorToOperationDone(Cursor cursor) {
        OperationDone result = new OperationDone(
                cursor.getString(1 ),//targetPath
                cursor.getString(2 ),//sourcePath
                cursor.getInt(3 ),//operationCode
                cursor.getString(4 ),//resultCode
                (cursor.getInt(5) == 1 ) //is success
        );
        result.setId( cursor.getInt(0)); //Id
        result.setNewEtag(cursor.getString(6)); //Etag
        return result;
    }
}
Loading