public static bool DownloadContractFile(int contractId, ContractIndex docIndex, int?fileId, Grid layoutRoot)
        {
            int docid = (int)docIndex;

            if (docid == -1)
            {
                return(false);
            }
            layoutRoot.Children.Add(busy);
            Task <bool> .Factory.StartNew(delegate
            {
                FileDataObject t = DataManagement.RetrieveContractFile(contractId, docid, fileId);
                if (t == null)
                {
                    return(false);
                }
                OpenFileHandler.OpenFileFromByte(t.FileContent, t.FileName);
                t = null;
                GC.Collect();
                return(true);
            }).ContinueWith(Pre =>
            {
                layoutRoot.Children.Remove(busy);
                if (!Pre.Result)
                {
                    ErrorHandler.ShowErrorMessage(ErrorHandler.ErrorMessages["NoFile"]);
                }
                if (TransactionFinished != null)
                {
                    TransactionFinished();
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());

            return(true);
        }
        public static bool DeleteContractFile(int contractId, ContractIndex docIndex, int fileId, Grid layoutRoot)
        {
            if (!UserData.CurrentAccessRight.ContractDelete)
            {
                ErrorHandler.ShowErrorMessage("امکان انجام این عملیات وجود ندارد");
                return(false);
            }
            int docid = (int)(docIndex);

            if (docid == -1)
            {
                return(false);
            }
            if (ErrorHandler.PromptUserForPermision(ErrorHandler.ErrorMessages["Prompt"]) == MessageBoxResult.No)
            {
                return(false);
            }
            layoutRoot.Children.Add(busy);
            Task.Factory.StartNew(delegate
            {
                DataManagement.DeleteContractFile(fileId);
            }).ContinueWith(delegate
            {
                layoutRoot.Children.Remove(busy);
                if (TransactionFinished != null)
                {
                    TransactionFinished();
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
            return(true);
        }
        public static bool UploadContractFile(int contractId, ContractIndex docIndex, string version, Grid layoutRoot)
        {
            ContractFile f = new ContractFile();

            f.Version      = version;
            f.AttachedDate = DateTime.Now;
            f.FileGuid     = Guid.NewGuid();
            string fileLocation = OpenFileHandler.OpenFileToUpload();

            if (fileLocation == null)
            {
                return(false);
            }
            layoutRoot.Children.Add(busy);
            Task <bool> .Factory.StartNew(delegate
            {
                f.FileContent = OpenFileHandler.GetFileFromLocation(fileLocation);
                f.Name        = System.IO.Path.GetFileName(fileLocation);

                if (DataManagement.AddContractFile(contractId, docIndex, f) != -1)
                {
                    f = null;
                    GC.Collect();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }).ContinueWith(prev =>
            {
                layoutRoot.Children.Remove(busy);
                bool result = prev.Result;
                if (result)
                {
                    ErrorHandler.NotifyUser("فایل با موفقیت ثبت شد.");
                    if (TransactionFinished != null)
                    {
                        TransactionFinished();
                    }
                }
                else
                {
                    ErrorHandler.ShowErrorMessage("ثبت فایل امکان پذیر نبود لطفا دوباره سعی کنید.");
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());

            return(true);
        }