예제 #1
0
        } // DropFillBooks()

        //
        //
        //
        //
        // *********************************************************
        // ****                 TryLoadBooks()                  ****
        // *********************************************************
        /// <summary>
        /// Reads the local drop file at the GetLocalPath() location named
        /// GetLocalFileName(), and obtains a list of IStringifiable.Nodes in that file.
        /// This list of nodes is examined (in reverse order) until the most recent FillHub
        /// node object is found.
        /// The fillhub node is broken down into its parts, which are actually created and
        /// store in another list.  The remaining nodes are also created and loading into
        /// the list as well.  (These are usually fills that came after the fillhub snapshot.)
        /// Version 4:  This is improved because it relies on read-methods from Stringifiable namespace.
        /// </summary>
        /// <returns>True upon success</returns>
        public bool TryLoadBooks(string loadFileName = "")
        {
            string filePath = string.Empty;

            if (string.IsNullOrEmpty(loadFileName))
            {
                filePath = string.Format("{0}{1}", GetLocalPath(), GetLocalFileName());
            }
            else
            {
                filePath = string.Format("{0}{1}", GetLocalPath(), loadFileName);
            }
            this.m_FillHub.Log.NewEntry(LogLevel.Major, "DropRules.TryLoadBooks: {0}", filePath);
            List <Node> nodeList;

            try
            {
                using (StringifiableReader reader = new StringifiableReader(filePath))
                {
                    nodeList = reader.ReadNodesToEnd();
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                this.m_FillHub.Log.NewEntry(LogLevel.Error, "DropRules.TryLoadBooks: StrigifiableReader exception: {0}", e.Message);
                return(false);
            }
            // Go thru the nodes, looking for the last FillHub node.
            Node   hubNode         = null;
            string fillHubTypeName = m_FillHub.GetType().FullName;
            int    ptr             = nodeList.Count - 1;    // point to last element

            while (ptr >= 0)
            {
                if (hubNode != null)
                {                                           // We read backwards, and once we've passed the hub
                    nodeList.RemoveAt(ptr);                 // From here on out, we want to dump everthing!
                }
                else if (nodeList[ptr].Name == fillHubTypeName)
                {
                    hubNode = nodeList[ptr];
                    nodeList.RemoveAt(ptr);                 // remove the hub also; we already have a pointer to it.
                }
                ptr--;                                      // move backwards thru list
            }//wend ptr

            // Extract the info from the FillHub node.
            List <IStringifiable> objectList = new List <IStringifiable>();

            ((IStringifiable)m_FillHub).SetAttributes(hubNode.Attributes);  // initialize this hub!
            foreach (IStringifiable subElem in hubNode.SubElements)
            {
                IStringifiable obj = Stringifiable.Create((Node)subElem);
                objectList.Add(obj);                        // keep all the sub elements (below) we load them in specific order!
            }
            // Now create the objects we found after the hub - these are usually additional fills not in the fill hub.
            foreach (Node anode in nodeList)
            {
                IStringifiable obj = Stringifiable.Create(anode);
                objectList.Add(obj);
            }

            // Load objects we found.
            if (objectList != null && objectList.Count > 0)
            {
                foreach (IStringifiable obj in objectList)
                {
                    if (obj is InstrumentMapEntry)                      // load all InstrumentMapEntries first; needed to create fill books
                    {
                        ((IStringifiable)m_FillHub).AddSubElement(obj);
                    }
                }
                foreach (IStringifiable obj in objectList)
                {
                    if (!(obj is InstrumentMapEntry))                   // load everything else now.
                    {
                        ((IStringifiable)m_FillHub).AddSubElement(obj);
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }//TryLoadBooks()
예제 #2
0
        } // DropFillBooks()

        //
        //
        //
        //
        // *********************************************************
        // ****                 TryLoadBooks()                  ****
        // *********************************************************
        /// <summary>
        /// Reads the local drop file at the GetLocalPath() location named
        /// GetLocalFileName(), and obtains a list of IStringifiable.Nodes in that file.
        /// This list of nodes is examined (in reverse order) until the most recent FillHub
        /// node object is found.
        /// The fillhub node is broken down into its parts, which are actually created and
        /// store in another list.  The remaining nodes are also created and loading into
        /// the list as well.  (These are usually fills that came after the fillhub snapshot.)
        /// Version 4:  This is improved because it relies on read-methods from Stringifiable namespace.
        /// </summary>
        /// <returns>True upon success</returns>
        public bool TryLoadBooks(string loadFileName = "")
        {
            string filePath = string.Empty;                 // place to store the desired book to load.

            // Find most recent book that matches unique name.
            List <string> dirPathList = new List <string>(System.IO.Directory.GetDirectories(UV.Lib.Application.AppInfo.GetInstance().DropPath, "20*")); // presumes all fills in 21st century!

            dirPathList.Sort();
            int           dirPtr       = dirPathList.Count - 1; // point to last entry.
            List <string> filePathList = new List <string>();
            string        pattern      = string.Format("*{0}*", this.GetArchiveFileNameBase(DropType_FillBook));

            while (string.IsNullOrEmpty(filePath) && dirPtr >= 0)
            {
                filePathList.Clear();
                string currentDirPath = dirPathList[dirPtr];
                filePathList.AddRange(System.IO.Directory.GetFiles(currentDirPath, pattern));
                if (filePathList.Count > 0)
                {
                    filePathList.Sort();
                    int filePtr = filePathList.Count - 1; // point to last entry
                    while (string.IsNullOrEmpty(filePath) && filePtr >= 0)
                    {                                     // We can set to checking here and validation, or use a slightly earlier drop, etc.
                        System.IO.FileInfo info = new System.IO.FileInfo(filePathList[filePtr]);
                        bool isGood             = info.Length > 0;
                        if (isGood)
                        {
                            filePath = filePathList[filePathList.Count - 1];
                        }
                        else
                        {
                            filePtr--;
                        }
                    }
                }
                else
                {
                    dirPtr--;
                }
            }// while file not found.
            if (string.IsNullOrEmpty(filePath))
            {
                m_FillHub.Log.BeginEntry(LogLevel.Major, "DropRules.TryLoadBooks: No drop file found for pattern {0}. Searched directories: ", pattern);
                foreach (string s in dirPathList)
                {
                    int n = s.LastIndexOf('\\');
                    m_FillHub.Log.AppendEntry("{0} ", s.Substring(n + 1, s.Length - (n + 1)));
                }
                m_FillHub.Log.EndEntry();
                return(false);
            }

            //
            // Load the drop file.
            //
            this.m_FillHub.Log.NewEntry(LogLevel.Major, "DropRules.TryLoadBooks: {0}", filePath);
            List <Node> nodeList;

            try
            {
                using (StringifiableReader reader = new StringifiableReader(filePath))
                {
                    nodeList = reader.ReadNodesToEnd();
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                this.m_FillHub.Log.NewEntry(LogLevel.Error, "DropRules.TryLoadBooks: StrigifiableReader exception: {0}", e.Message);
                return(false);
            }
            // Go thru the nodes, looking for the last FillHub node.
            Node   hubNode         = null;
            string fillHubTypeName = m_FillHub.GetType().FullName;
            int    ptr             = nodeList.Count - 1;    // point to last element

            while (ptr >= 0)
            {
                if (hubNode != null)
                {                                           // We read backwards, and once we've passed the hub
                    nodeList.RemoveAt(ptr);                 // From here on out, we want to dump everthing!
                }
                else if (nodeList[ptr].Name == fillHubTypeName)
                {
                    hubNode = nodeList[ptr];
                    nodeList.RemoveAt(ptr);                 // remove the hub also; we already have a pointer to it.
                }
                ptr--;                                      // move backwards thru list
            }//wend ptr

            // Extract the info from the FillHub node.
            List <IStringifiable> objectList = new List <IStringifiable>();

            ((IStringifiable)m_FillHub).SetAttributes(hubNode.Attributes);  // initialize this hub!
            foreach (IStringifiable subElem in hubNode.SubElements)
            {
                IStringifiable obj = Stringifiable.Create((Node)subElem);
                objectList.Add(obj);                        // keep all the sub elements (below) we load them in specific order!
            }
            // Now create the objects we found after the hub - these are usually additional fills not in the fill hub.
            foreach (Node anode in nodeList)
            {
                IStringifiable obj = Stringifiable.Create(anode);
                objectList.Add(obj);
            }

            // Load objects we found.
            if (objectList != null && objectList.Count > 0)
            {
                foreach (IStringifiable obj in objectList)
                {
                    if (obj is InstrumentMapEntry)                      // load all InstrumentMapEntries first; needed to create fill books
                    {
                        ((IStringifiable)m_FillHub).AddSubElement(obj);
                    }
                }
                foreach (IStringifiable obj in objectList)
                {
                    if (!(obj is InstrumentMapEntry))                   // load everything else now.
                    {
                        ((IStringifiable)m_FillHub).AddSubElement(obj);
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }//TryLoadBooks()
예제 #3
0
        // *****************************************************************
        // ****                     Public Methods                      ****
        // *****************************************************************
        //
        //
        //
        /// <summary>
        /// Get drop file specified by user using a Ambre position recovery start date time.
        /// It loads the most recent drop file before this date time.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="fillHubName"></param>
        /// <param name="selectedDateTime"></param>
        /// <param name="fillHub"></param>
        /// <returns></returns>
        public bool TryPlayDropFileForOneFillHub(string userName, string fillHubName, DateTime selectedDateTime, out AuditTrailFillHub fillHub)
        {
            fillHub = null;

            // Find the fill book with the closest date time to the input date time.
            string   currentFilePath    = null;
            string   pattern            = string.Format("*FillBooks_{0}_{1}.txt", userName, fillHubName);
            DateTime searchFileDateTime = DateTime.MinValue;
            bool     isBookFound        = false;

            // Get the directories with date format in the drop path.
            List <string> dirPathList = new List <string>(System.IO.Directory.GetDirectories(m_DropPath, "20*"));

            dirPathList.Sort();
            int    dirPtr = dirPathList.Count - 1;
            string currentDirPath;
            int    indexDir;

            // Create the file path list to store the searched files.
            List <string> filePathList = new List <string>();
            int           indexPath;
            int           filePtr;
            string        fileName;
            string        fileTime;
            string        fileDate;
            string        fileDateTime;

            // Start searching for the correct file. Loop through directory.
            while (!isBookFound && dirPtr >= 0)
            {
                currentDirPath = dirPathList[dirPtr];
                filePathList.Clear();
                filePathList.AddRange(System.IO.Directory.GetFiles(currentDirPath, pattern));
                if (filePathList.Count > 0)
                {
                    filePathList.Sort();
                    filePtr = filePathList.Count - 1;

                    // Loop through files in that directory.
                    while (!isBookFound && filePtr >= 0)
                    {
                        currentFilePath = filePathList[filePtr];
                        indexDir        = currentDirPath.LastIndexOf('\\');
                        indexPath       = currentFilePath.LastIndexOf('\\');
                        fileName        = currentFilePath.Substring(indexPath + 1, currentFilePath.Length - (indexPath + 1));
                        fileTime        = fileName.Substring(0, fileName.IndexOf("_"));
                        fileDate        = currentDirPath.Substring(indexDir + 1, currentDirPath.Length - (indexDir + 1));
                        fileDateTime    = string.Format("{0}{1}", fileDate, fileTime);

                        // Parse the date time out.
                        if (!DateTime.TryParseExact(fileDateTime, "yyyyMMddHHmmss", null, System.Globalization.DateTimeStyles.None, out searchFileDateTime))
                        {
                            Log.NewEntry(LogLevel.Major, "Failed to parse the file date time of {0}.", fileDateTime);
                        }
                        else
                        {
                            Log.NewEntry(LogLevel.Minor, "Successfully get the file path of {0}.", searchFileDateTime);
                            System.IO.FileInfo info = new System.IO.FileInfo(filePathList[filePtr]);
                            m_SelectedDropFileDateTime = searchFileDateTime;
                            isBookFound = searchFileDateTime <= selectedDateTime && info.Length > 0;
                        }

                        // If no desired file is found in this directory, find in the next directory.
                        filePtr--;
                        if (!isBookFound && filePtr < 0)
                        {
                            dirPtr--;
                            break;
                        }
                    }
                }
                else
                {
                    dirPtr--;
                }
            }

            // Check finally whether we have found the file.
            if (!isBookFound || string.IsNullOrEmpty(currentFilePath))
            {
                Log.NewEntry(LogLevel.Major, "Failed to locate the file before date time of {0}.", selectedDateTime);
                return(false);
            }
            else
            {
                // Start to load every element from the file.
                Log.NewEntry(LogLevel.Minor, "Start load elements from drop file.");
                List <Node> nodeList;
                try
                {
                    using (StringifiableReader reader = new StringifiableReader(currentFilePath))
                    {
                        nodeList = reader.ReadNodesToEnd();
                        reader.Close();
                    }
                }
                catch (Exception e)
                {
                    Log.NewEntry(LogLevel.Major, "TryPlayDropFileForOneFillHub: Strigifiable Reader Exception: {0}", e.Message);
                    return(false);
                }

                // Go through the node list backward, looking for the last fill hub node.
                Node   hubNode         = null;
                string fillHubTypeName = typeof(FillHub).FullName;
                int    ptr             = nodeList.Count - 1;
                while (ptr >= 0)
                {
                    if (hubNode != null)
                    {
                        nodeList.RemoveAt(ptr);
                    }
                    else if (nodeList[ptr].Name == fillHubTypeName)
                    {
                        hubNode = nodeList[ptr];
                        nodeList.RemoveAt(ptr);
                    }
                    ptr--;
                }

                // Extract the information from the fill hub node.
                fillHub = new AuditTrailFillHub(fillHubName, false);
                List <IStringifiable> objectList = new List <IStringifiable>();
                ((IStringifiable)fillHub).SetAttributes(hubNode.Attributes);
                foreach (IStringifiable subElement in hubNode.SubElements)
                {
                    IStringifiable obj = Stringifiable.Create((Node)subElement);
                    objectList.Add(obj);
                }

                foreach (Node anode in nodeList)
                {
                    IStringifiable obj = Stringifiable.Create(anode);
                    objectList.Add(obj);
                }

                // Load objects we found.
                if (objectList != null && objectList.Count > 0)
                {
                    foreach (IStringifiable obj in objectList)
                    {
                        if (obj is InstrumentMapEntry)
                        {
                            ((IStringifiable)fillHub).AddSubElement(obj);
                        }
                    }
                    foreach (IStringifiable obj in objectList)
                    {
                        if (!(obj is InstrumentMapEntry))
                        {
                            ((IStringifiable)fillHub).AddSubElement(obj);
                        }
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }