private DeployProjectRolePermissions UpdatePermissions(DeployProjectRolePermissions permissions, DeployProjectRole role)
		{
            permissions = permissions ?? new DeployProjectRolePermissions();
            //if(string.IsNullOrEmpty(permissions.Id))
            //{
            //    permissions.Id = Guid.NewGuid().ToString();
            //    permissions.CreatedByUserName = _userIdentity.UserName;
            //    permissions.CreatedDateTimeUtc = DateTime.UtcNow;
            //}
            //permissions.ProjectId = role.ProjectId;
            //permissions.ProjectRoleId = role.Id;
            //permissions.UpdatedByUserName = _userIdentity.UserName;
            //permissions.UpdateDateTimeUtc = DateTime.UtcNow;

			UpdateEnvironmentPermissions(role, permissions.RequestDeployPermissionList);
			UpdateEnvironmentPermissions(role, permissions.ApproveRejectDeployPermissionList);
			UpdateEnvironmentPermissions(role, permissions.RunDeploymentPermissionList);
			UpdateEnvironmentPermissions(role, permissions.EditEnvironmentPermissionList);
			UpdateEnvironmentPermissions(role, permissions.EditEnvironmentPermissionsPermissionList);
			return permissions;
		}
		private DeployProjectRolePermissions ValidatePermissions(DeployProjectRolePermissions permissions, string projectRoleId, DeployProject project)
		{
			permissions = permissions ?? new DeployProjectRolePermissions();

			permissions.RequestDeployPermissionList = permissions.RequestDeployPermissionList ?? new List<DeployProjectRoleEnvironmentPermission>();
			this.ValidateEnvironmentPermissions(permissions.RequestDeployPermissionList, projectRoleId, project);

			permissions.ApproveRejectDeployPermissionList = permissions.ApproveRejectDeployPermissionList ?? new List<DeployProjectRoleEnvironmentPermission>();
			this.ValidateEnvironmentPermissions(permissions.ApproveRejectDeployPermissionList, projectRoleId, project);

			permissions.RunDeploymentPermissionList = permissions.RunDeploymentPermissionList ?? new List<DeployProjectRoleEnvironmentPermission>();
			this.ValidateEnvironmentPermissions(permissions.RunDeploymentPermissionList, projectRoleId, project);

			permissions.EditEnvironmentPermissionList = permissions.EditEnvironmentPermissionList ?? new List<DeployProjectRoleEnvironmentPermission>();
			this.ValidateEnvironmentPermissions(permissions.EditEnvironmentPermissionList, projectRoleId, project);

			permissions.EditEnvironmentPermissionsPermissionList = permissions.EditEnvironmentPermissionsPermissionList ?? new List<DeployProjectRoleEnvironmentPermission>();
			this.ValidateEnvironmentPermissions(permissions.EditEnvironmentPermissionsPermissionList, projectRoleId, project);

			return permissions;
		}
		public DeployProjectRole UpdateRole(string roleId, string projectId, string roleName, DeployProjectRolePermissions permissions, DeployProjectRoleAssignments assignments, bool everyoneRoleIndicator)
		{
			var project = _projectRepository.GetProject(projectId);
			permissions = this.ValidatePermissions(permissions, roleId, project);
			assignments = this.ValidateAssignments(assignments);
			return _permissionRepository.UpdateProjectRole(roleId, projectId, project.ProjectName, roleName, permissions, assignments, everyoneRoleIndicator);
		}
        public DeployProjectRole UpdateProjectRole(string roleId, string projectId, string projectName, string roleName, DeployProjectRolePermissions permissions, DeployProjectRoleAssignments assignments, bool everyoneRoleIndicator)
        {
            if (string.IsNullOrEmpty(projectId))
            {
                throw new ArgumentNullException("projectId");
            }
            if (string.IsNullOrEmpty(projectName))
            {
                throw new ArgumentNullException("projectName");
            }
            if (string.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName");
            }
            permissions = permissions ?? new DeployProjectRolePermissions();
            assignments = assignments ?? new DeployProjectRoleAssignments();

            using(var db = _sqlConnectionInfo.GetDB())
            {
                var count = db.ExecuteScalar<int>("SELECT COUNT(*) FROM DeployProjectRole WHERE ID<>@0 AND ProjectID=@1 AND RoleName=@2", roleId, projectId, roleName);
                if(count != 0)
                {
                    throw new ArgumentException(string.Format("DeployProjectRole already exists for Project {0} and RoleName {1}", projectId, roleName));
                }
            }

            var dbItem = this.GetDBItem(roleId);
            dbItem.ProjectId = projectId;
            dbItem.ProjectName = projectName;
            dbItem.RoleName = roleName;
            dbItem.EveryoneRoleIndicator = everyoneRoleIndicator;
            dbItem.PermissionsJson = permissions.ToJson();
            dbItem.SetUpdatedFields(_userIdentity.UserName);
            
            var itemsToAdd = new List<string>();
            var itemsToDelete = new List<SqlDeployProjectRoleUser>();
            foreach(var userName in assignments.UserNameList)
            {
                if(!dbItem.UserList.Any(i=>i.UserName.Equals(userName)))
                {
                    itemsToAdd.Add(userName);
                }
            }
            foreach(var item in dbItem.UserList)
            {
                if(!assignments.UserNameList.Contains(item.UserName, StringComparer.CurrentCultureIgnoreCase))
                {
                    itemsToDelete.Add(item);
                }
            }
            using(var db = _sqlConnectionInfo.GetDB())
            {
                db.Update("DeployProjectRole", "ID", dbItem, dbItem.Id);

                foreach(var userName in itemsToAdd)
                {
                    var newItem = new SqlDeployProjectRoleUser
                    {
                        ID = Guid.NewGuid().ToString(),
                        DeployProjectRoleID = dbItem.Id,
                        UserName = userName 
                    };
                    db.Insert("DeployProjectRoleUser", "ID", false, newItem);
                }
                foreach(var item in itemsToDelete)
                {
                    db.Execute("DELETE FROM DeployProjectRoleUser WHERE ID=@0", item.ID);
                }
            }
            return this.GetProjectRole(roleId);
        }
 public DeployProjectRole CreateProjectRole(string projectId, string projectName, string roleName, DeployProjectRolePermissions permissions, DeployProjectRoleAssignments assignments, bool everyoneRoleIndicator)
 {
     if (string.IsNullOrEmpty(projectId))
     {
         throw new ArgumentNullException("projectId");
     }
     if (string.IsNullOrEmpty(projectName))
     {
         throw new ArgumentNullException("projectName");
     }
     if (string.IsNullOrEmpty(roleName))
     {
         throw new ArgumentNullException("roleName");
     }
     using (var db = _sqlConnectionInfo.GetDB())
     {
         var count = db.ExecuteScalar<int>("SELECT COUNT(*) FROM DeployProjectRole WHERE ProjectID=@0 AND RoleName=@1", projectId, roleName);
         if (count != 0)
         {
             throw new ArgumentException(string.Format("DeployProjectRole already exists for Project {0} and RoleName {1}", projectId, roleName));
         }
     }
     permissions = permissions ?? new DeployProjectRolePermissions();
     assignments = assignments ?? new DeployProjectRoleAssignments();
     var dbItem = new SqlDeployProjectRole
     {
         Id = Guid.NewGuid().ToString(),
         ProjectId = projectId,
         ProjectName = projectName,
         RoleName = roleName, 
         PermissionsJson = permissions.ToJson(),
         EveryoneRoleIndicator = everyoneRoleIndicator
     };
     dbItem.UserList = assignments.UserNameList.Select(i=>
                             new SqlDeployProjectRoleUser 
                             {
                                 ID = Guid.NewGuid().ToString(),
                                 DeployProjectRoleID = dbItem.Id,
                                 UserName = i
                             }).ToList();
     dbItem.SetCreatedFields(_userIdentity.UserName);
     using(var db = _sqlConnectionInfo.GetDB())
     {
         db.Insert("DeployProjectRole", "ID", false, dbItem);
         foreach(var item in dbItem.UserList)
         {
             db.Insert("DeployProjectRoleUser", "ID", false, item);
         }
     }
     return this.GetProjectRole(dbItem.Id);
 }
		public DeployProjectRole UpdateProjectRole(string roleId, string projectId, string projectName, string roleName, DeployProjectRolePermissions permissions, DeployProjectRoleAssignments assignments, bool everyoneRoleIndicator)
		{
            if(string.IsNullOrEmpty(projectId))
            {
                throw new ArgumentNullException("projectId");
            }
            if(string.IsNullOrEmpty(projectName))
            {
                throw new ArgumentNullException("projectName");
            }
            if(string.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName");
            }
            var existingItem = _documentSession.QueryNoCacheNotStale<DeployProjectRole>().Any(i => i.Id != roleId && i.ProjectId == projectId && i.RoleName == roleName);
            if (existingItem)
            {
                throw new ArgumentException(string.Format("DeployProjectRole already exists for Project {0} and RoleName {1}", projectId, roleName));
            }
            var item = this.GetProjectRole(roleId);
            item.ProjectId = projectId;
			item.ProjectName = projectName;
			item.RoleName = roleName;
			item.Permissions = this.UpdatePermissions(permissions, item);
			item.Assignments = this.UpdateAssignments(assignments, item);
			item.UpdatedByUserName = _userIdentity.UserName;
			item.UpdatedDateTimeUtc = DateTime.UtcNow;
			item.EveryoneRoleIndicator = everyoneRoleIndicator;
			_documentSession.SaveChanges();
			return item;
		}
		public DeployProjectRole CreateProjectRole(string projectId, string projectName, string roleName, DeployProjectRolePermissions permissions, DeployProjectRoleAssignments assignments, bool everyoneRoleIndicator)
		{
			if(string.IsNullOrEmpty(projectId))
			{
				throw new ArgumentNullException("projectId");
			}
            if(string.IsNullOrEmpty(projectName))
            {
                throw new ArgumentNullException("projectName");
            }
			if(string.IsNullOrEmpty(roleName))
			{
				throw new ArgumentNullException("roleName");
			}
			var existingItem = _documentSession.QueryNoCacheNotStale<DeployProjectRole>().Any(i=>i.ProjectId == projectId && i.RoleName == roleName);
			if(existingItem)
			{
				throw new ArgumentException(string.Format("DeployProjectRole already exists for Project {0} and RoleName {1}", projectId, roleName));
			}
			var newItem = new DeployProjectRole
			{
				Id = Guid.NewGuid().ToString(),
				ProjectId = projectId,
				ProjectName = projectName,
				RoleName = roleName,
				EveryoneRoleIndicator = everyoneRoleIndicator,
				CreatedByUserName = _userIdentity.UserName,
				CreatedDateTimeUtc = DateTime.UtcNow,
				UpdatedByUserName = _userIdentity.UserName,
				UpdatedDateTimeUtc = DateTime.UtcNow
			};
			newItem.Permissions = this.UpdatePermissions(permissions, newItem);
			newItem.Assignments = this.UpdateAssignments(assignments, newItem);
			_documentSession.Store(newItem);
			_documentSession.SaveChanges();
			return newItem;
		}