private void VerifyStore()
        {
            bool result = true;

            result = result && !Directory.Exists(BackupDataFolder("Content"));
            result = result && !Directory.Exists(BackupDataFolder("Views"));
            result = result && !Directory.Exists(BackupDataFolder("Context"));
            if (!result)
            {
                StoreProgress.Set(Name, ErrorCode.BACKUPFOLDER_ALREADY_EXISTS);
                throw new BackupNotPossibleException(StoreProgress.Get(Name).Message());
            }

            result = result && Directory.Exists(ContentFolder);
            if (!result)
            {
                StoreProgress.Set(Name, ErrorCode.CONTENTFOLDER_NOT_EXISTS);
                throw new BackupNotPossibleException(StoreProgress.Get(Name).Message());
            }

            result = result && Directory.Exists(ViewsFolder);
            if (!result)
            {
                StoreProgress.Set(Name, ErrorCode.VIEWSFOLDER_NOT_EXISTS);
                throw new BackupNotPossibleException(StoreProgress.Get(Name).Message());
            }
        }
        private static string GetErrorMessage(string name, ErrorCode?ec, params KeyValuePair <string, object>[] infos)
        {
            string message = ec == null?StoreProgress.Get(name).Message() : ec.Value.Message();

            var info = string.Join(", ", infos.Select(s => s.Key + "=" + s.Value.ToString()));

            return("[" + name + "] " + message + (string.IsNullOrEmpty(info) ? string.Empty : " -> ") + info);
        }
        private void SetDatas()
        {
            Data data = new Data();

            data.SiteSetting = Unit.SiteSettingRepository.Find(s => s.Id == SiteSettingId);
            if (data.SiteSetting == null)
            {
                StoreProgress.Set(Name, ErrorCode.SITE_SETTING_ID_NOT_FOUND);
                throw new BackupNotPossibleException();
            }
            data.SiteManagers   = Unit.SiteManagerRepository.Get().Where(sm => sm.SiteSettingId == SiteSettingId).ToList();
            data.WidgetManagers = Unit.WidgetManagerRepository.Get().Where(wm => wm.SitesettingId == SiteSettingId).ToList();
            this.Data           = data;
        }
        public static void Delete(string name)
        {
            BackupController bc = new BackupController(null, name, Guid.NewGuid());

            try
            {
                Directory.Delete(bc.BackupFolder, recursive: true);
            }
            catch (Exception)
            {
                ErrorCode ec = ErrorCode.COULD_NOT_DELETE_FOLDER;
                throw new BackupNotPossibleException(name, ec, "Folder".PairedWith(bc.BackupFolder));
            }
            StoreProgress.Done(name);
        }
        public static ErrorCode Store(UnitOfWork unit, string name, Guid siteSettingId)
        {
            unit = _Globals.Instance.ChangeSiteSettingId(siteSettingId, unit);
            if (StoreProgress.Get(name) != ErrorCode.NULL)
            {
                return(ErrorCode.NAME_ALREADY_EXISTS);
            }

            StoreProgress.Set(name, ErrorCode.RUNNING);
            Task.Run(() =>
            {
                try
                {
                    lock (PADLOCK_ACTION)
                    {
                        if (string.IsNullOrWhiteSpace(name) || name.Where(c => !char.IsLetterOrDigit(c)).Count() > 0)
                        {
                            StoreProgress.Set(name, ErrorCode.NAME_INVALID);
                            throw new BackupNotPossibleException(name, "Name".PairedWith(name));
                        }

                        BackupController bc = new BackupController(unit, name, siteSettingId);
                        bc.SetDatas();

                        bc.VerifyStore();

                        Directory.CreateDirectory(bc.BackupDataFolder("Context"));

                        try
                        {
                            XmlDocument xmlDocument  = new XmlDocument();
                            XmlSerializer serializer = new XmlSerializer(bc.Data.GetType());
                            using (MemoryStream stream = new MemoryStream())
                            {
                                serializer.Serialize(stream, bc.Data);
                                stream.Position = 0;
                                xmlDocument.Load(stream);
                                xmlDocument.Save(bc.BackupDataFolder("Context") + @"appdata.xml");
                                stream.Close();
                            }
                        }
                        catch (Exception e)
                        {
                            StoreProgress.Set(name, ErrorCode.COULD_NOT_SAVE_TO_XML);
                            throw new BackupNotPossibleException(e, name);
                        }

                        try
                        {
                            bc.CopyDirectoryContentWithIncludedFiles(bc.ContentFolder, bc.BackupDataFolder("Content"));
                        }
                        catch (Exception e)
                        {
                            StoreProgress.Set(name, ErrorCode.COULD_NOT_COPY_FOLDER);
                            throw new BackupNotPossibleException(e, name, "From".PairedWith(bc.ContentFolder), "To".PairedWith(bc.BackupDataFolder("Content")));
                        }
                        try
                        {
                            bc.CopyDirectoryContentWithIncludedFiles(bc.ViewsFolder, bc.BackupDataFolder("Views"));
                        }
                        catch (Exception e)
                        {
                            StoreProgress.Set(name, ErrorCode.COULD_NOT_COPY_FOLDER);
                            throw new BackupNotPossibleException(e, name, "From".PairedWith(bc.ViewsFolder), "To".PairedWith(bc.BackupDataFolder("Views")));
                        }

                        ZipFile.CreateFromDirectory(bc.BackupDataFolder(), bc.ZipFolder(), CompressionLevel.Fastest, true);
                        StoreProgress.Done(name);
                    }
                }
                catch (Exception e)
                {
                    StoreProgress.SetFailureIfRunning(name);
                    throw new BackupNotPossibleException(e, name);
                }
            });
            return(StoreProgress.Get(name));
        }