Skip to content

Commit

Permalink
added keypair dictionary support
Browse files Browse the repository at this point in the history
  • Loading branch information
zmn committed Apr 8, 2017
1 parent 60a3de0 commit b7d4369
Show file tree
Hide file tree
Showing 21 changed files with 913 additions and 20 deletions.
6 changes: 5 additions & 1 deletion Wlog.Library/BLL/Classes/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ namespace Wlog.BLL.Classes
{
public static class Constants
{
public static class Roles
public static class DictionaryNames
{
public const string Main = "MAIN";
}
public static class Roles
{
public const string Admin = "ADMIN";
public const string WriteLog = "WRITELOG";
Expand Down
4 changes: 2 additions & 2 deletions Wlog.Library/BLL/Classes/EntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public virtual IPagedList<T> Find(Expression<Func<T, bool>> where, int pageNumbe
int rowCount = count.Count();
List<T> result = query.ToList();

return new StaticPagedList<T>(result, pageNumber, pageSize, rowCount);
return new StaticPagedList<T>(result, pageNumber+1, pageSize, rowCount);
}


Expand Down Expand Up @@ -165,7 +165,7 @@ private static IQueryable<T> GetQuery(Expression<Func<T, bool>> where, int start

if (numberOfRow > 0)
{
query = query.Skip(numberOfRow);
query = query.Take(numberOfRow);
}

if (sortField != null && sordDirection == SortDirection.ASC)
Expand Down
17 changes: 17 additions & 0 deletions Wlog.Library/BLL/Entities/DictionaryEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wlog.Library.BLL.Interfaces;

namespace Wlog.BLL.Entities
{

public class DictionaryEntity : IEntityBase
{
public virtual string Name { get; set; }
public virtual Guid ApplicationId { get; set; }

}
}
16 changes: 16 additions & 0 deletions Wlog.Library/BLL/Entities/KeyPairEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wlog.Library.BLL.Interfaces;

namespace Wlog.BLL.Entities
{
public class KeyPairEntity : IEntityBase
{
public virtual string ItemKey { get; set; }
public virtual string ItemValue { get; set; }
public virtual Guid DictionaryId { get; set; }
}
}
143 changes: 143 additions & 0 deletions Wlog.Library/BLL/Reporitories/DBKeyPairRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PagedList;
using Wlog.BLL.Entities;
using Wlog.Library.BLL.Classes;
using Wlog.Library.BLL.Reporitories.Interfaces;

namespace Wlog.Library.BLL.Reporitories
{
public class DBKeyPairRepository : EntityRepository<KeyPairEntity>, IKeyPairRepository
{
public DictionaryEntity CreateDictionary(DictionaryEntity d)
{
using (var op = this.BeginUnitOfWork())
{
op.SaveOrUpdate(d);
op.Commit();
return d;
}
}

public string GetByKey(Guid dictionaryId, string key)
{
using (var op = this.BeginUnitOfWork())
{
var item = op.Query<KeyPairEntity>().Where(x => x.DictionaryId.CompareTo(dictionaryId) == 0 && x.ItemKey.Equals(key)).FirstOrDefault();
if (item == null) return null;
return item.ItemValue;
}
}

public IPagedList<DictionaryEntity> GetDictionaries(Guid applicationId, string dictionaryName, int start, int count)
{
using (var op = this.BeginUnitOfWork())
{
var query = op.Query<DictionaryEntity>();
if (applicationId != null)
{
query = query.Where(x => x.ApplicationId == applicationId);
}

if (dictionaryName != null)
{
query = query.Where(x => x.Name.Contains(dictionaryName));
}

if (start > 0)
{
query = query.Skip(start);
}


int total = query.Count();

if (count > 0)
{
query = query.Take(count);
}

var items = query.ToList();
var page = 1;
if (start > 0 && count > 0)
{
page = (start / count)+1;
}
if (count == 0) count = int.MaxValue;
return new StaticPagedList<DictionaryEntity>(items, page, count, total);
}
}

public KeyPairEntity Save(Guid dictionaryId, string key, string value)
{



using (var op = this.BeginUnitOfWork())
{
op.BeginTransaction();

var kpe = op.Query<KeyPairEntity>().Where(x => x.ItemKey == key && x.DictionaryId == dictionaryId).FirstOrDefault();
if (kpe == null)
{
kpe = new KeyPairEntity()
{
DictionaryId = dictionaryId,

};
}

kpe.ItemKey = key;
kpe.ItemValue = value;

op.SaveOrUpdate(kpe);
op.Commit();
return kpe;
}

}

public IPagedList<KeyPairEntity> Search(Guid dictionaryId, string key, int start, int count)
{
using (var op = this.BeginUnitOfWork())
{
var query = op.Query<KeyPairEntity>();
if (dictionaryId != null)
{
query = query.Where(x => x.DictionaryId == dictionaryId);
}

if (key != null)
{
query = query.Where(x => x.ItemKey.Contains(key));
}

int total = query.Count();


if (start > 0)
{
query = query.Skip(start);
}


if (count > 0)
{
query = query.Take(count);
}

var items = query.ToList();
var page = 1;
if (start > 0 && count > 0)
{
page = (start / count) + 1;
}
if (count == 0) count = int.MaxValue;
return new StaticPagedList<KeyPairEntity>(items, page, count, total);
}
}
}
}
1 change: 1 addition & 0 deletions Wlog.Library/BLL/Reporitories/DeletedLogRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Interfaces;
using NLog;
using System.Diagnostics;
using BLL.Interfaces;

/// <summary>
/// Repository to store deleted logs
Expand Down
28 changes: 28 additions & 0 deletions Wlog.Library/BLL/Reporitories/Interfaces/IKeyPairRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PagedList;
using Wlog.BLL.Entities;

namespace Wlog.Library.BLL.Reporitories.Interfaces
{
public interface IKeyPairRepository
{
string GetByKey(Guid dictionaryId,string key);

KeyPairEntity GetById(Guid itemId);

KeyPairEntity Save(Guid dictionaryId, string key, string value);


IPagedList<KeyPairEntity> Search(Guid dictionaryId, string key, int start, int count);



IPagedList<DictionaryEntity> GetDictionaries(Guid id, string dictionaryName, int start, int count);
DictionaryEntity CreateDictionary(DictionaryEntity d);
bool Delete(KeyPairEntity value);
}
}
4 changes: 4 additions & 0 deletions Wlog.Library/BLL/Reporitories/RepositoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Text;
using System.Threading.Tasks;
using NLog;
using Wlog.Library.BLL.Reporitories.Interfaces;

namespace Wlog.Library.BLL.Reporitories
{
Expand Down Expand Up @@ -51,6 +52,8 @@ public class RepositoryContext

public JobInstanceRespository JobInstance { get; private set; }

public IKeyPairRepository KeyPairRepository { get; private set; }

private static RepositoryContext current;

public static RepositoryContext Current
Expand All @@ -76,6 +79,7 @@ public static RepositoryContext Current
current.JobDefinition = new JobDefinitionRepository();
current.DeletedLogs = new DeletedLogRepository();
current.JobInstance = new JobInstanceRespository();
current.KeyPairRepository = new DBKeyPairRepository();
}

return current;
Expand Down
19 changes: 19 additions & 0 deletions Wlog.Library/BLL/Utils/SystemDataInitialisation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ public void EnsureSampleData()
}
}

public void InsertMissingDictionary()
{
var apps=RepositoryContext.Current.Applications.Find(null, 0, int.MaxValue, null, Enums.SortDirection.ASC);
foreach(var app in apps)
{
var dict=RepositoryContext.Current.KeyPairRepository.GetDictionaries(app.Id,Constants.DictionaryNames.Main,0,0);
if (dict == null || dict.Count == 0)
{
DictionaryEntity d = new DictionaryEntity()
{
ApplicationId = app.Id,
Name = Constants.DictionaryNames.Main
};

RepositoryContext.Current.KeyPairRepository.CreateDictionary(d);
}
}
}

private RolesEntity InsertRoleIfNotExists(string rolename, bool global, bool application)
{
_logger.Debug("[SystemDataHelper]: InsertRoleIfNotExists");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@
// <author>Daniele Fontani, Emanuele Bucaelli</author>
// <autogenerated>true</autogenerated>
//******************************************************************************
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson.Serialization.Attributes;
using Wlog.Library.BLL.Interfaces;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Mapping.ByCode.Conformist;
using Wlog.BLL.Entities;
using NHibernate.Mapping.ByCode;

namespace Wlog.BLL.Entities
namespace Wlog.Library.DAL.Nhibernate.Mappings
{
public class ApplicationRoleEntity : IEntityBase
public class DictionaryMap : ClassMapping<DictionaryEntity>
{
[BsonId]
public override Guid Id { get; set; }
public virtual Guid ApplicationId { get; set; }
public virtual Guid RoleId{ get; set; }
}
public DictionaryMap()
{
Table("wl_dictionary");
//Schema("dbo");

Id(x => x.Id, map => { map.Column("DictionaryId"); map.Generator(Generators.Guid); });


}
Property(x => x.ApplicationId);
Property(x => x.Name,m=> { m.Index("idx_name");m.Unique(true); });
}
}
}
27 changes: 27 additions & 0 deletions Wlog.Library/DAL/Nhibernate/Mappings/KeyPairMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using Wlog.BLL.Entities;

namespace Wlog.Library.DAL.Nhibernate.Mappings
{
public class KeyPairMap : ClassMapping<KeyPairEntity>
{
public KeyPairMap()
{
Table("wl_keypair");
//Schema("dbo");

Id(x => x.Id, map => { map.Column("KeyPairId"); map.Generator(Generators.Guid); });

Property(x => x.DictionaryId, map => { map.UniqueKey("idx_dictuk"); });
Property(x => x.ItemKey, map=> { map.UniqueKey("idx_dictuk"); });
Property(x => x.ItemValue);
}
}

}
Loading

0 comments on commit b7d4369

Please sign in to comment.