Search in sources :

Example 6 with BwDbentity

use of org.bedework.calfacade.base.BwDbentity in project bw-calendar-engine by Bedework.

the class CoreEvents method updateEvent.

@Override
public UpdateEventResult updateEvent(final EventInfo ei) throws CalFacadeException {
    final BwEvent val = ei.getEvent();
    final Collection<BwEventProxy> overrides = ei.getOverrideProxies();
    final Collection<BwEventProxy> deletedOverrides = ei.getDeletedOverrideProxies(cb.getPrincipalInfo().getPrincipal().getPrincipalRef());
    final UpdateEventResult ue = new UpdateEventResult();
    if (!ac.checkAccess(val, privWrite, true).getAccessAllowed()) {
        // XXX Is this correct?
        try {
            ac.checkAccess(val, privWriteContent, false);
        } catch (final CalFacadeException cfe) {
            throwException(cfe);
        }
    }
    BwEventProxy proxy = null;
    if (val instanceof BwEventProxy) {
        proxy = (BwEventProxy) val;
    }
    final BwCalendar col = getCollection(val.getColPath());
    final boolean shared = col.getPublick() || col.getShared();
    if ((proxy != null) && (proxy.getRef().getOverride())) {
        final BwEventAnnotation ann = proxy.getRef();
        final BwEvent mstr = ann.getMaster();
        if (!proxy.getUid().equals(mstr.getUid())) {
            throwException("org.bedework.cannot.overrideuid");
        }
        if (!proxy.getName().equals(mstr.getName())) {
            throwException("org.bedework.cannot.overridename");
        }
    } else {
        /* 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
       */
        final CollectionInfo collInf = col.getCollectionInfo();
        if (collInf.uniqueKey) {
            String name = calendarGuidExists(val, false, false);
            if (name == null) {
                name = calendarGuidExists(val, true, false);
            }
            if (name != null) {
                throwException(CalFacadeException.duplicateGuid, name);
            }
        }
        /* Similarly for event names which must be unique within a collection
       */
        if (calendarNameExists(val, false, false) || calendarNameExists(val, true, false)) {
            throwException(new CalFacadeDupNameException(val.getName()));
        }
    }
    if (!(val instanceof BwEventProxy)) {
        dao.update(val);
        final Collection<BwDbentity<?>> deleted = val.getDeletedEntities();
        if (deleted != null) {
            for (final BwDbentity ent : deleted) {
                dao.delete(ent);
            }
            deleted.clear();
        }
        if (val.testRecurring()) {
            if (!Util.isEmpty(overrides)) {
                for (final BwEventProxy pxy : overrides) {
                    final BwEventAnnotation ann = pxy.getRef();
                    boolean updated = false;
                    if ((ann.getRecurring() != null) && ann.getRecurring()) {
                        // be safe
                        ann.setRecurring(false);
                        updated = true;
                    }
                    if (ann.getTombstoned() == null) {
                        // be safe
                        ann.setTombstoned(false);
                        updated = true;
                    }
                    if (ann.unsaved()) {
                        dao.save(ann);
                    } else if (updated) {
                        updateProxy(new BwEventProxy(ann));
                    }
                }
            }
            updateRecurrences(ei, ue, overrides, shared);
        }
        if (!val.testRecurring() || (Util.isEmpty(overrides) && Util.isEmpty(deletedOverrides))) {
            notify(SysEvent.SysCode.ENTITY_UPDATED, val, shared);
            indexEntity(ei);
            return ue;
        }
        if (!Util.isEmpty(overrides)) {
            updateOverrides: for (final BwEventProxy pxy : overrides) {
                final BwEventAnnotation ann = pxy.getRef();
                if (ue.deleted != null) {
                    for (final BwRecurrenceInstance ri : ue.deleted) {
                        if (ri.getRecurrenceId().equals(ann.getRecurrenceId())) {
                            continue updateOverrides;
                        }
                    }
                }
                if (ue.added != null) {
                    for (final BwRecurrenceInstance ri : ue.added) {
                        if (ri.getRecurrenceId().equals(ann.getRecurrenceId())) {
                            continue updateOverrides;
                        }
                    }
                }
                // 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);
                    /* See if there is an instance for this override
             */
                    BwRecurrenceInstance ri = dao.getInstance(val, ann.getRecurrenceId());
                    if (ri == null) {
                        final BwDateTime rid = BwDateTime.fromUTC(ann.getRecurrenceId().length() == 8, ann.getRecurrenceId());
                        final Dur dur = new Dur(val.getDuration());
                        final BwDateTime end = rid.addDur(dur);
                        ri = new BwRecurrenceInstance();
                        ri.setDtstart(rid);
                        ri.setDtend(end);
                        ri.setRecurrenceId(rid.getDate());
                        ri.setMaster(val);
                        ri.setOverride(ann);
                        dao.save(ri);
                    } else {
                        ri.setOverride(ann);
                        dao.update(ri);
                    }
                }
                notifyInstanceChange(SysEvent.SysCode.ENTITY_UPDATED, val, shared, ann.getRecurrenceId());
            }
        }
        if (!Util.isEmpty(deletedOverrides)) {
            final Collection<String> rids = new ArrayList<>();
            for (final BwEventProxy pxy : deletedOverrides) {
                rids.add(pxy.getRecurrenceId());
            }
            removeInstances(val, rids, ue, deletedOverrides, shared);
        }
    } else {
        if (proxy.getChangeFlag()) {
            updateProxy(proxy);
        }
    }
    notify(SysEvent.SysCode.ENTITY_UPDATED, val, shared);
    if (proxy != null) {
        if (ei.getRetrievedEvent() == null) {
            warn("No retrieved event for indexer");
        } else {
            final EventInfo rei = ei.getRetrievedEvent();
            rei.addOverride(ei);
            indexEntity(rei);
        }
    } else {
        indexEntity(ei);
    }
    return ue;
}
Also used : Dur(net.fortuna.ical4j.model.Dur) BwDateTime(org.bedework.calfacade.BwDateTime) CoreEventInfo(org.bedework.calcorei.CoreEventInfo) EventInfo(org.bedework.calfacade.svc.EventInfo) CollectionInfo(org.bedework.calfacade.BwCalendar.CollectionInfo) ArrayList(java.util.ArrayList) BwEvent(org.bedework.calfacade.BwEvent) BwCalendar(org.bedework.calfacade.BwCalendar) BwEventProxy(org.bedework.calfacade.BwEventProxy) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BwRecurrenceInstance(org.bedework.calfacade.BwRecurrenceInstance) CalFacadeDupNameException(org.bedework.calfacade.exc.CalFacadeDupNameException) BwEventAnnotation(org.bedework.calfacade.BwEventAnnotation) BwDbentity(org.bedework.calfacade.base.BwDbentity)

