/// <summary>
 /// Read contents of the customer csv file
 /// </summary>
 /// <param name="folder">Location of the customer csv file</param>
 /// <param name="fileName">Name of the customer csv file</param>
 /// <returns>List of customers or null if file does not exist</returns>
 public List <string> GetCustomers(string sourceFile)
 {
     if (fileHandler.FileExists(sourceFile))
     {
         return(fileHandler.ReadAllLines(sourceFile).ToList());
     }
     return(null);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Open datafile - returns true if new
        /// </summary>
        public bool Initialize()
        {
            var exists = _fileHandler.FileExists(_filename);

            if (exists)
            {
                this.TryRecovery();
            }

            return(!exists);
        }
Ejemplo n.º 3
0
        public bool Create()
        {
            if (!_fileHandler.FileExists(_configHandler.GoogleBookmarks))
            {
                throw new FileNotFoundException($"Could not find file: {_configHandler.GoogleBookmarks}");
            }
            string document     = _fileHandler.ReadAllText(_configHandler.GoogleBookmarks);
            var    parser       = new HtmlParser();
            var    htmlDocument = parser.Parse(document);

            JArray jsonArray = new JArray();

            foreach (IHtmlAnchorElement element in htmlDocument.QuerySelectorAll("dt > a")
                     .Where(x => ((IHtmlAnchorElement)x)
                            .Href.Contains("http://")))
            {
                jsonArray.Add(new JObject(
                                  new JProperty("text", element.Text),
                                  new JProperty("href", element.Href)
                                  ));
            }
            _fileHandler.WriteAllText(_configHandler.JsonFileLocation, jsonArray.ToString());

            return(true);
        }
Ejemplo n.º 4
0
        public bool CreateTestFile(TestFile testFile)
        {
            testFile.Filename = _fileHandler.GetFilenameWithExtension(testFile.Filename);

            string fullPath   = _fileHandler.CreateFileFullPath(testFile.Filename);
            bool   fileExists = _fileHandler.FileExists(fullPath);

            if (fileExists)
            {
                throw new IOException("File already exists");
            }

            return(SaveTestFile(testFile, fullPath));
        }
Ejemplo n.º 5
0
        public bool CreateTestFile(TestFile testFile)
        {
            testFile.Filename = _fileHandler.GetFilenameWithExtension(testFile.Filename);

            string filePath   = _fileHandler.CreateFileFullPath(testFile.Filename);
            bool   fileExists = _fileHandler.FileExists(filePath);

            if (fileExists)
            {
                throw new IOException("File already exists");
            }

            string contents = _testFileWriter.Write(testFile);

            return(_fileHandler.WriteAllText(filePath, contents));
        }
Ejemplo n.º 6
0
        private static void SynchronizeFile(Sync sync, IFileHandler contentFileHandler, IFileHandler outputFileHandler)
        {
            switch (sync.SyncType)
            {
            case SyncType.SourceToTarget:
            {
                var fileDiff = GetFileDiff(sync, contentFileHandler, outputFileHandler);
                if (fileDiff == FileDiff.Equal)
                {
                    _logger.Debug(@"{SyncLogType}: [{TargetPath}] [{SyncType}]", SyncLogType.CopyToTarget.GetDescription(), sync.TargetPath, sync.SyncType);
                    _logger.Debug(@"{SyncLogType} : (files are equal!) [{SourcePath}] [{SyncType}]", SyncLogType.CopyFromSource.GetDescription(), sync.SourcePath, sync.TargetPath, sync.SyncType);
                }
                else
                {
                    if (fileDiff == FileDiff.DiffTargetMissing || fileDiff == FileDiff.DiffSourceNewer)
                    {
                        _logger.Information(@"{SyncLogType}: [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        _logger.Information(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                    }
                    else
                    {
                        _logger.Warning(@"{SyncLogType}: [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        _logger.Warning(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                    }

                    outputFileHandler.FileCopy(contentFileHandler, sync.SourcePath, sync.TargetPath);
                }

                break;
            }

            case SyncType.TargetToSource:
            {
                var fileDiff = GetFileDiff(sync, contentFileHandler, outputFileHandler);
                {
                    switch (fileDiff)
                    {
                    case FileDiff.Equal:
                    {
                        _logger.Debug(@"{SyncLogType}: [{SourcePath}] [{SyncType}]", SyncLogType.CopyToSource.GetDescription(), sync.SourcePath, sync.SyncType);
                        _logger.Debug(@"{SyncLogType}: (files are equal!) [{TargetPath}] [{SyncType}]", SyncLogType.CopyFromTarget.GetDescription(), sync.TargetPath, sync.SyncType);
                        break;
                    }

                    case FileDiff.DiffSourceMissing:
                    case FileDiff.DiffTargetNewer:
                    {
                        _logger.Information(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                        _logger.Information(@"{SyncLogType}: [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        contentFileHandler.FileCopyBack(sync.SourcePath, outputFileHandler, sync.TargetPath);
                        break;
                    }

                    case FileDiff.DiffTargetMissing:
                    {
                        _logger.Information(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        _logger.Information(@"{SyncLogType}: (invserse) [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                        outputFileHandler.FileCopy(contentFileHandler, sync.SourcePath, sync.TargetPath);
                        break;
                    }

                    case FileDiff.DiffContent:
                    case FileDiff.DiffSourceNewer:
                    default:
                    {
                        _logger.Warning(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                        _logger.Warning(@"{SyncLogType}: [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        contentFileHandler.FileCopyBack(sync.SourcePath, outputFileHandler, sync.TargetPath);
                        //outputFileHandler.FileCopy(contentFileHandler, sync.SourcePath, sync.TargetPath);
                        break;
                    }
                    }
                }
                break;
            }

            case SyncType.DeleteTarget:
            {
                if (outputFileHandler.FileExists(sync.TargetPath))
                {
                    _logger.Information(@"{SyncLogType}: [{TargetPath}] [{SyncType}]", SyncLogType.DeleteTarget.GetDescription(), sync.TargetPath, sync.SyncType);
                    outputFileHandler.FileDelete(sync.TargetPath);
                }
                else
                {
                    _logger.Debug(@"{SyncLogType}: (nothing to delete!) [{TargetPath}] [{SyncType}]", SyncLogType.DeleteTarget.GetDescription(), sync.TargetPath, sync.SyncType);
                }
                break;
            }

            case SyncType.Unknown:
            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Loads a board from a persisted state into <paramref name="boardDto"/>.
        /// </summary>
        /// <param name="rules">The rules to use with the loaded board.</param>
        /// <param name="boardDto">The object to store the state in.</param>
        /// <returns></returns>
        public bool LoadBoard(IRules rules, out BoardDto boardDto)
        {
            try
            {
                boardDto = new BoardDto();
                if (!_fileHandler.FileExists())
                {
                    return(false);
                }
                List <bool>               activeSubboards;
                List <MarkerType>         subboardWinners;
                List <List <MarkerType> > subboardsAsMarkerTypeArrays;
                try
                {
                    var jsonDataString = _fileHandler.Read();
                    var jsonObject     = JObject.Parse(jsonDataString);

                    boardDto.IsPlayerOneTurn = (bool)jsonObject["game"]["isPlayerOneTurn"];
                    boardDto.Winner          = (MarkerType)(int)jsonObject["game"]["winner"];
                    boardDto.PlayerOne       = (MarkerType)(int)jsonObject["game"]["playerOne"];
                    boardDto.PlayerTwo       = (MarkerType)(int)jsonObject["game"]["playerTwo"];

                    activeSubboards = jsonObject["game"]["activeSubboards"].Select(isActive => (bool)isActive)
                                      .ToList();
                    subboardWinners = jsonObject["game"]["subboardWinners"].Select(winner => (MarkerType)(int)winner)
                                      .ToList();
                    var subboards = from sb in jsonObject["game"]["board"] select(JArray) sb;
                    subboardsAsMarkerTypeArrays = subboards.Select(b => b.ToObject <List <MarkerType> >()).ToList();
                }
                catch (Exception e)
                {
                    throw new SerializationException("Failed to deserialize from file, see inner exception for details",
                                                     e);
                }

                var subboardArray    = new SubBoard[3, 3];
                var tempSubboardList = new List <SubBoard>();
                int counter;
                for (var i = 0; i < 9; i++)
                {
                    counter = 0;
                    var subboardAsArray = subboardsAsMarkerTypeArrays[i];
                    var markerTypeArray = new MarkerType[3, 3];

                    for (var j = 0; j < 3; j++)
                    {
                        for (var k = 0; k < 3; k++)
                        {
                            markerTypeArray[j, k] = subboardAsArray[counter];
                            counter++;
                        }
                    }
                    tempSubboardList.Add(new SubBoard(rules, markerTypeArray, activeSubboards[i], subboardWinners[i]));
                }

                counter = 0;
                for (var i = 0; i < 3; i++)
                {
                    for (var j = 0; j < 3; j++)
                    {
                        subboardArray[i, j] = tempSubboardList[counter];
                        counter++;
                    }
                }
                boardDto.Subboards = subboardArray;
                return(true);
            }
            catch (Exception e)
            {
                throw new LoadStateFailException("Failed to LoadBoard, see inner exception for details", e);
            }
        }