TimesheetPersistence implements IPersistTimesheet, IDeleteTimesheetEntry { @Transactional @Override public List<ProjectAssignmentStatus> persistTimesheetWeek(Collection<TimesheetEntry> timesheetEntries, TimesheetComment comment, DateRange weekRange, User forUser) { Map<ProjectAssignment, List<TimesheetEntry>> timesheetRows = getTimesheetAsRows(timesheetEntries); List<ProjectAssignmentStatus> errorStatusses = new ArrayList<>(); Seq<Interval> lockedDatesInRange = timesheetLockService.findLockedDatesInRange(weekRange.getDateStart(), weekRange.getDateEnd(), forUser); List<Date> lockedDates = TimesheetLockService$.MODULE$.intervalToJavaDates(lockedDatesInRange); for (Map.Entry<ProjectAssignment, List<TimesheetEntry>> entry : timesheetRows.entrySet()) { try { getTimesheetPersister().validateAndPersist(entry.getKey(), entry.getValue(), weekRange, lockedDates); } catch (OverBudgetException e) { errorStatusses.add(e.getStatus()); } } boolean wholeWeekLocked = TimesheetLockService$.MODULE$.isRangeLocked(weekRange.getDateStart(), weekRange.getDateEnd(), lockedDatesInRange); if (!wholeWeekLocked && (!comment.getNewComment() || StringUtils.isNotBlank(comment.getComment()))) { timesheetCommentDAO.persist(comment); } return errorStatusses; } @Autowired  TimesheetPersistence(TimesheetDao timesheetDAO,
                                TimesheetCommentDao timesheetCommentDAO,
                                ProjectAssignmentStatusService projectAssignmentStatusService,
                                ProjectManagerNotifierService projectManagerNotifierService,
                                TimesheetLockService timesheetLockService,
                                ApplicationContext context); @Transactional void deleteAllTimesheetDataForUser(User user); @Transactional @Override List<ProjectAssignmentStatus> persistTimesheetWeek(Collection<TimesheetEntry> timesheetEntries,
                                                              TimesheetComment comment,
                                                              DateRange weekRange,
                                                              User forUser); @Transactional(rollbackFor = OverBudgetException.class, propagation = Propagation.REQUIRES_NEW) @NonAuditable void validateAndPersist(ProjectAssignment assignment,
                                   List<TimesheetEntry> entries,
                                   DateRange weekRange,
                                   List<Date> lockedDates);  }
@Test public void should_persist_individual_timesheet_entries_for_a_week() { TimesheetCommentId commentId = new TimesheetCommentId(1, new Date()); TimesheetComment comment = new TimesheetComment(commentId, "comment"); when(context.getBean(IPersistTimesheet.class)).thenReturn(persister); okStatus(); noLocks(); persister.persistTimesheetWeek(newEntries, comment, new DateRange(), UserObjectMother.createUser()); verify(commentDao).persist(comment); }
@Test public void should_not_persist_when_whole_week_is_locked() throws OverBudgetException { okStatus(); TimesheetCommentId commentId = new TimesheetCommentId(1, new Date()); TimesheetComment comment = new TimesheetComment(commentId, "comment"); Date s = baseDate.toDate(); DateTime end = baseDate.plusWeeks(1); Date e = end.toDate(); User user = UserObjectMother.createUser(); when(context.getBean(IPersistTimesheet.class)).thenReturn(persister); withLock(new Interval(baseDate, end)); persister.persistTimesheetWeek(newEntries, comment, new DateRange(s, e), user); verify(timesheetDAO, never()).persist((any(TimesheetEntry.class))); }
@Test public void should_not_persist_for_locked_day() throws OverBudgetException { okStatus(); TimesheetCommentId commentId = new TimesheetCommentId(1, new Date()); TimesheetComment comment = new TimesheetComment(commentId, "comment"); Date s = baseDate.toDate(); DateTime end = baseDate.plusWeeks(1); Date e = end.toDate(); User user = UserObjectMother.createUser(); when(context.getBean(IPersistTimesheet.class)).thenReturn(persister); TimesheetEntry entry = new TimesheetEntry(); TimesheetEntryId id = new TimesheetEntryId(); id.setProjectAssignment(assignment); id.setEntryDate(baseDate.plusDays(2).toDate()); entry.setEntryId(id); entry.setHours(5f); newEntries.add(entry); withLock(new Interval(baseDate.plusDays(2), end)); persister.persistTimesheetWeek(newEntries, comment, new DateRange(s, e), user); verify(timesheetDAO, times(1)).persist((any(TimesheetEntry.class))); verify(timesheetDAO, never()).delete((any(TimesheetEntry.class))); verify(commentDao, times(1)).persist(any(TimesheetComment.class)); }
@Test public void should_not_persist_comment_when_whole_week_is_locked() throws OverBudgetException { okStatus(); TimesheetCommentId commentId = new TimesheetCommentId(1, new Date()); TimesheetComment comment = new TimesheetComment(commentId, "comment"); Date s = baseDate.toDate(); DateTime end = baseDate.plusWeeks(1); Date e = end.toDate(); User user = UserObjectMother.createUser(); when(context.getBean(IPersistTimesheet.class)).thenReturn(persister); withLock(new Interval(baseDate, end)); persister.persistTimesheetWeek(newEntries, comment, new DateRange(s, e), user); verify(timesheetDAO, never()).persist((any(TimesheetEntry.class))); verify(commentDao, never()).persist(any(TimesheetComment.class)); }