use of org.bedework.calfacade.BwResource in project bw-calendar-engine by Bedework.
the class ResourceContentRule method end.
@Override
public void end(final String ns, final String name) throws Exception {
BwResourceContent entity = (BwResourceContent) pop();
if (!(top() instanceof BwResource)) {
warn("Expected a resource object: found " + top());
return;
}
BwResource r = (BwResource) top();
r.setContent(entity);
}
use of org.bedework.calfacade.BwResource in project bw-calendar-engine by Bedework.
the class Calendars method delete.
boolean delete(final BwCalendar val, final boolean emptyIt, final boolean reallyDelete, final boolean sendSchedulingMessage, final boolean unsubscribe) throws CalFacadeException {
if (!emptyIt) {
/* Only allow delete if not in use
*/
if (!getCal().isEmpty(val)) {
throw new CalFacadeException(CalFacadeException.collectionNotEmpty);
}
}
final BwPreferences prefs = getSvc().getPrefsHandler().get(getSvc().getUsersHandler().getPrincipal(val.getOwnerHref()));
if (val.getPath().equals(prefs.getDefaultCalendarPath())) {
throw new CalFacadeException(CalFacadeException.cannotDeleteDefaultCalendar);
}
/* Remove any sharing */
if (val.getCanAlias()) {
getSvc().getSharingHandler().delete(val);
}
if (unsubscribe) {
getSvc().getSharingHandler().unsubscribe(val);
}
getSvc().getSynch().unsubscribe(val, true);
/* Remove from preferences */
((Preferences) getSvc().getPrefsHandler()).updateAdminPrefs(true, val, null, null, null);
/* If it' an alias we just delete it - otherwise we might need to empty it.
*/
if (!val.getInternalAlias() && emptyIt) {
if (val.getCalendarCollection()) {
final Events events = ((Events) getSvc().getEventsHandler());
for (final EventInfo ei : events.getSynchEvents(val.getPath(), null)) {
events.delete(ei, false, sendSchedulingMessage, true);
}
}
/* Remove resources */
final ResourcesI resI = getSvc().getResourcesHandler();
final Collection<BwResource> rs = resI.getAll(val.getPath());
if (!Util.isEmpty(rs)) {
for (final BwResource r : rs) {
resI.delete(Util.buildPath(false, r.getColPath(), "/", r.getName()));
}
}
for (final BwCalendar cal : getChildren(val)) {
if (!delete(cal, true, true, sendSchedulingMessage, true)) {
// Somebody else at it
getSvc().rollbackTransaction();
throw new CalFacadeException(CalFacadeException.collectionNotFound, cal.getPath());
}
}
}
val.getProperties().clear();
/* Attempt to tombstone it
*/
return getSvc().getCal().deleteCalendar(val, reallyDelete);
}
use of org.bedework.calfacade.BwResource in project bw-calendar-engine by Bedework.
the class Notifications method add.
@Override
public boolean add(final NotificationType val) throws CalFacadeException {
if ((val == null) || (val.getNotification() == null) || (val.getNotification().getElementName() == null)) {
return false;
}
final BwCalendar ncol = getCols().getSpecial(BwCalendar.calTypeNotifications, true);
if (ncol == null) {
return false;
}
final BwResource noteRsrc = new BwResource();
noteRsrc.setName(val.getName());
noteRsrc.setEncoding(val.getNotification().getEncoding());
final BwResourceContent rc = new BwResourceContent();
noteRsrc.setContent(rc);
try {
final String xml = val.toXml(true);
if (xml == null) {
return false;
}
final byte[] xmlData = xml.getBytes();
rc.setValue(getSvc().getBlob(xmlData));
noteRsrc.setContentLength(xmlData.length);
noteRsrc.setContentType(val.getContentType());
} catch (final Throwable t) {
throw new CalFacadeException(t);
}
for (int i = 0; i <= 100; i++) {
if (getRess().saveNotification(ncol.getPath(), noteRsrc)) {
getNoteClient().informNotifier(getPrincipalHref(), noteRsrc.getName());
return true;
}
noteRsrc.setName(val.getName() + "-" + i);
}
throw new CalFacadeException(CalFacadeException.duplicateResource, val.getName());
}
use of org.bedework.calfacade.BwResource in project bw-calendar-engine by Bedework.
the class ResourcesImpl method copyMove.
@Override
public boolean copyMove(final BwResource val, final String to, final String name, final boolean copy, final boolean overwrite) throws CalFacadeException {
try {
setupSharableEntity(val, getPrincipal().getPrincipalRef());
final BwCalendar collTo = getCols().get(to);
if (collTo == null) {
throw new CalFacadeException(CalFacadeException.collectionNotFound, to);
}
if (collTo.getCalType() != BwCalendar.calTypeFolder) {
// Only allowed into a folder.
throw new CalFacadeException(CalFacadeException.badRequest, to);
}
final int access;
if (copy) {
access = PrivilegeDefs.privWrite;
} else {
access = PrivilegeDefs.privBind;
}
checkAccess(collTo, access, false);
BwResource r = getCal().getResource(val.getName(), collTo, access);
boolean createdNew = false;
getContent(val);
if (r != null) {
/* Update of the target from the source */
if (!overwrite) {
throw new CalFacadeException(CalFacadeException.targetExists, val.getName());
}
getContent(r);
r.setContentType(val.getContentType());
final BwResourceContent rc = r.getContent();
final BwResourceContent toRc = val.getContent();
r.setContentLength(toRc.getValue().length());
r.updateLastmod(getCurrentTimestamp());
rc.setValue(val.getContent().getValue());
getCal().saveOrUpdate(r);
getCal().saveOrUpdate(rc);
} else {
/* Create a new resource */
r = new BwResource();
setupSharableEntity(r, getPrincipal().getPrincipalRef());
r.setName(name);
r.setColPath(collTo.getPath());
r.setContentType(val.getContentType());
r.setContentLength(val.getContentLength());
r.updateLastmod(getCurrentTimestamp());
getCal().saveOrUpdate(r);
final BwResourceContent fromRc = val.getContent();
final BwResourceContent rc = new BwResourceContent();
rc.setColPath(collTo.getPath());
rc.setName(val.getName());
rc.setValue(fromRc.getValue());
getCal().saveOrUpdate(rc);
createdNew = true;
}
if (!copy) {
// Delete (tombstone) the old one
final BwCalendar collFrom = getCols().get(val.getColPath());
checkAccess(collFrom, PrivilegeDefs.privUnbind, false);
final BwResourceContent rc = val.getContent();
getCal().delete(rc);
/* Remove any previous tombstoned version */
final BwResource tr = getCal().getResource(val.getName() + BwResource.tombstonedSuffix, collFrom, PrivilegeDefs.privUnbind);
if (tr != null) {
getCal().delete(tr);
}
val.setContent(null);
val.tombstone();
val.updateLastmod(getCurrentTimestamp());
getCal().saveOrUpdate(val);
touchCalendar(collFrom);
}
touchCalendar(collTo);
return createdNew;
} catch (final CalFacadeException cfe) {
getSvc().rollbackTransaction();
throw cfe;
} catch (final Throwable t) {
getSvc().rollbackTransaction();
throw new CalFacadeException(t);
}
}
use of org.bedework.calfacade.BwResource in project bw-calendar-engine by Bedework.
the class ResourcesImpl method save.
private boolean save(final String path, final BwResource val, final boolean forNotification, final boolean returnIfExists) throws CalFacadeException {
try {
final BwCalendar coll = getCols().get(path);
if (coll == null) {
throw new CalFacadeException(CalFacadeException.collectionNotFound, path);
}
if (forNotification) {
// We allow this for subscription only
if (coll.getCalType() != BwCalendar.calTypeNotifications) {
throw new CalFacadeException(CalFacadeException.badRequest, path);
}
} else if (getPrincipalInfo().getSubscriptionsOnly()) {
throw new CalFacadeForbidden("User has read only access");
}
final BwResource r = getCal().getResource(val.getName(), coll, PrivilegeDefs.privAny);
if (r != null) {
if (returnIfExists) {
return false;
}
throw new CalFacadeException(CalFacadeException.duplicateResource, val.getName());
}
final BwResourceContent rc = val.getContent();
if (rc == null) {
throw new CalFacadeException(CalFacadeException.missingResourceContent);
}
setupSharableEntity(val, getPrincipal().getPrincipalRef());
val.setColPath(path);
if ((coll.getCalType() == BwCalendar.calTypeCalendarCollection) || (coll.getCalType() == BwCalendar.calTypeExtSub)) {
throw new CalFacadeException(CalFacadeException.badRequest, path);
}
checkAccess(coll, PrivilegeDefs.privBind, false);
val.updateLastmod(getCurrentTimestamp());
getCal().add(val);
rc.setColPath(val.getColPath());
rc.setName(val.getName());
getCal().add(rc);
touchCalendar(coll);
return true;
} catch (final CalFacadeException cfe) {
getSvc().rollbackTransaction();
throw cfe;
}
}
Aggregations