public object Get(DeployProjectRole request)
		{
			if(request == null)
			{
				throw new ArgumentNullException("request is null");
			}
			if(!string.IsNullOrEmpty(request.Id))
			{
				if(request.Id.Equals("Everyone", StringComparison.CurrentCultureIgnoreCase))
				{
					if(string.IsNullOrEmpty(request.ProjectId))
					{
						throw new ArgumentNullException("project id is null");
					}
					return _projectRoleManager.GetProjectEveryoneRole(request.ProjectId);
				}
				else 
				{
					return _projectRoleManager.GetProjectRole(request.Id);
				}
			}
			else if (!string.IsNullOrEmpty(request.ProjectId))
			{
				return _projectRoleManager.GetProjectRoleList(request.ProjectId);
			}
			else
			{
				throw new ArgumentNullException("request.Id and request.ProjectId are null");
			}
		}
 public static SqlDeployProjectRole FromDto(DeployProjectRole item)
 {
     var returnValue = AutoMapper.Mapper.Map(item, new SqlDeployProjectRole());
     if(item.Permissions != null)
     {
         returnValue.PermissionsJson = item.Permissions.ToJson();
     }
     if(item.Assignments != null && item.Assignments.UserNameList != null)
     {
         returnValue.UserList = item.Assignments.UserNameList.Select(i=> new SqlDeployProjectRoleUser { DeployProjectRoleID=item.Id, UserName=i }).ToList();
     }
     return returnValue;
 }
		private DeployProjectRoleAssignments UpdateAssignments(DeployProjectRoleAssignments assignments, DeployProjectRole role)
		{
			assignments = assignments??new DeployProjectRoleAssignments();
            //if(string.IsNullOrEmpty(assignments.Id))
            //{
            //    assignments.Id = Guid.NewGuid().ToString();
            //    assignments.CreatedByUserName = _userIdentity.UserName;
            //    assignments.CreatedDateTimeUtc = DateTime.UtcNow;
            //}
            //assignments.ProjectId = role.ProjectId;
            //assignments.ProjectRoleId = role.Id;
            //assignments.UpdatedByUserName = _userIdentity.UserName;
            //assignments.UpdateDateTimeUtc = DateTime.UtcNow;
			return assignments;
		}
		private void UpdateEnvironmentPermissions(DeployProjectRole role, List<DeployProjectRoleEnvironmentPermission> list)
		{
            //if (list != null)
            //{
            //    foreach (var item in list)
            //    {
            //        if (string.IsNullOrEmpty(item.Id))
            //        {
            //            item.Id = Guid.NewGuid().ToString();
            //            item.CreatedByUserName = _userIdentity.UserName;
            //            item.CreatedDateTimeUtc = DateTime.UtcNow;
            //        }
            //        item.ProjectId = role.ProjectId;
            //        item.ProjectRoleId = role.Id;
            //        item.UpdatedByUserName = _userIdentity.UserName;
            //        item.UpdateDateTimeUtc = DateTime.UtcNow;
            //    }
            //}
		}
		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;
		}
		public object Post(DeployProjectRole role)
		{
			if(role == null)
			{
				throw new ArgumentNullException("role is null");
			}
			if(!string.IsNullOrEmpty(role.Id) && role.Id.Equals("Everyone", StringComparison.CurrentCultureIgnoreCase))
			{
				if(string.IsNullOrEmpty(role.ProjectId))
				{
					throw new ArgumentNullException("Missing project ID");
				}
				var existingRole = _projectRoleManager.TryGetProjectEveryoneRole(role.ProjectId);
				if(existingRole != null)
				{
					role.Id = existingRole.Id;
					role.EveryoneRoleIndicator = true;
				}
				else 
				{
					role.Id = null;
				}
			}
			if(!string.IsNullOrEmpty(role.Id))
			{
				return _projectRoleManager.UpdateRole(role.Id, role.ProjectId, role.RoleName, role.Permissions, role.Assignments, role.EveryoneRoleIndicator);
			}
			else if(!string.IsNullOrEmpty(role.ProjectId))
			{
				return _projectRoleManager.CreateRole(role.ProjectId, role.RoleName, role.Permissions, role.Assignments, role.EveryoneRoleIndicator);
			}
			else 
			{
				throw new ArgumentNullException("role.id and role.projectId are null");
			}
		}
				public DeployProjectRole AddRoleAssignment(DeployProject project, DeployEnvironment environment, 
							EnumPermissionAccess createEnvironmentAccess=EnumPermissionAccess.None,
							EnumPermissionAccess editComponentConfigurationAccess=EnumPermissionAccess.None, 
							EnumPermissionAccess editProjectPermissionsAccess=EnumPermissionAccess.None,
							EnumPermissionAccess approveRejectDeployAccess=EnumPermissionAccess.None, 
							EnumPermissionAccess requestDeploymentAccess=EnumPermissionAccess.None,
							EnumPermissionAccess runDeploymentmentAccess=EnumPermissionAccess.None,
							EnumPermissionAccess editEnvironmentAccess=EnumPermissionAccess.None,
							EnumPermissionAccess editEnvironmentPermissionsAccess = EnumPermissionAccess.None)
				{
					string roleId = this.Fixture.Create<string>();
					var role = new DeployProjectRole
					{
						Id = roleId,
						ProjectId = project.Id,
						RoleName = this.Fixture.Create<string>("RoleName"),
						Assignments = new DeployProjectRoleAssignments
						{
							UserNameList = new List<string> { this.UserName }
						},
						EveryoneRoleIndicator = false
					};
					role.Permissions.EditComponentConfigurationAccess = editComponentConfigurationAccess;
					role.Permissions.CreateEnvironmentAccess = createEnvironmentAccess;
					role.Permissions.EditProjectPermissionsAccess = editProjectPermissionsAccess;
					this.SetPermission(project, environment, role.Permissions.ApproveRejectDeployPermissionList, approveRejectDeployAccess);
					this.SetPermission(project, environment, role.Permissions.RequestDeployPermissionList, requestDeploymentAccess);
					this.SetPermission(project, environment, role.Permissions.RunDeploymentPermissionList, runDeploymentmentAccess);
					this.SetPermission(project, environment, role.Permissions.EditEnvironmentPermissionList, editEnvironmentAccess);
					this.SetPermission(project, environment, role.Permissions.EditEnvironmentPermissionsPermissionList, editEnvironmentPermissionsAccess);

					this.DeployProjectRoleList.Add(role);

					return role;
				}
		private DeployProjectRole ValidateRole(DeployProjectRole role, DeployProject project)
		{
			role.Permissions = this.ValidatePermissions(role.Permissions, role.Id, project);
			role.ProjectName = project.ProjectName;
			return role;
		}
		private DeployProjectRole CreateEveryoneRole(DeployProject project)
		{
			var role = new DeployProjectRole
			{
				Id = "Everyone",
				ProjectId = project.Id,
				ProjectName = project.ProjectName,
				RoleName = "Everyone",
				EveryoneRoleIndicator = true
			};
			role.Permissions = this.ValidatePermissions(role.Permissions, role.Id, project);
			role.Permissions.EditComponentConfigurationAccess = EnumPermissionAccess.Grant;
			role.Permissions.CreateEnvironmentAccess = EnumPermissionAccess.Grant;
			role.Permissions.EditProjectPermissionsAccess = EnumPermissionAccess.Grant;
			foreach (var item in role.Permissions.RequestDeployPermissionList)
			{
				item.Access = EnumPermissionAccess.Grant;
			}
			foreach(var item in role.Permissions.RunDeploymentPermissionList)
			{
				item.Access = EnumPermissionAccess.Grant;
			}
			foreach(var item in role.Permissions.ApproveRejectDeployPermissionList)
			{
				item.Access = EnumPermissionAccess.Grant;
			}
			foreach(var item in role.Permissions.EditEnvironmentPermissionList)
			{
				item.Access = EnumPermissionAccess.Grant;
			}
			foreach(var item in role.Permissions.EditEnvironmentPermissionsPermissionList)
			{
				item.Access = EnumPermissionAccess.Grant;
			}
			return role;
		}
		public object Delete(DeployProjectRole role) 
		{
			if(role == null)
			{
				throw new ArgumentNullException("role is null");
			}
			if(string.IsNullOrEmpty(role.Id))
			{
				throw new ArgumentNullException("role is null");
			}
			return _projectRoleManager.DeleteRole(role.Id);
		}
		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;
		}