Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project.service;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import javax.annotation.Resource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import project.model.project.CollaboratorInProject;
- import project.model.project.Project;
- import project.model.project.ProjectCostCalculationType;
- import project.model.project.ProjectList;
- import project.model.project.costcalculationinterface.AverageCost;
- import project.model.project.costcalculationinterface.CostCalculation;
- import project.model.project.costcalculationinterface.NewestCost;
- import project.model.project.costcalculationinterface.OldestCost;
- import project.model.task.Task;
- import project.model.task.TaskComparatorByEstimatedEndDate;
- import project.model.task.TaskList;
- import project.model.task.TaskReport;
- import project.model.user.User;
- import project.model.user.UserList;
- import static project.model.project.ProjectCostCalculationType.*;
- @Service
- @Transactional
- /*
- * Methods from task/tasklist that need information from two aggregate must be implement here
- */
- public class TaskService {
- @Autowired
- private ProjectList projectList;
- @Autowired
- private TaskList taskList;
- @Autowired
- private UserList userList;
- @Resource(name = "oldest")
- private CostCalculation oldest;
- @Resource(name = "newest")
- private CostCalculation newest;
- @Resource(name = "average")
- private CostCalculation average;
- /**
- * Task creator method. Creates task from TaskList. Invokes Task constructor, allowing the Task
- * object to do the management of exceptions.
- *
- * @param name refers to the task name
- * @param description refers to the task description
- * @return a task object if when creating a Task did not threw an exception.
- */
- public Task createNewTask(String projectId, String name, String description,
- Calendar startDate, Calendar endDate) {
- Project project = projectList.getProjectById(projectId);
- if (project.areDatesValid(startDate, endDate)) {
- return taskList.createNewTask(projectId, name, description, startDate, endDate);
- } else {
- throw new IllegalArgumentException("The period given to create a "
- + "task is not contained in the given Project period.");
- }
- }
- /**
- * Task creator method with estimated effort and budgetUnitaryCost It invokes the Task
- * constructor, allowing the Task object to manage itself regarding exceptions.
- *
- * @param name refers to the task name.
- * @param description refers to the task description.
- * @param estimatedEffort refers to the task estimated effort to be spent.
- * @param budgetUnitaryCost refers to the task budgeted unitary cost.
- * @return a task object if when creating a Task did not threw an exception.
- */
- public Task createNewTask(String projectId, String name, String description,
- Calendar startDate, Calendar endDate, double estimatedEffort,
- double budgetUnitaryCost) {
- Task createdTask = this.createNewTask(projectId, name, description, startDate, endDate);
- createdTask.setEstimatedEffort(estimatedEffort);
- createdTask.setBudgetUnitaryCost(budgetUnitaryCost);
- return createdTask;
- }
- /**
- * Method to create a task in {@link Project}, which is a parameter received. It ensures the task
- * is not created when the project reaches the closed status and that the task period is inside
- * the project's. It has to create the Calendar objects first to avoid validation overkill. Throws
- * IllegalArgumentExceptions if any of these validations fail and allows the Task to self-manage
- * its own restrictions.
- *
- * @param name refers to the task name.
- * @param description refers to the task description.
- * @param start refers to the relative days after the project started.
- * @param end refers to the relative days after the project started.
- * @return an Object task if successfully created, an exception if any validation fails.
- */
- public Task createNewTask(String projectId, String name, String description,
- int start, int end) {
- int zero = 0;
- Project project = projectList.getProjectById(projectId);
- if (start >= zero && end >= zero) {
- List<Calendar> taskDates = project.intToCalendarConverter(start, end);
- return taskList
- .createNewTask(projectId, name, description, taskDates.get(0), taskDates.get(1));
- } else {
- throw new IllegalArgumentException("Cannot create task with invalid dates. Did you write "
- + "negative numbers?");
- }
- }
- /**
- * Method to create a new task when the user is considering project estimated effort and budget
- * unitary cost.
- *
- * @param name refers to the name of the project.
- * @param description refers to the description of the project.
- * @param start refers to an integer which is the differential of days regarding the project's.
- * @param end refers to the an integer which is the differential of days regarding the project's.
- * @param estimatedEffort refers to the estimated effort in integer form.
- * @param budgetUnitaryCost reefers to the budget unitary cost in double form.
- * @return a new {@link Task} object if no checks fail.
- */
- public Task createNewTask(String projectId, String name, String description,
- int start, int end, int estimatedEffort, double budgetUnitaryCost) {
- Task createdTask = this.createNewTask(projectId, name, description, start, end);
- createdTask.setEstimatedEffort(estimatedEffort);
- createdTask.setBudgetUnitaryCost(budgetUnitaryCost);
- return createdTask;
- }
- /**
- * Obtains a list of unique user that, in a given Project and at a given date, do not have any
- * active connection to any task (i.e. are not attributed to tasks).
- *
- * @param projectId refers the ID of the Project.
- * @param atDate refers the Date that we're searching.
- * @return a List of unique Users.
- */
- public List<User> getUsersInProjectWithoutTasksAtDate(String projectId, Calendar atDate) {
- // Checks if projectId parameter is null or empty string and throws exception is either.
- if ("".equals(projectId) || null == projectId) {
- throw new IllegalArgumentException("Project Id cannot be empty or null.");
- }
- // Checks if atDate parameter is null and throws exception if either.
- if (null == atDate) {
- throw new IllegalArgumentException("Date must not be null argument.");
- }
- Project searchProject = projectList.getProjectById(projectId);
- List<CollaboratorInProject> collaboratorsOfProject = searchProject
- .getListCollaboratorsInProject();
- List<User> totalUsers = createAndReturnListOfUniqueUsersInProject(searchProject);
- List<Task> tasksOfProject = taskList.getTasks(projectId);
- // Using a set to avoid having to check for duplicate users
- Set<User> usersWithTasks = new HashSet<>();
- User user;
- for (CollaboratorInProject cip : collaboratorsOfProject) {
- for (Task task : tasksOfProject) {
- user = userList.searchUserByFullEmail(cip.getCipEmail());
- if (task.isCollaboratorInTaskActive(cip, atDate)) {
- usersWithTasks.add(user);
- }
- }
- }
- List<User> usersWithoutTasks = new ArrayList<>(totalUsers);
- usersWithoutTasks.removeAll(usersWithTasks);
- return usersWithoutTasks;
- }
- /**
- * Method to create an ArrayList containing all the unique Users in a given Project.
- *
- * @param project refers to the Project being considered.
- * @return ArrayList of User.
- */
- private List<User> createAndReturnListOfUniqueUsersInProject(Project project) {
- List<CollaboratorInProject> collaboratorsOfProject = project.getListCollaboratorsInProject();
- List<User> uniqueUsersInProject = new ArrayList<>();
- User user;
- for (CollaboratorInProject cip : collaboratorsOfProject) {
- user = userList.searchUserByFullEmail(cip.getCipEmail());
- if (!uniqueUsersInProject.contains(user)) {
- uniqueUsersInProject.add(user);
- }
- }
- return uniqueUsersInProject;
- }
- /**
- * Auxiliary method to get all open {@link Task} of a given {@link User} in a specific {@link
- * Project}
- *
- * @param projectId refers to the project ID we are searching for tasks of an user.
- * @param userEmail refers to the user email we are searching tasks of.
- * @return a list of tasks, empty if no matches are found.
- */
- public List<Task> getOpenTasksOfUserInProject(String projectId, String userEmail) {
- List<Task> openTasksOfUserInProject = new ArrayList<>();
- Project p = this.projectList.getProjectById(projectId);
- User u = this.userList.searchUserByFullEmail(userEmail);
- if (p != null && u != null) {
- List<CollaboratorInProject> allCollaboratorsOfUser = p
- .getCollaboratorInProjectList(userEmail);
- openTasksOfUserInProject =
- taskList.getOpenTasksOfUserInProject(projectId, allCollaboratorsOfUser);
- }
- return openTasksOfUserInProject;
- }
- public boolean removeCollaborator(String idProject, String email, Calendar now) {
- Project project = projectList.getProjectById(idProject);
- List<CollaboratorInProject> cipList = project.getCollaboratorInProjectList(email);
- if (!cipList.isEmpty() && project.isValidRemoveDate(now)) {
- project.removeUserfromProjectAndAllHisTasks(now, email);
- taskList.removeUserfromProjectAndAllHisTasks(idProject, now, cipList);
- projectList.save(project);
- return true;
- }
- return false;
- }
- public CostCalculation defineStrategy(ProjectCostCalculationType calcType)
- {
- switch (calcType) {
- case OLDEST:
- return oldest;
- case NEWEST:
- return newest;
- case AVERAGE:
- default:
- return average;
- }
- }
- public double getTotalCostInProject(String projectId, Calendar moment) {
- Project project1 = projectList.getProjectById(projectId);
- double totalCost = 0.0;
- if (project1 != null) {
- ProjectCostCalculationType costCalculationType = project1.getCostCalculationType();
- totalCost = defineStrategy(costCalculationType).calculateProjectCost(projectId, moment);
- }
- return totalCost;
- }
- public double getTotalCostInTask(String projectId, String taskId, Calendar moment) {
- Project project1 = projectList.getProjectById(projectId);
- double taskCost = 0.0;
- if (project1 == null) {
- return taskCost;
- }
- Task t = taskList.getTaskById(projectId, taskId);
- if (t != null) {
- ProjectCostCalculationType costCalculationType = project1.getCostCalculationType();
- taskCost = defineStrategy(costCalculationType).calculateTaskCost(projectId, taskId, moment);
- }
- return taskCost;
- }
- /**
- * Method to return all the tasks where a Collaborator can work at the moment. Goes through all
- * the tasks of the User and makes a list of the ones that have the project
- *
- * @param email user email
- * @param projectId id of project
- * @return a list of tasks in which the User can work at the moment
- */
- public List<Task> getPendingTasksOfUserInProject(String email, String projectId) {
- List<Task> tasksFromProject = new ArrayList<>();
- User user = userList.searchUserByFullEmail(email);
- List<Task> allTasks = taskList.getTasksToDo(user);
- for (Task eachTask : allTasks) {
- if (eachTask.isProject(projectId)) {
- tasksFromProject.add(eachTask);
- }
- }
- TaskComparatorByEstimatedEndDate byEstimatedEndDate = new TaskComparatorByEstimatedEndDate();
- new ArrayList<>(tasksFromProject).sort(byEstimatedEndDate);
- return tasksFromProject;
- }
- private List<TaskReport> getAllTaskReportOfProject(String projectId) {
- List<Task> listOfTask = taskList.getTasks(projectId);
- List<TaskReport> listOfReport = new ArrayList<>();
- for (Task t : listOfTask) {
- listOfReport.addAll(t.getTaskReportList());
- }
- return listOfReport;
- }
- public List<TaskReport> getAllTaskReportsBeforeDate(String projectID, Calendar atDate) {
- List<TaskReport> listOfTaskReport = getAllTaskReportOfProject(projectID);
- List<TaskReport> listOfTaskReportAtDate = new ArrayList<>();
- for (TaskReport tr : listOfTaskReport) {
- if (!tr.getPeriod().getEndDate().after(atDate)) {
- listOfTaskReportAtDate.add(tr);
- }
- }
- return listOfTaskReportAtDate;
- }
- public List<CollaboratorInProject> getListOfCipsAssociatedToTaskReport(TaskReport taskReport,
- String projectId) {
- Project project = projectList.getProjectById(projectId);
- List<CollaboratorInProject> cipListInTaskReport = new ArrayList<>();
- List<CollaboratorInProject> cipListInTaskReportPeriod = project
- .getListOfCollaboratorInProjectInPeriod(taskReport.getPeriod().getStartDate(),
- taskReport.getPeriod().getEndDate());
- for (CollaboratorInProject cip : cipListInTaskReportPeriod) {
- if (taskReport.getCollaboratorInTask().getCollaboratorInProject().getCipEmail()
- .equals(cip.getCipEmail())) {
- cipListInTaskReport.add(cip);
- }
- }
- return cipListInTaskReport;
- }
- public boolean cipDateValidator(String projectID, String email, Calendar startDate,
- Calendar endDate) {
- Calendar testDate = (Calendar) startDate.clone();
- while (!testDate.after(endDate)) {
- if (!projectList.getProjectById(projectID).isCollaboratorActive(testDate, email)) {
- return false;
- }
- testDate.add(Calendar.DAY_OF_MONTH, 1);
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement