Search in sources :

Example 1 with ConstraintViolationException

use of net.fortuna.ical4j.model.ConstraintViolationException 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)

Aggregations

ICalendar (com.axelor.apps.base.db.ICalendar)1 ICalendarEvent (com.axelor.apps.base.db.ICalendarEvent)1 AxelorException (com.axelor.exception.AxelorException)1 Transactional (com.google.inject.persist.Transactional)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 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 FailedOperationException (net.fortuna.ical4j.connector.FailedOperationException)1 ObjectStoreException (net.fortuna.ical4j.connector.ObjectStoreException)1 ParserException (net.fortuna.ical4j.data.ParserException)1 Calendar (net.fortuna.ical4j.model.Calendar)1 ConstraintViolationException (net.fortuna.ical4j.model.ConstraintViolationException)1 VEvent (net.fortuna.ical4j.model.component.VEvent)1