예제 #1
0
        /// <date>10/03/19</date>
        /// <summary>
        /// Reads contents of an individual unit file and returns them to the caller.
        /// </summary>
        /// <param name="className">name of the class the unit file is in</param>
        /// <param name="fileName">name of the file/unit identifier</param>
        /// <returns>Contents of the file which has been read</returns>
        public static IndividualUnitFileContents ReadIndividualUnitFile(
            string className,
            string fileName)
        {
            string fullFilePath =
                IndividualUnitIOController.GetFilePath(
                    className,
                    fileName);

            if (!File.Exists(fullFilePath))
            {
                string loggerError =
                    $"ERROR: File {fullFilePath} does not exist";

                Logger.Instance.WriteLog(
                    loggerError);
                return(null);
            }

            try
            {
                using (StreamReader sr = new StreamReader(GetFilePath(className, fileName)))
                {
                    string currentLine = string.Empty;

                    // {Number}
                    // Version 1 does not have a version number, so in version 1 this would be the unit id.
                    currentLine = sr.ReadLine();

                    if (currentLine.Substring(0, VersionString.Length) == VersionString)
                    {
                        int version = 0;
                        if (int.TryParse(currentLine.Substring(VersionString.Length), out version))
                        {
                            return(ReadIndividualUnitFileLaterVersion(
                                       sr,
                                       version));
                        }
                        else
                        {
                            Logger.Instance.WriteLog($"ERROR: Error reading {fullFilePath}. Invalid Version: {currentLine}");
                            return(null);
                        }
                    }
                    else
                    {
                        // This is a version 1 file.
                        return(IndividualUnitIOController.ReadIndividualUnitFileVersion1(
                                   sr,
                                   currentLine));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog($"ERROR: Error reading {fullFilePath}. Error is {ex}");

                return(null);
            }
        }
예제 #2
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);
        }