Esempio n. 1
0
        /// <summary>
        /// Backup the existing file.
        /// </summary>
        /// <param name="fileName">name of the file to backup</param>
        /// <param name="year">year of the file</param>
        /// <param name="month">month of the file</param>
        private static void BackupFile(
            string fileName,
            int year,
            int month)
        {
            string backupFile =
                DailyInputFactory.BuildBackupFilePath(
                    year,
                    month);

            Logger.Instance.WriteLog($"TRACE: DailyIOController - backupfile is: {backupFile}");

            // does the directory exist. Create if needed.
            // if it currently exists, rename existing file to make a copy - overwrite if needed.
            // read copy into buffer. If available
            try
            {
                if (File.Exists(backupFile) &&
                    File.Exists(fileName))
                {
                    File.Delete(backupFile);
                    File.Copy(fileName, backupFile);
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog(
                    $"ERROR: DailyIOController: Days  - Failed to rearrange files. Error is {ex}");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Load an input file.
        /// </summary>
        /// <param name="filePath">name and path of file to load</param>
        /// <returns>file contents</returns>
        private static List <IJourneyDetailsType> LoadFile(
            string filePath)
        {
            if (!File.Exists(filePath))
            {
                return(new List <IJourneyDetailsType>());
            }

            int month;
            int year;

            try
            {
                month = DailyInputFactory.GetMonth(filePath);
                year  = DailyInputFactory.GetYear(filePath);
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog(
                    $"DailyInputFactory File name is invalid {ex}");
                return(new List <IJourneyDetailsType>());
            }

            List <JourneyDetailsType> fileList = new List <JourneyDetailsType>();

            return(DailyInputFactory.LoadFile(
                       filePath,
                       year,
                       month));
        }
Esempio n. 3
0
        /// <summary>
        /// Load an input file for a given month.
        /// </summary>
        /// <param name="path">directory containing the month files</param>
        /// <param name="month">month to load</param>
        /// <returns>file contents</returns>
        public static List <IJourneyDetailsType> LoadMonth(
            string path,
            int month)
        {
            string filePath =
                DailyInputFactory.BuildFilePath(
                    path,
                    month);

            if (string.IsNullOrEmpty(filePath))
            {
                Logger.Instance.WriteLog(
                    $"ERROR: DailyInputFactory: Load Month: Input is invalid (Path-{path}, Month-{month})");
                return(new List <IJourneyDetailsType>());
            }

            int year;

            try
            {
                year = DailyInputFactory.GetYear(path);
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog(
                    $"ERROR: DailyInputFactory: Load Month: Input is invalid (Path-{path}, Month-{month})");
                return(new List <IJourneyDetailsType>());
            }

            return(DailyInputFactory.LoadFile(
                       filePath,
                       year,
                       month));
        }
Esempio n. 4
0
        /// <summary>
        /// Save a day.
        /// </summary>
        /// <remarks>
        /// Need to revisit this to look at the correct division of labour.
        /// </remarks>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="dayJourneysList"></param>
        /// <returns></returns>
        public static void SaveDay(
            int year,
            int month,
            int day,
            List <IJourneyDetailsType> dayJourneysList)
        {
            StreamWriter writer;
            List <IJourneyDetailsType> newJourneys = new List <IJourneyDetailsType>();

            foreach (IJourneyDetailsType journey in dayJourneysList)
            {
                newJourneys.Add((IJourneyDetailsType)journey);
            }

            string saveFilePath =
                DailyInputFactory.BuildFilePath(
                    year,
                    month);

            List <string> baFileLines = new List <string>();

            DailyInputFactory.EnsureDirectoryExists(year);

            DailyInputFactory.BackupFile(
                saveFilePath,
                year,
                month);
            List <IJourneyDetailsType> existingJourneys =
                DailyInputFactory.LoadFile(saveFilePath);

            List <IJourneyDetailsType> beforeDay =
                DailyInputFactory.GetJnysBeforeDay(
                    existingJourneys,
                    day);
            List <IJourneyDetailsType> afterDay =
                DailyInputFactory.GetJnysAfterDay(
                    existingJourneys,
                    day);

            // create new file
            // output buffer line by line.
            // if its a earlier day then output the line straight back out
            // if current date output todays journeys
            // if its a later day then output the line straight back out
            using (writer = new StreamWriter(saveFilePath))
            {
                DailyInputFactory.OutputJourneys(
                    writer,
                    beforeDay);
                DailyInputFactory.OutputJourneys(
                    writer,
                    newJourneys);
                DailyInputFactory.OutputJourneys(
                    writer,
                    afterDay);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Get all the journeys for the given <paramref name="date"/>
        /// </summary>
        /// <param name="date">day to check</param>
        /// <returns>observable collection of journeys</returns>
        public static List <IJourneyDetailsType> LoadDay(DateTime date)
        {
            string filePath =
                DailyInputFactory.BuildFilePath(
                    date.Year,
                    date.Month);

            if (string.IsNullOrEmpty(filePath))
            {
                Logger.Instance.WriteLog(
                    $"ERROR: DailyInputFactory: Load File: Input is invalid (Date-{date})");
                return(new List <IJourneyDetailsType>());
            }

            return
                (DailyInputFactory.LoadFile(
                     filePath,
                     date.Year,
                     date.Month,
                     date.Day));
        }
Esempio n. 6
0
        /// <summary>
        /// Load an input file for a given month.
        /// </summary>
        /// <param name="year">year to load</param>
        /// <param name="month">month to load</param>
        /// <returns>file contents</returns>
        public static List <IJourneyDetailsType> LoadMonth(
            int year,
            int month)
        {
            List <IJourneyDetailsType> fileJourneys = new List <IJourneyDetailsType>();

            string filePath =
                DailyInputFactory.BuildFilePath(
                    year,
                    month);

            if (string.IsNullOrEmpty(filePath))
            {
                Logger.Instance.WriteLog(
                    $"ERROR: DailyInputFactory: Load File: Input is invalid (Year-{year}, Month-{month})");
                return(new List <IJourneyDetailsType>());
            }

            return(DailyInputFactory.LoadFile(
                       filePath,
                       year,
                       month));
        }
Esempio n. 7
0
        //DailyIOController dayController;

        ///// <summary>
        ///// Initialise a new instance of the <see cref="DailyInputFactory"/> class.
        ///// </summary>
        //public DailyInputFactory()
        //{
        //  //this.dayController = ioController;
        //}

        /// <summary>
        /// Get a list of all distances in a month
        /// </summary>
        /// <param name="year">current year</param>
        /// <param name="month">current year</param>
        /// <returns>list of all distances in a month</returns>
        public static ObservableCollection <DayViewModel> GetAllDaysInMonth(
            int year,
            int month)
        {
            List <MilesChains> daysList =
                DailyInputFactory.SetupDays(
                    year,
                    month);

            List <IJourneyDetailsType> jnyDetails =
                DailyInputFactory.LoadMonth(
                    year,
                    month);

            foreach (IJourneyDetailsType jny in jnyDetails)
            {
                daysList[jny.Date.Day - 1] += jny.Distance;
            }

            return(DailyInputFactory.SetupDayViewModel(
                       year,
                       month,
                       daysList));
        }