use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class RestoreImpl method getEvent.
@Override
public BwEvent getEvent(final BwPrincipal owner, final String colPath, final String recurrenceId, final String uid) throws Throwable {
startTransaction();
final Collection<CoreEventInfo> ceis = getCal().getEvent(colPath, uid);
if (ceis.size() != 1) {
error("Expected one event for {" + colPath + ", " + recurrenceId + ", " + uid + "} found " + ceis.size());
return null;
}
final CoreEventInfo ei = ceis.iterator().next();
BwEvent ev = null;
if (recurrenceId == null) {
ev = ei.getEvent();
} else {
for (final CoreEventInfo cei : ei.getOverrides()) {
if (cei.getEvent().getRecurrenceId().equals(recurrenceId)) {
ev = cei.getEvent();
break;
}
}
}
if (ev == null) {
return null;
}
if (ev instanceof BwEventAnnotation) {
ev = new BwEventProxy((BwEventAnnotation) ev);
}
return ev;
}
use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class CoreEvents method deleteEvent.
@Override
public DelEventResult deleteEvent(final EventInfo ei, final boolean scheduling, final boolean reallyDelete) throws CalFacadeException {
final DelEventResult der = new DelEventResult(false, 0);
BwEvent ev = ei.getEvent();
final boolean isMaster = ev.testRecurring() && (ev.getRecurrenceId() == null);
final boolean isInstance = (ev.getRecurrenceId() != null) && (ev instanceof BwEventProxy);
if (!isInstance && ev.unsaved()) {
final CoreEventInfo cei = getEvent(ev.getColPath(), ev.getName(), RecurringRetrievalMode.overrides);
if (cei == null) {
return der;
}
ev = cei.getEvent();
}
final long startTime = System.currentTimeMillis();
final int desiredAccess;
final boolean shared;
try {
final BwCalendar col = getEntityCollection(ev.getColPath(), privAny, scheduling, false);
shared = col.getPublick() || col.getShared();
if (!scheduling) {
desiredAccess = privUnbind;
} else {
/* Delete message while tidying up in/outbox.
* Set desiredAccess to something that works.
* */
final CalendarWrapper cw = (CalendarWrapper) col;
desiredAccess = cw.getLastDesiredAccess();
}
ac.checkAccess(ev, desiredAccess, false);
} catch (final CalFacadeException cfe) {
dao.rollback();
throw cfe;
}
if (!reallyDelete && ev.getTombstoned()) {
// no-op - just pretend
der.eventDeleted = true;
return der;
}
if (isMaster) {
// Master event - delete all instances and overrides.
deleteInstances(ev, shared);
notifyDelete(reallyDelete, ev, shared);
if (reallyDelete) {
dao.delete(ev);
} else {
tombstoneEvent(ev);
}
der.eventDeleted = true;
stat(StatsEvent.deleteTime, startTime);
unindexEntity(ei);
return der;
}
if (isInstance) {
/* Deleting a single instance. Delete any overrides, delete the instance
* and add an exdate to the master.
*/
final BwEventProxy proxy = (BwEventProxy) ev;
final BwEventAnnotation ann = proxy.getRef();
BwEvent master = ann.getMaster();
if (master.unsaved()) {
final CoreEventInfo cei = getEvent(master.getColPath(), master.getName(), RecurringRetrievalMode.overrides);
if (cei == null) {
return der;
}
master = cei.getEvent();
}
/* Fetch the instance so we can delete it */
final BwRecurrenceInstance inst = dao.getInstance(master, ev.getRecurrenceId());
if (inst == null) {
stat(StatsEvent.deleteTime, startTime);
return der;
}
notifyDelete(true, ev, shared);
dao.delete(inst);
if (!ann.unsaved()) {
// der.alarmsDeleted = deleteAlarms(ann);
ann.getAttendees().clear();
dao.delete(ann);
}
final BwDateTime instDate = inst.getDtstart();
if (!master.getRdates().remove(instDate)) {
// Wasn't an rdate event
master.addExdate(instDate);
}
master.updateLastmod();
dao.update(master);
der.eventDeleted = true;
stat(StatsEvent.deleteTime, startTime);
indexEntity(ei.getRetrievedEvent());
return der;
}
// Single non recurring event.
BwEvent deletee = ev;
if (ev instanceof BwEventProxy) {
// Deleting an annotation
deletee = ((BwEventProxy) ev).getRef();
}
// I think we need something like this -- fixReferringAnnotations(deletee);
// XXX This could be wrong.
/* If this is a proxy we should only delete alarmas attached to the
* proxy - any attached to the underlying event should be left alone.
*/
// der.alarmsDeleted = deleteAlarms(deletee);
// sess.delete(sess.merge(deletee));
notifyDelete(reallyDelete, ev, shared);
if (reallyDelete) {
clearCollection(ev.getAttendees());
dao.delete(deletee);
} else {
tombstoneEvent(deletee);
}
der.eventDeleted = true;
stat(StatsEvent.deleteTime, startTime);
unindexEntity(ei);
return der;
}
use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class CoreEventsDAO method calendarGuidExists.
/* Return the name of any event which has the same uid
*/
protected String calendarGuidExists(final BwEvent val, final boolean annotation, final boolean adding) throws CalFacadeException {
final HibSession sess = getSess();
final StringBuilder sb = new StringBuilder();
if (!annotation) {
sb.append(calendarGuidExistsQuery);
} else {
sb.append(calendarGuidAnnotationExistsQuery);
}
BwEvent testEvent = null;
if (!adding) {
if (annotation) {
if (val instanceof BwEventProxy) {
final BwEventProxy proxy = (BwEventProxy) val;
testEvent = proxy.getRef();
}
sb.append("ev.override=false and ");
} else if (!(val instanceof BwEventProxy)) {
testEvent = val;
}
}
if (testEvent != null) {
sb.append("ev<>:event and ");
}
sb.append("ev.colPath=:colPath and ev.uid = :uid");
sess.createQuery(sb.toString());
if (testEvent != null) {
sess.setEntity("event", testEvent);
}
sess.setString("colPath", val.getColPath());
sess.setString("uid", val.getUid());
final Collection refs = sess.getList();
String res = null;
if (refs.size() != 0) {
res = (String) refs.iterator().next();
}
return res;
}
use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class CoreEventsDAO method calendarNameExists.
protected boolean calendarNameExists(final BwEvent val, final boolean annotation, final boolean adding) throws CalFacadeException {
final HibSession sess = getSess();
final StringBuilder sb = new StringBuilder();
if (!annotation) {
sb.append(calendarNameExistsQuery);
} else {
sb.append(calendarNameAnnotationExistsQuery);
}
BwEvent testEvent = null;
if (!adding) {
if (annotation) {
if (val instanceof BwEventProxy) {
final BwEventProxy proxy = (BwEventProxy) val;
testEvent = proxy.getRef();
}
sb.append("ev.override=false and ");
} else if (!(val instanceof BwEventProxy)) {
testEvent = val;
}
}
if (testEvent != null) {
sb.append("ev<>:event and ");
}
sb.append("ev.colPath=:colPath and ");
sb.append("ev.name = :name");
sess.createQuery(sb.toString());
if (testEvent != null) {
sess.setEntity("event", testEvent);
}
sess.setString("colPath", val.getColPath());
sess.setString("name", val.getName());
final Collection refs = sess.getList();
final Object o = refs.iterator().next();
final boolean res;
/* Apparently some get a Long - others get Integer */
if (o instanceof Long) {
final Long ct = (Long) o;
res = ct > 0;
} else {
final Integer ct = (Integer) o;
res = ct > 0;
}
return res;
}
use of org.bedework.calfacade.BwEventProxy in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method addOverrides.
private boolean addOverrides(final Response resp, final String indexName, final EventInfo ei) {
try {
final BwEvent ev = ei.getEvent();
if (!ev.testRecurring()) {
return true;
}
/* Fetch any overrides. */
final ESQueryFilter flts = getFilters(null);
final int batchSize = 100;
int start = 0;
while (true) {
// Search original for overrides
final SearchRequestBuilder srb = getClient().prepareSearch(Util.buildPath(false, indexName));
srb.setSearchType(SearchType.QUERY_THEN_FETCH).setPostFilter(flts.overridesOnly(ev.getUid()));
srb.setFrom(start);
srb.setSize(batchSize);
// if (debug) {
// debug("Overrides: targetIndex=" + indexName +
// "; srb=" + srb);
// }
final SearchResponse sresp = srb.execute().actionGet();
if (sresp.status() != RestStatus.OK) {
errorReturn(resp, "Search returned status " + sresp.status());
return false;
}
final SearchHit[] hits = sresp.getHits().getHits();
if ((hits == null) || (hits.length == 0)) {
// No more data - we're done
break;
}
for (final SearchHit hit : hits) {
final String dtype = hit.getType();
if (dtype == null) {
errorReturn(resp, "org.bedework.index.noitemtype");
return false;
}
final String kval = hit.getId();
if (kval == null) {
errorReturn(resp, "org.bedework.index.noitemkey");
return false;
}
final EntityBuilder eb = getEntityBuilder(hit.sourceAsMap());
final Object entity;
switch(dtype) {
case docTypeEvent:
case docTypePoll:
entity = eb.makeEvent(kval, false);
final EventInfo oei = (EventInfo) entity;
final BwEvent oev = oei.getEvent();
if ((uidsMap != null) && (oev.getLocationUid() == null)) {
String locuid = null;
if (uidsOverideMap != null) {
locuid = uidsOverideMap.get(oev.getUid() + "|" + oev.getRecurrenceId());
}
if (locuid == null) {
locuid = uidsMap.get(oev.getUid());
}
if (locuid != null) {
uidOverridesSet++;
oev.setLocationUid(locuid);
}
oev.setLocationUid(locuid);
}
if (!restoreEvProps(resp, oei)) {
return false;
}
if (oev instanceof BwEventAnnotation) {
final BwEventAnnotation ann = (BwEventAnnotation) oev;
final BwEvent proxy = new BwEventProxy(ann);
ann.setTarget(ev);
ann.setMaster(ev);
ei.addOverride(new EventInfo(proxy));
continue;
}
}
// Unexpected type
errorReturn(resp, "Expected override only: " + dtype);
return false;
}
if (hits.length < batchSize) {
// All remaining in this batch - we're done
break;
}
start += batchSize;
}
return true;
} catch (final Throwable t) {
errorReturn(resp, t);
return false;
}
}
Aggregations