Example #1
0
        private static bool resetSchema(FileInfo _SRC_CONFIG, WSSources <WSTableSource> _SOURCES)
        {
            WSLogRecord Log = new WSLogRecord("RESET SCHEMA");

            Log.Add($"RESET SCHEMA START");
            Log.Add($"Original schema:[{(_SRC_CONFIG != null ? ("Path:" + _SRC_CONFIG.FullName + ", Exists:" + _SRC_CONFIG.Exists) : "null")}]");
            Log.Add($"WSSources:[{((_SOURCES != null && _SOURCES.Any()) ? _SOURCES.Select(s => s.NAME).Aggregate((a, b) => a + "," + b) : "none")}]");
            bool done = false;

            try
            {
                if (_SOURCES != null && _SRC_CONFIG != null)
                {
                    if (!File.Exists(_SRC_CONFIG.FullName) || saveArchive(_SRC_CONFIG, ref Log))
                    {
                        if (File.Exists(_SRC_CONFIG.FullName))
                        {
                            _SRC_CONFIG.IsReadOnly = false;
                            Log.Add($"Original schema:[{_SRC_CONFIG.FullName}] set 'ReadOnly' attribute to:[{_SRC_CONFIG.IsReadOnly}]");
                        }
                        string orgPath  = _SRC_CONFIG.FullName;
                        string tempPath = orgPath + ".temp";
                        using (TextWriter writer = new StreamWriter(tempPath))
                        {
                            new XmlSerializer(typeof(WSSources <WSTableSource>)).Serialize(writer, _SOURCES);

                            Log.Add($"Temp schema:[{tempPath}] created");

                            File.Delete(orgPath);

                            Log.Add($"Original schema:[{orgPath}] deleted");

                            File.Copy(tempPath, orgPath);

                            Log.Add($"Temp schema:[{tempPath}] copied to [{orgPath}]");

                            _SRC_CONFIG = new FileInfo(orgPath);

                            Log.Add($"Original schema {(_SRC_CONFIG.Exists ? "recreated" : "FAILED recreate")}");
                        }

                        if (_SRC_CONFIG != null && File.Exists(_SRC_CONFIG.FullName))
                        {
                            File.Delete(tempPath);
                            Log.Add($"Temp schema {(!File.Exists(tempPath) ? "deleted" : "FAILED to delete")}");
                            done = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Add($"\n-----------------------Exception ------------------------");
                Log.Add($"Message:\n{e.Message}");
                Log.Add($"StackTrace:\n{e.StackTrace}");
                Log.Add($"-----------------------Exception END -------------------\n");
                WSStatus status = WSStatus.NONE.clone();
                LogError(typeof(WSServerMeta), e, ref status);
            }
            ReloadXMLSet[_SRC_CONFIG.Name] = (_SRC_CONFIG != null && File.Exists(_SRC_CONFIG.FullName)) ? _SRC_CONFIG.LastWriteTime.Ticks : ReloadXMLSet[_SRC_CONFIG.Name];
            Log.Add($"Schema 'LastModified' set to : [{ReloadXMLSet[_SRC_CONFIG.Name].ToString(WSConstants.DATE_FORMAT)}]");

            Log.Add($"RESET SCHEMA DONE");
            Log.Save();
            return(done);
        }