use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class CoreEvents method addEvent.
@Override
public UpdateEventResult addEvent(final EventInfo ei, final boolean scheduling, final boolean rollbackOnError) throws CalFacadeException {
final BwEvent val = ei.getEvent();
final Collection<BwEventProxy> overrides = ei.getOverrideProxies();
final long startTime = System.currentTimeMillis();
RecuridTable recurids = null;
final UpdateEventResult uer = new UpdateEventResult();
uer.addedUpdated = true;
final BwCalendar cal = getEntityCollection(val.getColPath(), privBind, scheduling, false);
/* Indicate if we want sharing notifications of changes */
final boolean shared = cal.getPublick() || cal.getShared();
final CollectionInfo collInf = cal.getCollectionInfo();
if (!Util.isEmpty(overrides)) {
if (!val.testRecurring()) {
throwException(CalFacadeException.overridesForNonRecurring);
}
recurids = new RecuridTable(overrides);
}
if (val.getUid() == null) {
throwException(CalFacadeException.noEventGuid);
}
if (val.getName() == null) {
throwException(CalFacadeException.noEventName);
}
/* The guid must not exist in the same calendar. We assign a guid if
* one wasn't assigned already. However, the event may have come with a guid
* (caldav, import, etc) so we need to check here.
*
* It also ensures our guid allocation is working OK
*/
if (collInf.uniqueKey) {
String name = calendarGuidExists(val, false, true);
if (name == null) {
name = calendarGuidExists(val, true, true);
}
if (name != null) {
throwException(CalFacadeException.duplicateGuid, name);
}
}
/* Similarly for event names which must be unique within a collection.
* Note that a duplicate name is essentially overwriting an event with a
* new uid - also disallowed.
*/
if ((val.getEntityType() != IcalDefs.entityTypeAvailable) && (calendarNameExists(val, false, true) || calendarNameExists(val, true, true))) {
throwException(CalFacadeException.duplicateName, val.getName());
}
setupDependentEntities(val);
/* Remove any tombstoned event in the collection with same uid */
deleteTombstoned(val.getColPath(), val.getUid());
/* If it's a recurring event see what we can do to optimize searching
* and retrieval
*/
if ((val instanceof BwEventAnnotation) || !val.getRecurring()) {
dao.save(val);
if (!getForRestore()) {
notify(SysEvent.SysCode.ENTITY_ADDED, val, shared);
}
stat(StatsEvent.createTime, startTime);
indexEntity(ei);
return uer;
}
/* Get all the times for this event. - this could be a problem. Need to
limit the number. Should we do this in chunks, stepping through the
whole period?
*/
final RecurPeriods rp = RecurUtil.getPeriods(val, getAuthprops().getMaxYears(), getAuthprops().getMaxInstances());
if (rp.instances.isEmpty()) {
// No instances for an alleged recurring event.
if (rollbackOnError) {
throwException(CalFacadeException.noRecurrenceInstances, val.getUid());
}
uer.addedUpdated = false;
uer.errorCode = CalFacadeException.noRecurrenceInstances;
stat(StatsEvent.createTime, startTime);
indexEntity(ei);
return uer;
}
/* We can save the master at this point */
dao.save(val);
final String stzid = val.getDtstart().getTzid();
final TimeZone stz = null;
/* try {
if (stzid != null) {
stz = Timezones.getTz(stzid);
}
val.setLatestDate(Timezones.getUtc(rp.rangeEnd.toString(),
stzid));
} catch (Throwable t) {
throwException(new CalFacadeException(t));
} */
int maxInstances = getAuthprops().getMaxInstances();
final boolean dateOnly = val.getDtstart().getDateType();
/* There appears to be a bug in ical4j in which the first instance gets
* duplicated. Rather than change that code and run the risk of breaking
* all recurrences I'll just look for that duplicate.
*/
String firstRecurrenceId = null;
for (final Period p : rp.instances) {
String dtval = p.getStart().toString();
if (dateOnly) {
dtval = dtval.substring(0, 8);
}
final BwDateTime rstart = BwDateTime.makeBwDateTime(dateOnly, dtval, stzid);
final DateTime edt = p.getEnd();
if (!dateOnly && (stz != null)) {
edt.setTimeZone(stz);
}
dtval = edt.toString();
if (dateOnly) {
dtval = dtval.substring(0, 8);
}
final BwDateTime rend = BwDateTime.makeBwDateTime(dateOnly, dtval, stzid);
final BwRecurrenceInstance ri = new BwRecurrenceInstance();
ri.setDtstart(rstart);
ri.setDtend(rend);
ri.setRecurrenceId(ri.getDtstart().getDate());
ri.setMaster(val);
if (firstRecurrenceId == null) {
firstRecurrenceId = ri.getRecurrenceId();
} else if (firstRecurrenceId.equals(ri.getRecurrenceId())) {
// Skip it
if (debug) {
debugMsg("Skipping duplicate recurid " + firstRecurrenceId);
}
continue;
}
if (recurids != null) {
/* See if we have a recurrence */
final String rid = ri.getRecurrenceId();
final BwEventProxy ov = recurids.get(rid);
if (ov != null) {
if (debug) {
debugMsg("Add override with recurid " + rid);
}
setupDependentEntities(ov);
addOverride(ov, ri);
recurids.remove(rid);
}
}
dao.save(ri);
maxInstances--;
if (maxInstances == 0) {
// That's all you're getting from me
break;
}
}
if ((recurids != null) && (recurids.size() != 0)) {
/* We removed all the valid overrides - we are left with those
* with recurrence ids that don't match.
*/
if (rollbackOnError) {
throwException(CalFacadeException.invalidOverride);
}
uer.failedOverrides = recurids.values();
}
if (!getForRestore()) {
notify(SysEvent.SysCode.ENTITY_ADDED, val, shared);
}
indexEntity(ei);
stat(StatsEvent.createTime, startTime);
return uer;
}
use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class CoreEvents method removeInstance.
/* Remove instances identified by the Collection of recurrence ids
*/
private void removeInstance(final BwEvent master, final UpdateEventResult uc, final Collection<BwEventProxy> overrides, final String rid, final boolean shared) throws CalFacadeException {
notifyInstanceChange(SysEvent.SysCode.ENTITY_DELETED, master, shared, rid);
if (overrides != null) {
for (final BwEventProxy pr : overrides) {
if (pr.getRecurrenceId() == null) {
throw new NullPointerException();
}
if (pr.getRecurrenceId().equals(rid)) {
// This one is being deleted
overrides.remove(pr);
break;
}
}
}
final BwRecurrenceInstance inst = dao.getInstance(master, rid);
if (inst != null) {
dao.delete(inst);
uc.addDeleted(inst);
}
}
use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class CoreEvents method addInstances.
/* Add instances identified by the Collection of recurrence ids
*/
private void addInstances(final BwEvent master, final UpdateEventResult uc, final Collection<BwEventProxy> overrides, final Collection rids, final boolean shared) throws CalFacadeException {
final Dur dur = new Dur(master.getDuration());
for (final Object rid : rids) {
final BwDateTime start = (BwDateTime) rid;
final BwDateTime end = start.addDur(dur);
final BwRecurrenceInstance ri = new BwRecurrenceInstance();
ri.setDtstart(start);
ri.setDtend(end);
ri.setRecurrenceId(start.getDate());
ri.setMaster(master);
if (!Util.isEmpty(overrides)) {
for (final BwEventProxy pxy : overrides) {
final BwEventAnnotation ann = pxy.getRef();
if (!ann.getRecurrenceId().equals(ri.getRecurrenceId())) {
continue;
}
// be safe
ann.setRecurring(false);
if (ann.getTombstoned() == null) {
// be safe
ann.setTombstoned(false);
}
if (!ann.unsaved()) {
updateProxy(new BwEventProxy(ann));
} else {
dao.save(ann);
}
ri.setOverride(ann);
break;
}
}
dao.save(ri);
notifyInstanceChange(SysEvent.SysCode.ENTITY_ADDED, master, shared, start.getDate());
uc.addAdded(ri);
}
}
use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class AttachmentRule method end.
@Override
public void end(final String ns, final String name) throws Exception {
BwAttachment entity = (BwAttachment) pop();
EventInfo ei = (EventInfo) getTop(EventInfo.class, name);
BwEvent e = ei.getEvent();
if (e instanceof BwEventProxy) {
e = ((BwEventProxy) e).getRef();
}
// Mark unsaved
entity.setId(CalFacadeDefs.unsavedItemKey);
e.addAttachment(entity);
}
use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class AttendeeRule method end.
@Override
public void end(final String ns, final String name) throws Exception {
BwAttendee entity = (BwAttendee) pop();
EventInfo ei = (EventInfo) getTop(EventInfo.class, name);
BwEvent e = ei.getEvent();
if (e instanceof BwEventProxy) {
e = ((BwEventProxy) e).getRef();
}
// Mark unsaved
entity.setId(CalFacadeDefs.unsavedItemKey);
e.addAttendee(entity);
}
Aggregations