Search in sources :

Example 1 with ICalendarUser

use of com.axelor.apps.base.db.ICalendarUser in project axelor-open-suite by axelor.

the class BatchEventReminder method generateMessageProcess.

protected void generateMessageProcess() {
    MessageRepository messageRepo = Beans.get(MessageRepository.class);
    if (!stop) {
        int i = 0;
        Query q = JPA.em().createQuery(" SELECT er FROM EventReminder as er WHERE er.isReminded = true and ?1 MEMBER OF er.batchSet");
        q.setParameter(1, batch);
        @SuppressWarnings("unchecked") List<EventReminder> eventReminderList = q.getResultList();
        for (EventReminder eventReminder : eventReminderList) {
            try {
                eventReminder = eventReminderRepo.find(eventReminder.getId());
                Message message = messageServiceCrmImpl.createMessage(eventReminder.getEvent());
                // Send reminder to owner of the reminder in any case
                if (eventReminder.getUser().getPartner() != null && eventReminder.getUser().getPartner().getEmailAddress() != null) {
                    message.addToEmailAddressSetItem(eventReminder.getUser().getPartner().getEmailAddress());
                } else if (eventReminder.getUser().getEmail() != null) {
                    message.addToEmailAddressSetItem(findOrCreateEmailAddress(eventReminder.getUser().getEmail(), "[" + eventReminder.getUser().getEmail() + "]"));
                } else {
                    messageRepo.remove(message);
                    throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CRM_CONFIG_USER_EMAIL), eventReminder.getUser().getName());
                }
                // Also send to attendees if needed
                if (EventReminderRepository.ASSIGN_TO_ALL.equals(eventReminder.getAssignToSelect()) && eventReminder.getEvent().getAttendees() != null) {
                    for (ICalendarUser iCalUser : eventReminder.getEvent().getAttendees()) {
                        if (iCalUser.getUser() != null && iCalUser.getUser().getPartner() != null) {
                            message.addToEmailAddressSetItem(iCalUser.getUser().getPartner().getEmailAddress());
                        } else {
                            message.addToEmailAddressSetItem(findOrCreateEmailAddress(iCalUser.getEmail(), iCalUser.getName()));
                        }
                    }
                }
                message = Beans.get(MessageService.class).sendByEmail(message);
            } catch (Exception e) {
                TraceBackService.trace(new Exception(String.format(I18n.get("Event") + " %s", eventRepo.find(eventReminder.getEvent().getId()).getSubject()), e), ExceptionOriginRepository.CRM, batch.getId());
                incrementAnomaly();
                LOG.error("Bug(Anomalie) généré(e) pour l'évènement {}", eventRepo.find(eventReminder.getEvent().getId()).getSubject());
            } finally {
                if (i % 1 == 0) {
                    JPA.clear();
                }
            }
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) ICalendarUser(com.axelor.apps.base.db.ICalendarUser) EventReminder(com.axelor.apps.crm.db.EventReminder) Query(javax.persistence.Query) IExceptionMessage(com.axelor.apps.crm.exception.IExceptionMessage) Message(com.axelor.apps.message.db.Message) MessageRepository(com.axelor.apps.message.db.repo.MessageRepository) AxelorException(com.axelor.exception.AxelorException)

Example 2 with ICalendarUser

use of com.axelor.apps.base.db.ICalendarUser in project axelor-open-suite by axelor.

the class EventManagementRepository method save.

