Search in sources :

Example 26 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.

the class ICalFileCalendarManager method getEvents.

@Override
public List<KalendarEvent> getEvents(Kalendar calendar, Date from, Date to, boolean privateEventsVisible) {
    List<KalendarEvent> allEvents = calendar.getEvents();
    List<KalendarEvent> events = new ArrayList<>(128);
    Map<String, List<KalendarRecurEvent>> idToRecurringEvents = new HashMap<>();
    // first pass, ignore events with recurrenceId
    for (KalendarEvent event : allEvents) {
        if (!privateEventsVisible && event.getClassification() == KalendarEvent.CLASS_PRIVATE) {
            continue;
        }
        if (StringHelper.containsNonWhitespace(event.getRecurrenceID())) {
            continue;
        }
        if (StringHelper.containsNonWhitespace(event.getRecurrenceRule())) {
            List<KalendarRecurEvent> recurringEvents = getRecurringEventsInPeriod(event, from, to, tz);
            if (recurringEvents.size() > 0) {
                idToRecurringEvents.put(event.getID(), recurringEvents);
                for (KalendarRecurEvent recurEvent : recurringEvents) {
                    events.add(recurEvent);
                }
            }
        } else if (isInRange(from, to, event)) {
            events.add(event);
        }
    }
    // process events with recurrenceId
    for (KalendarEvent event : allEvents) {
        if (!StringHelper.containsNonWhitespace(event.getRecurrenceID())) {
            continue;
        }
        String id = event.getID();
        if (idToRecurringEvents.containsKey(id)) {
            VEvent vEvent = getVEvent(event);
            RecurrenceId recurrenceId = vEvent.getRecurrenceId();
            net.fortuna.ical4j.model.Date startDate = recurrenceId.getDate();
            if (startDate instanceof net.fortuna.ical4j.model.DateTime) {
                List<KalendarRecurEvent> recurringEvents = idToRecurringEvents.get(id);
                for (KalendarRecurEvent recurEvent : recurringEvents) {
                    Date beginDate = recurEvent.getBegin();
                    if (beginDate.equals(startDate)) {
                        recurEvent.setRecurrenceEvent(event);
                    }
                }
            } else {
                List<KalendarRecurEvent> recurringEvents = idToRecurringEvents.get(id);
                for (KalendarRecurEvent recurEvent : recurringEvents) {
                    Date beginDate = recurEvent.getBegin();
                    net.fortuna.ical4j.model.Date occDate = CalendarUtils.createDate(beginDate);
                    if (occDate.equals(startDate)) {
                        recurEvent.setRecurrenceEvent(event);
                    }
                }
            }
        }
    }
    return events;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) DateTime(net.fortuna.ical4j.model.DateTime) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) KalendarRecurEvent(org.olat.commons.calendar.model.KalendarRecurEvent) PropertyList(net.fortuna.ical4j.model.PropertyList) List(java.util.List) DateList(net.fortuna.ical4j.model.DateList) ArrayList(java.util.ArrayList) PeriodList(net.fortuna.ical4j.model.PeriodList) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId)

Example 27 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.

the class ICalFileCalendarManager method createKalendarEventRecurringOccurence.

@Override
public KalendarEvent createKalendarEventRecurringOccurence(KalendarRecurEvent recurEvent) {
    KalendarEvent rootEvent = recurEvent.getCalendar().getEvent(recurEvent.getID(), null);
    VEvent vEvent = getVEvent(recurEvent);
    PropertyList vEventProperties = vEvent.getProperties();
    for (Iterator<?> objIt = vEventProperties.iterator(); objIt.hasNext(); ) {
        Object property = objIt.next();
        if (property instanceof RRule || property instanceof ExDate) {
            objIt.remove();
        }
    }
    try {
        Kalendar calendar = recurEvent.getCalendar();
        Date startDate = recurEvent.getOccurenceDate();
        String startString = CalendarUtils.formatRecurrenceDate(startDate, rootEvent.isAllDayEvent());
        RecurrenceId recurId;
        if (rootEvent.isAllDayEvent()) {
            recurId = new RecurrenceId(tz);
            recurId.setDate(CalendarUtils.createDate(startDate));
        } else {
            recurId = new RecurrenceId(startString, tz);
        }
        vEventProperties.add(recurId);
        KalendarEvent kEvent = getKalendarEvent(vEvent);
        kEvent.setKalendar(calendar);
        return kEvent;
    } catch (ParseException e) {
        log.error("", e);
        return null;
    }
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) PropertyList(net.fortuna.ical4j.model.PropertyList) RRule(net.fortuna.ical4j.model.property.RRule) Kalendar(org.olat.commons.calendar.model.Kalendar) ExDate(net.fortuna.ical4j.model.property.ExDate) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId) ParseException(java.text.ParseException) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate)

Example 28 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.

the class CalendarNotificationHandler method createSubscriptionInfo.

