Search in sources :

Example 6 with EventRepository

use of com.axelor.apps.crm.db.repo.EventRepository in project axelor-open-suite by axelor.

the class EventController method changeAll.

@Transactional(rollbackOn = { Exception.class })
public void changeAll(ActionRequest request, ActionResponse response) throws AxelorException {
    Long eventId = new Long(request.getContext().getParent().get("id").toString());
    EventRepository eventRepository = Beans.get(EventRepository.class);
    Event event = eventRepository.find(eventId);
    Event child = eventRepository.all().filter("self.parentEvent.id = ?1", event.getId()).fetchOne();
    Event parent = event.getParentEvent();
    child.setParentEvent(null);
    Event eventDeleted = child;
    child = eventRepository.all().filter("self.parentEvent.id = ?1", eventDeleted.getId()).fetchOne();
    while (child != null) {
        child.setParentEvent(null);
        eventRepository.remove(eventDeleted);
        eventDeleted = child;
        child = eventRepository.all().filter("self.parentEvent.id = ?1", eventDeleted.getId()).fetchOne();
    }
    while (parent != null) {
        Event nextParent = parent.getParentEvent();
        eventRepository.remove(parent);
        parent = nextParent;
    }
    RecurrenceConfiguration conf = request.getContext().asType(RecurrenceConfiguration.class);
    RecurrenceConfigurationRepository confRepo = Beans.get(RecurrenceConfigurationRepository.class);
    conf = confRepo.save(conf);
    event.setRecurrenceConfiguration(conf);
    event = eventRepository.save(event);
    if (conf.getRecurrenceType() == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.RECURRENCE_RECURRENCE_TYPE));
    }
    int recurrenceType = conf.getRecurrenceType();
    if (conf.getPeriodicity() == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.RECURRENCE_PERIODICITY));
    }
    int periodicity = conf.getPeriodicity();
    if (periodicity < 1) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.RECURRENCE_PERIODICITY));
    }
    boolean monday = conf.getMonday();
    boolean tuesday = conf.getTuesday();
    boolean wednesday = conf.getWednesday();
    boolean thursday = conf.getThursday();
    boolean friday = conf.getFriday();
    boolean saturday = conf.getSaturday();
    boolean sunday = conf.getSunday();
    Map<Integer, Boolean> daysMap = new HashMap<Integer, Boolean>();
    Map<Integer, Boolean> daysCheckedMap = new HashMap<Integer, Boolean>();
    if (recurrenceType == 2) {
        daysMap.put(DayOfWeek.MONDAY.getValue(), monday);
        daysMap.put(DayOfWeek.TUESDAY.getValue(), tuesday);
        daysMap.put(DayOfWeek.WEDNESDAY.getValue(), wednesday);
        daysMap.put(DayOfWeek.THURSDAY.getValue(), thursday);
        daysMap.put(DayOfWeek.FRIDAY.getValue(), friday);
        daysMap.put(DayOfWeek.SATURDAY.getValue(), saturday);
        daysMap.put(DayOfWeek.SUNDAY.getValue(), sunday);
        for (Integer day : daysMap.keySet()) {
            if (daysMap.get(day)) {
                daysCheckedMap.put(day, daysMap.get(day));
            }
        }
        if (daysMap.isEmpty()) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.RECURRENCE_DAYS_CHECKED));
        }
    }
    int monthRepeatType = conf.getMonthRepeatType();
    int endType = conf.getEndType();
    int repetitionsNumber = 0;
    if (endType == 1) {
        if (conf.getRepetitionsNumber() == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.RECURRENCE_REPETITION_NUMBER));
        }
        repetitionsNumber = conf.getRepetitionsNumber();
        if (repetitionsNumber < 1) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, IExceptionMessage.RECURRENCE_REPETITION_NUMBER);
        }
    }
    LocalDate endDate = Beans.get(AppBaseService.class).getTodayDate(event.getUser() != null ? event.getUser().getActiveCompany() : Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null));
    if (endType == 2) {
        if (conf.getEndDate() == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.RECURRENCE_END_DATE));
        }
        endDate = conf.getEndDate();
        if (endDate.isBefore(event.getStartDateTime().toLocalDate()) && endDate.isEqual(event.getStartDateTime().toLocalDate())) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.RECURRENCE_END_DATE));
        }
    }
    switch(recurrenceType) {
        case 1:
            Beans.get(EventService.class).addRecurrentEventsByDays(event, periodicity, endType, repetitionsNumber, endDate);
            break;
        case 2:
            Beans.get(EventService.class).addRecurrentEventsByWeeks(event, periodicity, endType, repetitionsNumber, endDate, daysCheckedMap);
            break;
        case 3:
            Beans.get(EventService.class).addRecurrentEventsByMonths(event, periodicity, endType, repetitionsNumber, endDate, monthRepeatType);
            break;
        case 4:
            Beans.get(EventService.class).addRecurrentEventsByYears(event, periodicity, endType, repetitionsNumber, endDate);
            break;
        default:
            break;
    }
    response.setCanClose(true);
    response.setReload(true);
}
Also used : AxelorException(com.axelor.exception.AxelorException) HashMap(java.util.HashMap) EventService(com.axelor.apps.crm.service.EventService) ICalendarEventService(com.axelor.base.service.ical.ICalendarEventService) LocalDate(java.time.LocalDate) RecurrenceConfigurationRepository(com.axelor.apps.crm.db.repo.RecurrenceConfigurationRepository) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) Event(com.axelor.apps.crm.db.Event) EventRepository(com.axelor.apps.crm.db.repo.EventRepository) RecurrenceConfiguration(com.axelor.apps.crm.db.RecurrenceConfiguration) Transactional(com.google.inject.persist.Transactional)

Aggregations

Event (com.axelor.apps.crm.db.Event)6 EventRepository (com.axelor.apps.crm.db.repo.EventRepository)6 Transactional (com.google.inject.persist.Transactional)4 EventService (com.axelor.apps.crm.service.EventService)3 ICalendarEventService (com.axelor.base.service.ical.ICalendarEventService)3 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)1 RecurrenceConfiguration (com.axelor.apps.crm.db.RecurrenceConfiguration)1 RecurrenceConfigurationRepository (com.axelor.apps.crm.db.repo.RecurrenceConfigurationRepository)1 AxelorException (com.axelor.exception.AxelorException)1 LocalDate (java.time.LocalDate)1 HashMap (java.util.HashMap)1 List (java.util.List)1