Beispiel #1
0
 void Config()
 {
     options        = optionsManager.GetOptions <ETLOptions>() as ETLOptions;
     movingOptions  = optionsManager.GetOptions <MovingOptions>() as MovingOptions;
     archiveOptions = optionsManager.GetOptions <ArchiveOptions>() as ArchiveOptions;
     watcherOptions = optionsManager.GetOptions <WatcherOptions>() as WatcherOptions;
     logger         = new Logger(movingOptions.TargetDirectory);
 }
Beispiel #2
0
        public ETLJsonOptions(string json) : base()
        {
            ETLOptions options = Converter.Converter.DeserializeJson <ETLOptions>(json);

            ArchiveOptions = options.ArchiveOptions;
            MovingOptions  = options.MovingOptions;
            WatcherOptions = options.WatcherOptions;
            Report         = Validator.Validate(this);
        }
Beispiel #3
0
        public ETLXmlOptions(string xml) : base()
        {
            ETLOptions options = Converter.Converter.DeserializeXML <ETLOptions>(xml);

            ArchiveOptions = options.ArchiveOptions;
            MovingOptions  = options.MovingOptions;
            WatcherOptions = options.WatcherOptions;
            Report         = Validator.Validate(this);
        }
Beispiel #4
0
        Options SeekForOption <T>(ETLOptions options)
        {
            if (typeof(T) == typeof(ETLOptions))
            {
                return(options);
            }

            string name = typeof(T).Name;

            try
            {
                return(options.GetType().GetProperty(name).GetValue(options, null) as Options);
            }
            catch
            {
                throw new NotImplementedException();
            }
        }
Beispiel #5
0
        public static string Validate(ETLOptions options)
        {
            string report = "";

            #region Moving Validation
            MovingOptions moving = options.MovingOptions;
            if (!CreateDirectoryIfNotExist(moving.SourceDirectory))
            {
                report += "Cannot open source directory, using default. ";
                moving.SourceDirectory = @"C:\Users\Public\Desktop\SourceFolder";
                CreateDirectoryIfNotExist(moving.SourceDirectory);
            }
            if (!CreateDirectoryIfNotExist(moving.TargetDirectory))
            {
                report += "Cannot open target directory, using default. ";
                moving.TargetDirectory = @"C:\Users\Public\Desktop\TargetFolder";
                CreateDirectoryIfNotExist(moving.TargetDirectory);
            }
            #endregion
            #region Archive Validation
            ArchiveOptions archive = options.ArchiveOptions;
            if ((int)archive.CompressionLevel < 0 || (int)archive.CompressionLevel > 2)
            {
                report += "Compression level value can't be below 0 or above 2, using default value. ";
                archive.CompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
            }
            #endregion
            #region Watcher Validation
            WatcherOptions watcher = options.WatcherOptions;
            if (watcher.Filter != "*.txt")
            {
                report        += "Filter can be only \"*.txt\", using default value. ";
                watcher.Filter = "*.txt";
            }
            #endregion
            return(report);
        }
Beispiel #6
0
        public OptionsManager(string path)
        {
            DefaultOptions = new ETLOptions();
            string options;

            //Пытаемся загрузить файл config.xml
            try
            {
                using (StreamReader sr = new StreamReader($@"{path}\config.xml"))
                {
                    options = sr.ReadToEnd();
                }
                Xml           = new ETLXmlOptions(options);
                xmlConfigured = true;
                Report        = Xml.Report;
                Report       += "Xml options loaded successfully. ";
            }
            catch
            {
                xmlConfigured = false;
            }
            //Пытаемся загрузить файл appsettings.json
            try
            {
                using (StreamReader sr = new StreamReader($@"{path}\appsettings.json"))
                {
                    options = sr.ReadToEnd();
                }
                Json           = new ETLJsonOptions(options);
                jsonConfigured = true;
                Report         = Json.Report;
                Report        += "Json options loaded successfully. ";
            }
            catch
            {
                jsonConfigured = false;
            }

            if (!jsonConfigured && !xmlConfigured)
            {
                Report += "Failed to load both of json and xml. Using default options, creating appsettings.json and config.xml";
                if (!File.Exists($@"{path}\appsettings.json"))
                {
                    string json = Converter.Converter.SerializeJson(DefaultOptions);
                    Validator.CreateDirectoryIfNotExist(path);
                    using (StreamWriter sw = new StreamWriter($@"{path}\appsettings.json"))
                    {
                        sw.Write(json);
                    }
                }
                if (!File.Exists($@"{path}\config.xml"))
                {
                    string xml = Converter.Converter.SerializeXML(DefaultOptions);
                    Validator.CreateDirectoryIfNotExist(path);
                    using (StreamWriter sw = new StreamWriter($@"{path}\config.xml"))
                    {
                        sw.Write(xml);
                    }
                }
            }
        }