using Bootstrap.Security;
using Longbow.Configuration;
using Longbow.Data;
using Microsoft.Extensions.Primitives;
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;
private static readonly object _locker = new object();
///
///
///
public static IMongoDatabase DBAccess
{
get
{
if (_db == null)
{
lock (_locker)
{
if (!_register)
{
_register = true;
ChangeToken.OnChange(() => ConfigurationManager.AppSettings.GetReloadToken(), () => _db = null);
InitClassMap();
}
InitDb();
}
}
return _db;
}
}
#region Collections
///
///
///
public static IMongoCollection Logs
{
get
{
return DBAccess.GetCollection("Logs");
}
}
///
///
///
public static IMongoCollection Exceptions
{
get
{
return DBAccess.GetCollection("Exceptions");
}
}
///
///
///
public static IMongoCollection Dicts
{
get
{
return DBAccess.GetCollection("Dicts");
}
}
///
///
///
public static IMongoCollection Users
{
get
{
return DBAccess.GetCollection("Users");
}
}
///
///
///
public static IMongoCollection Groups
{
get
{
return DBAccess.GetCollection("Groups");
}
}
///
///
///
public static IMongoCollection Roles
{
get
{
return DBAccess.GetCollection("Roles");
}
}
///
///
///
public static IMongoCollection Menus
{
get
{
return DBAccess.GetCollection("Navigations");
}
}
#endregion
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) throw new InvalidOperationException("ConnectionString invalid in configuration file. It should be mongodb://127.0.0.1:27017;Data Source=BootstrapAdmin");
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);
md.UnmapMember(m => m.CategoryName);
md.UnmapMember(m => m.Active);
md.UnmapMember(m => m.ParentName);
md.UnmapMember(m => m.Menus);
});
}
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);
md.UnmapMember(ex => ex.Period);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Log)))
{
BsonClassMap.RegisterClassMap(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
}
}
}