Esempio n. 1
0
        /// <date>10/03/19</date>
        /// <summary>
        /// Rename a unit file from one number to another, update the contents to include the
        /// new id and story the old id in the former number collection.
        /// </summary>
        /// <param name="newNumber">new unit number</param>
        /// <param name="className">name of the class which the unit is part of</param>
        /// <param name="formerNumber">former unit number</param>
        /// <returns>success flag</returns>
        public static bool RenameIndividualUnitFile(
            int newNumber,
            string className,
            int formerNumber)
        {
            string originalFullPath = string.Empty;
            string newFullPath      = string.Empty;

            try
            {
                originalFullPath =
                    IndividualUnitIOController.GetFilePath(
                        className,
                        formerNumber.ToString());
                newFullPath =
                    IndividualUnitIOController.GetFilePath(
                        className,
                        newNumber.ToString());

                if (!File.Exists(originalFullPath))
                {
                    Logger.Instance.WriteLog(
                        $"Rename Unit File failed, can't find the original file: {originalFullPath}");
                    return(false);
                }

                // read the file
                IndividualUnitFileContents unit =
                    IndividualUnitIOController.ReadIndividualUnitFile(
                        className,
                        formerNumber.ToString());

                if (unit == null)
                {
                    Logger.Instance.WriteLog(
                        "ERROR: IndividualUnitIOController.renameIndividualUnitFile: Read File failed");
                    return(false);
                }

                // change the number in the file.
                unit.UnitNumber = newNumber.ToString();

                // update the former numbers list
                unit.AddFormerNumber(formerNumber);

                // save the file.
                if (!WriteIndividualUnitFile(unit, className))
                {
                    Logger.Instance.WriteLog("ERROR: IndividualUnitIOController.renameIndividualUnitFile: Write File failed");
                    return(false);
                }

                // A copy of the old file has now been saved under the new number,
                // delete the old file.
                File.Delete(originalFullPath);
                //File.Move(
                //    originalFullPath,
                //    newFullPath);
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog($"ERROR: Error renumbering unit file. Error is {ex}");
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        /// <name>writeIndividualUnitFile</name>
        /// <date>10/03/19</date>
        /// <summary>
        /// Saves the contents of a <see cref="IndividualUnitFileContents"/> object to the current
        /// <see cref="VersionString"/> of a unit details file.
        /// </summary>
        /// <param name="unitDetails">unit details</param>
        /// <param name="className">name of the class which the unit is part of</param>
        /// <returns>success flag</returns>
        public static bool WriteIndividualUnitFile(
            IndividualUnitFileContents 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.Distance.ToString());

                    // {number_entries}
                    writer.WriteLine(unitDetails.EntriesCount.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.LastCheckDate;
                    writer.WriteLine(lastCheck.Day + "\t" + lastCheck.Month + "\t" + lastCheck.Year);

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

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

                    if (unitDetails.Journeys.Count > 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}
                        //Not this is the other way around to the IVehicleDataType
                        for (int index = 0; index < unitDetails.Journeys.Count; ++index)
                        {
                            writer.WriteLine(unitDetails.Journeys[index].ToFullString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog(
                    $"ERROR: Error writing {directoryPath}, {unitDetails.UnitNumber}. Error is {ex}");

                success = false;
            }

            return(success);
        }