예제 #1
0
        /// <date>10/03/19</date>
        /// <summary>
        /// Saves the contents of a <see cref="IVehicleDetailsType"/> object to the current
        /// <see cref="VersionString"/> of a unit details file.
        /// </summary>
        /// <param name="unitDetails">unit details to be saved</param>
        /// <param name="className">name of the class which the unit is part of</param>
        /// <returns>save success flag</returns>
        public static bool WriteIndividualUnitFile(
            IVehicleDetailsType unitDetails,
            string className)
        {
            bool   success       = true;
            string directoryPath = string.Empty;

            try
            {
                directoryPath =
                    IndividualUnitIOController.GetDirectoryPath(
                        className);

                // Create the directory if it does not exist.
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                using (StreamWriter writer = new StreamWriter(GetFilePath(className, unitDetails.UnitNumber)))
                {
                    // {Version}
                    writer.WriteLine(VersionString + CurrentDataVersion.ToString());

                    // {Number}
                    writer.WriteLine(unitDetails.UnitNumber);

                    // {mileage}
                    writer.WriteLine(unitDetails.UnitDistance.ToString());

                    // {number_entries}
                    writer.WriteLine(unitDetails.NumberOfJnys.ToString());

                    // {former_number1}^t{former_number2}^t{former_number3}
                    writer.WriteLine(unitDetails.FormerNumbers.ToString());

                    // {last_entry_day}^t{last_entry_month}^t{last_entry_year}
                    DateTime lastEntry = new DateTime(1, 1, 1);
                    writer.WriteLine(lastEntry.Day + "\t" + lastEntry.Month + "\t" + lastEntry.Year);

                    // {last_check_day}^t{last_check_month}^t{last_check_year}
                    DateTime lastCheck = unitDetails.UnitLastCheck;
                    writer.WriteLine(lastCheck.Day + "\t" + lastCheck.Month + "\t" + lastCheck.Year);

                    // {Status}
                    writer.WriteLine(unitDetails.Status);

                    // {Notes}
                    writer.WriteLine(unitDetails.Notes);

                    if (unitDetails.NumberOfJnys > 0)
                    {
                        // {day}^t{month}^t{year}^t{from}^t{to}^t{via}^t{miles}^t{chains}^t{total_vehicles}^t{number1}^t{number2}^t{number3}^t{number4}
                        for (int index = unitDetails.NumberOfJnys - 1; index >= 0; --index)
                        {
                            // Bit of a mess really. There is a model class which knows how to write to files and a view model
                            // class which know how to display to a view. This converts from view model to model. May need to be
                            // reworked. (There are other places as well).
                            List <string> units = new List <string>();
                            IndividualUnitIOController.Add(units, unitDetails.JourneysList[index].UnitOne);
                            IndividualUnitIOController.Add(units, unitDetails.JourneysList[index].UnitTwo);
                            IndividualUnitIOController.Add(units, unitDetails.JourneysList[index].UnitThree);
                            IndividualUnitIOController.Add(units, unitDetails.JourneysList[index].UnitFour);

                            IJourneyDetailsType journey =
                                new JourneyDetailsType(
                                    unitDetails.JourneysList[index].JnyId.Date,
                                    unitDetails.JourneysList[index].JnyId.JnyNumber,
                                    unitDetails.JourneysList[index].From,
                                    unitDetails.JourneysList[index].To,
                                    unitDetails.JourneysList[index].Route,
                                    unitDetails.JourneysList[index].Distance,
                                    units);
                            writer.WriteLine(journey.ToFullString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog(
                    $"ERROR: Error writing {directoryPath}, {unitDetails.UnitNumber}. Error is {ex}");

                success = false;
            }

            return(success);
        }