@Override
public SubscriptionInfo createSubscriptionInfo(Subscriber subscriber, Locale locale, Date compareDate) {
    SubscriptionInfo si = null;
    Publisher p = subscriber.getPublisher();
    Date latestNews = p.getLatestNewsDate();
    // can't be loaded when already deleted
    if (notificationsManager.isPublisherValid(p) && compareDate.before(latestNews)) {
        Long id = p.getResId();
        String type = p.getSubidentifier();
        try {
            Translator translator = Util.createPackageTranslator(CalendarModule.class, locale);
            String calType = null;
            String title = null;
            if (type.equals(CalendarController.ACTION_CALENDAR_COURSE)) {
                RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(OresHelper.createOLATResourceableInstance("CourseModule", id), false);
                if (re.getRepositoryEntryStatus().isClosed() || re.getRepositoryEntryStatus().isUnpublished()) {
                    return NotificationsManager.getInstance().getNoSubscriptionInfo();
                }
                String displayName = re.getDisplayname();
                calType = CalendarManager.TYPE_COURSE;
                title = translator.translate("cal.notifications.header.course", new String[] { displayName });
            } else if (type.equals(CalendarController.ACTION_CALENDAR_GROUP)) {
                BusinessGroup group = businessGroupDao.load(id);
                calType = CalendarManager.TYPE_GROUP;
                if (group == null) {
                    return notificationsManager.getNoSubscriptionInfo();
                }
                title = translator.translate("cal.notifications.header.group", new String[] { group.getName() });
            }
            if (calType != null) {
                Formatter form = Formatter.getInstance(locale);
                si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(title, CSS_CLASS_CALENDAR_ICON), null);
                String bPath;
                if (StringHelper.containsNonWhitespace(p.getBusinessPath())) {
                    bPath = p.getBusinessPath();
                } else if ("CalendarManager.course".equals(p.getResName())) {
                    try {
                        OLATResourceable ores = OresHelper.createOLATResourceableInstance(CourseModule.getCourseTypeName(), p.getResId());
                        RepositoryEntry re = repositoryManager.lookupRepositoryEntry(ores, true);
                        // Fallback
                        bPath = "[RepositoryEntry:" + re.getKey() + "]";
                    } catch (Exception e) {
                        log.error("Error processing calendar notifications of publisher:" + p.getKey(), e);
                        return notificationsManager.getNoSubscriptionInfo();
                    }
                } else {
                    // cannot make link without business path
                    return notificationsManager.getNoSubscriptionInfo();
                }
                Kalendar cal = calendarManager.getCalendar(calType, id.toString());
                Collection<KalendarEvent> calEvents = cal.getEvents();
                for (KalendarEvent kalendarEvent : calEvents) {
                    if (showEvent(compareDate, kalendarEvent)) {
                        log.debug("found a KalendarEvent: " + kalendarEvent.getSubject() + " with time: " + kalendarEvent.getBegin() + " modified before: " + compareDate.toString(), null);
                        // found a modified event in this calendar
                        Date modDate = null;
                        if (kalendarEvent.getLastModified() > 0) {
                            modDate = new Date(kalendarEvent.getLastModified());
                        } else if (kalendarEvent.getCreated() > 0) {
                            modDate = new Date(kalendarEvent.getCreated());
                        } else if (kalendarEvent.getBegin() != null) {
                            modDate = kalendarEvent.getBegin();
                        }
                        String subject = kalendarEvent.getSubject();
                        String author = kalendarEvent.getCreatedBy();
                        if (author == null)
                            author = "";
                        String location = "";
                        if (StringHelper.containsNonWhitespace(kalendarEvent.getLocation())) {
                            location = kalendarEvent.getLocation() == null ? "" : translator.translate("cal.notifications.location", new String[] { kalendarEvent.getLocation() });
                        }
                        String dateStr;
                        if (kalendarEvent.isAllDayEvent()) {
                            dateStr = form.formatDate(kalendarEvent.getBegin());
                        } else {
                            dateStr = form.formatDate(kalendarEvent.getBegin()) + " - " + form.formatDate(kalendarEvent.getEnd());
                        }
                        String desc = translator.translate("cal.notifications.entry", new String[] { subject, dateStr, location, author });
                        String businessPath = bPath + "[path=" + kalendarEvent.getID() + ":0]";
                        String urlToSend = BusinessControlFactory.getInstance().getURLFromBusinessPathString(businessPath);
                        SubscriptionListItem subListItem = new SubscriptionListItem(desc, urlToSend, businessPath, modDate, CSS_CLASS_CALENDAR_ICON);
                        si.addSubscriptionListItem(subListItem);
                    }
                }
            }
        } catch (Exception e) {
            log.error("Unexpected exception", e);
            checkPublisher(p);
            si = notificationsManager.getNoSubscriptionInfo();
        }
    } else {
        si = notificationsManager.getNoSubscriptionInfo();
    }
    return si;
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) OLATResourceable(org.olat.core.id.OLATResourceable) Formatter(org.olat.core.util.Formatter) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) Publisher(org.olat.core.commons.services.notifications.Publisher) RepositoryEntry(org.olat.repository.RepositoryEntry) TitleItem(org.olat.core.commons.services.notifications.model.TitleItem) Date(java.util.Date) SubscriptionListItem(org.olat.core.commons.services.notifications.model.SubscriptionListItem) Kalendar(org.olat.commons.calendar.model.Kalendar) Translator(org.olat.core.gui.translator.Translator)