@Override
public Event save(Event entity) {
    if (entity.getTypeSelect() == EventRepository.TYPE_MEETING) {
        super.save(entity);
        Beans.get(EventService.class).manageFollowers(entity);
    }
    User creator = entity.getCreatedBy();
    if (creator == null) {
        creator = AuthUtils.getUser();
    }
    if (entity.getOrganizer() == null && creator != null) {
        if (creator.getPartner() != null && creator.getPartner().getEmailAddress() != null) {
            String email = creator.getPartner().getEmailAddress().getAddress();
            if (!Strings.isNullOrEmpty(email)) {
                ICalendarUser organizer = Beans.get(ICalendarUserRepository.class).all().filter("self.email = ?1 AND self.user.id = ?2", email, creator.getId()).fetchOne();
                if (organizer == null) {
                    organizer = new ICalendarUser();
                    organizer.setEmail(email);
                    organizer.setName(creator.getFullName());
                    organizer.setUser(creator);
                }
                entity.setOrganizer(organizer);
            }
        }
    }
    entity.setSubjectTeam(entity.getSubject());
    if (entity.getVisibilitySelect() == ICalendarEventRepository.VISIBILITY_PRIVATE) {
        entity.setSubjectTeam(I18n.get("Available"));
        if (entity.getDisponibilitySelect() == ICalendarEventRepository.DISPONIBILITY_BUSY) {
            entity.setSubjectTeam(I18n.get("Busy"));
        }
    }
    return super.save(entity);
}
Also used : ICalendarUser(com.axelor.apps.base.db.ICalendarUser) ICalendarUser(com.axelor.apps.base.db.ICalendarUser) User(com.axelor.auth.db.User) EventService(com.axelor.apps.crm.service.EventService) ICalendarUserRepository(com.axelor.apps.base.db.repo.ICalendarUserRepository)

Example 3 with ICalendarUser

use of com.axelor.apps.base.db.ICalendarUser in project axelor-open-suite by axelor.

the class ICalendarService method findOrCreateEvent.

@Transactional
protected ICalendarEvent findOrCreateEvent(VEvent vEvent, ICalendar calendar) {
    String uid = vEvent.getUid().getValue();
    DtStart dtStart = vEvent.getStartDate();
    DtEnd dtEnd = vEvent.getEndDate();
    ICalendarEvent event = iEventRepo.findByUid(uid);
    if (event == null) {
        event = ICalendarEventFactory.getNewIcalEvent(calendar);
        event.setUid(uid);
        event.setCalendar(calendar);
    }
    ZoneId zoneId = OffsetDateTime.now().getOffset();
    if (dtStart.getDate() != null) {
        if (dtStart.getTimeZone() != null) {
            zoneId = dtStart.getTimeZone().toZoneId();
        }
        event.setStartDateTime(LocalDateTime.ofInstant(dtStart.getDate().toInstant(), zoneId));
    }
    if (dtEnd.getDate() != null) {
        if (dtEnd.getTimeZone() != null) {
            zoneId = dtEnd.getTimeZone().toZoneId();
        }
        event.setEndDateTime(LocalDateTime.ofInstant(dtEnd.getDate().toInstant(), zoneId));
    }
    event.setAllDay(!(dtStart.getDate() instanceof DateTime));
    event.setSubject(getValue(vEvent, Property.SUMMARY));
    event.setDescription(getValue(vEvent, Property.DESCRIPTION));
    event.setLocation(getValue(vEvent, Property.LOCATION));
    event.setGeo(getValue(vEvent, Property.GEO));
    event.setUrl(getValue(vEvent, Property.URL));
    event.setSubjectTeam(event.getSubject());
    if (Clazz.PRIVATE.getValue().equals(getValue(vEvent, Property.CLASS))) {
        event.setVisibilitySelect(ICalendarEventRepository.VISIBILITY_PRIVATE);
    } else {
        event.setVisibilitySelect(ICalendarEventRepository.VISIBILITY_PUBLIC);
    }
    if (Transp.TRANSPARENT.getValue().equals(getValue(vEvent, Property.TRANSP))) {
        event.setDisponibilitySelect(ICalendarEventRepository.DISPONIBILITY_AVAILABLE);
    } else {
        event.setDisponibilitySelect(ICalendarEventRepository.DISPONIBILITY_BUSY);
    }
    if (event.getVisibilitySelect() == ICalendarEventRepository.VISIBILITY_PRIVATE) {
        event.setSubjectTeam(I18n.get("Available"));
        if (event.getDisponibilitySelect() == ICalendarEventRepository.DISPONIBILITY_BUSY) {
            event.setSubjectTeam(I18n.get("Busy"));
        }
    }
    ICalendarUser organizer = findOrCreateUser(vEvent.getOrganizer(), event);
    if (organizer != null) {
        event.setOrganizer(organizer);
        iCalendarUserRepository.save(organizer);
    }
    for (Object item : vEvent.getProperties(Property.ATTENDEE)) {
        ICalendarUser attendee = findOrCreateUser((Property) item, event);
        if (attendee != null) {
            event.addAttendee(attendee);
            iCalendarUserRepository.save(attendee);
        }
    }
    iEventRepo.save(event);
    return event;
}
Also used : ICalendarEvent(com.axelor.apps.base.db.ICalendarEvent) ICalendarUser(com.axelor.apps.base.db.ICalendarUser) DtStart(net.fortuna.ical4j.model.property.DtStart) ZoneId(java.time.ZoneId) DateTime(net.fortuna.ical4j.model.DateTime) OffsetDateTime(java.time.OffsetDateTime) LocalDateTime(java.time.LocalDateTime) DtEnd(net.fortuna.ical4j.model.property.DtEnd) Transactional(com.google.inject.persist.Transactional)

