Example #1
0
        public int AddTeamServer(string name, string url, int port)
        {
            try
            {
                TeamServer server = new TeamServer
                {
                    Name = name,
                    Url  = url.ToLower().Trim(),
                    Port = port
                };

                using (TeamServerRepository repository = new TeamServerRepository())
                {
                    TeamServer ts = repository.Find(x => x.Url == server.Url);
                    if (ts == null)
                    {
                        return(repository.Insert(server));
                    }
                    else
                    {
                        throw new Exception("Server already exists.");
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
        }
Example #2
0
        public List <WorkItemDto> GetIncompleteItemsByUserIdList(int serverId, int employeeId, List <int> employeeIdList)
        {
            TeamServer         server           = null;
            List <WorkItem>    workItems        = null;
            List <WorkItemDto> workItemsDtoList = new List <WorkItemDto>();
            UserServerInfo     userServerInfo   = null;

            try
            {
                using (TeamServerRepository teamServerRepository = new TeamServerRepository())
                {
                    server = teamServerRepository.GetById(serverId);
                    if (server == null)
                    {
                        throw new Exception(string.Format("Invalid Server Id : {0}", serverId));
                    }
                }
                UserInfo userInfo = null;
                using (UserInfoRepository userInfoRepository = new UserInfoRepository())
                {
                    userInfo = userInfoRepository.Find(x => x.EmployeeId == employeeId);
                    if (userInfo == null)
                    {
                        throw new Exception(string.Format("User with Employee ID {0} Not Found", employeeId));
                    }
                }
                List <string> serverUserIdList = new List <string>();
                using (UserServerInfoRepository userServerInfoRepository = new UserServerInfoRepository())
                {
                    userServerInfo = userServerInfoRepository.FindLocal(x => x.EmployeeId == employeeId && x.TfsId == serverId);
                    if (userServerInfo == null)
                    {
                        throw new Exception(string.Format("User with employee id : {0} is not registered with server id : {1}", employeeId, serverId));
                    }

                    string test = userServerInfoRepository.Filter(x => employeeIdList.Contains(x.EmployeeId))
                                  .Select(x => x.UserId).Aggregate((current, next) => "'" + current + "' OR ");

                    string credentialHash = userServerInfo.CredentialHash;
                    string url            = server.Url;
                    string query          = "Select * From WorkItems " +
                                            "Where [System.AssignedTo] = @me " +
                                            "AND ([System.State] = 'To Do' OR [System.State] = 'In Progress')"
                                            + "Order By [State] Asc, [Changed Date] Desc";

                    workItems = _teamServerManagementService.GetWorkItemByQuery(query, url, credentialHash);
                }
                foreach (WorkItem workItem in workItems)
                {
                    workItemsDtoList.Add(workItem.ToDto());
                }
                return(workItemsDtoList);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
        }
Example #3
0
        public int RegisterServer(int serverId, string userId, string serverUserId, string serverPassword, string serverDomain)
        {
            TeamServer teamServerEntity;

            try
            {
                using (TeamServerRepository teamServerRepository = new TeamServerRepository())
                {
                    teamServerEntity = teamServerRepository.GetById(serverId);
                    if (teamServerEntity == null)
                    {
                        throw new Exception("Invalid server id");
                    }
                }
                using (UserInfoRepository userInfoRepository = new UserInfoRepository())
                {
                    UserInfo userInfoEntity = userInfoRepository.Find(x => x.UserId == userId);
                    if (userInfoEntity == null)
                    {
                        throw new Exception("Invalid user id");
                    }
                }
                using (UserServerInfoRepository userServerInfoRepository = new UserServerInfoRepository())
                {
                    UserServerInfo userServerInfoEntity = userServerInfoRepository.Find(
                        x => x.UserId.ToUpper() == userId.ToUpper() && x.TfsId == serverId);
                    if (userServerInfoEntity != null)
                    {
                        throw new Exception(string.Format("Server {0} is already registered to the user {1} .", serverId, userId));
                    }

                    // Dependency Injection of Team Service.
                    NetworkCredential credential = new NetworkCredential(serverUserId, serverPassword, serverDomain);
                    string            hash       = JsonConvert.SerializeObject(credential).Encrypt();
                    _authenticationService.Authenticate(serverId, hash);

                    userServerInfoEntity = new UserServerInfo()
                    {
                        UserId         = userId,
                        TfsId          = serverId,
                        TfsUserId      = serverUserId,
                        CredentialHash = hash
                    };
                    return(userServerInfoRepository.Insert(userServerInfoEntity));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                throw;
            }
        }
        public WorkItemDto GetWorkItemById(int taskId, int serverId, string userId)
        {
            TeamServer     server         = null;
            UserServerInfo userServerInfo = null;

            try
            {
                using (TeamServerRepository teamServerRepository = new TeamServerRepository())
                {
                    server = teamServerRepository.GetById(serverId);
                    if (server == null)
                    {
                        throw new Exception(string.Format("Invalid Server Id : {0}", serverId));
                    }
                }
                UserInfo userInfo = null;
                using (UserInfoRepository userInfoRepository = new UserInfoRepository())
                {
                    userInfo = userInfoRepository.Find(x => x.UserId != null && x.UserId.ToUpper() == userId.ToUpper());
                    if (userInfo == null)
                    {
                        throw new Exception(string.Format("User with ID {0} Not Found", userId));
                    }
                }

                using (UserServerInfoRepository userServerInfoRepository = new UserServerInfoRepository())
                {
                    userServerInfo = userServerInfoRepository.FindLocal(
                        x => x.UserId != null &&
                        x.UserId.ToUpper() == userId.ToUpper() &&
                        x.TfsId == serverId);
                    if (userServerInfo == null)
                    {
                        throw new Exception(string.Format("User : {0} is not registered with server id : {1}", userId, serverId));
                    }
                    string   credentialHash = userServerInfo.CredentialHash;
                    string   url            = server.Url;
                    WorkItem workItem       = _teamServerManagementService.GetWorkItemById(taskId, url, credentialHash);
                    if (workItem != null)
                    {
                        return(workItem.ToEntity(serverId).ToDto(workItem.Id));
                    }
                }
                return(null);
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                throw;
            }
        }
Example #5
0
 public int DeleteTeamServer(int id)
 {
     using (TeamServerRepository repository = new TeamServerRepository())
     {
         TeamServer server = repository.GetById(id);
         if (server != null)
         {
             return(repository.Delete(server));
         }
         else
         {
             throw new Exception(string.Format("Server with specifed id {0} does not exist.", id));
         }
     }
 }
Example #6
0
        public bool Authenticate(int tfsId, string credentialHash)
        {
            TeamServer teamServer = null;
            string     url        = string.Empty;

            try
            {
                using (TeamServerRepository teamServerRepository = new TeamServerRepository())
                {
                    teamServer = teamServerRepository.GetById(tfsId);
                    if (teamServer == null)
                    {
                        throw new Exception(string.Format("Invalid Team Server Id : {0}", tfsId));
                    }
                }
                url = teamServer.Url;
                NetworkCredential credential = JsonConvert.DeserializeObject <NetworkCredential>(credentialHash.Decrypt());
                WindowsCredential winCred    = new WindowsCredential(credential);
                VssCredentials    vssCred    = new VssClientCredentials(winCred)
                {
                    PromptType = CredentialPromptType.DoNotPrompt
                };
                using (TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(url), vssCred))
                {
                    tpc.EnsureAuthenticated();
                    return(true);
                }
            }
            catch (TeamFoundationServiceUnavailableException ex)
            {
                logger.Error(ex, "Service not available");
                throw new ServiceUnavailableException(url, "Service not Available", ex);
            }
            catch (TeamFoundationServerUnauthorizedException ex)
            {
                logger.Error(ex, "Authentication Failure.");
                throw new TFSAuthenticationException("Authentication Failure.", ex);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Authentication Failure.");
                throw;
            }
        }
Example #7
0
        public List <UserServerDto> GetUserServerList(string userId)
        {
            List <UserServerDto> userServerList = new List <UserServerDto>();
            List <UserServerDto> dtoList        = new List <UserServerDto>();

            try
            {
                using (UserServerInfoRepository repository = new UserServerInfoRepository())
                {
                    System.Linq.IQueryable <UserServerInfo> servers = repository.Filter(x => x.UserId == userId);
                    foreach (UserServerInfo server in servers)
                    {
                        UserServerDto dto = new UserServerDto
                        {
                            TfsId  = server.TfsId,
                            UserId = server.UserId
                        };
                        dtoList.Add(dto);
                    }
                }
                using (TeamServerRepository serverRepository = new TeamServerRepository())
                {
                    foreach (UserServerDto server in dtoList)
                    {
                        TeamServer teamServer = serverRepository.Find(x => x.Id == server.TfsId);
                        if (teamServer != null)
                        {
                            server.ServerName = teamServer.Name;
                            server.ServerUrl  = teamServer.Url;

                            userServerList.Add(server);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Failed to get servers for User-Id " + userId);
                throw;
            }
            return(userServerList);
        }
        public WorkItemDto FindWorkItemFromServer(int taskid, int serverId, int weekId, string userId)
        {
            WorkItemDto workItemDto = null;
            TeamServer  server      = null;

            using (TeamServerRepository teamServerRepository = new TeamServerRepository())
            {
                server = teamServerRepository.FindLocal(x => x.Id == serverId);
                if (server == null)
                {
                    _logger.Error("Server not found with id " + serverId);
                    throw new Exception("Server not found.");
                }
            }
            string         serverUrl  = server.Url;
            UserServerInfo userServer = null;

            using (UserServerInfoRepository userServerInfoRepository = new UserServerInfoRepository())
            {
                userServer = userServerInfoRepository.Find(x => x.TfsId == serverId && x.UserId == userId);
                if (userServer == null)
                {
                    throw new Exception(string.Format("User {0} not registered to server {1}", userId, serverId));
                }
            }
            WorkItem workItem = _teamServerManagementService.GetWorkItemById(taskid, serverUrl, userServer.CredentialHash);

            if (workItem != null)
            {
                workItemDto          = workItem.ToEntity(serverId).ToDto(-1);
                workItemDto.WeekId   = weekId;
                workItemDto.ServerId = serverId;
                workItemDto.TaskId   = taskid;
            }
            return(workItemDto);
        }
        public List <WorkItemDto> GetUserCurrentWeekSyncedTasks(string userId, bool includeIncompleteItems = true)
        {
            List <WorkItemDto> currentWeekTasks = new List <WorkItemDto>();
            WeekInfo           currentWeek      = null;

            UserInfo currentUser = null;

            using (UserInfoRepository userInfoRepository = new UserInfoRepository())
            {
                currentUser = userInfoRepository.Find(x => x.UserId == userId);
                if (currentUser == null)
                {
                    throw new Exception(string.Format("User Id {0} not found", userId));
                }
            }

            // Get current week.
            using (WeekInfoRepository repository = new WeekInfoRepository())
            {
                currentWeek = repository.GetCurrentWeekInfo();
                if (currentWeek == null)
                {
                    throw new Exception("Current Week is not registered.");
                }
            }

            // Get servers configured for the user.
            List <UserServerInfo> userServers = new List <UserServerInfo>();

            using (UserServerInfoRepository repository = new UserServerInfoRepository())
            {
                userServers = repository.Filter(x => x.UserId == userId).ToList();
            }

            // Get user incomplete tasks from task repository.
            List <int> currentWeekTaskIdList    = new List <int>();
            List <int> currentWeekMovedTaskList = new List <int>();

            foreach (UserServerInfo server in userServers)
            {
                using (WorkItemRepository workItemRepository = new WorkItemRepository())
                {
                    IQueryable <UserWorkItem> currentWeekSavedTasks = workItemRepository
                                                                      .Filter(x => x.ServerId == server.Id &&
                                                                              x.UserId == userId &&
                                                                              x.State != WorkItemState.Moved &&
                                                                              x.WeekId == currentWeek.Id);

                    IQueryable <UserWorkItem> currentWeekMovedTasks = workItemRepository
                                                                      .Filter(x => x.ServerId == server.Id &&
                                                                              x.UserId == userId &&
                                                                              x.State == WorkItemState.Moved &&
                                                                              x.WeekId == currentWeek.Id);

                    currentWeekTaskIdList    = currentWeekSavedTasks.Select(x => x.TaskId).ToList();
                    currentWeekMovedTaskList = currentWeekSavedTasks.Select(x => x.TaskId).ToList();
                }

                TeamServer teamServer = null;
                using (TeamServerRepository repository = new TeamServerRepository())
                {
                    teamServer = repository.Find(x => x.Id == server.TfsId);
                    if (teamServer == null)
                    {
                        continue;
                    }
                }

                // Get all tasks by id including to do items.
                List <WorkItem> userIncompleteItems = _teamServerManagementService
                                                      .GetWorkItemsByIds(currentWeekTaskIdList, teamServer.Url, server.CredentialHash, includeIncompleteItems);
                using (WorkItemRepository workItemRepository = new WorkItemRepository())
                {
                    foreach (WorkItem item in userIncompleteItems)
                    {
                        // Add new items.
                        UserWorkItem existingWorkItem = workItemRepository.Find(x => x.UserId == userId &&
                                                                                x.WeekId == currentWeek.Id &&
                                                                                x.TaskId == item.Id);
                        if (existingWorkItem == null)
                        {
                            UserWorkItem newWorkItem = item.ToEntity(server.TfsId);
                            newWorkItem.UserId     = userId;
                            newWorkItem.AssignedTo = currentUser.FirstName + " " + currentUser.LastName;
                            newWorkItem.WeekId     = currentWeek.Id;
                            int newWorkItemId = workItemRepository.Insert(newWorkItem);

                            currentWeekTasks.Add(newWorkItem.ToDto(newWorkItemId));
                        }
                        else
                        {
                            existingWorkItem.Title       = item.Title;
                            existingWorkItem.Status      = item.State;
                            existingWorkItem.Sprint      = item.IterationPath;
                            existingWorkItem.Project     = item.AreaPath;
                            existingWorkItem.Description = item.Description;

                            workItemRepository.Update(existingWorkItem);
                            currentWeekTasks.Add(existingWorkItem.ToDto(existingWorkItem.Id));
                        }
                    }
                }
            }
            return(currentWeekTasks);
        }
        public List <WorkItemDto> GetUserIncompleteSyncedTasks(int serverId, string userId)
        {
            TeamServer         server           = null;
            List <WorkItemDto> workItemsDtoList = new List <WorkItemDto>();
            UserServerInfo     userServerInfo   = null;

            try
            {
                using (TeamServerRepository teamServerRepository = new TeamServerRepository())
                {
                    server = teamServerRepository.GetById(serverId);
                    if (server == null)
                    {
                        throw new Exception(string.Format("Invalid Server Id : {0}", serverId));
                    }
                }

                UserInfo userInfo = null;
                using (UserInfoRepository userInfoRepository = new UserInfoRepository())
                {
                    userInfo = userInfoRepository.Find(x => x.UserId == userId);
                    if (userInfo == null)
                    {
                        throw new Exception(string.Format("User with ID {0} Not Found", userId));
                    }
                }

                using (UserServerInfoRepository userServerInfoRepository = new UserServerInfoRepository())
                {
                    userServerInfo = userServerInfoRepository.FindLocal(
                        x => x.UserId == userId &&
                        x.TfsId == serverId
                        );
                    if (userServerInfo == null)
                    {
                        throw new Exception(string.Format("User with id : {0} is not registered with server id : {1}", userId, serverId));
                    }
                }

                string credentialHash = userServerInfo.CredentialHash;
                string url            = server.Url;

                WeekInfo currentWeek = null;
                using (WeekInfoRepository weekInfoRepository = new WeekInfoRepository())
                {
                    currentWeek = weekInfoRepository.GetCurrentWeekInfo();
                    if (currentWeek == null)
                    {
                        throw new Exception("Current Week is not registered.");
                    }
                }
                List <WorkItem> tfsWorkItems = _teamServerManagementService.GetUserIncompleteItems(url, credentialHash);
                using (WorkItemRepository workItemRepository = new WorkItemRepository())
                {
                    foreach (WorkItem item in tfsWorkItems)
                    {
                        // Add new items.
                        UserWorkItem existingWorkItem = workItemRepository.Find(x => x.UserId == userId &&
                                                                                x.WeekId == currentWeek.Id &&
                                                                                x.TaskId == item.Id);
                        if (existingWorkItem == null)
                        {
                            UserWorkItem newWorkItem = item.ToEntity(userServerInfo.TfsId);
                            newWorkItem.UserId     = userId;
                            newWorkItem.AssignedTo = userInfo.FirstName + " " + userInfo.LastName;
                            newWorkItem.WeekId     = currentWeek.Id;
                            int newWorkItemId = workItemRepository.Insert(newWorkItem);

                            workItemsDtoList.Add(newWorkItem.ToDto(newWorkItemId));
                        }
                        else
                        {
                            existingWorkItem.Title       = item.Title;
                            existingWorkItem.Status      = item.State;
                            existingWorkItem.Sprint      = item.IterationPath;
                            existingWorkItem.Project     = item.AreaPath;
                            existingWorkItem.Description = item.Description;

                            workItemRepository.Update(existingWorkItem);
                            workItemsDtoList.Add(existingWorkItem.ToDto(existingWorkItem.Id));
                        }
                    }
                }

                return(workItemsDtoList);
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                throw;
            }
        }