Example 29 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.

the class CalWebService method deleteEventByCalendar.

@DELETE
@Path("events/{eventId}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response deleteEventByCalendar(@PathParam("eventId") String eventId, @Context HttpServletRequest httpRequest) {
    UserRequest ureq = getUserRequest(httpRequest);
    if (!ureq.getUserSession().isAuthenticated()) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    if (calendar == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    } else if (!hasWriteAccess(calendar)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    CalendarManager calendarManager = CoreSpringFactory.getImpl(CalendarManager.class);
    if (eventId == null) {
        return Response.ok().status(Status.NOT_FOUND).build();
    } else {
        KalendarEvent kalEvent = calendar.getKalendar().getEvent(eventId, null);
        if (kalEvent == null) {
            return Response.ok().status(Status.NOT_FOUND).build();
        } else {
            calendarManager.removeEventFrom(calendar.getKalendar(), kalEvent);
        }
    }
    return Response.ok().build();
}
Also used : CalendarManager(org.olat.commons.calendar.CalendarManager) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 30 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.

the class CalWebService method addEventsByCalendar.

private Response addEventsByCalendar(List<EventVO> events, HttpServletRequest httpRequest) {
    UserRequest ureq = getUserRequest(httpRequest);
    if (!ureq.getUserSession().isAuthenticated()) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    if (calendar == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    } else if (!hasWriteAccess(calendar)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    List<KalendarEvent> kalEventToAdd = new ArrayList<>();
    List<KalendarEvent> kalEventToUpdate = new ArrayList<>();
    CalendarManager calendarManager = CoreSpringFactory.getImpl(CalendarManager.class);
    for (EventVO event : events) {
        KalendarEvent kalEvent;
        if (!StringHelper.containsNonWhitespace(event.getId())) {
            String id = UUID.randomUUID().toString();
            kalEvent = new KalendarEvent(id, event.getRecurrenceId(), event.getSubject(), event.getBegin(), event.getEnd());
            transfer(event, kalEvent);
            kalEventToAdd.add(kalEvent);
        } else {
            kalEvent = calendar.getKalendar().getEvent(event.getId(), event.getRecurrenceId());
            if (kalEvent == null) {
                kalEvent = new KalendarEvent(event.getId(), event.getRecurrenceId(), event.getSubject(), event.getBegin(), event.getEnd());
                transfer(event, kalEvent);
                kalEventToAdd.add(kalEvent);
            } else {
                kalEvent.setBegin(event.getBegin());
                kalEvent.setEnd(event.getEnd());
                kalEvent.setSubject(event.getSubject());
                transfer(event, kalEvent);
                kalEventToUpdate.add(kalEvent);
            }
        }
    }
    if (kalEventToAdd.size() > 0) {
        calendarManager.addEventTo(calendar.getKalendar(), kalEventToAdd);
    }
    if (kalEventToUpdate.size() > 0) {
        calendarManager.updateEventsFrom(calendar.getKalendar(), kalEventToUpdate);
    }
    return Response.ok().build();
}
Also used : CalendarManager(org.olat.commons.calendar.CalendarManager) ArrayList(java.util.ArrayList) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest)

Aggregations

KalendarEvent (org.olat.commons.calendar.model.KalendarEvent)172 Date (java.util.Date)80 Kalendar (org.olat.commons.calendar.model.Kalendar)68 Test (org.junit.Test)48 KalendarRenderWrapper (org.olat.commons.calendar.ui.components.KalendarRenderWrapper)46 Identity (org.olat.core.id.Identity)46 ArrayList (java.util.ArrayList)42 Calendar (java.util.Calendar)40 CalendarImportTest (org.olat.commons.calendar.CalendarImportTest)34 CalendarManager (org.olat.commons.calendar.CalendarManager)32 OLATResourceable (org.olat.core.id.OLATResourceable)20 CalendarGUIModifiedEvent (org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)18 File (java.io.File)16 ExDate (net.fortuna.ical4j.model.property.ExDate)16 ICourse (org.olat.course.ICourse)16 URI (java.net.URI)14 HttpResponse (org.apache.http.HttpResponse)14 URL (java.net.URL)12 KalendarRecurEvent (org.olat.commons.calendar.model.KalendarRecurEvent)12 EventVO (org.olat.commons.calendar.restapi.EventVO)12