using Bootstrap.Security;
using Longbow.Data;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using System;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
///
///
///
public static class MongoDbAccessManager
{
private static IMongoDatabase _db = null;
private static bool _register = false;
///
///
///
public static IMongoDatabase DBAccess
{
get
{
if (_db == null)
{
if (!_register)
{
_register = true;
DbAdapterManager.RegisterConfigChangeCallback("MongoDB", InitDb);
}
InitDb();
InitClassMap();
}
return _db;
}
}
///
///
///
public static IMongoCollection Users
{
get
{
return DBAccess.GetCollection("Users");
}
}
///
///
///
public static IMongoCollection Users
{
get
{
return DBAccess.GetCollection("Users");
}
}
private static void InitDb()
{
var connectString = DbAdapterManager.GetConnectionString("ba");
if (string.IsNullOrEmpty(connectString)) throw new InvalidOperationException("Please set the BA default value in configuration file.");
var seq = connectString.Split(";", StringSplitOptions.RemoveEmptyEntries);
if (seq.Length != 2) return;
var client = new MongoClient(seq[0]);
_db = client.GetDatabase(seq[1].Split("=", StringSplitOptions.RemoveEmptyEntries).LastOrDefault());
}
private static void InitClassMap()
{
BsonSerializer.RegisterSerializer(DateTimeSerializer.LocalInstance);
if (!BsonClassMap.IsClassMapRegistered(typeof(BootstrapDict)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.User)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
md.UnmapMember(user => user.Checked);
md.UnmapMember(user => user.Period);
md.UnmapMember(user => user.NewPassword);
md.UnmapMember(user => user.UserStatus);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(BootstrapMenu)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Group)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
md.UnmapMember(group => group.Checked);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Role)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
md.UnmapMember(role => role.Checked);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Task)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Message)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Exceptions)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Log)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
}
}
}