NHibernate开源框架Cuyahoga学习之数据访问实现

代码
using System;
using System.Collections;
using System.Collections.Generic;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
using Cuyahoga.Core.Util;
using NHibernate;
using NHibernate.Criterion;

using Cuyahoga.Core.Domain;

namespace Cuyahoga.Core.DataAccess
{
    
/// <summary>
    
/// Provides data access for user-related components.
    
/// </summary>
    [Transactional]
    
public class UserDao : IUserDao
    {
        
private ISessionManager _sessionManager;
        
private ICommonDao _commonDao;

        
/// <summary>
        
/// Default constructor;
        
/// </summary>
        
/// <param name="sessionManager"></param>
        public UserDao(ISessionManager sessionManager, ICommonDao commonDao)
        {
            
this._sessionManager = sessionManager;
            
this._commonDao = commonDao;
        }

        
#region IUserDao Members

        
public User GetUserByUsernameAndPassword(string username, string password)
        {
            ISession session 
= this._sessionManager.OpenSession();

            ICriteria crit 
= session.CreateCriteria(typeof(User));
            crit.Add(Expression.Eq(
"UserName", username));
            crit.Add(Expression.Eq(
"Password", password));
            IList results 
= crit.List();
            
if (results.Count == 1)
            {
                
return (User)results[0];
            }
            
else if (results.Count > 1)
            {
                
throw new Exception(String.Format("Multiple users found with the give username and password. Something is pretty wrong here"));
            }
            
else
            {
                
return null;
            }
        }

        
public IList FindUsersByUsername(string searchString)
        {
            
if (searchString.Length > 0)
            {
                ISession session 
= this._sessionManager.OpenSession();
                
string hql = "from User u where u.UserName like ? order by u.UserName ";
                
return session.Find(hql, searchString + "%", NHibernateUtil.String);
            }
            
else
            {
                
return this._commonDao.GetAll(typeof(User), "UserName");
            }
        }
        
//根据条件分页
        public IList<User> FindUsers(string username, int? roleId, bool? isActive, int? siteId, int pageSize, int pageNumber, out int totalCount)
        {
            ISession session 
= this._sessionManager.OpenSession();
            ICriteria userCriteria 
= session.CreateCriteria(typeof(User));
            ICriteria countCriteria 
= session.CreateCriteria(typeof(User), "userCount");

            
if (!String.IsNullOrEmpty(username))
            {
                
//username相似:从开始匹配
                userCriteria.Add(Expression.InsensitiveLike("UserName", username, MatchMode.Start));
                countCriteria.Add(Expression.InsensitiveLike(
"UserName", username, MatchMode.Start));
            }
            
if (roleId.HasValue)
            {
                userCriteria.CreateCriteria(
"Roles""r1").Add(Expression.Eq("r1.Id", roleId));
                countCriteria.CreateCriteria(
"Roles""r1").Add(Expression.Eq("r1.Id", roleId));
            }
            
if (isActive.HasValue)
            {
                userCriteria.Add(Expression.Eq(
"IsActive", isActive));
                countCriteria.Add(Expression.Eq(
"IsActive", isActive));
            }
            
if (siteId.HasValue && ! roleId.HasValue)
            {
                
                DetachedCriteria roleIdsForSite 
= DetachedCriteria.For(typeof (Role))
                    .SetProjection(Projections.Property(
"Id"))
                    .CreateCriteria(
"Sites""site")//Sites 别名site
                    .Add(Expression.Eq("site.Id", siteId.Value));
                DetachedCriteria userIdsForRoles 
= DetachedCriteria.For(typeof(User))
                    .SetProjection(Projections.Distinct(Projections.Property(
"Id")))
                    .CreateCriteria(
"Roles")
                    .Add(Subqueries.PropertyIn(
"Id", roleIdsForSite));
                userCriteria.Add(Subqueries.PropertyIn(
"Id", userIdsForRoles));
                countCriteria.Add(Subqueries.PropertyIn(
"Id", userIdsForRoles));
                
//里面的属性方法有待进一步查阅资料
            }
            userCriteria.SetFirstResult((pageNumber 
- 1* pageSize);
            userCriteria.SetMaxResults(pageSize);
            countCriteria.SetProjection(Projections.RowCount());
            totalCount 
= (int) countCriteria.UniqueResult();
            
return userCriteria.List<User>();
        }

        
public IList<Section> GetViewableSectionsByUser(User user)
        {

            
//:userId参数User和Role进行了关联映射所以可以用u.Rolse来获取Role的相关数据
            string hql = "select s from User u join u.Roles as r, Section s join s.SectionPermissions sp " +
                        
"where u.Id = :userId and r.Id = sp.Role.Id and sp.ViewAllowed = 1";
            IQuery q 
= this._sessionManager.OpenSession().CreateQuery(hql);
            q.SetInt32(
"userId", user.Id);
            
return q.List<Section>();
        }

             
public IList<Section> GetViewableSectionsByAccessLevel(AccessLevel accessLevel)
        {
            
int permission = (int)accessLevel;

            
string hql = "select s from Section s join s.SectionPermissions sp, Role r "+
                
"where r.PermissionLevel = :permission and r.Id = sp.Role.Id and sp.ViewAllowed = 1";
            IQuery q 
= this._sessionManager.OpenSession().CreateQuery(hql);
            q.SetInt32(
"permission", permission);
            
return q.List<Section>();
        }
        
public IList<Role> GetRolesByRightName(string rightName)
        {
            
string hql = "select r from Role r join r.Rights right where right.Name = :rightName";
            IQuery q 
= this._sessionManager.OpenSession().CreateQuery(hql);
            q.SetString(
"rightName", rightName);
            
return q.List<Role>();
        }

        
public IList<Role> GetAllRolesBySite(Site site)
        {
            ISession session 
= this._sessionManager.OpenSession();
            ICriteria crit 
= session.CreateCriteria(typeof (Role))
                .AddOrder(Order.Asc(
"Name"))
                .CreateCriteria(
"Sites")
                .Add(Expression.Eq(
"Id", site.Id));
            
return crit.List<Role>();
        }

        [Transaction(TransactionMode.Requires)]
        
public void SaveOrUpdateUser(User user)
        {
            ISession session 
= this._sessionManager.OpenSession();
            session.SaveOrUpdate(user);
        }

        [Transaction(TransactionMode.Requires)]
        
public void DeleteUser(User user)
        {
            ISession session 
= this._sessionManager.OpenSession();
            session.Delete(user);
        }

        
#endregion
    }
}

 

posted @ 2011-03-15 21:04  Blue Sky ......  阅读(531)  评论(0编辑  收藏  举报