Search in sources :

Example 1 with ICalendarEvent

use of com.axelor.apps.base.db.ICalendarEvent 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 2 with ICalendarEvent

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

the class ICalendarService method doSync.

@Transactional(rollbackOn = { Exception.class })
protected ICalendar doSync(ICalendar calendar, CalDavCalendarCollection collection, LocalDateTime startDate, LocalDateTime endDate) throws IOException, URISyntaxException, ParseException, ObjectStoreException, ConstraintViolationException, DavException, ParserConfigurationException, ParserException, AxelorException {
    final boolean keepRemote = calendar.getKeepRemote() == Boolean.TRUE;
    final Map<String, VEvent> modifiedRemoteEvents = new HashMap<>();
    final List<ICalendarEvent> modifiedLocalEvents = getICalendarEvents(calendar);
    final Set<String> allRemoteUids = new HashSet<>();
    final Set<VEvent> updatedEvents = new HashSet<>();
    List<VEvent> events = null;
    Instant lastSynchro = null;
    if (calendar.getLastSynchronizationDateT() != null) {
        lastSynchro = calendar.getLastSynchronizationDateT().toInstant(OffsetDateTime.now().getOffset());
    } else {
        lastSynchro = LocalDateTime.MIN.toInstant(OffsetDateTime.now().getOffset());
    }
    if (startDate == null || endDate == null) {
        events = ICalendarStore.getModifiedEvents(collection, null, allRemoteUids);
    } else {
        events = ICalendarStore.getModifiedEventsInRange(collection, lastSynchro, allRemoteUids, startDate, endDate);
    }
    if (CollectionUtils.isEmpty(events) && CollectionUtils.isEmpty(modifiedLocalEvents)) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CALENDAR_NO_EVENTS_FOR_SYNC_ERROR));
    }
    if (events != null) {
        for (VEvent item : events) {
            modifiedRemoteEvents.put(item.getUid().getValue(), item);
        }
    }
    for (ICalendarEvent item : modifiedLocalEvents) {
        VEvent source = createVEvent(item);
        VEvent target = modifiedRemoteEvents.get(source.getUid().getValue());
        // If uid is empty, the event is new
        if (StringUtils.isBlank(item.getUid())) {
            item.setUid(source.getUid().getValue());
            Calendar cal = newCalendar();
            cal.getComponents().add(source);
            collection.addCalendar(cal);
            allRemoteUids.add(item.getUid());
        } else // else it has been modified
        {
            // if target is null, then it hasn't been modified or it has been deleted
            if (target == null) {
                target = source;
            } else {
                updateEvent(source, target, keepRemote);
            // modifiedRemoteEvents.remove(target.getUid().getValue());
            }
            updatedEvents.add(target);
        }
    }
    // corresponding ICalendarEvent
    for (Map.Entry<String, VEvent> entry : modifiedRemoteEvents.entrySet()) {
        findOrCreateEvent(entry.getValue(), calendar);
    }
    // update remote events
    for (VEvent item : updatedEvents) {
        Calendar cal = newCalendar();
        cal.getComponents().add(item);
        // collection.updateCalendar(cal);
        try {
            collection.addCalendar(cal);
        } catch (Exception e) {
            TraceBackService.trace(e);
        }
    }
    // remove deleted remote events
    removeDeletedEventsInRange(allRemoteUids, calendar, startDate, endDate);
    return calendar;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) ICalendarEvent(com.axelor.apps.base.db.ICalendarEvent) AxelorException(com.axelor.exception.AxelorException) HashMap(java.util.HashMap) Instant(java.time.Instant) Calendar(net.fortuna.ical4j.model.Calendar) ICalendar(com.axelor.apps.base.db.ICalendar) FailedOperationException(net.fortuna.ical4j.connector.FailedOperationException) URISyntaxException(java.net.URISyntaxException) ObjectStoreException(net.fortuna.ical4j.connector.ObjectStoreException) ParseException(java.text.ParseException) ParserException(net.fortuna.ical4j.data.ParserException) ValidationException(net.fortuna.ical4j.validate.ValidationException) SocketException(java.net.SocketException) AxelorException(com.axelor.exception.AxelorException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DavException(org.apache.jackrabbit.webdav.DavException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConstraintViolationException(net.fortuna.ical4j.model.ConstraintViolationException) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Example 3 with ICalendarEvent

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

the class LeaveServiceImpl method cancel.

@Override
@Transactional(rollbackOn = { Exception.class })
public void cancel(LeaveRequest leaveRequest) throws AxelorException {
    if (leaveRequest.getLeaveReason().getManageAccumulation()) {
        manageCancelLeaves(leaveRequest);
    }
    if (leaveRequest.getIcalendarEvent() != null) {
        ICalendarEvent event = leaveRequest.getIcalendarEvent();
        leaveRequest.setIcalendarEvent(null);
        icalEventRepo.remove(icalEventRepo.find(event.getId()));
    }
    leaveRequest.setStatusSelect(LeaveRequestRepository.STATUS_CANCELED);
}
Also used : ICalendarEvent(com.axelor.apps.base.db.ICalendarEvent) Transactional(com.google.inject.persist.Transactional)

Example 4 with ICalendarEvent

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

the class LeaveServiceImpl method createEvents.

@Override
@Transactional(rollbackOn = { Exception.class })
public LeaveRequest createEvents(LeaveRequest leave) throws AxelorException {
    Employee employee = leave.getUser().getEmployee();
    if (employee == null) {
        throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_USER_EMPLOYEE), leave.getUser().getName());
    }
    WeeklyPlanning weeklyPlanning = employee.getWeeklyPlanning();
    if (weeklyPlanning == null) {
        throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_PLANNING), employee.getName());
    }
    LocalDateTime fromDateTime;
    LocalDateTime toDateTime;
    if (leave.getLeaveReason().getUnitSelect() == LeaveReasonRepository.UNIT_SELECT_DAYS) {
        fromDateTime = getDefaultStart(weeklyPlanning, leave);
        toDateTime = getDefaultEnd(weeklyPlanning, leave);
    } else {
        fromDateTime = leave.getFromDateT();
        toDateTime = leave.getToDateT();
    }
    ICalendarEvent event = icalendarService.createEvent(fromDateTime, toDateTime, leave.getUser(), leave.getComments(), 4, leave.getLeaveReason().getName() + " " + leave.getUser().getFullName());
    icalEventRepo.save(event);
    leave.setIcalendarEvent(event);
    return leave;
}
Also used : LocalDateTime(java.time.LocalDateTime) ICalendarEvent(com.axelor.apps.base.db.ICalendarEvent) AxelorException(com.axelor.exception.AxelorException) Employee(com.axelor.apps.hr.db.Employee) WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) Transactional(com.google.inject.persist.Transactional)

