using EnVisage.Code.DAL; using EnVisage.Code.DAL.Mongo; using EnVisage.Models; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Web; namespace EnVisage.Code.BLL { class MongoDBBackupManager : ManagerBase { public override DbSet DataTable { get { return DbContext.supt_tbl_MongoDBBackup; } } public MongoDBBackupManager(EnVisageEntities dbContext) : base(dbContext) { } protected override supt_tbl_MongoDBBackup InitInstance() { return new supt_tbl_MongoDBBackup { id = Guid.NewGuid() }; } public override supt_tbl_MongoDBBackup Save(MongoDBBackupModel model) { if (model.Id != Guid.Empty) { var items2Delete = DbContext.supt_tbl_MongoDBBackup.Where(t => t.id == model.Id); foreach (var supt_tbl_MongoDBBackup in items2Delete) { DbContext.Entry(supt_tbl_MongoDBBackup).State = EntityState.Deleted; } } var obj = base.Save(model); return obj; } public void DoBackup(string userid, DateTime backupdate) { IDictionary> mongoData = getDataFromMongo(); foreach(string type in mongoData.Keys) { List mlist = mongoData[type]; foreach (string s in mlist) { MongoDBBackupModel m = new MongoDBBackupModel() { CollectionName = type, BackupData =s, BackupedBy = userid, BackupDate = backupdate }; supt_tbl_MongoDBBackup dboj= this.Save(m); string data = dboj.BackupData; } } } public void RestoreMongo(DateTime backupdate, string collectionName) { var mongoRecords = DbContext.supt_tbl_MongoDBBackup.Where(x => x.BackupDate == backupdate && (x.CollectionName == collectionName || string.IsNullOrEmpty(collectionName))); bool clearMix = true; bool clearExpAlloc = true; bool clearResAlloc = true; bool clearTeamAlloc = true; bool clearTrans = true; foreach (var mongoRecord in mongoRecords) { var mixManager = ResolveMixManager(DbProvider.MongoDb); var document = BsonSerializer.Deserialize(mongoRecord.BackupData); var collection = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS); if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_EXP_ALLOCATIONS) { if (clearExpAlloc) MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult(); collection = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS); clearExpAlloc = false; } if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_MIXES) { if (clearMix) MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_MIXES).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult(); collection = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_MIXES); clearMix = false; } if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS) { if (clearResAlloc) MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult(); collection = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS); clearResAlloc = false; } if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS) { if (clearTeamAlloc) MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult(); collection = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS); clearTeamAlloc = false; } if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_TRANSACTIONS) { if (clearTrans) MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_TRANSACTIONS).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult(); collection = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_TRANSACTIONS); clearTrans = false; } collection.InsertOneAsync(document).GetAwaiter().GetResult(); } } private IMixManager ResolveMixManager(DbProvider dbProvider) { switch (dbProvider) { //case DbProvider.SqlServer: // return new MixManager(DbContext); case DbProvider.MongoDb: return new MongoMixManager(DbContext, HttpContext.Current.User.Identity.GetID()); default: throw new InvalidOperationException(string.Format("DbProvider with value = {0} not defined", dbProvider.GetHashCode())); } } public void delete(string key) { char[] splprm = { '-' }; string[] parts = key.Split(splprm); DateTime dts = DateTime.Parse(parts[0]); string userid = parts[1]; var items2Delete = DbContext.supt_tbl_MongoDBBackup.Where(t => t.BackupDate == dts && t.BackupedBy == userid); foreach (var supt_tbl_MongoDBBackup in items2Delete) { DbContext.Entry(supt_tbl_MongoDBBackup).State = EntityState.Deleted; } } protected override supt_tbl_MongoDBBackup RetrieveReadOnlyById(Guid key) { return DataTable.AsNoTracking().FirstOrDefault(t => t.id == key); } public IDictionary GetAllBackupDates() { var resources = new Dictionary(); foreach (var x in DataTable.Select(x=> new { x.BackupDate, x.BackupedBy }).Distinct()) { string key = x.BackupDate.ToString("mm/dd/yyyy hh:MM:ss") + "-" + x.BackupedBy; resources.Add(key, x.BackupDate); } return resources; } public IList GetRecordsByDate(DateTime dts) { var resources = new List(); foreach (var x in DataTable.Where(x => x.BackupDate == dts)) { resources.Add(new MongoDBBackupModel() { Id = x.id, BackupDate=x.BackupDate, BackupedBy=x.BackupedBy, BackupData=x.BackupData, CollectionName=x.CollectionName }); } return resources; } private IDictionary> getDataFromMongo() { // .GetAwaiter().GetResult(); Dictionary> data = new Dictionary>(); var exp_allocations = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS); var mixes = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_MIXES); var res_alliocations = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS); var team_allocations = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS); var trans = MongoDataContext.GetCollection(MongoDataContext.COLL_NAME_TRANSACTIONS); var filter = new BsonDocument(); var count = 0; List expList = new List(); using (var cursor = exp_allocations.FindAsync(filter).GetAwaiter().GetResult()) { while (cursor.MoveNextAsync().GetAwaiter().GetResult()) { var batch = cursor.Current; foreach (var document in batch) { string d = document.ToJson(); expList.Add(d); count++; } } } List mixList = new List(); using (var cursor = mixes.FindAsync(filter).GetAwaiter().GetResult()) { while (cursor.MoveNextAsync().GetAwaiter().GetResult()) { var batch = cursor.Current; foreach (var document in batch) { string d = document.ToJson(); mixList.Add(d); count++; } } } List resList = new List(); using (var cursor = res_alliocations.FindAsync(filter).GetAwaiter().GetResult()) { while (cursor.MoveNextAsync().GetAwaiter().GetResult()) { var batch = cursor.Current; foreach (var document in batch) { string d = document.ToJson(); resList.Add(d); count++; } } } List teamList = new List(); using (var cursor = team_allocations.FindAsync(filter).GetAwaiter().GetResult()) { while (cursor.MoveNextAsync().GetAwaiter().GetResult()) { var batch = cursor.Current; foreach (var document in batch) { string d = document.ToJson(); teamList.Add(d); count++; } } } List tranList = new List(); using (var cursor = trans.FindAsync(filter).GetAwaiter().GetResult()) { while (cursor.MoveNextAsync().GetAwaiter().GetResult()) { var batch = cursor.Current; foreach (var document in batch) { string d = document.ToJson(); tranList.Add(d); count++; } } } data.Add(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS, expList); data.Add(MongoDataContext.COLL_NAME_MIXES, mixList); data.Add(MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS, resList); data.Add(MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS, teamList); data.Add(MongoDataContext.COLL_NAME_TRANSACTIONS, tranList); return data; } } }