/// <summary> /// Deletes a given task. /// </summary> /// <param name="t">Receives the task that is meant to be deleted.</param> /// <returns>Returns the task that has been deleted.</returns> public static pojo.Task deleteTask(pojo.Task t) { using (var context = new synupEntities()) { pojo.Task foundTask = readTask(t.code, context); //Finds the received task in the database. if (foundTask != null) //If the task has been found - meaning that it exists: { foundTask.state = (int)TaskState.CANCELLED; if (updateTask(foundTask)) { if (commitChanges(context)) { return(foundTask); } else { return(null); } } else { return(null); } } } return(null); }
/// <summary> /// Deletes a given team. /// </summary> /// <param name="pTeam">Receives the team that is meant to be deleted</param> /// <returns></returns> public static pojo.Team deleteTeam(pojo.Team pTeam) { using (var context = new synupEntities()) { pojo.Team _oTeam = readTeam(pTeam.code, context); //Finds the received team in the database. if (_oTeam != null) //If the team has been found - meaning that it exists: { if (checkTeamMembers(_oTeam)) { //tryAttach(context, _oTeam); context.Teams.Remove(_oTeam); //Will be deleted. if (commitChanges(context)) { return(_oTeam); //If the changes are commited succesfully it will return the deleted Team. } else { return(null); } } } } return(null); }
/// <summary> /// Updates a given task with the new parameters of it. /// </summary> /// <param name="t">Receives the task that will be updated.</param> /// <returns>Returns a boolean whether the task has been updated succesfully or not.</returns> public static bool updateTask(pojo.Task t) { using (var context = new synupEntities()) { pojo.Task modifiedTask = readTask(t.code, context); if (modifiedTask != null) { //tryAttach(context, modifiedTask); //tryAttach(context, modifiedTask); //modifiedTask.code = t.code; modifiedTask.description = t.description; modifiedTask.id_team = t.id_team; modifiedTask.localization = t.localization; modifiedTask.name = t.name; modifiedTask.priorityDate = t.priorityDate; modifiedTask.project = t.project; modifiedTask.priority = t.priority; return(commitChanges(context)); } } return(false); }
/// <summary> /// Deletes a given employee if it is found in the database. It will leave all the tasks given to it as ABANDONED. /// </summary> /// <param name="pEmployee">Receives the employee that is meant to be deleted</param> /// <returns></returns> public static pojo.Employee deleteEmployee(pojo.Employee pEmployee) { using (var context = new synupEntities()) { pojo.Employee _oEmployee = readEmployee(pEmployee.nif, context); //Finds the received employee in the database. if (_oEmployee != null) //If the employee has been found - meaning that it exists: { if (checkRelations(_oEmployee)) //Checks the employee foreign keys and performs an update. If the update is succesfull it will step in the next code block. { tryAttach(context, _oEmployee); context.Employees.Remove(_oEmployee); //Will be deleted. if (commitChanges(context)) { return(_oEmployee); //If the changes are commited succesfully it will return the deleted Employee. } else { return(null); } } } } return(null); }
public static pojo.Task readTask(String code, synupEntities database) { var query = from task in database.Tasks where task.code == code select task; return(query.SingleOrDefault()); }
/// <summary> /// /// </summary> /// <param name="pCode"></param> /// <param name="seContext"></param> /// <returns></returns> public static pojo.Team readTeam(String pCode, synupEntities seContext) { var query = from team in seContext.Teams where team.code == pCode select team; return(query.SingleOrDefault()); }
/// <summary> /// Given the code, it is searched in the database and will return the first employee found with the given code. /// </summary> /// <param name="pNif">Nif, Code key of the employee</param> /// <param name="_database">Context of the connection</param> /// <returns>If the code is already on the database it will return the Employee, otherwise it will return a null.</returns> public static pojo.Employee readEmployee(String _pNif, synupEntities _database) { var query = from employee in _database.Employees where employee.nif == _pNif select employee; return(query.SingleOrDefault()); }
/// <summary> /// Attachs teams /// </summary> /// <param name="pContext"></param> /// <param name="pTeamHistory">The object which were attachs</param> private static void tryAttach(synupEntities pContext, pojo.TeamHistory pTeamHistory) { var entry = pContext.Entry(pTeamHistory); if (entry.State == System.Data.Entity.EntityState.Detached) { pContext.TeamHistories.Attach(pTeamHistory); } }
/// <summary> /// Attachs employees to the current session so it can be updated. /// </summary> /// <param name="pContext">The session the operation will be performed under.</param> /// <param name="pEmployee">The object which were attachs</param> private static void tryAttach(synupEntities pContext, pojo.Employee pEmployee) { var entry = pContext.Entry(pEmployee); if (entry.State == System.Data.Entity.EntityState.Detached) { pContext.Employees.Attach(pEmployee); } }
private static void tryAttach(synupEntities _context, pojo.Task _task) { var entry = _context.Entry(_task); if (entry.State == System.Data.Entity.EntityState.Detached) { _context.Tasks.Attach(_task); } }
/// <summary> /// Receives the team that is meant to be updated /// </summary> /// <param name="pTeam">Receives the team that will be updated</param> /// <returns>Returns a boolean whether the team has been updated succesfully or not.</returns> public static bool updateTeam(pojo.Team pTeam) { using (var context = new synupEntities()) { pojo.Team _oTeam = readTeam(pTeam.code, context); //tryAttach(context, _oTeam); _oTeam.name = pTeam.name; return(commitChanges(context)); } }
/// <summary> /// Given the code, it is searched in the database and will return the first result. /// </summary> /// <param name="code">Code key of the teamHistory.</param> /// <returns>If the code is already on the database it will return the TeamHistory, otherwise it will return a null.</returns> public static pojo.TeamHistory readTeamHistory(String pNif, String pCodeTeam) { //var query = from teamHistory in new synupEntities().TeamHistories // where teamHistory.id_employee == pNif && teamHistory.id_team == pCodeTeam && // select teamHistory; synupEntities _synupE = new synupEntities(); var subquery = _synupE.TeamHistories.Where(x => x.id_employee == pNif && x.id_team == pCodeTeam).Max(x => x.id); var query = _synupE.TeamHistories.Where(x => x.id == subquery); return(query.SingleOrDefault()); }
private static bool commitChanges(synupEntities _database) { try { _database.SaveChanges(); return(true); } catch (Exception e) { Debug.WriteLine(e); return(false); } }
//private static synupEntities database = new synupEntities(); /* DELETED METHOD - No used. * /// <summary> * /// Saves the changes done over the database. * /// </summary> * /// <returns>Returns a boolean depending in whether the operation is completed succesfully or not.</returns> * private static bool commitChanges() * { * try * { * new synupEntities().SaveChanges(); * return true; * } * catch (Exception e) * { * Debug.WriteLine(e); * return false; * } * } */ /// <summary> /// Saves the changes done over the database. /// </summary> /// <returns>Returns a boolean depending in whether the operation is completed succesfully or not.</returns> private static bool commitChanges(synupEntities _database) { //The saveChanges will be called under a trycatch instruction. If the command is completed succesfully it will return a true. try { _database.SaveChanges(); return(true); } catch (Exception e) { //We write the error in the Debug window of the system console. Debug.WriteLine(e); return(false); } }
/// <summary> /// Creates a given task. /// </summary> /// <param name="t">Receives the object Task that will be inserted in the database</param> /// <returns>Returns a boolean depending in the outcome of the insert - true if it is successfull</returns> public static bool createTask(pojo.Task t) { pojo.Task foundTask = readTask(t.code); //Finds the received task in the database. if (foundTask == null) { //database.Tasks.Add(t); //If the task doesn't exist already in the database, it will be inserted. using (var context = new synupEntities()) { context.Tasks.Add(t); return(commitChanges(context)); } } return(false); }
/// <summary> /// Checks the Employee foreign keys and performs an update on their values. /// </summary> /// <param name="_employee">The employee object we want to check</param> /// <returns>Returns a boolean with the outcome of the update operation</returns> public static bool checkRelations(Employee _employee) { using (var context = new synupEntities()) { //Finds all the Teams the employee is in. var query = from th in context.TeamHistories where th.id_employee.Equals(_employee.nif) select th; //Deletes all the relations with teams the employee might have. foreach (var member in query) { if (TeamHistoryConnection.deleteTeamHistory(member.id_employee, member.id_team) == null) { return(false); } } //Finds all the tasks the employee has been given. //If the task is not finished, it will be abandonned. If the task is ongoing, it will be abandonned and therefore given a finishdate. var query2 = from th in context.TaskHistories where th.id_employee.Equals(_employee.nif) && (th.isFinished == 0 || th.finishDate == null) select th; //For each TaskHistories register found. foreach (var q in query2) { //It will find the tasks tied to it. var qu = from task in context.Tasks where task.code.Equals(q.id_task) select task; //Will update the state of the found tasks to ABANDONNED. foreach (var ta in qu) { ta.state = (int)TaskState.ABANDONED; TaskConnection.updateTask(ta); } //Will set the finish date to todays date. q.finishDate = DateTime.Now; } //Returns whether the update has been done sucesfully or not. return(commitChanges(context)); } }
/// <summary> /// Creates a given team. /// </summary> /// <param name="pTeam">Receives the object Team that will be inserted in the database.</param> /// <returns>Returns a boolean depending in the outcome of the insert - true if it is successfull</returns> public static bool createTeam(pojo.Team pTeam) { //openConnection(); pojo.Team _oTeam = readTeam(pTeam.code); //Finds the received team in the database. if (_oTeam == null) //If the team doesn't exist already in the database, it will be inserted. { using (var context = new synupEntities()) { context.Teams.Add(pTeam); return(commitChanges(context)); } } return(false); }
/// <summary> /// Inserts in the database the given employee by parameter. Checks if the employee already exists. /// </summary> /// <param name="pEmployee">Receives the object Employee that will be inserted in the database.</param> /// <returns>Returns a boolean depending in the outcome of the insert - true if it is successfull</returns> public static bool createEmployee(pojo.Employee pEmployee) { pojo.Employee _oEmployee = readEmployee(pEmployee.nif); //Finds the received employee in the database. if (_oEmployee == null) { using (var context = new synupEntities()) { pEmployee.password = MD5Hash(pEmployee.password); context.Employees.Add(pEmployee); //If the employee doesn't exist already in the database, it will be inserted. return(commitChanges(context)); } } return(false); }
/// <summary> /// Updates a given teamHistory with the new parameters of it. /// </summary> /// <param name="pTeamHistory">Receives the teamHistory that will be updated.</param> /// <returns>Returns a boolean whether the teamHistory has been updated succesfully or not.</returns> public static bool updateTeamHistory(String pNif, String pCodeTeam /*, DateTime pEntranceDate,*/, DateTime pExitDate) { using (var context = new synupEntities()) { pojo.TeamHistory modifiedTeamHistory = null; modifiedTeamHistory = readTeamHistory(pNif, pCodeTeam, context); if (modifiedTeamHistory != null) { tryAttach(context, modifiedTeamHistory); //modifiedTeamHistory.id_employee = pNif; //modifiedTeamHistory.id_team = pCodeTeam; //modifiedTeamHistory.entranceDay = pEntranceDate; modifiedTeamHistory.exitDate = pExitDate; } return(commitChanges(context)); } }
/// <summary> /// Receives the employee that is meant to be updated and updates it. /// </summary> /// <param name="pEmployee">Receives the employee that will be updated</param> /// <returns>Returns a boolean whether the employee has been updated succesfully or not.</returns> public static bool updateEmployee(pojo.Employee pEmployee) { using (var context = new synupEntities()) { pojo.Employee _oEmployee = readEmployee(pEmployee.nif, context); if (_oEmployee != null) { _oEmployee.name = pEmployee.name; _oEmployee.surname = pEmployee.surname; _oEmployee.phone = pEmployee.phone; _oEmployee.email = pEmployee.email; _oEmployee.adress = pEmployee.adress; _oEmployee.username = pEmployee.username; _oEmployee.password = _oEmployee.password; return(commitChanges(context)); } } return(false); }
/// <summary> /// /// </summary> /// <param name="_EmpNif"></param> /// <param name="_TeamCode"></param> /// <returns></returns> public static bool addToTeam(/*pojo.Employee pEmployee, pojo.Team pTeam*/ String _EmpNif, String _TeamCode) { model.pojo.TeamHistory _oTeamHistory = new model.pojo.TeamHistory(); if (_EmpNif != null && _TeamCode != null) { using (var q = new synupEntities()) { _oTeamHistory.id_employee = _EmpNif; _oTeamHistory.id_team = _TeamCode; //_oTeamHistory.Employee = pEmployee; //_oTeamHistory.Team = pTeam; _oTeamHistory.entranceDay = DateTime.Today; //database.TeamHistories.Add(_oTeamHistory); q.TeamHistories.Add(_oTeamHistory); return(commitChanges(q)); } } return(false); }
/// <summary> /// CHECK THAT THE TEAM THAT IS DELETED WONT HAVE ANY FOREIGN KEYS THAT REFERENCE IT /// </summary> /// <param name="team"></param> private static bool checkTeamMembers(Team team) { using (var _context = new synupEntities()) { var query = from th in _context.TeamHistories where th.id_team.Equals(team.code) select th; foreach (var member in query) { if (TeamHistoryConnection.deleteTeamHistory(member.id_employee, member.id_team) == null) { return(false); } } var query2 = from task in _context.Tasks where task.id_team.Equals(team.code) select task; foreach (var task in query2) { task.id_team = null; task.state = (int)TaskState.UNSELECTED; task.Team = null; if (!TaskConnection.updateTask(task)) { return(false); } } /*team.Tasks = null; * team.TeamHistories = null;*/ return(commitChanges(_context)); } }
/// <summary> /// /// </summary> /// <param name="_codeTeam"></param> /// <returns></returns> public static List <pojo.TeamMember> readTeamMembers(String _codeTeam) { using (var context = new synupEntities()) { List <TeamMember> toReturn = new List <TeamMember>(); var query = from employee in context.Employees join record in context.TeamHistories on employee.nif equals record.id_employee where record.id_team.Equals(_codeTeam) && record.exitDate == null select new { Emp = employee, His = record }; foreach (var s in query) { TeamMember tm = new TeamMember(s.Emp.nif, s.Emp.name, s.Emp.surname, s.His.entranceDay); toReturn.Add(tm); } return(toReturn); } }
/// <summary> /// Deletes a given teamHistory. /// </summary> /// <param name="t">Receives the teamHistory that is meant to be deleted.</param> /// <returns>Returns the teamHistory that has been deleted.</returns> public static pojo.TeamHistory deleteTeamHistory(String pNif, String pCode) { using (var context = new synupEntities()) { pojo.TeamHistory foundTeamHistory = readTeamHistory(pNif, pCode, context); //Finds the received teamHistory in the database. if (foundTeamHistory != null) //If the teamHistory has been found - meaning that it exists: { tryAttach(context, foundTeamHistory); //foundTeamHistory //foundTeamHistory.Team = null; context.TeamHistories.Remove(foundTeamHistory); //Will be deleted. if (commitChanges(context)) { return(foundTeamHistory); //If the changes are commited succesfully it will return the deleted TeamHistory. } else { return(null); } } } return(null); }
/// <summary> /// Given the code, it is searched in the database and will return the first result. /// </summary> /// <param name="code">Code key of the teamHistory.</param> /// <returns>If the code is already on the database it will return the TeamHistory, otherwise it will return a null.</returns> public static pojo.TeamHistory readTeamHistory(String pNif, String pCodeTeam, synupEntities seContext) { //var query = from teamHistory in seContext.TeamHistories // where teamHistory.id_employee == pNif && teamHistory.id_team == pCodeTeam // select teamHistory; var subquery = seContext.TeamHistories.Where(x => x.id_employee == pNif && x.id_team == pCodeTeam).Max(x => x.id); var query = seContext.TeamHistories.Where(x => x.id == subquery); return(query.SingleOrDefault()); }