Exemplo n.º 1
0
        public void InstallDefaultDatabase()
        {
            try
            {  // Prepare database
                var adr = Properties.Settings.Default.MongoServerSettings;
                var db  = Properties.Settings.Default.dbUrl;

                bool         addDefault      = false;
                DbSourceLink DefaultDbSource = new DbSourceLink();
                // CHeck if DefaultDB exist
                var dblinks = DS.db.GetAll <DbSourceLink>(a => true) as IEnumerable <DbSourceLink>;
                if (dblinks != null)
                {
                    if (!dblinks.Select(a => a.DbName).Contains("Default"))
                    {
                        DefaultDbSource          = new DbSourceLink();
                        DefaultDbSource.DbName   = "Default";
                        DefaultDbSource.SourceIp = adr;
                        addDefault = true;
                    }
                }
                setting = ElvaSettings.getInstance();
                if (setting != null)
                {
                    if (addDefault)
                    {
                        DefaultDbSource.Save();
                    }

                    if (setting.DbSourceLink.IsValide())
                    {
                        // i have db set
                        var DbSource = DS.db.GetOne <DbSourceLink>(a => a.Id == setting.DbSourceLink) as DbSourceLink;
                        if (DbSource != null)
                        {
                            Properties.Settings.Default.MongoServerSettings = DbSource.SourceIp;
                            Properties.Settings.Default.dbUrl = DbSource.DbName;
                            DataHelpers.DbAdresse             = DbSource.SourceIp;
                            DataHelpers.DbName = DbSource.DbName;
                            DS._db             = null;
                        }
                        else
                        {
                            SetDefaultDB();
                        }
                    }
                    else
                    {
                        SetDefaultDB();
                    }
                }
                else
                {
                    SetDefaultDB();
                }
            }
            catch (Exception s)
            {
                SetError(s.Message);
                Console.Write(s.Message);
                SetDefaultDB();
            }
        }
Exemplo n.º 2
0
        public static void SetupDb()
        {
            try
            {
                // Prepare database
                var adr = Properties.Settings.Default.MongoServerSettings;
                var db  = Properties.Settings.Default.dbUrl;

                #region START LOCAL DB

                // Check Connection
                //var cmd = $"dbs/MongoDBPortable.exe";
                //Process.Start(Path.GetFullPath(cmd));

                var lockFiles = "dbs/data";
                var fls       = Directory.EnumerateFiles(lockFiles);

                var locks = fls.Where(a => a.Contains("lock"));
                foreach (var item in locks)
                {
                    try
                    {
                        File.Delete(Path.GetFullPath(item));
                    }
                    catch (Exception s)
                    {
                        Console.Write(s.Message);
                    }
                }

                var dbfolder = Path.GetFullPath("dbs/data");

                //starting the mongod server (when app starts)
                ProcessStartInfo start = new ProcessStartInfo();
                start.FileName    = @"dbs/bin/mongod.exe";
                start.WindowStyle = ProcessWindowStyle.Hidden;
                // set UseShellExecute to false
                start.UseShellExecute = false;

                //@"" prevents need for backslashes
                //var dbPaht = Path.GetFullPath("dbs/db");
                start.Arguments = $"--dbpath=\"{dbfolder}\" --storageEngine=mmapv1 --bind_ip=0.0.0.0";

                DataHelpers.mongod = Process.Start(start);



                // RESTHEART

                //starting the mongod server (when app starts)
                ProcessStartInfo startRestHeart = new ProcessStartInfo();
                startRestHeart.FileName    = @"java";
                startRestHeart.WindowStyle = ProcessWindowStyle.Hidden;
                // set UseShellExecute to false
                startRestHeart.UseShellExecute = false;

                //@"" prevents need for backslashes
                //var dbPaht = Path.GetFullPath("dbs/db");
                startRestHeart.Arguments = $"-jar restheart/restheart.jar";

                DataHelpers.restheart = Process.Start(startRestHeart);



                #endregion



                // i have one instance settings with one adresse
                // Check if db source exist
                try
                {
                    bool         addDefault      = false;
                    DbSourceLink DefaultDbSource = new DbSourceLink();
                    // CHeck if DefaultDB exist
                    var dblinks = DataHelpers.GetMongoDataSync("DbSourceLink") as IEnumerable <DbSourceLink>;
                    if (dblinks != null)
                    {
                        if (!dblinks.Select(a => a.DbName).Contains("Default"))
                        {
                            DefaultDbSource          = new DbSourceLink();
                            DefaultDbSource.DbName   = "Default";
                            DefaultDbSource.SourceIp = adr;
                            addDefault = true;
                        }
                    }
                    var setting = (new ElvaSettings()).getInstance();
                    if (setting != null)
                    {
                        if (addDefault)
                        {
                            DefaultDbSource.Save();
                        }

                        if (setting.DbSourceLink != null && setting.DbSourceLink != ObjectId.Empty)
                        {
                            // i have db set
                            var DbSource = setting.DbSourceLink.GetObject("DbSourceLink") as DbSourceLink;
                            if (DbSource != null)
                            {
                                Properties.Settings.Default.MongoServerSettings = DbSource.SourceIp;
                                Properties.Settings.Default.dbUrl = DbSource.DbName;
                                DataHelpers.DbAdresse             = DbSource.SourceIp;
                                DataHelpers.DbName = DbSource.DbName;
                            }
                            else
                            {
                                SetDefaultDB();
                            }
                        }
                        else
                        {
                            SetDefaultDB();
                        }
                    }
                    else
                    {
                        SetDefaultDB();
                    }
                }
                catch (Exception s)
                {
                    Console.Write(s.Message);
                    SetDefaultDB();
                }



                // StartLocal Db if local;
            }
            catch (Exception s)
            {
                MessageBox.Show(s.Message);
            }
        }