Exemple #1
0
        /// <summary>
        /// Convert a <see cref="TimetableDocument"/> instance to a <see cref="TimetableFileModel"/> for serialization.
        /// </summary>
        /// <param name="document">The instance to be converted.</param>
        /// <returns>The data prepared for serialization.</returns>
        public static TimetableFileModel ToTimetableFileModel(this TimetableDocument document)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            TimetableFileModel fileModel = new TimetableFileModel
            {
                Version = Versions.CurrentTimetableDocument,

                Options          = document.Options.ToDocumentOptionsModel(),
                ExportOptions    = document.ExportOptions.ToExportOptionsModel(),
                Title            = document.Title,
                Subtitle         = document.Subtitle,
                DateDescription  = document.DateDescription,
                WrittenBy        = document.WrittenBy,
                CheckedBy        = document.CheckedBy,
                TimetableVersion = document.TimetableVersion,
                PublishedDate    = document.PublishedDate,
            };

            NetworkMapModel nmm = new NetworkMapModel();

            nmm.LocationList.AddRange(document.LocationList.Select(l => l.ToLocationModel()));
            nmm.Signalboxes.AddRange(document.Signalboxes.Select(b => b.ToSignalboxModel()));
            fileModel.Maps.Add(nmm);
            fileModel.NoteDefinitions.AddRange(document.NoteDefinitions.Select(n => n.ToNoteModel()));
            fileModel.TrainClassList.AddRange(document.TrainClassList.Select(tc => tc.ToTrainClassModel()));
            fileModel.TrainList.AddRange(document.TrainList.Select(t => t.ToTrainModel()));
            fileModel.SignalboxHoursSets.AddRange(document.SignalboxHoursSets.Select(h => h.ToSignalboxHoursSetModel()));

            return(fileModel);
        }
Exemple #2
0
        /// <summary>
        /// Convert a <see cref="TimetableDocument" /> instance to a <see cref="TimetableFileModel" /> instance.
        /// </summary>
        /// <param name="doc">The object to convert.</param>
        /// <returns>A <see cref="TimetableFileModel" /> instance containing the same data as the parameter, in serialisable form.</returns>
        /// <exception cref="NullReferenceException">Thrown if the parameter is <c>null</c>.</exception>
        public static TimetableFileModel ToYamlTimetableFileModel(this TimetableDocument doc)
        {
            if (doc is null)
            {
                throw new NullReferenceException();
            }

            TimetableFileModel fileModel = new TimetableFileModel
            {
                Version = Versions.CurrentTimetableDocument,

                FileOptions   = doc.Options.ToYamlDocumentOptionsModel(),
                ExportOptions = doc.ExportOptions.ToYamlExportOptionsModel(),

                Title            = doc.Title,
                Subtitle         = doc.Subtitle,
                DateDescription  = doc.DateDescription,
                WrittenBy        = doc.WrittenBy,
                CheckedBy        = doc.CheckedBy,
                TimetableVersion = doc.TimetableVersion,
                PublishedDate    = doc.PublishedDate,
            };

            fileModel.Maps.Add(BuildNetworkMapModel(doc));
            fileModel.TrainList.AddRange(doc.TrainList.Select(t => t.ToYamlTrainModel()));
            fileModel.NoteDefinitions.AddRange(doc.NoteDefinitions.Select(n => n.ToYamlNoteModel()));
            fileModel.TrainClassList.AddRange(doc.TrainClassList.Select(c => c.ToYamlTrainClassModel()));
            fileModel.SignalboxHoursSets.AddRange(doc.SignalboxHoursSets.Select(s => s.ToYamlSignalboxHoursSetModel()));
            return(fileModel);
        }
Exemple #3
0
        public void TimetableDocumentExtensionsClass_ToTimetableFileModelMethod_ReturnsModelWithExportOptionsPropertyThatIsNotNull()
        {
            TimetableDocument document = new TimetableDocument();

            TimetableFileModel result = document.ToTimetableFileModel();

            Assert.IsNotNull(result.ExportOptions);
        }
Exemple #4
0
        private static NetworkMapModel BuildNetworkMapModel(TimetableDocument doc)
        {
            NetworkMapModel model = new NetworkMapModel();

            model.LocationList.AddRange(doc.LocationList.Select(c => c.ToYamlLocationModel()));
            model.Signalboxes.AddRange(doc.Signalboxes.Select(b => b.ToYamlSignalboxModel()));
            return(model);
        }
