Example #1
0
        public static ISessionFactory CreateSessionFactory()
        {
            var currentGuiVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);

            var newdatabase = false;
            var root = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"Bumberdash");
            if (!Directory.Exists(root))
                Directory.CreateDirectory(root);
            var dataFile = Path.Combine(root, "Bumbershoot " + currentGuiVersion + ".db");
            var newfactory = CreateSessionFactory(dataFile, true);
            var session = newfactory.OpenSession();

            //check that all preloaded templates are present
            if (session.QueryOver<ConfigFile>().List().Count<16)
            {
                //check for presence of completely empty database
                if (session.QueryOver<HistoryItem>().List().Count == 0 &&
                    session.QueryOver<ConfigFile>().List().Count == 0)
                    newdatabase = true;

                //load base database
                var baseRoot = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                if (baseRoot == null)
                    throw new Exception("Cannot find base database file");
                if (!File.Exists(Path.Combine(baseRoot, "lib\\Bumbershoot.db")))
                    throw new Exception("Looking for \"" + Path.Combine(baseRoot, "lib\\Bumbershoot.db") + "\", however I cant find it");
                var baseFactory = CreateSessionFactory(Path.Combine(baseRoot, "lib\\Bumbershoot.db"), false);
                var baseSession = baseFactory.OpenSession();

                //load base templates
                var connectedBaseConfigs = baseSession.QueryOver<ConfigFile>().List();
                var baseConfigs = new List<ConfigFile>();
                foreach (var config in connectedBaseConfigs)
                {
                    var newInstrument = new ConfigFile
                                            {
                                                Name = config.Name,
                                                DestinationProgram = config.DestinationProgram,
                                                FilePath = config.FilePath,
                                                PropertyList = new List<ConfigProperty>()
                                            };
                    foreach (var property in config.PropertyList)
                    {
                        var newProperty = new ConfigProperty
                                              {
                                                  ConfigAssociation = newInstrument,
                                                  Name = property.Name,
                                                  Type = property.Type,
                                                  Value = property.Value
                                              };
                        newInstrument.PropertyList.Add(newProperty);
                    }
                    baseConfigs.Add(newInstrument);
                }

                //delete old templates (if any remain) and load base template
                foreach (var config in baseConfigs)
                {
                    ConfigFile config1 = config;
                    var deleteList = session.QueryOver<ConfigFile>()
                        .Where(x => x.Name == config1.Name &&
                                    x.DestinationProgram == config1.DestinationProgram)
                        .List();
                    foreach (var item in deleteList)
                        session.Delete(item);
                    session.Flush();
                    var newInstrument = new ConfigFile()
                                          {
                                              Name = config.Name,
                                              DestinationProgram = config.DestinationProgram,
                                              FilePath = config.FilePath
                                          };
                    session.SaveOrUpdate(newInstrument);
                    session.Flush();
                    foreach (var property in config.PropertyList)
                    {
                        var newProperty = new ConfigProperty()
                                              {
                                                  ConfigAssociation = newInstrument,
                                                  Name = property.Name,
                                                  Type = property.Type,
                                                  Value = property.Value
                                              };
                        session.SaveOrUpdate(newProperty);
                    }
                    session.Flush();
                }

                //automatically try to recover old history items if database is flagged as new
                if (newdatabase)
                {
                    //if database structure is ever changed this will need to be updated to include conversion (probably in the form of basic sqlite queries)
                    var directoryInfo = new DirectoryInfo(root);
                    var bdFiles = directoryInfo.GetFiles("Bumbershoot*.db").Select(file => file.Name).ToList();
                    bdFiles.Sort();

                    if (bdFiles.Count > 1)
                    {
                        //reorder to proper location
                        if (bdFiles[bdFiles.Count - 1] == "Bumbershoot.db")
                        {
                            bdFiles.RemoveAt(bdFiles.Count - 1);
                            bdFiles.Insert(0, "Bumbershoot.db");
                        }

                        var restorePath = Path.Combine(root, bdFiles[bdFiles.Count - 2]);
                        var restoreFactory = CreateSessionFactory(restorePath, true);
                        var restoreSession = restoreFactory.OpenSession();
                        var restoreJobs = restoreSession.QueryOver<HistoryItem>().List<HistoryItem>();
                        var restoredConfigs = new Dictionary<int,ConfigFile>();

                        //start restoring jobs
                        foreach (var job in restoreJobs)
                        {
                            var newjob = new HistoryItem
                                             {
                                                 Cpus = job.Cpus, CurrentStatus = job.CurrentStatus,
                                                 EndTime = job.EndTime, JobName = job.JobName,
                                                 JobType = job.JobType, OutputDirectory = job.OutputDirectory,
                                                 ProteinDatabase = job.ProteinDatabase, RowNumber = job.RowNumber,
                                                 SpectralLibrary = job.SpectralLibrary, StartTime = job.StartTime
                                             };
                            if (!restoredConfigs.ContainsKey(job.InitialConfigFile.ConfigId))
                            {
                                var newConfig = new ConfigFile
                                                    {
                                                        DestinationProgram = job.InitialConfigFile.DestinationProgram,
                                                        FilePath = job.InitialConfigFile.FilePath,
                                                        FirstUsedDate = job.InitialConfigFile.FirstUsedDate,
                                                        Name = job.InitialConfigFile.Name
                                                    };
                                session.SaveOrUpdate(newConfig);
                                session.Flush();
                                foreach (var property in job.InitialConfigFile.PropertyList)
                                {
                                    var newProperty = new ConfigProperty
                                                          {
                                                              Name = property.Name,
                                                              ConfigAssociation = newConfig,
                                                              Type = property.Type,
                                                              Value = property.Value
                                                          };
                                    session.SaveOrUpdate(newProperty);
                                }
                                session.Flush();
                                newjob.InitialConfigFile = newConfig;
                                restoredConfigs.Add(job.InitialConfigFile.ConfigId, newConfig);
                            }
                            else
                                newjob.InitialConfigFile = restoredConfigs[job.InitialConfigFile.ConfigId];

                            if (job.TagConfigFile != null)
                            {
                                if (!restoredConfigs.ContainsKey(job.TagConfigFile.ConfigId))
                                {
                                    var newConfig = new ConfigFile
                                                        {
                                                            DestinationProgram = job.TagConfigFile.DestinationProgram,
                                                            FilePath = job.TagConfigFile.FilePath,
                                                            FirstUsedDate = job.TagConfigFile.FirstUsedDate,
                                                            Name = job.TagConfigFile.Name
                                                        };
                                    session.SaveOrUpdate(newConfig);
                                    session.Flush();
                                    foreach (var property in job.TagConfigFile.PropertyList)
                                    {
                                        var newProperty = new ConfigProperty
                                                              {
                                                                  Name = property.Name,
                                                                  ConfigAssociation = newConfig,
                                                                  Type = property.Type,
                                                                  Value = property.Value
                                                              };
                                        session.SaveOrUpdate(newProperty);
                                    }
                                    session.Flush();
                                    newjob.TagConfigFile = newConfig;
                                    restoredConfigs.Add(job.TagConfigFile.ConfigId,newConfig);
                                }
                                else
                                    newjob.TagConfigFile = restoredConfigs[job.TagConfigFile.ConfigId];
                            }
                            session.SaveOrUpdate(newjob);
                            session.Flush();
                            foreach (var file in job.FileList)
                            {
                                var newFile = new InputFile {FilePath = file.FilePath, HistoryItem = newjob};
                                session.SaveOrUpdate(newFile);
                                session.Flush();
                            }
                        }
                    }
                }
            }
            session.Close();
            return newfactory;
        }
