Example #1
0
        public void UpdateLocalStorageForOfflineUse(ConcurrentDictionary <string, string> localFileChanges, HtmlDocument vehicleWikiPage, string vehicleName, LocalWikiFileTypeEnum fileType, IVehicle vehicle = null)
        {
            try
            {
                if (fileType == LocalWikiFileTypeEnum.Undefined)
                {
                    throw new ArgumentException("The 'fileType' parameter for the 'UpdateLocalStorageForOfflineUse' is required but was not provided.");
                }

                // Build vars that will be used for the local file
                string fileName   = _stringHelper.RemoveInvalidCharacters(vehicleName.Replace(' ', '_').Replace('/', '-'));
                string folderPath = fileType == LocalWikiFileTypeEnum.Html ? ConfigurationManager.AppSettings["LocalWikiHtmlPath"] : ConfigurationManager.AppSettings["LocalWikiJsonPath"];
                string filePath   = $@"{folderPath}{fileName}.{fileType.ToString().ToLower()}";

                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }

                // Handle HTML files
                if (fileType == LocalWikiFileTypeEnum.Html)
                {
                    _htmlLogger.CreateHtmlFile(localFileChanges, vehicleWikiPage, vehicleName, fileName, filePath);
                }
                // Handle JSON files
                else if (fileType == LocalWikiFileTypeEnum.Json)
                {
                    _jsonLogger.CreateJsonFile(localFileChanges, vehicleName, vehicle, fileName, filePath);
                }
            }
            catch (Exception ex)
            {
                _consoleManager.WriteException(ex.Message);
            }
        }
Example #2
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);
                }
            }
        }
Example #3
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);
                }
            }
        }
Example #4
0
        public void UpdateLocalStorageForOfflineUse(ConcurrentDictionary<string, string> localFileChanges, HtmlDocument vehicleWikiPage, string vehicleName, LocalWikiFileTypeEnum fileType, IVehicle vehicle = null)
        {
            try
            {
                if (fileType == LocalWikiFileTypeEnum.Undefined)
                    throw new ArgumentException("The 'fileType' parameter for the 'UpdateLocalStorageForOfflineUse' is required but was not provided.");

                // Build vars that will be used for the local file
                string fileName = _stringHelper.RemoveInvalidCharacters(vehicleName.Replace(' ', '_').Replace('/', '-'));
                string folderPath = fileType == LocalWikiFileTypeEnum.HTML ? ConfigurationManager.AppSettings["LocalWikiHtmlPath"] : ConfigurationManager.AppSettings["LocalWikiJsonPath"];
                string filePath = $@"{folderPath}{fileName}.{fileType.ToString().ToLower()}";

                if (!Directory.Exists(folderPath))
                    Directory.CreateDirectory(folderPath);

                // Handle HTML files
                if (fileType == LocalWikiFileTypeEnum.HTML)
                {
                    _htmlLogger.CreateHtmlFile(localFileChanges, vehicleWikiPage, vehicleName, fileName, filePath);
                }
                // Handle JSON files
                else if (fileType == LocalWikiFileTypeEnum.JSON)
                {
                    _jsonLogger.CreateJsonFile(localFileChanges, vehicleName, vehicle, fileName, filePath);
                }
            }
            catch (Exception ex)
            {
                _consoleManager.WriteException(ex.Message);
            }
        }