Search in sources :

Example 61 with DateTime

use of net.fortuna.ical4j.model.DateTime in project bw-calendar-engine by Bedework.

the class EventPeriods method addPeriod.

/**
 * @param pstart
 * @param pend
 * @param type
 * @throws CalFacadeException
 */
public void addPeriod(final BwDateTime pstart, final BwDateTime pend, final int type) throws CalFacadeException {
    // Ignore if times were specified and this period is outside the times
    /* Don't report out of the requested period */
    String dstart;
    String dend;
    if ((pstart.after(end)) || (pend.before(start))) {
        // XXX Should get here - but apparently we do.
        return;
    }
    if (pstart.before(start)) {
        dstart = start.getDate();
    } else {
        dstart = pstart.getDate();
    }
    if (pend.after(end)) {
        dend = end.getDate();
    } else {
        dend = pend.getDate();
    }
    try {
        DateTime psdt = new DateTime(dstart);
        DateTime pedt = new DateTime(dend);
        psdt.setUtc(true);
        pedt.setUtc(true);
        add(new EventPeriod(psdt, pedt, type));
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : DateTime(net.fortuna.ical4j.model.DateTime) BwDateTime(org.bedework.calfacade.BwDateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 62 with DateTime

use of net.fortuna.ical4j.model.DateTime in project bw-calendar-engine by Bedework.

the class Sharing method share.

@Override
public ShareResultType share(final BwCalendar col, final ShareType share) throws CalFacadeException {
    if (!col.getCanAlias()) {
        throw new CalFacadeForbidden("Cannot share");
    }
    final ShareResultType sr = new ShareResultType();
    final List<String> removePrincipalHrefs = new ArrayList<>();
    final List<AddPrincipal> addPrincipals = new ArrayList<>();
    final String calAddr = principalToCaladdr(getPrincipal());
    final InviteType invite = getInviteStatus(col);
    if (invite.getOrganizer() == null) {
        final OrganizerType org = new OrganizerType();
        org.setHref(calAddr);
        invite.setOrganizer(org);
    }
    final List<InviteNotificationType> notifications = new ArrayList<>();
    boolean addedSharee = false;
    boolean removedSharee = false;
    /* If there are any removal elements in the invite, remove those
     * sharees. We'll flag hrefs as bad if they are not actually sharees.
     *
     * If we do remove a sharee we'll add notifications to the list
     * to send later.
     */
    for (final RemoveType rem : share.getRemove()) {
        final InviteNotificationType n = doRemove(col, rem, calAddr, invite);
        if (n != null) {
            removedSharee = true;
            if ((n.getPreviousStatus() != null) && !n.getPreviousStatus().equals(declineStatus)) {
                // We don't notify if the user had declined
                notifications.add(n);
            }
            sr.addGood(rem.getHref());
            removePrincipalHrefs.add(rem.getHref());
        } else {
            sr.addBad(rem.getHref());
        }
    }
    /* Now deal with the added sharees if there are any.
     */
    for (final SetType set : share.getSet()) {
        final InviteNotificationType n = doSet(col, set, addPrincipals, calAddr, invite);
        if (n != null) {
            addedSharee = true;
            notifications.add(n);
            sr.addGood(set.getHref());
        } else {
            sr.addBad(set.getHref());
        }
    }
    if (!addedSharee && !removedSharee) {
        // Nothing changed
        return sr;
    }
    /* Send any invitations and update the sharing status.
     * If it's a removal and the current status is not
     * accepted then just delete the current invitation
     */
    final Notifications notify = (Notifications) getSvc().getNotificationsHandler();
    sendNotifications: for (final InviteNotificationType in : notifications) {
        final Sharee sh = getSharee(in.getHref());
        final boolean remove = in.getInviteStatus().equals(removeStatus);
        final List<NotificationType> notes = notify.getMatching(in.getHref(), AppleServerTags.inviteNotification);
        if (!Util.isEmpty(notes)) {
            for (final NotificationType n : notes) {
                final InviteNotificationType nin = (InviteNotificationType) n.getNotification();
                if (!nin.getHostUrl().equals(in.getHostUrl())) {
                    continue;
                }
                if (remove) {
                    if (nin.getInviteStatus().equals(noresponseStatus)) {
                        notify.remove(sh.pr, n);
                        continue sendNotifications;
                    }
                } else {
                    notify.remove(sh.pr, n);
                }
            }
        }
        final NotificationType note = new NotificationType();
        note.setDtstamp(new DtStamp(new DateTime(true)).getValue());
        note.setNotification(in);
        notify.send(sh.pr, note);
        /* Add the invite to the set of properties associated with this collection
       * We give it a name consisting of the inviteNotification tag + uid.
       */
        final QName qn = new QName(AppleServerTags.inviteNotification.getNamespaceURI(), AppleServerTags.inviteNotification.getLocalPart() + in.getUid());
        try {
            col.setProperty(NamespaceAbbrevs.prefixed(qn), in.toXml());
        } catch (final CalFacadeException cfe) {
            throw cfe;
        } catch (final Throwable t) {
            throw new CalFacadeException(t);
        }
    }
    if (addedSharee && !col.getShared()) {
        // Mark the collection as shared
        col.setShared(true);
    }
    try {
        col.setQproperty(AppleServerTags.invite, invite.toXml());
        getCols().update(col);
        for (final String principalHref : removePrincipalHrefs) {
            removeAccess(col, principalHref);
        }
        for (final AddPrincipal ap : addPrincipals) {
            setAccess(col, ap);
        }
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
    return sr;
}
Also used : ShareResultType(org.bedework.caldav.util.sharing.ShareResultType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) OrganizerType(org.bedework.caldav.util.sharing.OrganizerType) DateTime(net.fortuna.ical4j.model.DateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) RemoveType(org.bedework.caldav.util.sharing.RemoveType) DtStamp(net.fortuna.ical4j.model.property.DtStamp) SetType(org.bedework.caldav.util.sharing.SetType) NotificationType(org.bedework.caldav.util.notifications.NotificationType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) InviteType(org.bedework.caldav.util.sharing.InviteType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 63 with DateTime

use of net.fortuna.ical4j.model.DateTime in project bw-calendar-engine by Bedework.

the class Sharing method updateSharingStatus.

/* ====================================================================
   *                   Private methods
   * ==================================================================== */
/* This requires updating the shared calendar to reflect the accept/decline
   * status
   */
private boolean updateSharingStatus(final String sharerHref, final String path, final InviteReplyType reply, final Holder<AccessType> access) throws CalFacadeException {
    pushPrincipal(sharerHref);
    try {
        final BwCalendar col = getCols().get(path);
        if (col == null) {
            // Bad hosturl?
            throw new CalFacadeForbidden(CalFacadeException.shareTargetNotFound);
        }
        /* See if we have an outstanding invite for this user */
        final QName qn = new QName(AppleServerTags.inviteNotification.getNamespaceURI(), AppleServerTags.inviteNotification.getLocalPart() + reply.getInReplyTo());
        final String pname = NamespaceAbbrevs.prefixed(qn);
        final String xmlInvite = col.getProperty(pname);
        if (xmlInvite == null) {
            // No invite
            if (debug) {
                trace("No invite notification on collection with name: " + pname);
            }
            throw new CalFacadeForbidden(CalFacadeException.noInvite);
        }
        /* Remove the invite */
        col.setProperty(pname, null);
        /* Get the invite property and locate and update this sharee */
        final InviteType invite = getInviteStatus(col);
        UserType uentry = null;
        final String invitee = getSvc().getDirectories().normalizeCua(reply.getHref());
        if (invite != null) {
            uentry = invite.finduser(invitee);
        }
        if (uentry == null) {
            if (debug) {
                trace("Cannot find invitee: " + invitee);
            }
            throw new CalFacadeForbidden(CalFacadeException.noInviteeInUsers);
        }
        if (reply.testAccepted()) {
            uentry.setInviteStatus(AppleServerTags.inviteAccepted);
        } else {
            uentry.setInviteStatus(AppleServerTags.inviteDeclined);
        }
        access.value = uentry.getAccess();
        col.setProperty(NamespaceAbbrevs.prefixed(AppleServerTags.invite), invite.toXml());
        getCols().update(col);
        /* Now send the sharer the reply as a notification */
        final NotificationType note = new NotificationType();
        note.setDtstamp(new DtStamp(new DateTime(true)).getValue());
        final InviteReplyType irt = (InviteReplyType) reply.clone();
        note.setNotification(irt);
        /* Fill in the summary (the sharer's summary) on the reply. */
        irt.setSummary(reply.getSummary());
        getSvc().getNotificationsHandler().add(note);
        return irt.testAccepted();
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    } finally {
        popPrincipal();
    }
}
Also used : DtStamp(net.fortuna.ical4j.model.property.DtStamp) QName(javax.xml.namespace.QName) NotificationType(org.bedework.caldav.util.notifications.NotificationType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) InviteType(org.bedework.caldav.util.sharing.InviteType) InviteReplyType(org.bedework.caldav.util.sharing.InviteReplyType) CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) BwCalendar(org.bedework.calfacade.BwCalendar) UserType(org.bedework.caldav.util.sharing.UserType) DateTime(net.fortuna.ical4j.model.DateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 64 with DateTime

use of net.fortuna.ical4j.model.DateTime in project bw-calendar-engine by Bedework.

the class FreeAndBusyHandler method granulateFreeBusy.

/* ====================================================================
   *                         Private methods
   * ==================================================================== */
/*
  private void addFbcal(Collection<BwCalendar> cals,
                        BwCalendar cal) throws CalFacadeException {
    if (cal.getCalType() == BwCalendar.calTypeCollection) {
      // Leaf
      cals.add(cal);
      return;
    }

    Collection<BwCalendar> chs = getSvc().getCalendarsHandler().getChildren(cal);
    for (BwCalendar ch: chs) {
      addFbcal(cals, ch);
    }
  }
  */
private void granulateFreeBusy(final FbGranulatedResponse fbresp, final BwEvent fb, final BwDateTime start, final BwDateTime end, final BwDuration granularity) throws CalFacadeException {
    DateTime startDt;
    DateTime endDt;
    try {
        startDt = new DateTime(start.getDate());
        endDt = new DateTime(end.getDate());
    } catch (ParseException pe) {
        throw new CalFacadeException(pe);
    }
    if (fb.getDtstart().after(start)) {
    // XXX Should warn - or fill in with tentative?
    // warn("Response start after requested start");
    }
    if (fb.getDtend().before(end)) {
    // XXX Should warn - or fill in with tentative?
    // warn("Response end before requested end");
    }
    fbresp.setStart(start);
    fbresp.setEnd(end);
    Collection<EventPeriod> periods = new ArrayList<EventPeriod>();
    if (fb.getFreeBusyPeriods() != null) {
        for (BwFreeBusyComponent fbcomp : fb.getFreeBusyPeriods()) {
            for (Period p : fbcomp.getPeriods()) {
                DateTime pstart = p.getStart();
                DateTime pend = p.getEnd();
                if (!pend.isUtc()) {
                    pend.setUtc(true);
                }
                /* Adjust for servers sending times outside requested range */
                if (pend.after(endDt)) {
                    pend = endDt;
                }
                if (pstart.before(startDt)) {
                    pstart = startDt;
                }
                if (!pend.after(pstart)) {
                    continue;
                }
                periods.add(new EventPeriod(pstart, pend, fbcomp.getType()));
            }
        }
    }
    GetPeriodsPars gpp = new GetPeriodsPars();
    gpp.periods = periods;
    gpp.startDt = start;
    gpp.dur = granularity;
    BwDateTime bwend = end;
    Collection<EventPeriod> respeps = new ArrayList<EventPeriod>();
    fbresp.eps = respeps;
    // XXX do this better
    int limit = 10000;
    /* endDt is null first time through, then represents end of last
     * segment.
     */
    while ((gpp.endDt == null) || (gpp.endDt.before(bwend))) {
        // }
        if (limit < 0) {
            throw new CalFacadeException("org.bedework.svci.limit.exceeded");
        }
        limit--;
        Collection<?> periodEvents = Granulator.getPeriodsEvents(gpp);
        /* Some events fall in the period. Add an entry.
       * We eliminated cancelled events earler. Now we should set the
       * free/busy type based on the events status.
       */
        DateTime psdt;
        DateTime pedt;
        try {
            psdt = new DateTime(gpp.startDt.getDtval());
            pedt = new DateTime(gpp.endDt.getDtval());
        } catch (ParseException pe) {
            throw new CalFacadeException(pe);
        }
        psdt.setUtc(true);
        pedt.setUtc(true);
        EventPeriod ep = new EventPeriod(psdt, pedt, 0);
        setFreeBusyType(ep, periodEvents);
        respeps.add(ep);
    }
}
Also used : EventPeriod(org.bedework.calfacade.util.Granulator.EventPeriod) BwFreeBusyComponent(org.bedework.calfacade.BwFreeBusyComponent) GetPeriodsPars(org.bedework.calfacade.util.Granulator.GetPeriodsPars) BwDateTime(org.bedework.calfacade.BwDateTime) ArrayList(java.util.ArrayList) Period(net.fortuna.ical4j.model.Period) EventPeriod(org.bedework.calfacade.util.Granulator.EventPeriod) ParseException(java.text.ParseException) DateTime(net.fortuna.ical4j.model.DateTime) BwDateTime(org.bedework.calfacade.BwDateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 65 with DateTime

use of net.fortuna.ical4j.model.DateTime in project openmeetings by apache.

the class TestSendIcalMessage method simpleInvitionIcalLink.

public void simpleInvitionIcalLink() {
    // Create a TimeZone
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    TimeZone timezone = registry.getTimeZone("America/Mexico_City");
    VTimeZone tz = timezone.getVTimeZone();
    // Start Date is on: April 1, 2008, 9:00 am
    java.util.Calendar startDate = new GregorianCalendar();
    startDate.setTimeZone(timezone);
    startDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
    startDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
    startDate.set(java.util.Calendar.YEAR, 2008);
    startDate.set(java.util.Calendar.HOUR_OF_DAY, 9);
    startDate.set(java.util.Calendar.MINUTE, 0);
    startDate.set(java.util.Calendar.SECOND, 0);
    // End Date is on: April 1, 2008, 13:00
    java.util.Calendar endDate = new GregorianCalendar();
    endDate.setTimeZone(timezone);
    endDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
    endDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
    endDate.set(java.util.Calendar.YEAR, 2008);
    endDate.set(java.util.Calendar.HOUR_OF_DAY, 13);
    endDate.set(java.util.Calendar.MINUTE, 0);
    endDate.set(java.util.Calendar.SECOND, 0);
    // Create the event
    String eventName = "Progress Meeting";
    DateTime start = new DateTime(startDate.getTime());
    DateTime end = new DateTime(endDate.getTime());
    VEvent meeting = new VEvent(start, end, eventName);
    // add timezone info..
    meeting.getProperties().add(tz.getTimeZoneId());
    // generate unique identifier..
    Uid uid = new Uid(UUID.randomUUID().toString());
    meeting.getProperties().add(uid);
    // add attendees..
    Attendee dev1 = new Attendee(URI.create("mailto:dev1@mycompany.com"));
    dev1.getParameters().add(Role.REQ_PARTICIPANT);
    dev1.getParameters().add(new Cn("Developer 1"));
    meeting.getProperties().add(dev1);
    Attendee dev2 = new Attendee(URI.create("mailto:dev2@mycompany.com"));
    dev2.getParameters().add(Role.OPT_PARTICIPANT);
    dev2.getParameters().add(new Cn("Developer 2"));
    meeting.getProperties().add(dev2);
    // Create a calendar
    net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
    icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
    icsCalendar.getProperties().add(CalScale.GREGORIAN);
    icsCalendar.getProperties().add(Version.VERSION_2_0);
    // Add the event and print
    icsCalendar.getComponents().add(meeting);
    Organizer orger = new Organizer(URI.create("seba.wagner@gmail.com"));
    orger.getParameters().add(new Cn("Sebastian Wagner"));
    meeting.getProperties().add(orger);
    icsCalendar.getProperties().add(Method.REQUEST);
    log.debug(icsCalendar.toString());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    CalendarOutputter outputter = new CalendarOutputter();
    try {
        outputter.output(icsCalendar, bout);
        iCalMimeBody = bout.toByteArray();
        sendIcalMessage();
    } catch (Exception e) {
        log.error("Error", e);
    }
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) Organizer(net.fortuna.ical4j.model.property.Organizer) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) TimeZoneRegistry(net.fortuna.ical4j.model.TimeZoneRegistry) GregorianCalendar(java.util.GregorianCalendar) Cn(net.fortuna.ical4j.model.parameter.Cn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProdId(net.fortuna.ical4j.model.property.ProdId) DateTime(net.fortuna.ical4j.model.DateTime) Attendee(net.fortuna.ical4j.model.property.Attendee) Uid(net.fortuna.ical4j.model.property.Uid) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) TimeZone(net.fortuna.ical4j.model.TimeZone) Calendar(java.util.Calendar) CalendarOutputter(net.fortuna.ical4j.data.CalendarOutputter)

Aggregations

DateTime (net.fortuna.ical4j.model.DateTime)70 Period (net.fortuna.ical4j.model.Period)20 VEvent (net.fortuna.ical4j.model.component.VEvent)18 BwDateTime (org.bedework.calfacade.BwDateTime)15 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)15 Date (java.util.Date)13 TimeZone (net.fortuna.ical4j.model.TimeZone)13 ParseException (java.text.ParseException)12 DtStart (net.fortuna.ical4j.model.property.DtStart)11 Date (net.fortuna.ical4j.model.Date)10 Dur (net.fortuna.ical4j.model.Dur)10 PeriodList (net.fortuna.ical4j.model.PeriodList)10 PropertyList (net.fortuna.ical4j.model.PropertyList)9 DtStamp (net.fortuna.ical4j.model.property.DtStamp)9 LastModified (net.fortuna.ical4j.model.property.LastModified)9 Uid (net.fortuna.ical4j.model.property.Uid)9 Calendar (net.fortuna.ical4j.model.Calendar)8 Recur (net.fortuna.ical4j.model.Recur)8 ExDate (net.fortuna.ical4j.model.property.ExDate)8 GregorianCalendar (java.util.GregorianCalendar)7