Aggregations

BwDbentity (org.bedework.calfacade.base.BwDbentity)6 BwCalendar (org.bedework.calfacade.BwCalendar)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 BwPrincipal (org.bedework.calfacade.BwPrincipal)2 List (java.util.List)1 Dur (net.fortuna.ical4j.model.Dur)1 CoreEventInfo (org.bedework.calcorei.CoreEventInfo)1 TimeRange (org.bedework.caldav.util.TimeRange)1 AndFilter (org.bedework.caldav.util.filter.AndFilter)1 EntityTimeRangeFilter (org.bedework.caldav.util.filter.EntityTimeRangeFilter)1 FilterBase (org.bedework.caldav.util.filter.FilterBase)1 ObjectFilter (org.bedework.caldav.util.filter.ObjectFilter)1 OrFilter (org.bedework.caldav.util.filter.OrFilter)1 TimeRangeFilter (org.bedework.caldav.util.filter.TimeRangeFilter)1 CollectionInfo (org.bedework.calfacade.BwCalendar.CollectionInfo)1 BwCategory (org.bedework.calfacade.BwCategory)1 BwDateTime (org.bedework.calfacade.BwDateTime)1 BwEvent (org.bedework.calfacade.BwEvent)1 BwEventAnnotation (org.bedework.calfacade.BwEventAnnotation)1