Exemple #5
0
        public void TimetableDocumentExtensionsClass_ToTimetableFileModelMethod_ThrowsArgumentNullException_IfParameterIsNull()
        {
            TimetableDocument document = null;

            _ = document.ToTimetableFileModel();

            Assert.Fail();
        }
        /// <summary>
        /// Save a timetable document to a stream.
        /// </summary>
        /// <param name="document">The document to be saved.</param>
        /// <param name="destination">The stream to save the document to.</param>
        public static void Save(TimetableDocument document, Stream destination)
        {
            Serializer serializer = GetSerializer();

            using (StreamWriter writer = new StreamWriter(destination))
            {
                writer.WriteLine("%WTT");
                serializer.Serialize(writer, document.ToYamlTimetableFileModel());
            }
        }
Exemple #7
0
        public void TimetableFileModelExtensionsClassToTimetableDocumentMethodReturnsTimetableDocumentObjectWithExportOptionsPropertyThatIsNotNullIfExportOptionsPropertyOfParameterIsNotNull()
        {
            TimetableFileModel testSourceObject = new TimetableFileModel {
                ExportOptions = new ExportOptionsModel()
            };

            TimetableDocument testResultObject = testSourceObject.ToTimetableDocument();

            Assert.IsNotNull(testResultObject.ExportOptions);
        }
Exemple #8
0
        public void PdfExporterClass_ExportMethod_ThrowsArgumentNullException_IfFirstParameterIsNull()
        {
            PdfExporter       testObject = new PdfExporter(new Mock <IDocumentDescriptorFactory>().Object);
            TimetableDocument testParam0 = null;
            Stream            testParam1 = new Mock <Stream>().Object;

            testObject.Export(testParam0, testParam1);

            Assert.Fail();
        }
        /// <summary>
        /// Default constructor.  Creates an empty document, and initialises the file dialogs' default folders to My Documents.
        /// </summary>
        public MainForm()
        {
            Log.Info("Starting up Timetabler {0}", GetType().Assembly.GetName().Version);
            InitializeComponent();

            Model = new TimetableDocument();
            UpdateTrainGraphLocationModel();

            ofdDocument.InitialDirectory  = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            sfdDocument.InitialDirectory  = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofdDocument.InitialDirectory  = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            sfdLocations.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            sfdExport.InitialDirectory    = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }
Exemple #10
0
        public void TimetableFileDocumentExtensionsClass_ToTimetableFileModelMethod_ThrowsArgumentNullExceptionWithCorrectParamNameProperty_IfParameterIsNull()
        {
            TimetableDocument document = null;

            try
            {
                _ = document.ToTimetableFileModel();
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual("document", ex.ParamName);
            }
        }
Exemple #11
0
        public void PdfExporterClass_ExportMethod_ThrowsArgumentNullExceptionWithCorrectParamNameProperty_IfFirstParameterIsNull()
        {
            PdfExporter       testObject = new PdfExporter(new Mock <IDocumentDescriptorFactory>().Object);
            TimetableDocument testParam0 = null;
            Stream            testParam1 = new Mock <Stream>().Object;

            try
            {
                testObject.Export(testParam0, testParam1);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual("document", ex.ParamName);
            }
        }
Exemple #12
0
        public void TimetableFileModelExtensionsClassToTimetableDocumentMethodReturnsTimetableDocumentObjectWithExportOptionsPropertyWithCorrectDisplayLocoDiagramRowPropertyIfExportOptionsPropertyOfParameterIsNotNull()
        {
            Random random = new Random();

            for (int i = 0; i < TestMultipleRuns; ++i)
            {
                bool testValue = random.NextBoolean();
                TimetableFileModel testSourceObject = new TimetableFileModel {
                    ExportOptions = new ExportOptionsModel {
                        DisplayLocoDiagramRow = testValue
                    }
                };

                TimetableDocument testResultObject = testSourceObject.ToTimetableDocument();

                Assert.AreEqual(testValue, testResultObject.ExportOptions.DisplayLocoDiagramRow);
            }
        }
