Search in sources :

Example 16 with Event

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

the class EventServiceImpl method applyChangesToAll.

@Override
@Transactional
public void applyChangesToAll(Event event) {
    Event child = eventRepo.all().filter("self.parentEvent.id = ?1", event.getId()).fetchOne();
    Event parent = event.getParentEvent();
    Event copyEvent = eventRepo.copy(event, false);
    while (child != null) {
        child.setSubject(event.getSubject());
        child.setCalendar(event.getCalendar());
        child.setStartDateTime(child.getStartDateTime().withHour(event.getStartDateTime().getHour()));
        child.setStartDateTime(child.getStartDateTime().withMinute(event.getStartDateTime().getMinute()));
        child.setEndDateTime(child.getEndDateTime().withHour(event.getEndDateTime().getHour()));
        child.setEndDateTime(child.getEndDateTime().withMinute(event.getEndDateTime().getMinute()));
        child.setDuration(event.getDuration());
        child.setUser(event.getUser());
        child.setTeam(event.getTeam());
        child.setDisponibilitySelect(event.getDisponibilitySelect());
        child.setVisibilitySelect(event.getVisibilitySelect());
        child.setDescription(event.getDescription());
        child.setPartner(event.getPartner());
        child.setContactPartner(event.getContactPartner());
        child.setLead(event.getLead());
        child.setTypeSelect(event.getTypeSelect());
        child.setLocation(event.getLocation());
        eventRepo.save(child);
        copyEvent = child;
        child = eventRepo.all().filter("self.parentEvent.id = ?1", copyEvent.getId()).fetchOne();
    }
    while (parent != null) {
        Event nextParent = parent.getParentEvent();
        parent.setSubject(event.getSubject());
        parent.setCalendar(event.getCalendar());
        parent.setStartDateTime(parent.getStartDateTime().withHour(event.getStartDateTime().getHour()));
        parent.setStartDateTime(parent.getStartDateTime().withMinute(event.getStartDateTime().getMinute()));
        parent.setEndDateTime(parent.getEndDateTime().withHour(event.getEndDateTime().getHour()));
        parent.setEndDateTime(parent.getEndDateTime().withMinute(event.getEndDateTime().getMinute()));
        parent.setDuration(event.getDuration());
        parent.setUser(event.getUser());
        parent.setTeam(event.getTeam());
        parent.setDisponibilitySelect(event.getDisponibilitySelect());
        parent.setVisibilitySelect(event.getVisibilitySelect());
        parent.setDescription(event.getDescription());
        parent.setPartner(event.getPartner());
        parent.setContactPartner(event.getContactPartner());
        parent.setLead(event.getLead());
        parent.setTypeSelect(event.getTypeSelect());
        parent.setLocation(event.getLocation());
        eventRepo.save(parent);
        parent = nextParent;
    }
}
Also used : Event(com.axelor.apps.crm.db.Event) Transactional(com.google.inject.persist.Transactional)

Example 17 with Event

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

the class EventServiceImpl method manageFollowers.

