List of usage examples for org.joda.time LocalDateTime minusWeeks
public LocalDateTime minusWeeks(int weeks)
From source file:com.axelor.apps.crm.service.batch.BatchEventReminder.java
License:Open Source License
private boolean isExpired(EventReminder eventReminder) { LocalDateTime startDateTime = eventReminder.getEvent().getStartDateTime(); int durationTypeSelect = eventReminder.getDurationTypeSelect(); switch (durationTypeSelect) { case IEventReminder.DURATION_MINUTES: if ((startDateTime.minusMinutes(eventReminder.getDuration())).isBefore(today)) { return true; }/*from ww w .j a va 2 s . co m*/ break; case IEventReminder.DURATION_HOURS: if ((startDateTime.minusHours(eventReminder.getDuration())).isBefore(today)) { return true; } break; case IEventReminder.DURATION_DAYS: if ((startDateTime.minusDays(eventReminder.getDuration())).isBefore(today)) { return true; } break; case IEventReminder.DURATION_WEEKS: if ((startDateTime.minusWeeks(eventReminder.getDuration())).isBefore(today)) { return true; } break; } return false; }
From source file:com.axelor.apps.crm.service.EventService.java
License:Open Source License
@Transactional public void addRecurrentEventsByMonths(Event event, int periodicity, int endType, int repetitionsNumber, LocalDate endDate, int monthRepeatType) { Event lastEvent = event;// w w w.jav a 2 s . c om if (monthRepeatType == 1) { int dayOfMonth = event.getStartDateTime().getDayOfMonth(); if (endType == 1) { int repeated = 0; while (repeated != repetitionsNumber) { Event copy = eventRepo.copy(lastEvent, false); copy.setParentEvent(lastEvent); if (copy.getStartDateTime().plusMonths(periodicity).dayOfMonth() .getMaximumValue() >= dayOfMonth) { copy.setStartDateTime(copy.getStartDateTime().plusMonths(periodicity)); copy.setEndDateTime(copy.getEndDateTime().plusMonths(periodicity)); eventRepo.save(copy); repeated++; lastEvent = copy; } } } else { while (!lastEvent.getStartDateTime().plusMonths(periodicity).isAfter(endDate)) { Event copy = eventRepo.copy(lastEvent, false); copy.setParentEvent(lastEvent); if (copy.getStartDateTime().plusMonths(periodicity).dayOfMonth() .getMaximumValue() >= dayOfMonth) { copy.setStartDateTime(copy.getStartDateTime().plusMonths(periodicity)); copy.setEndDateTime(copy.getEndDateTime().plusMonths(periodicity)); eventRepo.save(copy); lastEvent = copy; } } } } else { int dayOfWeek = event.getStartDateTime().getDayOfWeek(); int positionInMonth = 0; if (event.getStartDateTime().getDayOfMonth() % 7 == 0) { positionInMonth = event.getStartDateTime().getDayOfMonth() / 7; } else { positionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1; } if (endType == 1) { int repeated = 0; while (repeated != repetitionsNumber) { Event copy = eventRepo.copy(lastEvent, false); copy.setParentEvent(lastEvent); LocalDateTime nextDateTime = new LocalDateTime(copy.getStartDateTime()); nextDateTime.plusMonths(periodicity); int nextDayOfWeek = nextDateTime.getDayOfWeek(); if (nextDayOfWeek > dayOfWeek) { nextDateTime.minusDays(nextDayOfWeek - dayOfWeek); } else { nextDateTime.plusDays(dayOfWeek - nextDayOfWeek); } int nextPositionInMonth = 0; if (event.getStartDateTime().getDayOfMonth() % 7 == 0) { nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7; } else { nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1; } if (nextPositionInMonth > positionInMonth) { nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth); } else { nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth); } Duration dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime()); copy.setStartDateTime(nextDateTime); copy.setEndDateTime(nextDateTime.plus(dur)); eventRepo.save(copy); repeated++; lastEvent = copy; } } else { LocalDateTime nextDateTime = new LocalDateTime(lastEvent.getStartDateTime()); nextDateTime.plusMonths(periodicity); int nextDayOfWeek = nextDateTime.getDayOfWeek(); if (nextDayOfWeek > dayOfWeek) { nextDateTime.minusDays(nextDayOfWeek - dayOfWeek); } else { nextDateTime.plusDays(dayOfWeek - nextDayOfWeek); } int nextPositionInMonth = 0; if (event.getStartDateTime().getDayOfMonth() % 7 == 0) { nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7; } else { nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1; } if (nextPositionInMonth > positionInMonth) { nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth); } else { nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth); } while (!nextDateTime.isAfter(endDate)) { Event copy = eventRepo.copy(lastEvent, false); copy.setParentEvent(lastEvent); Duration dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime()); copy.setStartDateTime(nextDateTime); copy.setEndDateTime(nextDateTime.plus(dur)); eventRepo.save(copy); lastEvent = copy; nextDateTime = new LocalDateTime(lastEvent.getStartDateTime()); nextDateTime.plusMonths(periodicity); nextDayOfWeek = nextDateTime.getDayOfWeek(); if (nextDayOfWeek > dayOfWeek) { nextDateTime.minusDays(nextDayOfWeek - dayOfWeek); } else { nextDateTime.plusDays(dayOfWeek - nextDayOfWeek); } nextPositionInMonth = 0; if (event.getStartDateTime().getDayOfMonth() % 7 == 0) { nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7; } else { nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1; } if (nextPositionInMonth > positionInMonth) { nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth); } else { nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth); } } } } }