Exemple #13
0
        public void TimetableDocumentExtensionsClass_ToTimetableFileModelMethod_ReturnsModelWithExportOptionsPropertyWithCorrectDisplayLocoDiagramRowValue()
        {
            Random random = new Random();

            for (int i = 0; i < TestMultipleRuns; ++i)
            {
                bool testValue             = random.NextBoolean();
                TimetableDocument document = new TimetableDocument {
                    ExportOptions = new DocumentExportOptions {
                        DisplayLocoDiagramRow = testValue
                    }
                };

                TimetableFileModel result = document.ToTimetableFileModel();

                Assert.AreEqual(testValue, result.ExportOptions.DisplayLocoDiagramRow);
            }
        }
        private void SetUpNewDocument(DocumentTemplate template)
        {
            TimetableDocument newDocument = new TimetableDocument();

            if (template != null)
            {
                newDocument.LocationList.Overwrite(template.Locations);
                newDocument.Options       = template.DocumentOptions;
                newDocument.ExportOptions = template.ExportOptions;
                newDocument.NoteDefinitions.Overwrite(template.NoteDefinitions);
                newDocument.TrainClassList.Overwrite(template.TrainClasses);
                newDocument.Signalboxes.Overwrite(template.Signalboxes);
            }
            Model = newDocument;
            UpdateTrainGraphLocationModel();
            UpdateFields();
            Model.UpdateTrainDisplays();
            UpdateSignalboxHours();
            _documentChanged = false;
        }
        private void OpenFile(string fn)
        {
            try
            {
                using (FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read))
                {
                    Model = Loader.LoadTimetableDocument(fs);
                }
            }
            catch (TimetableLoaderException ex)
            {
                LogHelper.LogWithMessageBox(Log, LogLevel.Error, ex, this, Resources.MainForm_FileOpen_Failure, ex.GetType().Name, ex.Message, ofdDocument.FileName);
            }

            Model.FileName = fn;
            UpdateFields();
            UpdateSignalboxHours();
            Model.DownTrainsDisplay.CheckCompulsaryLocationsAreVisible();
            Model.UpTrainsDisplay.CheckCompulsaryLocationsAreVisible();
            UpdateTrainGraphLocationModel();
            trainGraph.Model.SetPropertiesFromDocumentOptions(Model.Options);
            _documentChanged = false;
        }
        /// <summary>
        /// Convert a deserialized <see cref="SerialData.Xml.Legacy.V3.TimetableFileModel"/> instance to a <see cref="TimetableDocument"/> instance.
        /// </summary>
        /// <param name="file">The deserialized data to convert.</param>
        /// <returns>The data.</returns>
        public static TimetableDocument ToTimetableDocument(this SerialData.Xml.Legacy.V3.TimetableFileModel file)
        {
            if (file is null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            TimetableDocument document = new TimetableDocument
            {
                Version = file.Version,
                Options = file.Options != null?file.Options.ToDocumentOptions() : new DocumentOptions(),
                              ExportOptions                                               = file.ExportOptions != null?file.ExportOptions.ToDocumentExportOptions() : new DocumentExportOptions(),
                                                                         Title            = file.Title ?? string.Empty,
                                                                         Subtitle         = file.Subtitle ?? string.Empty,
                                                                         DateDescription  = file.DateDescription ?? string.Empty,
                                                                         WrittenBy        = file.WrittenBy ?? string.Empty,
                                                                         CheckedBy        = file.CheckedBy ?? string.Empty,
                                                                         TimetableVersion = file.TimetableVersion ?? string.Empty,
                                                                         PublishedDate    = file.PublishedDate ?? string.Empty,
            };

            if (file.Maps != null && file.Maps.Count > 0 && file.Maps[0] != null)
            {
                if (file.Maps[0].LocationList != null)
                {
                    foreach (LocationModel loc in file.Maps[0].LocationList)
                    {
                        document.LocationList.Add(loc.ToLocation());
                    }
                }
                if (file.Maps[0].Signalboxes != null)
                {
                    foreach (SignalboxModel box in file.Maps[0].Signalboxes)
                    {
                        document.Signalboxes.Add(box.ToSignalbox());
                    }
                }
            }

            if (file.NoteDefinitions != null)
            {
                foreach (NoteModel note in file.NoteDefinitions)
                {
                    document.NoteDefinitions.Add(note.ToNote());
                }
            }

            if (file.TrainClassList != null)
            {
                foreach (TrainClassModel tc in file.TrainClassList)
                {
                    document.TrainClassList.Add(tc.ToTrainClass());
                }
            }

            Dictionary <string, Location>   locationMap = document.LocationList.ToDictionary(o => o.Id);
            Dictionary <string, TrainClass> classMap    = document.TrainClassList.ToDictionary(c => c.Id);
            Dictionary <string, Note>       noteMap     = document.NoteDefinitions.ToDictionary(n => n.Id);

            if (file.TrainList != null)
            {
                foreach (TrainModel trn in file.TrainList)
                {
                    document.TrainList.Add(trn.ToTrain(locationMap, classMap, noteMap, document.Options));
                }
            }

            return(document);
        }
        /// <summary>
        /// Convert a <see cref="TimetableFileModel" /> instance to a <see cref="TimetableDocument" /> instance.
        /// </summary>
        /// <param name="model">The object to convert.</param>
        /// <returns>A <see cref="TimetableDocument" /> instance containing the same data as the parameter.</returns>
        /// <exception cref="NullReferenceException">Thrown if the parameter is <c>null</c>.</exception>
        public static TimetableDocument ToTimetableDocument(this TimetableFileModel model)
        {
            if (model is null)
            {
                throw new NullReferenceException();
            }

            Dictionary <string, Signalbox> signalboxes = new Dictionary <string, Signalbox>();

            Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_TimetableFileModelConversionVersion, model.Version);
            TimetableDocument document = new TimetableDocument
            {
                Version = model.Version,
                Options = model.FileOptions != null?model.FileOptions.ToDocumentOptions() : new DocumentOptions(),
                              ExportOptions                                               = model.ExportOptions != null?model.ExportOptions.ToDocumentExportOptions() : new DocumentExportOptions(),
                                                                         Title            = model.Title ?? string.Empty,
                                                                         Subtitle         = model.Subtitle ?? string.Empty,
                                                                         DateDescription  = model.DateDescription ?? string.Empty,
                                                                         WrittenBy        = model.WrittenBy ?? string.Empty,
                                                                         CheckedBy        = model.CheckedBy ?? string.Empty,
                                                                         TimetableVersion = model.TimetableVersion ?? string.Empty,
                                                                         PublishedDate    = model.PublishedDate ?? string.Empty,
            };

            if (model.Maps != null && model.Maps.Count > 0 && model.Maps[0] != null)
            {
                if (model.Maps[0].LocationList != null)
                {
                    Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_LocationCount, model.Maps[0].LocationList.Count);
                    foreach (LocationModel loc in model.Maps[0].LocationList)
                    {
                        document.LocationList.Add(loc.ToLocation());
                    }
                    Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_LocationsLoaded, document.LocationList.Count);
                }
                else
                {
                    Log.Trace("No locations to load.");
                }

                if (model.Maps[0].Signalboxes != null)
                {
                    Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_SignalboxCount, model.Maps[0].Signalboxes.Count);
                    foreach (SignalboxModel box in model.Maps[0].Signalboxes)
                    {
                        Signalbox b = box.ToSignalbox();
                        document.Signalboxes.Add(b);
                        if (!signalboxes.ContainsKey(b.Id))
                        {
                            signalboxes.Add(b.Id, b);
                        }
                    }
                    Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_SignalboxesLoaded, document.Signalboxes.Count);
                }
                else
                {
                    Log.Trace("No signalboxes to load");
                }
            }

            if (model.SignalboxHoursSets != null)
            {
                Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_SignalboxHoursSetCount, model.SignalboxHoursSets.Count);
                foreach (SignalboxHoursSetModel set in model.SignalboxHoursSets)
                {
                    document.SignalboxHoursSets.Add(set.ToSignalboxHoursSet(signalboxes, document.SignalboxHoursSets));
                }
                Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_SignalboxHoursSetsLoaded, document.SignalboxHoursSets.Count);
            }
            else
            {
                Log.Trace("No signalbox hours sets to load.");
            }

            if (model.NoteDefinitions != null)
            {
                Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_NoteDefinitionCount, model.NoteDefinitions.Count);
                foreach (NoteModel note in model.NoteDefinitions)
                {
                    document.NoteDefinitions.Add(note.ToNote());
                }
                Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_NoteDefinitionsLoaded, document.NoteDefinitions.Count);
            }
            else
            {
                Log.Trace("No note definitions to load.");
            }

            if (model.TrainClassList != null)
            {
                Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_TrainClassCount, model.TrainClassList.Count);
                foreach (TrainClassModel tc in model.TrainClassList)
                {
                    document.TrainClassList.Add(tc.ToTrainClass());
                }
                Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_TrainClassesLoaded, document.TrainClassList.Count);
            }
            else
            {
                Log.Trace("No train classes to load.");
            }

            Dictionary <string, Location>   locationMap = document.LocationList.ToDictionary(o => o.Id);
            Dictionary <string, TrainClass> classMap    = document.TrainClassList.ToDictionary(c => c.Id);
            Dictionary <string, Note>       noteMap     = document.NoteDefinitions.ToDictionary(n => n.Id);

            if (model.TrainList != null)
            {
                Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_TrainCount, model.TrainList.Count);
                foreach (TrainModel trn in model.TrainList)
                {
                    document.TrainList.Add(trn.ToTrain(locationMap, classMap, noteMap, document.Options));
                }
                Log.Trace(CultureInfo.CurrentCulture, Resources.LogMessage_TrainsLoaded, document.TrainList.Count);
            }
            else
            {
                Log.Trace("No trains to load.");
            }

            return(document);
        }