/// <summary> /// Loads the schedule from file. /// </summary> /// <returns></returns> private void LoadScheduleFromFile() { // Empty the current schedule collection _layoutSchedule.Clear(); // Clear the list of commands _commands.Clear(); // Get the schedule XML XmlDocument scheduleXml = GetScheduleXml(); // Parse the schedule xml XmlNodeList nodes = scheduleXml["schedule"].ChildNodes; // Are there any nodes in the document if (nodes.Count == 0) { SetEmptySchedule(); return; } // We have nodes, go through each one and add them to the layoutschedule collection foreach (XmlNode node in nodes) { LayoutSchedule temp = new LayoutSchedule(); // Node name temp.NodeName = node.Name; if (temp.NodeName == "dependants") { // Do nothing for now } else if (temp.NodeName == "command") { // Try to get the command using the code try { // Pull attributes from layout nodes XmlAttributeCollection attributes = node.Attributes; ScheduleCommand command = new ScheduleCommand(); command.Date = DateTime.Parse(attributes["date"].Value, CultureInfo.InvariantCulture); command.Code = attributes["code"].Value; command.ScheduleId = int.Parse(attributes["scheduleid"].Value); // Add to the collection _commands.Add(command); } catch (Exception e) { Trace.WriteLine(new LogMessage("ScheduleManager - LoadScheduleFromFile", e.Message), LogType.Error.ToString()); } } else { // Pull attributes from layout nodes XmlAttributeCollection attributes = node.Attributes; // All nodes have file properties temp.layoutFile = attributes["file"].Value; // Replace the .xml extension with nothing string replace = ".xml"; string layoutFile = temp.layoutFile.TrimEnd(replace.ToCharArray()); // Set these on the temp layoutschedule temp.layoutFile = ApplicationSettings.Default.LibraryPath + @"\" + layoutFile + @".xlf"; temp.id = int.Parse(layoutFile); // Get attributes that only exist on the default if (temp.NodeName != "default") { // Priority flag temp.Priority = (attributes["priority"].Value == "1") ? true : false; // Get the fromdt,todt temp.FromDt = DateTime.Parse(attributes["fromdt"].Value, CultureInfo.InvariantCulture); temp.ToDt = DateTime.Parse(attributes["todt"].Value, CultureInfo.InvariantCulture); // Pull out the scheduleid if there is one string scheduleId = ""; if (attributes["scheduleid"] != null) scheduleId = attributes["scheduleid"].Value; // Add it to the layout schedule if (scheduleId != "") temp.scheduleid = int.Parse(scheduleId); // Dependents if (attributes["dependents"] != null) { foreach (string dependent in attributes["dependents"].Value.Split(',')) { temp.Dependents.Add(dependent); } } } // Look for dependents nodes foreach (XmlNode childNode in node.ChildNodes) { if (childNode.Name == "dependents") { foreach (XmlNode dependent in childNode.ChildNodes) { if (dependent.Name == "file") { temp.Dependents.Add(dependent.InnerText); } } } } _layoutSchedule.Add(temp); } } // Clean up nodes = null; scheduleXml = null; // We now have the saved XML contained in the _layoutSchedule object }
/// <summary> /// Sets an empty schedule into the _layoutSchedule Collection /// </summary> private void SetEmptySchedule() { Debug.WriteLine("Setting an empty schedule", LogType.Info.ToString()); // Remove the existing schedule _layoutSchedule.Clear(); // Schedule up the default LayoutSchedule temp = new LayoutSchedule(); temp.layoutFile = ApplicationSettings.Default.LibraryPath + @"\Default.xml"; temp.id = 0; temp.scheduleid = 0; _layoutSchedule.Add(temp); }
/// <summary> /// Loads a new schedule from _layoutSchedules /// </summary> /// <returns></returns> private Collection<LayoutSchedule> LoadNewSchdule() { // We need to build the current schedule from the layout schedule (obeying date/time) Collection<LayoutSchedule> newSchedule = new Collection<LayoutSchedule>(); Collection<LayoutSchedule> prioritySchedule = new Collection<LayoutSchedule>(); // Temporary default Layout incase we have no layout nodes. LayoutSchedule defaultLayout = new LayoutSchedule(); // Store the valid layout id's List<int> validLayoutIds = new List<int>(); List<int> invalidLayouts = new List<int>(); // For each layout in the schedule determine if it is currently inside the _currentSchedule, and whether it should be foreach (LayoutSchedule layout in _layoutSchedule) { // Is this already invalid if (invalidLayouts.Contains(layout.id)) continue; // If we haven't already assessed this layout before, then check that it is valid if (!validLayoutIds.Contains(layout.id)) { // Is the layout valid in the cachemanager? try { if (!_cacheManager.IsValidLayout(layout.id + ".xlf")) { invalidLayouts.Add(layout.id); Trace.WriteLine(new LogMessage("ScheduleManager - LoadNewSchedule", "Layout invalid: " + layout.id), LogType.Error.ToString()); continue; } } catch { // Ignore this layout.. raise an error? invalidLayouts.Add(layout.id); Trace.WriteLine(new LogMessage("ScheduleManager - LoadNewSchedule", "Unable to determine if layout is valid or not"), LogType.Error.ToString()); continue; } // Check dependents foreach (string dependent in layout.Dependents) { if (!_cacheManager.IsValidPath(dependent)) { invalidLayouts.Add(layout.id); Trace.WriteLine(new LogMessage("ScheduleManager - LoadNewSchedule", "Layout has invalid dependent: " + dependent), LogType.Info.ToString()); continue; } } } // Add to the valid layout ids validLayoutIds.Add(layout.id); // If this is the default, skip it if (layout.NodeName == "default") { // Store it before skipping it defaultLayout = layout; continue; } // Look at the Date/Time to see if it should be on the schedule or not if (layout.FromDt <= DateTime.Now && layout.ToDt >= DateTime.Now) { // Priority layouts should generate their own list if (layout.Priority) { prioritySchedule.Add(layout); } else { newSchedule.Add(layout); } } } // If we have any priority schedules then we need to return those instead if (prioritySchedule.Count > 0) return prioritySchedule; // If the current schedule is empty by the end of all this, then slip the default in if (newSchedule.Count == 0) newSchedule.Add(defaultLayout); return newSchedule; }