@Override
@Transactional
public void manageFollowers(Event event) {
    MailFollowerRepository mailFollowerRepo = Beans.get(MailFollowerRepository.class);
    List<MailFollower> followers = mailFollowerRepo.findAll(event);
    List<ICalendarUser> attendeesSet = event.getAttendees();
    if (followers != null)
        followers.forEach(x -> mailFollowerRepo.remove(x));
    mailFollowerRepo.follow(event, event.getUser());
    if (attendeesSet != null) {
        for (ICalendarUser user : attendeesSet) {
            if (user.getUser() != null) {
                mailFollowerRepo.follow(event, user.getUser());
            } else {
                MailAddress mailAddress = Beans.get(MailAddressRepository.class).findOrCreate(user.getEmail(), user.getName());
                mailFollowerRepo.follow(event, mailAddress);
            }
        }
    }
}
Also used : PartnerService(com.axelor.apps.base.service.PartnerService) Inject(com.google.inject.Inject) BiFunction(java.util.function.BiFunction) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) Lead(com.axelor.apps.crm.db.Lead) Function(java.util.function.Function) EmailAddress(com.axelor.apps.message.db.EmailAddress) ICalendarService(com.axelor.apps.base.ical.ICalendarService) Transactional(com.google.inject.persist.Transactional) ICalendarUser(com.axelor.apps.base.db.ICalendarUser) Strings(com.google.common.base.Strings) Event(com.axelor.apps.crm.db.Event) AxelorException(com.axelor.exception.AxelorException) Duration(java.time.Duration) Map(java.util.Map) IExceptionMessage(com.axelor.apps.crm.exception.IExceptionMessage) I18n(com.axelor.i18n.I18n) RecurrenceConfiguration(com.axelor.apps.crm.db.RecurrenceConfiguration) MailFollower(com.axelor.mail.db.MailFollower) EmailAddressRepository(com.axelor.apps.message.db.repo.EmailAddressRepository) LeadRepository(com.axelor.apps.crm.db.repo.LeadRepository) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) PartnerRepository(com.axelor.apps.base.db.repo.PartnerRepository) Collectors(java.util.stream.Collectors) TooManyIterationsException(org.apache.commons.math3.exception.TooManyIterationsException) MessageService(com.axelor.apps.message.service.MessageService) EventRepository(com.axelor.apps.crm.db.repo.EventRepository) List(java.util.List) Beans(com.axelor.inject.Beans) DayOfWeek(java.time.DayOfWeek) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) RecurrenceConfigurationRepository(com.axelor.apps.crm.db.repo.RecurrenceConfigurationRepository) TemporalAdjusters(java.time.temporal.TemporalAdjusters) Address(com.axelor.apps.base.db.Address) TemplateMessageService(com.axelor.apps.message.service.TemplateMessageService) MailAddressRepository(com.axelor.mail.db.repo.MailAddressRepository) MailFollowerRepository(com.axelor.mail.db.repo.MailFollowerRepository) Partner(com.axelor.apps.base.db.Partner) MailAddress(com.axelor.mail.db.MailAddress) User(com.axelor.auth.db.User) MailFollowerRepository(com.axelor.mail.db.repo.MailFollowerRepository) ICalendarUser(com.axelor.apps.base.db.ICalendarUser) MailAddress(com.axelor.mail.db.MailAddress) MailFollower(com.axelor.mail.db.MailFollower) MailAddressRepository(com.axelor.mail.db.repo.MailAddressRepository) Transactional(com.google.inject.persist.Transactional)

Example 18 with Event

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

the class EventServiceImpl method addRecurrentEventsByYears.

@Override
@Transactional
public void addRecurrentEventsByYears(Event event, int periodicity, int endType, int repetitionsNumber, LocalDate endDate) {
    Event lastEvent = event;
    if (endType == RecurrenceConfigurationRepository.END_TYPE_REPET) {
        int repeated = 0;
        while (repeated != repetitionsNumber) {
            Event copy = eventRepo.copy(lastEvent, false);
            copy.setParentEvent(event);
            copy.setStartDateTime(copy.getStartDateTime().plusYears(periodicity));
            copy.setEndDateTime(copy.getEndDateTime().plusYears(periodicity));
            lastEvent = eventRepo.save(copy);
            repeated++;
        }
    } else {
        while (lastEvent.getStartDateTime().plusYears(periodicity).isBefore(endDate.atStartOfDay().plusYears(1))) {
            Event copy = eventRepo.copy(lastEvent, false);
            copy.setParentEvent(event);
            copy.setStartDateTime(copy.getStartDateTime().plusYears(periodicity));
            copy.setEndDateTime(copy.getEndDateTime().plusYears(periodicity));
            lastEvent = eventRepo.save(copy);
        }
    }
}
Also used : Event(com.axelor.apps.crm.db.Event) Transactional(com.google.inject.persist.Transactional)

Example 19 with Event

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

the class EventServiceImpl method createEvent.

@Override
public Event createEvent(LocalDateTime fromDateTime, LocalDateTime toDateTime, User user, String description, int type, String subject) {
    Event event = new Event();
    event.setSubject(subject);
    event.setStartDateTime(fromDateTime);
    event.setEndDateTime(toDateTime);
    event.setUser(user);
    event.setTypeSelect(type);
    if (!Strings.isNullOrEmpty(description)) {
        event.setDescription(description);
    }
    if (fromDateTime != null && toDateTime != null) {
        long duration = Duration.between(fromDateTime, toDateTime).getSeconds();
        event.setDuration(duration);
    }
    return event;
}
Also used : Event(com.axelor.apps.crm.db.Event)