Example 4 with ICalendarUser

use of com.axelor.apps.base.db.ICalendarUser in project axelor-open-suite by axelor.

the class ICalendarService method findOrCreateUser.

public ICalendarUser findOrCreateUser(User user) {
    String email = null;
    if (user.getPartner() != null && user.getPartner().getEmailAddress() != null && !Strings.isNullOrEmpty(user.getPartner().getEmailAddress().getAddress())) {
        email = user.getPartner().getEmailAddress().getAddress();
    } else if (!Strings.isNullOrEmpty(user.getEmail())) {
        email = user.getEmail();
    } else {
        return null;
    }
    ICalendarUserRepository repo = Beans.get(ICalendarUserRepository.class);
    ICalendarUser icalUser = null;
    icalUser = repo.all().filter("self.email = ?1 AND self.user.id = ?2", email, user.getId()).fetchOne();
    if (icalUser == null) {
        icalUser = repo.all().filter("self.user.id = ?1", user.getId()).fetchOne();
    }
    if (icalUser == null) {
        icalUser = repo.all().filter("self.email = ?1", email).fetchOne();
    }
    if (icalUser == null) {
        icalUser = new ICalendarUser();
        icalUser.setEmail(email);
        icalUser.setName(user.getFullName());
        EmailAddress emailAddress = Beans.get(EmailAddressRepository.class).findByAddress(email);
        if (emailAddress != null && emailAddress.getPartner() != null && emailAddress.getPartner().getUser() != null) {
            icalUser.setUser(emailAddress.getPartner().getUser());
        }
    }
    return icalUser;
}
Also used : ICalendarUser(com.axelor.apps.base.db.ICalendarUser) EmailAddressRepository(com.axelor.apps.message.db.repo.EmailAddressRepository) EmailAddress(com.axelor.apps.message.db.EmailAddress) ICalendarUserRepository(com.axelor.apps.base.db.repo.ICalendarUserRepository)

Example 5 with ICalendarUser

use of com.axelor.apps.base.db.ICalendarUser in project axelor-open-suite by axelor.

the class ICalendarService method createVEvent.