Example 5 with ICalendarEvent

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

the class ICalendarService method export.

/**
 * Export the calendar to the given output writer.
 *
 * @param calendar the source {@link ICalendar}
 * @param writer the output writer
 * @throws IOException
 * @throws ParseException
 * @throws ValidationException
 */
public void export(ICalendar calendar, Writer writer) throws IOException, ParseException, ValidationException {
    Preconditions.checkNotNull(calendar, "calendar can't be null");
    Preconditions.checkNotNull(writer, "writer can't be null");
    Preconditions.checkNotNull(getICalendarEvents(calendar), "can't export empty calendar");
    Calendar cal = newCalendar();
    cal.getProperties().add(new XProperty(X_WR_CALNAME, calendar.getName()));
    for (ICalendarEvent item : getICalendarEvents(calendar)) {
        VEvent event = createVEvent(item);
        cal.getComponents().add(event);
    }
    CalendarOutputter outputter = new CalendarOutputter();
    outputter.output(cal, writer);
}
Also used : ICalendarEvent(com.axelor.apps.base.db.ICalendarEvent) VEvent(net.fortuna.ical4j.model.component.VEvent) XProperty(net.fortuna.ical4j.model.property.XProperty) Calendar(net.fortuna.ical4j.model.Calendar) ICalendar(com.axelor.apps.base.db.ICalendar) CalendarOutputter(net.fortuna.ical4j.data.CalendarOutputter)

Aggregations

ICalendarEvent (com.axelor.apps.base.db.ICalendarEvent)7 Transactional (com.google.inject.persist.Transactional)5 ICalendar (com.axelor.apps.base.db.ICalendar)2 AxelorException (com.axelor.exception.AxelorException)2 LocalDateTime (java.time.LocalDateTime)2 Calendar (net.fortuna.ical4j.model.Calendar)2 VEvent (net.fortuna.ical4j.model.component.VEvent)2 ICalendarUser (com.axelor.apps.base.db.ICalendarUser)1 WeeklyPlanning (com.axelor.apps.base.db.WeeklyPlanning)1 ICalendarEventRepository (com.axelor.apps.base.db.repo.ICalendarEventRepository)1 Employee (com.axelor.apps.hr.db.Employee)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 SocketException (java.net.SocketException)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1 Instant (java.time.Instant)1 OffsetDateTime (java.time.OffsetDateTime)1 ZoneId (java.time.ZoneId)1 HashMap (java.util.HashMap)1