/// <summary> /// Get information from HQ about: employees and salaries. /// Then, ensure that BO db contains only those employees and salaries that also /// exist in HQ. /// Then, delete any employeeHours and salaries which are connected to a deleted employee. /// </summary> /// <returns></returns> public async Task Synchronize() { if (hqApiClient != null) { try { lock (this.locker) { if (synchronizing) { _log.Warn("Synchronization skipped, because it is already running"); return; } synchronizing = true; } _log.Debug("Starting synchronization"); List <HQEmployee> hqEmployees = await hqApiClient.ListEmployees( this.confService.GetBranchOfficeId()); List <Employee> boEmployees; if (hqEmployees != null && hqEmployees.Count != 0) { boEmployees = daoService.GetAllEmployees(); // add all the employes from HQ into BO for (int i = 0; i < hqEmployees.Count; i++) { var hqEmp = hqEmployees[i]; int hqEmpId = hqEmp.ID; var hqAsBoEmployee = new Employee(hqEmp); bool empPresent = VerifyEmployeeInCollection(boEmployees, hqAsBoEmployee); if (!empPresent) { daoService.AddEmployee(hqAsBoEmployee); } // now, if the employee was just added, it will most surely have different Id // We need to know this Id, so that we can assign later a salary // to an employee in BO. int boEmployeeId = daoService.GetEmployeeIdByMail(hqAsBoEmployee.Email); var boSalaries = daoService.GetSalariesForAnEmployee(boEmployeeId); // add all the salaries from HQ into BO List <HQSalary> hqSalaries = await hqApiClient.ListSalariesForEmployee(hqEmpId); if (hqSalaries != null && hqSalaries.Count != 0) { for (int s = 0; s < hqSalaries.Count; s++) { var hqAsBoSalary = new Salary(hqSalaries[s]); hqAsBoSalary.EmployeeId = boEmployeeId; bool salaryPresent = VerifySalaryInCollection(boSalaries, hqAsBoSalary); if (!salaryPresent) { daoService.AddSalary(hqAsBoSalary); } } } } } else { _log.Info("No employees found in headquarters"); } // remove all the employees in BO that are not present in HQ boEmployees = daoService.GetAllEmployees(); if (boEmployees != null) { _log.DebugFormat("BO Employees count: {0}", boEmployees.Count()); for (int i = 0; i < boEmployees.Count; i++) { var boEmp = boEmployees[i]; bool empPresent = VerifyEmployeeInCollection(hqEmployees, boEmp); if (!empPresent) { _log.InfoFormat("Deleting Employee {0} together with its related EmployeeHours and Salaries", boEmp.EmployeeId); daoService.DeleteEmployee(boEmp.EmployeeId); // remove all the employeesHours for the deleted employee var employeeHoursColl = daoService.GetEmployeeHoursForAnEmployee(boEmp.EmployeeId); for (int j = 0; j < employeeHoursColl.Count; j++) { daoService.DeleteEmployeeHours(employeeHoursColl[j].EmployeeHoursId); } // remove all the salaries for the deleted employee var salariesColl = daoService.GetSalariesForAnEmployee(boEmp.EmployeeId); for (int j = 0; j < salariesColl.Count; j++) { daoService.DeleteSalary(salariesColl[j].SalaryId); } } else { _log.DebugFormat("Not deleting BO Employee, because it exists in HQ too: {0}", boEmp); } } } else { _log.DebugFormat("BO Employees count: 0"); } _log.Info("Synchronization was successful"); this.daoService.InformOnDBContents(); } catch (System.Net.Http.HttpRequestException ex) { _log.WarnFormat("Synchronizing with HQ failed (is the HQ server running?). Ex: {0}", ex.Message); } finally { synchronizing = false; } } }
public void DeleteEmployeeHours(int employeeHoursId) { daoService.DeleteEmployeeHours(employeeHoursId); }