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);
}
}
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;
}
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();
}
}
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);
}
}
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);
}
}
Aggregations