Exemplo n.º 1
0
        public void CreateHtmlFile(ConcurrentDictionary <string, string> localFileChanges, HtmlDocument vehicleWikiPage, string vehicleName, string fileName, string filePath)
        {
            LocalWikiFileTypeEnum fileType = LocalWikiFileTypeEnum.Html;
            string fileExtension           = fileType.ToString();

            if (!File.Exists(filePath))
            {
                // Add new item
                vehicleWikiPage.Save($"{filePath}", Encoding.UTF8);
                _filePerVehicleLogger.RecordAddFileToLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
            }
            else
            {
                string existingFileText = File.ReadAllText(filePath);

                //Create a fake document so we can use helper methods to traverse through the existing file as an HTML document
                HtmlDocument htmlDoc      = new HtmlDocument();
                HtmlNode     existingHtml = HtmlNode.CreateNode(existingFileText);
                htmlDoc.DocumentNode.AppendChild(existingHtml);

                // Get out the last modified times for comparison
                var newLastModSection = vehicleWikiPage.DocumentNode.Descendants().SingleOrDefault(x => x.Id == ConfigurationManager.AppSettings["LastModifiedSectionId"]);
                var oldLastModSection = existingHtml.OwnerDocument.DocumentNode.Descendants().SingleOrDefault(x => x.Id == ConfigurationManager.AppSettings["LastModifiedSectionId"]);

                // If both files have a last modified time
                if (newLastModSection != null && oldLastModSection != null)
                {
                    // Update the existing one if the times are different
                    if (!_filePerVehicleLogger.AreLastModifiedTimesTheSame(oldLastModSection.InnerHtml, newLastModSection.InnerHtml))
                    {
                        // Update existing item
                        vehicleWikiPage.Save($"{filePath}", Encoding.UTF8);
                        _filePerVehicleLogger.RecordUpdateFileInLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
                    }
                }
                // Add the item if the existing one has no last modified time
                else if (oldLastModSection == null)
                {
                    // Update existing item
                    vehicleWikiPage.Save($"{filePath}", Encoding.UTF8);
                    _filePerVehicleLogger.RecordUpdateFileInLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
                }
                else
                {
                    string noLastModifiedSectionExceptionMessage = $"Unable to find the '{ConfigurationManager.AppSettings["LastModifiedSectionId"]}' section, information comparision failed.";

                    _consoleManager.WriteException(noLastModifiedSectionExceptionMessage);
                    throw new InvalidOperationException(noLastModifiedSectionExceptionMessage);
                }
            }
        }
Exemplo n.º 2
0
        public void CreateJsonFile(ConcurrentDictionary <string, string> localFileChanges, string vehicleName, IVehicle vehicle, string fileName, string filePath)
        {
            LocalWikiFileTypeEnum fileType = LocalWikiFileTypeEnum.Json;
            string fileExtension           = fileType.ToString();

            GroundVehicle groundVehicle = (GroundVehicle)vehicle;
            string        vehicleJson   = Newtonsoft.Json.JsonConvert.SerializeObject(groundVehicle, Newtonsoft.Json.Formatting.Indented);

            if (!File.Exists(filePath))
            {
                // Add new item
                File.WriteAllText(filePath, vehicleJson);
                _filePerVehicleLogger.RecordAddFileToLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
            }
            else
            {
                string        existingFileText = File.ReadAllText(filePath);
                GroundVehicle existingVehicle  = Newtonsoft.Json.JsonConvert.DeserializeObject <GroundVehicle>(existingFileText);

                // Get out the last modified times for comparison
                string newLastModSection = groundVehicle.LastModified;
                string oldLastModSection = existingVehicle?.LastModified;

                // If both files have a last modified time
                if (newLastModSection != null && oldLastModSection != null)
                {
                    if (!_filePerVehicleLogger.AreLastModifiedTimesTheSame(oldLastModSection, newLastModSection))
                    {
                        // Update existing
                        File.WriteAllText(filePath, vehicleJson);
                        _filePerVehicleLogger.RecordUpdateFileInLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
                    }
                }
                // Add the item if the existing one has no last modified time
                else if (oldLastModSection == null)
                {
                    // Update existing item
                    File.WriteAllText(filePath, vehicleJson);
                    _filePerVehicleLogger.RecordUpdateFileInLocalWiki(localFileChanges, vehicleName, fileName, fileExtension);
                }
                else
                {
                    string noLastModifiedSectionExceptionMessage = $"Unable to find the '{ConfigurationManager.AppSettings["LastModifiedSectionId"]}' section, information comparision failed.";

                    _consoleManager.WriteException(noLastModifiedSectionExceptionMessage);
                    throw new InvalidOperationException(noLastModifiedSectionExceptionMessage);
                }
            }
        }