Example #2
0
        public static ISessionFactory CreateSessionFactory()
        {
            var currentGuiVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);

            var newdatabase = false;
            var root        = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Bumberdash");

            if (!Directory.Exists(root))
            {
                Directory.CreateDirectory(root);
            }
            var dataFile   = Path.Combine(root, "Bumbershoot " + currentGuiVersion + ".db");
            var newfactory = CreateSessionFactory(dataFile, true);
            var session    = newfactory.OpenSession();

            //check that all preloaded templates are present
            if (session.QueryOver <ConfigFile>().List().Count < 16)
            {
                //check for presence of completely empty database
                if (session.QueryOver <HistoryItem>().List().Count == 0 &&
                    session.QueryOver <ConfigFile>().List().Count == 0)
                {
                    newdatabase = true;
                }

                //load base database
                var baseRoot = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                if (baseRoot == null)
                {
                    throw new Exception("Cannot find base database file");
                }
                if (!File.Exists(Path.Combine(baseRoot, "lib\\Bumbershoot.db")))
                {
                    throw new Exception("Looking for \"" + Path.Combine(baseRoot, "lib\\Bumbershoot.db") + "\", however I cant find it");
                }
                var baseFactory = CreateSessionFactory(Path.Combine(baseRoot, "lib\\Bumbershoot.db"), false);
                var baseSession = baseFactory.OpenSession();

                //load base templates
                var connectedBaseConfigs = baseSession.QueryOver <ConfigFile>().List();
                var baseConfigs          = new List <ConfigFile>();
                foreach (var config in connectedBaseConfigs)
                {
                    var newInstrument = new ConfigFile
                    {
                        Name = config.Name,
                        DestinationProgram = config.DestinationProgram,
                        FilePath           = config.FilePath,
                        PropertyList       = new List <ConfigProperty>()
                    };
                    foreach (var property in config.PropertyList)
                    {
                        var newProperty = new ConfigProperty
                        {
                            ConfigAssociation = newInstrument,
                            Name  = property.Name,
                            Type  = property.Type,
                            Value = property.Value
                        };
                        newInstrument.PropertyList.Add(newProperty);
                    }
                    baseConfigs.Add(newInstrument);
                }

                //delete old templates (if any remain) and load base template
                foreach (var config in baseConfigs)
                {
                    ConfigFile config1    = config;
                    var        deleteList = session.QueryOver <ConfigFile>()
                                            .Where(x => x.Name == config1.Name &&
                                                   x.DestinationProgram == config1.DestinationProgram)
                                            .List();
                    foreach (var item in deleteList)
                    {
                        session.Delete(item);
                    }
                    session.Flush();
                    var newInstrument = new ConfigFile()
                    {
                        Name = config.Name,
                        DestinationProgram = config.DestinationProgram,
                        FilePath           = config.FilePath
                    };
                    session.SaveOrUpdate(newInstrument);
                    session.Flush();
                    foreach (var property in config.PropertyList)
                    {
                        var newProperty = new ConfigProperty()
                        {
                            ConfigAssociation = newInstrument,
                            Name  = property.Name,
                            Type  = property.Type,
                            Value = property.Value
                        };
                        session.SaveOrUpdate(newProperty);
                    }
                    session.Flush();
                }

                //automatically try to recover old history items if database is flagged as new
                if (newdatabase)
                {
                    //if database structure is ever changed this will need to be updated to include conversion (probably in the form of basic sqlite queries)
                    var directoryInfo = new DirectoryInfo(root);
                    var bdFiles       = directoryInfo.GetFiles("Bumbershoot*.db").Select(file => file.Name).ToList();
                    bdFiles.Sort();

                    if (bdFiles.Count > 1)
                    {
                        //reorder to proper location
                        if (bdFiles[bdFiles.Count - 1] == "Bumbershoot.db")
                        {
                            bdFiles.RemoveAt(bdFiles.Count - 1);
                            bdFiles.Insert(0, "Bumbershoot.db");
                        }

                        var restorePath     = Path.Combine(root, bdFiles[bdFiles.Count - 2]);
                        var restoreFactory  = CreateSessionFactory(restorePath, true);
                        var restoreSession  = restoreFactory.OpenSession();
                        var restoreJobs     = restoreSession.QueryOver <HistoryItem>().List <HistoryItem>();
                        var restoredConfigs = new Dictionary <int, ConfigFile>();

                        //start restoring jobs
                        foreach (var job in restoreJobs)
                        {
                            var newjob = new HistoryItem
                            {
                                Cpus            = job.Cpus, CurrentStatus = job.CurrentStatus,
                                EndTime         = job.EndTime, JobName = job.JobName,
                                JobType         = job.JobType, OutputDirectory = job.OutputDirectory,
                                ProteinDatabase = job.ProteinDatabase, RowNumber = job.RowNumber,
                                SpectralLibrary = job.SpectralLibrary, StartTime = job.StartTime
                            };
                            if (!restoredConfigs.ContainsKey(job.InitialConfigFile.ConfigId))
                            {
                                var newConfig = new ConfigFile
                                {
                                    DestinationProgram = job.InitialConfigFile.DestinationProgram,
                                    FilePath           = job.InitialConfigFile.FilePath,
                                    FirstUsedDate      = job.InitialConfigFile.FirstUsedDate,
                                    Name = job.InitialConfigFile.Name
                                };
                                session.SaveOrUpdate(newConfig);
                                session.Flush();
                                foreach (var property in job.InitialConfigFile.PropertyList)
                                {
                                    var newProperty = new ConfigProperty
                                    {
                                        Name = property.Name,
                                        ConfigAssociation = newConfig,
                                        Type  = property.Type,
                                        Value = property.Value
                                    };
                                    session.SaveOrUpdate(newProperty);
                                }
                                session.Flush();
                                newjob.InitialConfigFile = newConfig;
                                restoredConfigs.Add(job.InitialConfigFile.ConfigId, newConfig);
                            }
                            else
                            {
                                newjob.InitialConfigFile = restoredConfigs[job.InitialConfigFile.ConfigId];
                            }

                            if (job.TagConfigFile != null)
                            {
                                if (!restoredConfigs.ContainsKey(job.TagConfigFile.ConfigId))
                                {
                                    var newConfig = new ConfigFile
                                    {
                                        DestinationProgram = job.TagConfigFile.DestinationProgram,
                                        FilePath           = job.TagConfigFile.FilePath,
                                        FirstUsedDate      = job.TagConfigFile.FirstUsedDate,
                                        Name = job.TagConfigFile.Name
                                    };
                                    session.SaveOrUpdate(newConfig);
                                    session.Flush();
                                    foreach (var property in job.TagConfigFile.PropertyList)
                                    {
                                        var newProperty = new ConfigProperty
                                        {
                                            Name = property.Name,
                                            ConfigAssociation = newConfig,
                                            Type  = property.Type,
                                            Value = property.Value
                                        };
                                        session.SaveOrUpdate(newProperty);
                                    }
                                    session.Flush();
                                    newjob.TagConfigFile = newConfig;
                                    restoredConfigs.Add(job.TagConfigFile.ConfigId, newConfig);
                                }
                                else
                                {
                                    newjob.TagConfigFile = restoredConfigs[job.TagConfigFile.ConfigId];
                                }
                            }
                            session.SaveOrUpdate(newjob);
                            session.Flush();
                            foreach (var file in job.FileList)
                            {
                                var newFile = new InputFile {
                                    FilePath = file.FilePath, HistoryItem = newjob
                                };
                                session.SaveOrUpdate(newFile);
                                session.Flush();
                            }
                        }
                    }
                }
            }
            session.Close();
            return(newfactory);
        }