summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/PermissaoUsuarioRepository.cs
blob: e0e226c606b838050d6dc1def09b86d80aaf6da8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using AutoMapper;
using Gestor.Infrastructure.Entities.Seguros;
using Gestor.Infrastructure.Helpers;
using Gestor.Infrastructure.Mappers;
using Gestor.Infrastructure.Repository.Generic;
using Gestor.Infrastructure.Repository.Interface;
using Gestor.Infrastructure.UnitOfWork.Generic;
using Gestor.Model.Common;
using Gestor.Model.Domain.Seguros;
using NHibernate;
using NHibernate.Connection;
using NHibernate.Impl;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;

namespace Gestor.Infrastructure.Repository.Logic
{
	public class PermissaoUsuarioRepository : GenericRepository<PermissaoUsuarioDb>, IPermissaoUsuarioRepository
	{
		private readonly GenericUnitOfWork _unitOfWork;

		public PermissaoUsuarioRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session)
		{
			this._unitOfWork = unitOfWork;
		}

		public PermissaoUsuario FindByPermissao(long id, TipoTela tela)
		{
			return this.Select(string.Format(" AND IdUsuario = {0} AND Tela = {1}", id, (int)tela)).FirstOrDefault<PermissaoUsuario>();
		}

		public List<PermissaoUsuario> FindByUsuario(long id)
		{
			return this.Select(string.Format(" AND IdUsuario = {0}", id));
		}

		public PermissaoUsuario Merge(PermissaoUsuario permissao)
		{
			PermissaoUsuarioDb permissaoUsuarioDb = ApplicationMapper.Mapper.Map<PermissaoUsuario, PermissaoUsuarioDb>(permissao);
			base.Merge(permissaoUsuarioDb);
			return ApplicationMapper.Mapper.Map<PermissaoUsuarioDb, PermissaoUsuario>(permissaoUsuarioDb);
		}

		public PermissaoUsuario SaveOrUpdate(PermissaoUsuario permissao)
		{
			PermissaoUsuarioDb permissaoUsuarioDb = ApplicationMapper.Mapper.Map<PermissaoUsuario, PermissaoUsuarioDb>(permissao);
			this.SaveOrUpdate(permissaoUsuarioDb);
			return ApplicationMapper.Mapper.Map<PermissaoUsuarioDb, PermissaoUsuario>(permissaoUsuarioDb);
		}

		private List<PermissaoUsuario> Select(string condition)
		{
			object connection;
			DataTable dataTable = new DataTable();
			SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
			if (sessionFactory != null)
			{
				connection = sessionFactory.ConnectionProvider.GetConnection();
			}
			else
			{
				connection = null;
			}
			using (SqlConnection sqlConnection = connection as SqlConnection)
			{
				using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
				{
					Auxiliar.CriarAuxiliarUsuario(sqlCommand);
					sqlCommand.CommandText = string.Concat("SELECT DISTINCT * FROM PermissaoUsuario WHERE 1=1 ", condition, ";");
					using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
					{
						sqlDataAdapter.SelectCommand = sqlCommand;
						sqlDataAdapter.Fill(dataTable);
					}
				}
			}
			return dataTable.MapPermissao();
		}
	}
}