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

Commit dc66fccb authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

write method to transform detected changement into Operation. Create Operation...

write method to transform detected changement into Operation. Create Operation class into Models (not fully iplemented)
parent 1a2a9877
Loading
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
package io.eelo.drive.models;

import com.owncloud.android.lib.resources.files.model.ISynchronizableContent;

public class Operation{
    private ISynchronizableContent file;
    private String operationCode;

    public Operation(ISynchronizableContent file, String operationCode){
        this.file = file;
        this.operationCode = operationCode;
    }


    public ISynchronizableContent getFile(){
        return this.file;
    }

    public String getOperationCode(){
        return this.operationCode;
    }
}
+73 −80
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@
package io.eelo.drive.Test;

import android.support.annotation.NonNull;
import android.util.Pair;

import com.owncloud.android.lib.resources.files.model.ISynchronizableContent;
import com.owncloud.android.lib.resources.files.model.RemoteFile;
@@ -18,14 +17,13 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;

import io.eelo.drive.models.SyncedFolder;
import io.eelo.drive.models.Operation;


/**
@@ -40,16 +38,6 @@ public class ObserverServiceUnitTest {
    List<String> pathListToIgnore;
    List<SyncedFolder> initialFolder;

    @Before
    public void initForGetOtherPath(){
        initialFolder = new ArrayList<>();
        initialFolder.add(new SyncedFolder("Photo", "/sdcard/photos/", "/photos/", true));
        initialFolder.add(new SyncedFolder("Videos", "/sdcard/videos/", "/videos/", true));
        initialFolder.add(new SyncedFolder("Music", "/sdcard/Musics/", "/Musics/", true));
        initialFolder.add(new SyncedFolder("Settings", "/user/0/", "/settings/", true));

        Assert.assertTrue("Wrong number of initialFOlder", initialFolder.size() == 4);
    }

    @Before
    public void initForModifDetection(){
@@ -328,10 +316,23 @@ public class ObserverServiceUnitTest {
    }

/* --------------------------------Test getOtherPath----------------------------------------*/
    @Before
    public void initForGetOtherPath(){
        initialFolder = new ArrayList<>();
        initialFolder.add(new SyncedFolder("Photo", "/sdcard/photos/", "/Photos/", true));
        initialFolder.add(new SyncedFolder("Videos", "/sdcard/videos/", "/Videos/", true));
        initialFolder.add(new SyncedFolder("Music", "/sdcard/Musics/", "/Musics/", true));
        initialFolder.add(new SyncedFolder("Settings", "/user/0/", "/Settings/", true));

        Assert.assertTrue("Wrong number of initialFOlder", initialFolder.size() == 4);
    }


    @Test
    public void getOtherPathUnitTest(){
        initForGetOtherPath();


        System.out.println("TEST 1");
        System.out.println("Description: simple case");
        String path = "/Musics/metallica/blackAlbum/nothingElseMatters.ogg";
@@ -341,6 +342,7 @@ public class ObserverServiceUnitTest {
        Assert.assertEquals("Wrong result obtained", "/sdcard/Musics/metallica/blackAlbum/nothingElseMatters.ogg", otherPath);
        System.out.println(otherPath);


       //Test with a file without correspondance
        System.out.println("\nTEST 2");
        System.out.println("Description: No correspondance");
@@ -379,7 +381,6 @@ public class ObserverServiceUnitTest {
        System.out.println(otherPath);
    }


    /**
     * Could be ameliorate
     * @param file
@@ -401,7 +402,6 @@ public class ObserverServiceUnitTest {
                 path = sf.getAbsolutePath();
            }
        }
        System.out.println(path);

        for(SyncedFolder sf : initialFolder){
            String FolderPath;
@@ -414,10 +414,6 @@ public class ObserverServiceUnitTest {
                newPath = sf.getLocalFolder();
            }
            if(path.startsWith(FolderPath)){

                System.out.println("Folder path : "+FolderPath);
                System.out.println("NewPath: "+newPath);

                return newPath+path.substring( FolderPath.length(), path.length());
            }
        }
@@ -425,87 +421,84 @@ public class ObserverServiceUnitTest {
    }


    /* --------------------------------Test list of file to list of operation to do----------------------------------------*/




    /**
     * Create a sublist of operation to perform but first need to get diff with local change
     */
    @Test
    public void handleChangementFoundUnitTest(){
        initForModifDetection();
        findRemoteModification();
        showResult();
        System.out.println("\n");

        HashMap<String, Operation> result = changeToOperation();
        Assert.assertTrue("Wrong size", result.size() == 3);
    }

        HashMap<String, Pair<ISynchronizableContent, String>> operationToDo = new HashMap<>();

    /**
     * TODO:
     *
     *
     * @return HashMap<String, Operation>
     */
    public HashMap<String, Operation> changeToOperation(){
       // long chronoStart = System.nanoTime();
        HashMap<String, Operation> operationToDo = new HashMap<String, Operation>();
        //Handle change found on server
        for(RemoteFile r : newRemoteFileList){
            if(!r.getMimeType().equals("DIR")) //No need to handle "DIR" type because they will be automaticaly added when creating new File
                operationToDo.put(r.getRemotePath(), new Pair<ISynchronizableContent, String>(r, "Download"));
            if(!r.getMimeType().equals("DIR")) { //No need to handle "DIR" type because they will be automaticaly added when creating new File
                operationToDo.put(getOtherPath(r), new Operation(r, "Download")); //May be replace getOtherPath by r.getRemotePath
                //System.out.println(getOtherPath(r));
            }

        for(RemoteFile r : previousRemoteFileList){
            operationToDo.put(r.getRemotePath(), new Pair<ISynchronizableContent, String>(r, "LocalDelete") );
        }

        //Then
        //Loop over previous list of local file (to get deleted)
        //getPath and convert it into remotePath. Check if the list of operation to do contains
        //it.
        List<SynchronizableFile> newFileLocalList =new ArrayList<>(); //FIle to upload
        for(SynchronizableFile f : newFileLocalList){
            //getRemotePath
            String remotePath = "reflectPath"; //getOtherPath(f.getPath());
            //if(!operationToDo.containsKey(remotePath))
               // operationToDo.put(f, f.isDirectory() ? "MKCol" : "Upload");
        for(RemoteFile r : previousRemoteFileList){
            operationToDo.put(getOtherPath(r), new Operation( r, "LocalDelete")); //May be replace getOtherPath by r.getRemotePath
            //System.out.println(getOtherPath(r));
        }

        List<SynchronizableFile> oldFileLocalList = new ArrayList<>();
        for(SynchronizableFile f : oldFileLocalList){
            //getRemotePath
            String remotePath = "reflectPath"; //getOtherPath(f.getPath());
           // operationToDo.get()
            ISynchronizableContent remoteFile;// = operationToDo.keySet();/* need to find how to play with path here*/
        //Handle change found on device

            //if(remoteFile == null);
              //      operationToDo.put(f, "Remove");
        List<SynchronizableFile> newLocalFileList = new ArrayList<>();
        for(SynchronizableFile sf : newLocalFileList){
            try {
                String localPath = sf.getCanonicalPath();
                Operation operation = operationToDo.get(localPath);
                if (operation != null && operation.getFile().getLastModified() <= sf.getLastModified()) {
                    operationToDo.remove(localPath);
                    operationToDo.put(getOtherPath(sf), new Operation(sf, sf.isDirectory()? "MKCol":"Upload"));
                }
            }catch(Exception e){
                //Todo
            }
        }

        List<SynchronizableFile> oldLocalFileList = new ArrayList<>();
        for(SynchronizableFile sf : oldLocalFileList){
            try {
                String localPath = sf.getCanonicalPath();
                Operation operation = operationToDo.get(localPath);
                if (operation != null && operation.getFile().getLastModified() <= sf.getLastModified()) {
                    operationToDo.remove(localPath);
                    operationToDo.put(getOtherPath(sf), new Operation(sf, "removeRemote"));
                }
            }catch(Exception e){
                //Todo
            }
        }

        //TO be able to match remote remote and local file I need a way a method that turn a local path into a remote path and reverse
        //What could be the rule that will match every case?
        //That should also allow to get the path for download or upload new file!
        //One way imagined is to use preference as it is just key/value
        //or store it in a file, or db...need to be seen.

        //Need also to ignore change cause by previous sync, but finaly...
        //How to know by example if a file has been upload last time and one more time from another device ?
        //If you remove it, we can loose extra information
        //1. Loas previous list of change
        //2. remove change in new list of change that have been cause  by last modified

        //3. Determine which change need to be done when a file has changed localy and remotly

        //4. add the operation

        //System.out.println("Chrono: "+(System.nanoTime()-chronoStart) );
        for(String opKey : operationToDo.keySet()){
            Operation op= operationToDo.get(opKey);
            System.out.println(opKey+"=> "+op.getOperationCode());
        }

        //modified timestamp in remote file is always GMT timestamp as for file.
        File file = new File("D:/body.txt");
        if(file.exists()) System.out.println(file.lastModified()); //Doc says it is a GMT
        return operationToDo;
    }


        //Remote file date is a "Date"
        RemoteFile remoteFile = new RemoteFile("/Documents/body.txt");
        Date d =new Date("04 Jan 2019 17:07:56 GMT");
        remoteFile.setModifiedTimestamp(d.getTime());

        System.out.println(d.getTime());

        if(file.lastModified() >= remoteFile.getModifiedTimestamp()){
            //Perform operation for local change
            System.out.println("Perform a local change");
        }else{
            //Perform operation for remote change
            System.out.println("Perform a remote change");
        }

}
}