Example 20 with Event

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

the class EventServiceImpl method addRecurrentEventsByMonths.

@Override
@Transactional
public void addRecurrentEventsByMonths(Event event, int periodicity, int endType, int repetitionsNumber, LocalDate endDate, int monthRepeatType) {
    int weekNo = 1 + (event.getStartDateTime().getDayOfMonth() - 1) / 7;
    Duration duration = Duration.between(event.getStartDateTime(), event.getEndDateTime());
    Event lastEvent = event;
    BiFunction<Integer, LocalDateTime, Boolean> breakConditionFunc;
    Function<LocalDateTime, LocalDateTime> nextStartDateTimeFunc;
    LocalDateTime nextStartDateTime;
    if (endType == RecurrenceConfigurationRepository.END_TYPE_REPET) {
        breakConditionFunc = (iteration, dateTime) -> iteration >= repetitionsNumber;
    } else {
        breakConditionFunc = (iteration, dateTime) -> dateTime.toLocalDate().isAfter(endDate);
    }
    if (monthRepeatType == RecurrenceConfigurationRepository.REPEAT_TYPE_MONTH) {
        nextStartDateTimeFunc = dateTime -> dateTime.withDayOfMonth(1).plusMonths(periodicity).withDayOfMonth(event.getStartDateTime().getDayOfMonth());
    } else {
        nextStartDateTimeFunc = dateTime -> {
            LocalDateTime baseNextDateTime = dateTime.withDayOfMonth(1).plusMonths(periodicity);
            dateTime = baseNextDateTime.with(TemporalAdjusters.dayOfWeekInMonth(weekNo, event.getStartDateTime().getDayOfWeek()));
            if (!dateTime.getMonth().equals(baseNextDateTime.getMonth()) && weekNo > 1) {
                dateTime = baseNextDateTime.with(TemporalAdjusters.dayOfWeekInMonth(weekNo - 1, event.getStartDateTime().getDayOfWeek()));
            }
            return dateTime;
        };
    }
    for (int iteration = 0; ; ++iteration) {
        if (iteration > ITERATION_LIMIT) {
            throw new TooManyIterationsException(iteration);
        }
        nextStartDateTime = nextStartDateTimeFunc.apply(lastEvent.getStartDateTime());
        if (breakConditionFunc.apply(iteration, nextStartDateTime)) {
            break;
        }
        Event copy = eventRepo.copy(lastEvent, false);
        copy.setParentEvent(event);
        copy.setStartDateTime(nextStartDateTime);
        copy.setEndDateTime(nextStartDateTime.plus(duration));
        lastEvent = eventRepo.save(copy);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) TooManyIterationsException(org.apache.commons.math3.exception.TooManyIterationsException) Event(com.axelor.apps.crm.db.Event) Duration(java.time.Duration) Transactional(com.google.inject.persist.Transactional)

Aggregations

Event (com.axelor.apps.crm.db.Event)36 Transactional (com.google.inject.persist.Transactional)21 EventRepository (com.axelor.apps.crm.db.repo.EventRepository)11 EventService (com.axelor.apps.crm.service.EventService)7 ICalendarEventService (com.axelor.base.service.ical.ICalendarEventService)7 TrainingRegister (com.axelor.apps.talent.db.TrainingRegister)6 Duration (java.time.Duration)6 AxelorException (com.axelor.exception.AxelorException)5 LocalDateTime (java.time.LocalDateTime)4 RecurrenceConfiguration (com.axelor.apps.crm.db.RecurrenceConfiguration)3 RecurrenceConfigurationRepository (com.axelor.apps.crm.db.repo.RecurrenceConfigurationRepository)3 HashMap (java.util.HashMap)3 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)3 Partner (com.axelor.apps.base.db.Partner)2 Lead (com.axelor.apps.crm.db.Lead)2 Training (com.axelor.apps.talent.db.Training)2 TrainingSession (com.axelor.apps.talent.db.TrainingSession)2 TrainingSessionRepository (com.axelor.apps.talent.db.repo.TrainingSessionRepository)2 User (com.axelor.auth.db.User)2 DayOfWeek (java.time.DayOfWeek)2