protected VEvent createVEvent(ICalendarEvent event) throws SocketException, ParseException {
    boolean allDay = event.getAllDay() == Boolean.TRUE;
    Date start = toDate(event.getStartDateTime(), allDay);
    Date end = toDate(event.getEndDateTime(), allDay);
    VEvent vevent = new VEvent();
    PropertyList<Property> items = vevent.getProperties();
    items.add(new DtStart(start));
    if (end != null) {
        items.add(new DtEnd(end));
    }
    if (event.getSubject() != null) {
        items.add(new Summary(event.getSubject()));
    }
    if (event.getDescription() != null) {
        items.add(new Description(event.getDescription()));
    }
    if (event.getStatus() != null) {
        items.add(new Status(event.getStatus()));
    }
    if (event.getLocation() != null) {
        items.add(new Location(event.getLocation()));
    }
    if (StringUtils.notEmpty(event.getGeo()) && event.getGeo().contains(";")) {
        // new Geo() object seperate the longitude and latitude using ; char
        items.add(new Geo(event.getGeo()));
    }
    if (event.getUid() == null) {
        items.add(nextUid());
    } else {
        items.add(new Uid(event.getUid()));
    }
    if (event.getUrl() != null) {
        items.add(new Url(createUri(event.getUrl())));
    }
    if (event.getUpdatedOn() != null) {
        DateTime date = new DateTime(java.util.Date.from(event.getUpdatedOn().toInstant(OffsetDateTime.now().getOffset())));
        date.setUtc(true);
        LastModified lastModified = new LastModified(date);
        items.add(lastModified);
    } else {
        DateTime date = new DateTime(java.util.Date.from(event.getCreatedOn().toInstant(OffsetDateTime.now().getOffset())));
        date.setUtc(true);
        LastModified lastModified = new LastModified(date);
        items.add(lastModified);
    }
    Organizer organizer = updateUser(new Organizer(), event.getOrganizer());
    if (organizer != null) {
        items.add(organizer);
    }
    if (event.getAttendees() != null) {
        for (ICalendarUser user : event.getAttendees()) {
            Attendee attendee = updateUser(new Attendee(), user);
            if (attendee != null) {
                items.add(attendee);
            }
        }
    }
    return vevent;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) Status(net.fortuna.ical4j.model.property.Status) Description(net.fortuna.ical4j.model.property.Description) Organizer(net.fortuna.ical4j.model.property.Organizer) Date(net.fortuna.ical4j.model.Date) Url(net.fortuna.ical4j.model.property.Url) DateTime(net.fortuna.ical4j.model.DateTime) OffsetDateTime(java.time.OffsetDateTime) LocalDateTime(java.time.LocalDateTime) Attendee(net.fortuna.ical4j.model.property.Attendee) Geo(net.fortuna.ical4j.model.property.Geo) LastModified(net.fortuna.ical4j.model.property.LastModified) Uid(net.fortuna.ical4j.model.property.Uid) ICalendarUser(com.axelor.apps.base.db.ICalendarUser) DtStart(net.fortuna.ical4j.model.property.DtStart) Summary(net.fortuna.ical4j.model.property.Summary) XProperty(net.fortuna.ical4j.model.property.XProperty) Property(net.fortuna.ical4j.model.Property) DtEnd(net.fortuna.ical4j.model.property.DtEnd) Location(net.fortuna.ical4j.model.property.Location)

Aggregations

ICalendarUser (com.axelor.apps.base.db.ICalendarUser)9 ICalendarUserRepository (com.axelor.apps.base.db.repo.ICalendarUserRepository)3 EmailAddress (com.axelor.apps.message.db.EmailAddress)3 EmailAddressRepository (com.axelor.apps.message.db.repo.EmailAddressRepository)3 User (com.axelor.auth.db.User)3 LocalDateTime (java.time.LocalDateTime)3 IExceptionMessage (com.axelor.apps.crm.exception.IExceptionMessage)2 AxelorException (com.axelor.exception.AxelorException)2 Transactional (com.google.inject.persist.Transactional)2 OffsetDateTime (java.time.OffsetDateTime)2 DateTime (net.fortuna.ical4j.model.DateTime)2 DtEnd (net.fortuna.ical4j.model.property.DtEnd)2 DtStart (net.fortuna.ical4j.model.property.DtStart)2 Address (com.axelor.apps.base.db.Address)1 ICalendarEvent (com.axelor.apps.base.db.ICalendarEvent)1 Partner (com.axelor.apps.base.db.Partner)1 PartnerRepository (com.axelor.apps.base.db.repo.PartnerRepository)1 ICalendarService (com.axelor.apps.base.ical.ICalendarService)1 PartnerService (com.axelor.apps.base.service.PartnerService)1 Event (com.axelor.apps.crm.db.Event)1