Esempio n. 1
0
        public virtual bool Read(IProgress <ProgressEventArgs> progress = null)
        {
            if (Exists)
            {
                try
                {
                    XmlReaderSettings settings = new XmlReaderSettings
                    {
                        IgnoreComments   = false,
                        IgnoreWhitespace = true,
                        CloseInput       = true
                    };
                    using (XmlReader reader = XmlReader.Create(FilePath))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(T));
                        Xml = (T)serializer.Deserialize(reader);
                    }
                }
                catch (Exception exception)
                {
                    var message = MessageFormat.GetWarning(exception);
                    Logs.Entry(message);
                    Report.Progress(progress, Report.Message("Message: '" + Logs.Message + "' Exception: '" + message + "'", MessageIcon.Error));
                }

                return(PostRead(progress));
            }
            else
            {
                var message = "Aborting deserialization. The file '" + FilePath + "' does not exist.";
                Logs.Entry(message);
                Report.Progress(progress, Report.Message(message, MessageIcon.Error));
                return(false);
            }
        }
Esempio n. 2
0
        public virtual bool Write(IProgress <ProgressEventArgs> progress = null)
        {
            bool success = false;

            try
            {
                XmlSerializer     serializer = new XmlSerializer(typeof(T));
                XmlWriterSettings settings   = new XmlWriterSettings
                {
                    Indent = true
                };

                using (var writer = XmlWriter.Create(FilePath, settings))
                {
                    serializer.Serialize(writer, Xml);
                    success = true;
                }
            }
            catch (Exception exception)
            {
                var message = MessageFormat.GetWarning(exception);
                Logs.Entry(message);
                Report.Progress(progress, Report.Message("Message: '" + Logs.Message + "' Exception: '" + message + "'", MessageIcon.Error));
                success = false;
            }
            PostWrite();
            return(success);
        }
Esempio n. 3
0
        public ZipArchiveEntry[] GetEntries()
        {
            if (File.Exists(FilePath))
            {
                try
                {
                    using (ZipArchive archive = ZipFile.Open(FilePath, ZipArchiveMode.Read))
                    {
                        foreach (ZipArchiveEntry entry in archive.Entries)
                        {
                            Trace.WriteLine("Name: " + entry.Name);
                            Trace.WriteLine("Size: " + entry.CompressedLength);
                            Trace.WriteLine("Last Write: " + entry.LastWriteTime);
                            Trace.WriteLine("FullName: " + entry.FullName);
                            Trace.WriteLine(String.Empty);
                        }
                        return(archive.Entries.ToArray());
                    }
                }
                catch (Exception exception)
                {
                    Logs.Entry(MessageFormat.GetWarning(exception));
                    throw;
                }
            }

            return(null);
        }
Esempio n. 4
0
        /// <summary>
        /// Exports the database to xml assets on disk.
        /// </summary>
        /// <param name="progress">The asynchronous progress reporter.</param>
        /// <returns>Returns true on successful completion.</returns>
        public bool Export(IProgress <ProgressEventArgs> progress = null)
        {
            bool success = false;

            try
            {
                using (var context = new EntityContext())
                {
                    var tables = context.GetTables();
                    foreach (var dbset in tables)
                    {
                        foreach (var entry in dbset)
                        {
                            if (entry is EntityType)
                            {
                                // TODO: exporting is not yet suported.

                                //var entity = entry as EntityType;
                                //entity.Meta.Asset.Write(progress);

                                throw new NotImplementedException("Exporting data stores is not yet suported.");
                            }
                        }
                    }
                }
                success = true;
            }
            catch (Exception exception)
            {
                Report.Progress(progress, Report.Message(MessageFormat.GetWarning(exception), MessageIcon.Error));
            }
            return(success);
        }
Esempio n. 5
0
        /// <summary>
        /// Imports assets from xml on disk into the database.
        /// </summary>
        /// <param name="progress">The asynchronous progress reporter.</param>
        /// <returns>Returns true on successful completion.</returns>
        public bool Import(IProgress <ProgressEventArgs> progress = null)
        {
            if (Modification == null)
            {
                Report.Progress(progress, Report.Message("Cannot import a null modification.", MessageIcon.Error));
                return(false);
            }

            bool success = false;

            try
            {
                Modification.Import(progress);
                Tables.ToList().ForEach(file => file.Import(progress));
                success = true;
            }
            catch (Exception exception)
            {
                Report.Progress(progress, Report.Message(MessageFormat.GetWarning(exception), MessageIcon.Error));
            }
            return(success);
        }
