예제 #1
0
        public bool ReloadDataFromDatabase()
        {
            SQLiteHelper oSQLHelper = new SQLiteHelper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, null);

            if (!oSQLHelper.InitConnection())
            {
                return(false);
            }

#if __MonoCS__
            Mono.Data.Sqlite.SqliteDataReader SqlDataReader;
#else
            System.Data.SQLite.SQLiteDataReader SqlDataReader;
#endif

            OnLog("Reload from database start");

            OnLog("Folders");

            //Get Folders
            oSQLHelper.SetCommandText("SELECT Id_Folder, Name, RelativePath, RemoteID, CustomInfo FROM VFolder WHERE Fk_Repository = " + this._RepositoryId + " ORDER BY LENGTH(RelativePath)");
            SqlDataReader = oSQLHelper.ExecuteReader();
            while (SqlDataReader.Read())
            {
                //Is there a known parent folder ?
                String CurrentPath = SqlDataReader["RelativePath"].ToString();
                String ParentPath  = Tools.GetParentPath(CurrentPath);

                VirtualFolder VF;

                VF = new VirtualFolder(Guid.Parse(SqlDataReader["Id_Folder"].ToString()),
                                       CurrentPath,
                                       SqlDataReader["RemoteID"].ToString(),
                                       SqlDataReader["CustomInfo"].ToString());

                if (ParentPath.Equals(""))
                {
                    this.SubElementAdd(VF);
                    this.FlatElementAdd(VF);
                }
                else
                {
                    VirtualFolder ParentFolder = (VirtualFolder)this.FlatElementsGetElementBasedOnPath(ParentPath, VirtualElementType.Folder, false);
                    if (ParentFolder == null)
                    {
                        OnLog(String.Format("Parent folder of folder {0} is missing. Database is corrupted. Bypassing Element", ParentPath));
                        //TODO : Delete folder from database
                    }
                    else
                    {
                        ParentFolder.SubElementAdd(VF);
                        this.FlatElementAdd(VF);
                    }
                }
            }
            SqlDataReader.Close();

            OnLog("Files");

            oSQLHelper.SetCommandText("SELECT Id_File, Fk_ParentFolder, Name, RelativePath, RemoteID, Hash, CustomInfo FROM VFile WHERE Fk_Repository = " + this._RepositoryId);
            SqlDataReader = oSQLHelper.ExecuteReader();
            while (SqlDataReader.Read())
            {
                //Is there a known parent folder ?
                String CurrentPath = SqlDataReader["RelativePath"].ToString();
                //String ParentPath = Tools.GetParentPath(CurrentPath);
                Guid ParentFolderId = Guid.Parse(SqlDataReader["Fk_ParentFolder"].ToString());

                VirtualFile VF;
                VF = new VirtualFile(Guid.Parse(SqlDataReader["Id_File"].ToString()),
                                     CurrentPath,
                                     SqlDataReader["RemoteID"].ToString(),
                                     SqlDataReader["Hash"].ToString(),
                                     SqlDataReader["CustomInfo"].ToString());

                if (ParentFolderId == Guid.Empty)
                {
                    this.SubElementAdd(VF);
                    this.FlatElementAdd(VF);
                }
                else
                {
                    VirtualFolder ParentFolder = (VirtualFolder)this.FlatElementsGetElementBasedOnId(ParentFolderId, VirtualElementType.Folder, false);

                    if (ParentFolder == null)
                    {
                        OnLog(String.Format("Parent folder with id {0} of file {1} is missing. Database is corrupted. Bypassing Element", ParentFolderId, CurrentPath));
                        //TODO : Delete file from database
                    }
                    else
                    {
                        ParentFolder.SubElementAdd(VF);
                        this.FlatElementAdd(VF);
                    }
                }
            }

            SqlDataReader.Close();
            oSQLHelper.Dispose();

            OnLog("Reload from database end");
            OnLog(String.Format("Found {0} folder(s) and {1} file(s)", this.FlatElementsGetAllFolders().Count, this.FlatElementsGetAllFiles().Count));

            return(true);
        }