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