EnVisageOnline/Main/Source/EnVisage/Code/BLL/MongoDBBackupManager.cs

271 lines
12 KiB
C#

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<supt_tbl_MongoDBBackup, MongoDBBackupModel>
{
public override DbSet<supt_tbl_MongoDBBackup> 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<string, List<string>> mongoData = getDataFromMongo();
foreach(string type in mongoData.Keys)
{
List<string> 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<BsonDocument>(mongoRecord.BackupData);
var collection = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS);
if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_EXP_ALLOCATIONS) {
if (clearExpAlloc)
MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult();
collection = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS);
clearExpAlloc = false;
}
if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_MIXES)
{
if (clearMix)
MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_MIXES).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult();
collection = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_MIXES);
clearMix = false;
}
if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS)
{
if (clearResAlloc)
MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult();
collection = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS);
clearResAlloc = false;
}
if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS)
{
if (clearTeamAlloc)
MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult();
collection = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS);
clearTeamAlloc = false;
}
if (mongoRecord.CollectionName == MongoDataContext.COLL_NAME_TRANSACTIONS)
{
if (clearTrans)
MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_TRANSACTIONS).DeleteManyAsync((new BsonDocument())).GetAwaiter().GetResult();
collection = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_TRANSACTIONS);
clearTrans = false;
}
collection.InsertOneAsync(document).GetAwaiter().GetResult();
}
}
private IMixManager<Code.DAL.Mongo.Mix> 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<string,DateTime> GetAllBackupDates()
{
var resources = new Dictionary<string, DateTime>();
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<MongoDBBackupModel> GetRecordsByDate(DateTime dts)
{
var resources = new List<MongoDBBackupModel>();
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<string, List<string>> getDataFromMongo()
{
// .GetAwaiter().GetResult();
Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
var exp_allocations = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_EXP_ALLOCATIONS);
var mixes = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_MIXES);
var res_alliocations = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_RESOURCE_ALLOCATIONS);
var team_allocations = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_TEAM_ALLOCATIONS);
var trans = MongoDataContext.GetCollection<BsonDocument>(MongoDataContext.COLL_NAME_TRANSACTIONS);
var filter = new BsonDocument();
var count = 0;
List<string> expList = new List<string>();
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<string> mixList = new List<string>();
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<string> resList = new List<string>();
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<string> teamList = new List<string>();
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<string> tranList = new List<string>();
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;
}
}
}