Esempio n. 6
0
        public bool Import(IProgress <ProgressEventArgs> progress = null)
        {
            // Reading the xml file was a success.
            // The xml file also contained viable elements to import.
            // Continue with trying to import these elements into the database context.
            Read(progress);

            if (!IsNull && Xml.Count > 0)
            {
                try
                {
                    using (var context = new EntityContext())
                    {
                        // Iterate over the datatable elements.
                        foreach (var kvp in Xml)
                        {
                            EntityType entity = kvp.Value;                             // for syntax

                            // Create meta data for this entity.
                            EntityTypeMeta meta = new EntityTypeMeta();
                            meta.TableStack.Push(Xml);
                            meta.Comments.Add("I updated my entity comment from " + Data.Index);

                            // Get the DbSet for the entities type and load it.
                            DbSet dbset = context.Set(entity.GetType());
                            dbset.Load();


                            if (dbset != null)                             // The database set was successfully retrieved.
                            {
                                Trace.WriteLine("Checking database for existing '" + entity.Name + "' entity.");
                                var found = dbset.Find(entity.Name);


                                if (found == null)                                 // No entity by this name already exists.
                                {
                                    // Set the entities meta data.
                                    meta.TableStack.Push(Xml);
                                    meta.Comments.Add("Created by " + FilePath);
                                    context.Entry(entity).Entity.SetMeta(meta);
                                    context.Entry(entity).State = EntityState.Added;
                                    context.SaveChanges();
                                    continue;
                                }
                                else if (found is EntityType)                                 // An entity with this name already exists!
                                {
                                    // modify the existing entity in the database.
                                    var existing     = found as EntityType;
                                    var existingMeta = existing.GetMeta();
                                    existingMeta.TableStack.Push(Xml);
                                    existingMeta.Comments.Add("Overriding an existing entity..");
                                    context.Entry(existing).State = EntityState.Modified;
                                    context.SaveChanges();

                                    var msg = "Dropped the entity '" + entity.Name + "' from " + Data.Index
                                              + " because it is overridden by an entity in " + existingMeta.GetTablePath();

                                    var arg = Report.Message(msg, MessageIcon.InformationB);
                                    Report.Progress(progress, arg);
                                    continue;
                                }
                                else
                                {
                                    Report.Progress(progress, Report.Message("Something unexpected happend. Ignored the entity '" + entity.Name + "'", MessageIcon.Warning));
                                    continue;
                                }
                            }
                            else
                            {
                                Report.Progress(progress, Report.Message("The DbSet for entity '" + entity.Name + "' count not be found.", MessageIcon.Warning));
                                continue;
                            }
                        }

                        context.SaveChanges();
                        return(true);
                    }
                }
                catch (DbUpdateException exception)
                {
                    var message1 = MessageFormat.GetWarning(exception);
                    var message2 = String.Empty;

                    if (exception.InnerException != null)
                    {
                        message2 = MessageFormat.GetWarning(exception.InnerException);
                    }
                    Report.Progress(progress, Report.Message("Entity Framework Exception: " + message1 + message2, MessageIcon.Error));
                }
                catch (Exception exception)
                {
                    var message = MessageFormat.GetWarning(exception);
                    Report.Progress(progress, Report.Message("Exception: '" + message + "', File Message: '" + Logs.Message + "'", MessageIcon.Error));
                }

                return(false);                // an exception was caught
            }
            else
            {
                Report.Progress(progress, Report.Message("The datatable was null or empty.", MessageIcon.Warning));
                return(false);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Allocates the files on disk for import.
        /// </summary>
        /// <param name="progress">The asynchronous progress reporter.</param>
        /// <returns>Returns true on successful completion.</returns>
        /// TODO: Allocation needs to invalidate previous allocations!
        /// TODO: Only allocate tables defined in the modifications runtime plugins.
        public bool Allocate(IProgress <ProgressEventArgs> progress = null)
        {
            Modification = null;
            try
            {
                var files = GetXmlFiles(SearchOption.TopDirectoryOnly);
                foreach (var file in files)
                {
                    var xml   = XDocument.Load(file.FullName);
                    int count = xml.Root.Descendants("RuntimeModule").Count();
                    if (count == 1)
                    {
                        Modification = new ModificationFile(this, file.FullName);
                        Report.Progress(progress, Report.Message("A modification index was allocated at " + Modification.FilePath, MessageIcon.CompleteB));
                        break;
                    }
                }
            }
            catch (Exception exception)
            {
                Report.Progress(progress, Report.Message(MessageFormat.GetWarning(exception), MessageIcon.Error));
            }

            try
            {
                if (Modification != null)
                {
                    Modification.Read(progress);                     // deserialize
                    if (Modification.Xml.FirstOrDefault().Value is RuntimeModule runtimeModule)
                    {
                        List <TableFile> allocated = new List <TableFile>();

                        foreach (var plugin in runtimeModule.Plugins)
                        {
                            if (plugin is AIPlugin aiPlugin)
                            {
                                Trace.WriteLine(string.Format("{0} has AI contributions. Plugin[Priority:{1}, Configuration:{2}, AssemblyPath:{3}]", runtimeModule.Name, aiPlugin.Priority, aiPlugin.Configuration, aiPlugin.AssemblyPath));
                            }
                            else if (plugin is RegistryPlugin registryPlugin)
                            {
                                Trace.WriteLine(string.Format("{0} has registry contributions. Plugin[Priority:{1}, FilePath:{2}]", runtimeModule.Name, registryPlugin.Priority, registryPlugin.FilePath));
                            }
                            else if (plugin is LocalizationPlugin localizationPlugin)
                            {
                                Trace.WriteLine(string.Format("{0} has localization contributions. Plugin[Priority:{1}, DefaultLanguage:{2}, Directory:{3}]", runtimeModule.Name, localizationPlugin.Priority, localizationPlugin.DefaultLanguage, localizationPlugin.Directory));
                            }
                            else if (plugin is DatabasePlugin databasePlugin)
                            {
                                Trace.WriteLine(string.Format("{0} has database contributions. Plugin[Priority:{1}, FilePath:{2}, DataType:{3}, ExtraTypes:{4}]", runtimeModule.Name, databasePlugin.Priority, databasePlugin.FilePath, databasePlugin.DataType, databasePlugin.ExtraTypes));

                                foreach (var pattern in databasePlugin.FilePath)
                                {
                                    string filePattern = pattern.Replace(Environment.NewLine, String.Empty).Replace("/", "\\").Trim();

                                    //string relDirectory = Path.GetDirectoryName(filePattern);
                                    string relFile = Path.GetFileName(filePattern);

                                    string fullPath          = Path.Combine(FolderPath, filePattern);
                                    string fullDirectoryPath = Path.GetDirectoryName(fullPath);

                                    if (Directory.Exists(fullDirectoryPath))
                                    {
                                        var files = Directory.GetFiles(fullDirectoryPath, relFile);
                                        foreach (var file in files)
                                        {
                                            TableFile asset = new TableFile(this, fullPath);
                                            allocated.Add(asset);
                                            Report.Progress(progress, Report.Message("Index, allocated file: " + asset.FilePath, MessageIcon.InformationB));
                                        }
                                    }
                                    else
                                    {
                                        Report.Progress(progress, Report.Message("Index, directory does not exist: " + fullDirectoryPath, MessageIcon.Warning));
                                    }
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException("Unexpected runtime plugin.");
                            }
                        }

                        allocated.Where(asset => Tables.Contains(asset) == false).ToList()
                        .ForEach(asset => Tables.Add(asset));
                    }

                    return(true);                    // COMPELTED
                }
                else
                {
                    Report.Progress(progress, Report.Message("A modification index containing a RuntimeModule could not be found.", MessageIcon.Error));
                    return(false);
                }
            }
            catch (Exception exception)
            {
                Report.Progress(progress, Report.Message(MessageFormat.GetWarning(exception), MessageIcon.Error));
            }

            throw new NotImplementedException("The 'Allocate' method is not implemented yet.");
        }