Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.70 KB | None | 0 0
  1. package project.service;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Calendar;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Set;
  8. import javax.annotation.Resource;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import project.model.project.CollaboratorInProject;
  13. import project.model.project.Project;
  14. import project.model.project.ProjectCostCalculationType;
  15. import project.model.project.ProjectList;
  16. import project.model.project.costcalculationinterface.AverageCost;
  17. import project.model.project.costcalculationinterface.CostCalculation;
  18. import project.model.project.costcalculationinterface.NewestCost;
  19. import project.model.project.costcalculationinterface.OldestCost;
  20. import project.model.task.Task;
  21. import project.model.task.TaskComparatorByEstimatedEndDate;
  22. import project.model.task.TaskList;
  23. import project.model.task.TaskReport;
  24. import project.model.user.User;
  25. import project.model.user.UserList;
  26.  
  27. import static project.model.project.ProjectCostCalculationType.*;
  28.  
  29. @Service
  30. @Transactional
  31. /*
  32.  * Methods from task/tasklist that need information from two aggregate must be implement here
  33.  */
  34. public class TaskService {
  35.  
  36.   @Autowired
  37.   private ProjectList projectList;
  38.   @Autowired
  39.   private TaskList taskList;
  40.   @Autowired
  41.   private UserList userList;
  42.  
  43.   @Resource(name = "oldest")
  44.   private CostCalculation oldest;
  45.  
  46.   @Resource(name = "newest")
  47.   private CostCalculation newest;
  48.  
  49.   @Resource(name = "average")
  50.   private CostCalculation average;
  51.  
  52.   /**
  53.    * Task creator method. Creates task from TaskList. Invokes Task constructor, allowing the Task
  54.    * object to do the management of exceptions.
  55.    *
  56.    * @param name refers to the task name
  57.    * @param description refers to the task description
  58.    * @return a task object if when creating a Task did not threw an exception.
  59.    */
  60.   public Task createNewTask(String projectId, String name, String description,
  61.       Calendar startDate, Calendar endDate) {
  62.     Project project = projectList.getProjectById(projectId);
  63.     if (project.areDatesValid(startDate, endDate)) {
  64.       return taskList.createNewTask(projectId, name, description, startDate, endDate);
  65.     } else {
  66.       throw new IllegalArgumentException("The period given to create a "
  67.           + "task is not contained in the given Project period.");
  68.     }
  69.   }
  70.  
  71.   /**
  72.    * Task creator method with estimated effort and budgetUnitaryCost It invokes the Task
  73.    * constructor, allowing the Task object to manage itself regarding exceptions.
  74.    *
  75.    * @param name refers to the task name.
  76.    * @param description refers to the task description.
  77.    * @param estimatedEffort refers to the task estimated effort to be spent.
  78.    * @param budgetUnitaryCost refers to the task budgeted unitary cost.
  79.    * @return a task object if when creating a Task did not threw an exception.
  80.    */
  81.   public Task createNewTask(String projectId, String name, String description,
  82.       Calendar startDate, Calendar endDate, double estimatedEffort,
  83.       double budgetUnitaryCost) {
  84.     Task createdTask = this.createNewTask(projectId, name, description, startDate, endDate);
  85.     createdTask.setEstimatedEffort(estimatedEffort);
  86.     createdTask.setBudgetUnitaryCost(budgetUnitaryCost);
  87.  
  88.     return createdTask;
  89.   }
  90.  
  91.   /**
  92.    * Method to create a task in {@link Project}, which is a parameter received. It ensures the task
  93.    * is not created when the project reaches the closed status and that the task period is inside
  94.    * the project's. It has to create the Calendar objects first to avoid validation overkill. Throws
  95.    * IllegalArgumentExceptions if any of these validations fail and allows the Task to self-manage
  96.    * its own restrictions.
  97.    *
  98.    * @param name refers to the task name.
  99.    * @param description refers to the task description.
  100.    * @param start refers to the relative days after the project started.
  101.    * @param end refers to the relative days after the project started.
  102.    * @return an Object task if successfully created, an exception if any validation fails.
  103.    */
  104.   public Task createNewTask(String projectId, String name, String description,
  105.       int start, int end) {
  106.     int zero = 0;
  107.     Project project = projectList.getProjectById(projectId);
  108.     if (start >= zero && end >= zero) {
  109.       List<Calendar> taskDates = project.intToCalendarConverter(start, end);
  110.       return taskList
  111.           .createNewTask(projectId, name, description, taskDates.get(0), taskDates.get(1));
  112.     } else {
  113.       throw new IllegalArgumentException("Cannot create task with invalid dates. Did you write "
  114.           + "negative numbers?");
  115.     }
  116.   }
  117.  
  118.   /**
  119.    * Method to create a new task when the user is considering project estimated effort and budget
  120.    * unitary cost.
  121.    *
  122.    * @param name refers to the name of the project.
  123.    * @param description refers to the description of the project.
  124.    * @param start refers to an integer which is the differential of days regarding the project's.
  125.    * @param end refers to the an integer which is the differential of days regarding the project's.
  126.    * @param estimatedEffort refers to the estimated effort in integer form.
  127.    * @param budgetUnitaryCost reefers to the budget unitary cost in double form.
  128.    * @return a new {@link Task} object if no checks fail.
  129.    */
  130.   public Task createNewTask(String projectId, String name, String description,
  131.       int start, int end, int estimatedEffort, double budgetUnitaryCost) {
  132.     Task createdTask = this.createNewTask(projectId, name, description, start, end);
  133.     createdTask.setEstimatedEffort(estimatedEffort);
  134.     createdTask.setBudgetUnitaryCost(budgetUnitaryCost);
  135.  
  136.     return createdTask;
  137.   }
  138.  
  139.   /**
  140.    * Obtains a list of unique user that, in a given Project and at a given date, do not have any
  141.    * active connection to any task (i.e. are not attributed to tasks).
  142.    *
  143.    * @param projectId refers the ID of the Project.
  144.    * @param atDate refers the Date that we're searching.
  145.    * @return a List of unique Users.
  146.    */
  147.   public List<User> getUsersInProjectWithoutTasksAtDate(String projectId, Calendar atDate) {
  148.     // Checks if projectId parameter is null or empty string and throws exception is either.
  149.     if ("".equals(projectId) || null == projectId) {
  150.       throw new IllegalArgumentException("Project Id cannot be empty or null.");
  151.     }
  152.  
  153.     // Checks if atDate parameter is null and throws exception if either.
  154.     if (null == atDate) {
  155.       throw new IllegalArgumentException("Date must not be null argument.");
  156.     }
  157.  
  158.     Project searchProject = projectList.getProjectById(projectId);
  159.     List<CollaboratorInProject> collaboratorsOfProject = searchProject
  160.         .getListCollaboratorsInProject();
  161.     List<User> totalUsers = createAndReturnListOfUniqueUsersInProject(searchProject);
  162.     List<Task> tasksOfProject = taskList.getTasks(projectId);
  163.     // Using a set to avoid having to check for duplicate users
  164.     Set<User> usersWithTasks = new HashSet<>();
  165.     User user;
  166.  
  167.     for (CollaboratorInProject cip : collaboratorsOfProject) {
  168.       for (Task task : tasksOfProject) {
  169.         user = userList.searchUserByFullEmail(cip.getCipEmail());
  170.         if (task.isCollaboratorInTaskActive(cip, atDate)) {
  171.           usersWithTasks.add(user);
  172.         }
  173.       }
  174.     }
  175.  
  176.     List<User> usersWithoutTasks = new ArrayList<>(totalUsers);
  177.     usersWithoutTasks.removeAll(usersWithTasks);
  178.  
  179.     return usersWithoutTasks;
  180.   }
  181.  
  182.   /**
  183.    * Method to create an ArrayList containing all the unique Users in a given Project.
  184.    *
  185.    * @param project refers to the Project being considered.
  186.    * @return ArrayList of User.
  187.    */
  188.   private List<User> createAndReturnListOfUniqueUsersInProject(Project project) {
  189.     List<CollaboratorInProject> collaboratorsOfProject = project.getListCollaboratorsInProject();
  190.     List<User> uniqueUsersInProject = new ArrayList<>();
  191.     User user;
  192.  
  193.     for (CollaboratorInProject cip : collaboratorsOfProject) {
  194.       user = userList.searchUserByFullEmail(cip.getCipEmail());
  195.       if (!uniqueUsersInProject.contains(user)) {
  196.         uniqueUsersInProject.add(user);
  197.       }
  198.     }
  199.  
  200.     return uniqueUsersInProject;
  201.   }
  202.  
  203.   /**
  204.    * Auxiliary method to get all open {@link Task} of a given {@link User} in a specific {@link
  205.    * Project}
  206.    *
  207.    * @param projectId refers to the project ID we are searching for tasks of an user.
  208.    * @param userEmail refers to the user email we are searching tasks of.
  209.    * @return a list of tasks, empty if no matches are found.
  210.    */
  211.   public List<Task> getOpenTasksOfUserInProject(String projectId, String userEmail) {
  212.     List<Task> openTasksOfUserInProject = new ArrayList<>();
  213.     Project p = this.projectList.getProjectById(projectId);
  214.     User u = this.userList.searchUserByFullEmail(userEmail);
  215.     if (p != null && u != null) {
  216.       List<CollaboratorInProject> allCollaboratorsOfUser = p
  217.           .getCollaboratorInProjectList(userEmail);
  218.       openTasksOfUserInProject =
  219.           taskList.getOpenTasksOfUserInProject(projectId, allCollaboratorsOfUser);
  220.     }
  221.     return openTasksOfUserInProject;
  222.   }
  223.  
  224.   public boolean removeCollaborator(String idProject, String email, Calendar now) {
  225.     Project project = projectList.getProjectById(idProject);
  226.     List<CollaboratorInProject> cipList = project.getCollaboratorInProjectList(email);
  227.     if (!cipList.isEmpty() && project.isValidRemoveDate(now)) {
  228.       project.removeUserfromProjectAndAllHisTasks(now, email);
  229.       taskList.removeUserfromProjectAndAllHisTasks(idProject, now, cipList);
  230.       projectList.save(project);
  231.       return true;
  232.     }
  233.     return false;
  234.   }
  235.  
  236.  
  237.   public CostCalculation defineStrategy(ProjectCostCalculationType calcType)
  238.   {
  239.     switch (calcType) {
  240.       case OLDEST:
  241.         return oldest;
  242.       case NEWEST:
  243.         return newest;
  244.       case AVERAGE:
  245.       default:
  246.         return average;
  247.     }
  248.  
  249.   }
  250.   public double getTotalCostInProject(String projectId, Calendar moment) {
  251.  
  252.     Project project1 = projectList.getProjectById(projectId);
  253.  
  254.     double totalCost = 0.0;
  255.  
  256.     if (project1 != null) {
  257.       ProjectCostCalculationType costCalculationType = project1.getCostCalculationType();
  258.       totalCost = defineStrategy(costCalculationType).calculateProjectCost(projectId, moment);
  259.     }
  260.  
  261.     return totalCost;
  262.  
  263.   }
  264.  
  265.   public double getTotalCostInTask(String projectId, String taskId, Calendar moment) {
  266.  
  267.     Project project1 = projectList.getProjectById(projectId);
  268.     double taskCost = 0.0;
  269.  
  270.     if (project1 == null) {
  271.       return taskCost;
  272.     }
  273.  
  274.     Task t = taskList.getTaskById(projectId, taskId);
  275.  
  276.     if (t != null) {
  277.  
  278.       ProjectCostCalculationType costCalculationType = project1.getCostCalculationType();
  279.       taskCost = defineStrategy(costCalculationType).calculateTaskCost(projectId, taskId, moment);
  280.     }
  281.  
  282.     return taskCost;
  283.   }
  284.  
  285.   /**
  286.    * Method to return all the tasks where a Collaborator can work at the moment. Goes through all
  287.    * the tasks of the User and makes a list of the ones that have the project
  288.    *
  289.    * @param email user email
  290.    * @param projectId id of project
  291.    * @return a list of tasks in which the User can work at the moment
  292.    */
  293.   public List<Task> getPendingTasksOfUserInProject(String email, String projectId) {
  294.     List<Task> tasksFromProject = new ArrayList<>();
  295.     User user = userList.searchUserByFullEmail(email);
  296.  
  297.     List<Task> allTasks = taskList.getTasksToDo(user);
  298.     for (Task eachTask : allTasks) {
  299.       if (eachTask.isProject(projectId)) {
  300.         tasksFromProject.add(eachTask);
  301.       }
  302.     }
  303.  
  304.     TaskComparatorByEstimatedEndDate byEstimatedEndDate = new TaskComparatorByEstimatedEndDate();
  305.     new ArrayList<>(tasksFromProject).sort(byEstimatedEndDate);
  306.  
  307.     return tasksFromProject;
  308.   }
  309.  
  310.   private List<TaskReport> getAllTaskReportOfProject(String projectId) {
  311.  
  312.     List<Task> listOfTask = taskList.getTasks(projectId);
  313.     List<TaskReport> listOfReport = new ArrayList<>();
  314.  
  315.     for (Task t : listOfTask) {
  316.       listOfReport.addAll(t.getTaskReportList());
  317.  
  318.     }
  319.  
  320.     return listOfReport;
  321.   }
  322.  
  323.   public List<TaskReport> getAllTaskReportsBeforeDate(String projectID, Calendar atDate) {
  324.  
  325.     List<TaskReport> listOfTaskReport = getAllTaskReportOfProject(projectID);
  326.     List<TaskReport> listOfTaskReportAtDate = new ArrayList<>();
  327.  
  328.     for (TaskReport tr : listOfTaskReport) {
  329.       if (!tr.getPeriod().getEndDate().after(atDate)) {
  330.         listOfTaskReportAtDate.add(tr);
  331.       }
  332.     }
  333.  
  334.     return listOfTaskReportAtDate;
  335.   }
  336.  
  337.   public List<CollaboratorInProject> getListOfCipsAssociatedToTaskReport(TaskReport taskReport,
  338.       String projectId) {
  339.  
  340.     Project project = projectList.getProjectById(projectId);
  341.  
  342.     List<CollaboratorInProject> cipListInTaskReport = new ArrayList<>();
  343.  
  344.     List<CollaboratorInProject> cipListInTaskReportPeriod = project
  345.         .getListOfCollaboratorInProjectInPeriod(taskReport.getPeriod().getStartDate(),
  346.             taskReport.getPeriod().getEndDate());
  347.  
  348.     for (CollaboratorInProject cip : cipListInTaskReportPeriod) {
  349.       if (taskReport.getCollaboratorInTask().getCollaboratorInProject().getCipEmail()
  350.           .equals(cip.getCipEmail())) {
  351.         cipListInTaskReport.add(cip);
  352.       }
  353.     }
  354.  
  355.     return cipListInTaskReport;
  356.   }
  357.  
  358.   public boolean cipDateValidator(String projectID, String email, Calendar startDate,
  359.       Calendar endDate) {
  360.     Calendar testDate = (Calendar) startDate.clone();
  361.     while (!testDate.after(endDate)) {
  362.       if (!projectList.getProjectById(projectID).isCollaboratorActive(testDate, email)) {
  363.         return false;
  364.       }
  365.       testDate.add(Calendar.DAY_OF_MONTH, 1);
  366.     }
  367.     return true;
  368.   }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement