use of org.bedework.icalendar.IcalTranslator in project bw-calendar-engine by Bedework.
the class Events method compileAlarms.
/**
* Compile an alarm component
*
* @param val
* @return alarms or null
* @throws CalFacadeException
*/
public Set<BwAlarm> compileAlarms(final String val) throws CalFacadeException {
try {
StringReader sr = new StringReader(ValidateAlarmPrefix + val + ValidateAlarmSuffix);
IcalTranslator trans = new IcalTranslator(getSvc().getIcalCallback());
Icalendar ic = trans.fromIcal(null, sr);
if ((ic == null) || (ic.getEventInfo() == null)) {
if (debug) {
trace("Not single event");
}
return null;
}
/* There should be alarms in the Calendar object
*/
EventInfo ei = ic.getEventInfo();
BwEvent ev = ei.getEvent();
Set<BwAlarm> alarms = ev.getAlarms();
if (Util.isEmpty(alarms)) {
return null;
}
return alarms;
} catch (CalFacadeException cfe) {
if (debug) {
error(cfe);
}
return null;
}
}
use of org.bedework.icalendar.IcalTranslator in project bw-calendar-engine by Bedework.
the class DumpPrincipal method open.
public boolean open(final String dirName) {
if (!super.open()) {
return false;
}
/* Create a directory for the principal */
Response resp = makeDir(dirName, true);
if (resp.getStatus() == failed) {
return false;
}
if (resp.getStatus() == exists) {
// Duplicate user entry?
incCount(DumpGlobals.duplicateUsers);
for (int i = 0; i < 100; i++) {
resp = makeDir(dirName + "-dup-" + i, true);
if (resp.getStatus() == failed) {
return false;
}
if (resp.getStatus() == ok) {
break;
}
if (i == 99) {
addLn("Too many duplicates for " + dirName);
return false;
}
popPath();
}
}
icalTrans = new IcalTranslator(getSvc().getIcalCallback(true));
created = new TreeSet<>();
return true;
}
use of org.bedework.icalendar.IcalTranslator in project bw-calendar-engine by Bedework.
the class BwSysIntfImpl method getSvci.
private CalSvcI getSvci(final String account, final String runAs, final boolean service, final boolean publicAdmin, final String clientId, final boolean allowCreateEprops) throws WebdavException {
try {
/* account is what we authenticated with.
* user, if non-null, is the user calendar we want to access.
*/
final boolean possibleSuperUser = // allow SuperUser
"root".equals(account) || "admin".equals(account);
String runAsUser = null;
String clientIdent = null;
if (possibleSuperUser) {
runAsUser = runAs;
clientIdent = clientId;
}
final CalSvcIPars pars = CalSvcIPars.getCaldavPars("bwcaldav", account, runAsUser, clientIdent, // allow SuperUser
possibleSuperUser, service, publicAdmin, allowCreateEprops);
svci = new CalSvcFactoryDefault().getSvc(pars);
svci.open();
svci.beginTransaction();
trans = new IcalTranslator(svci.getIcalCallback());
} catch (Throwable t) {
throw new WebdavException(t);
}
return svci;
}
use of org.bedework.icalendar.IcalTranslator in project bw-calendar-engine by Bedework.
the class OutScheduler method processOutBox.
/* Process pending messages in outbox.
*
*/
private ProcessMessageResult processOutBox() throws CalFacadeException {
IcalTranslator trans = new IcalTranslator(getSvc().getIcalCallback());
Collection<EventInfo> eis = getOutboxEvents();
if (eis == null) {
return ProcessMessageResult.NO_ACTION;
}
class DedupKey implements Comparable<DedupKey> {
private String uid;
private String rid;
DedupKey(final String uid, final String rid) {
this.uid = uid;
this.rid = rid;
}
public int compareTo(final DedupKey that) {
int res = uid.compareTo(that.uid);
if (res != 0) {
return res;
}
if ((rid == null) && (that.rid == null)) {
return 0;
}
if ((rid != null) && (that.rid != null)) {
return rid.compareTo(that.rid);
}
if (rid != null) {
return 1;
}
return -1;
}
@Override
public int hashCode() {
int h = uid.hashCode();
if (rid != null) {
h *= rid.hashCode();
}
return h;
}
@Override
public boolean equals(final Object o) {
return compareTo((DedupKey) o) == 0;
}
}
Map<DedupKey, EventInfo> deduped = new HashMap<DedupKey, EventInfo>();
int discarded = 0;
for (EventInfo ei : eis) {
BwEvent ev = ei.getEvent();
DedupKey evKey = new DedupKey(ev.getUid(), ev.getRecurrenceId());
EventInfo mapei = deduped.get(evKey);
if (mapei == null) {
deduped.put(evKey, ei);
continue;
}
// Decide which to discard
discarded++;
BwEvent mapev = mapei.getEvent();
if (mapev.getSequence() > ev.getSequence()) {
continue;
}
if (mapev.getSequence() < ev.getSequence()) {
// Replace
deduped.put(evKey, ei);
continue;
}
// sequence is equal -- try for dtstamp.
int cmp = mapev.getDtstamp().compareTo(ev.getDtstamp());
if (cmp >= 0) {
continue;
}
deduped.put(evKey, ei);
}
if (debug) {
trace("Outbox process discarded " + discarded);
}
boolean allOk = true;
for (EventInfo ei : deduped.values()) {
BwEvent ev = ei.getEvent();
Calendar cal = trans.toIcal(ei, ev.getScheduleMethod());
Collection<String> recipients = new ArrayList<String>();
for (String r : ev.getRecipients()) {
if (r.toLowerCase().startsWith("mailto:")) {
recipients.add(r.substring(7));
} else {
recipients.add(r);
}
}
String orig = ev.getOriginator();
if (orig.toLowerCase().startsWith("mailto:")) {
orig = orig.substring(7);
}
try {
if (getSvc().getMailer().mailEntity(cal, orig, recipients, ev.getSummary())) {
/* Save sent messages somewhere - keep in outbox?
ev.setScheduleState(BwEvent.scheduleStateExternalDone);
updateEvent(ev, ei.getOverrideProxies(), null);
*/
getSvc().getEventsHandler().delete(ei, false);
}
} catch (CalFacadeException cfe) {
// Should count the exceptions and discard after a number of retries.
error(cfe);
allOk = false;
}
}
if (allOk) {
return ProcessMessageResult.PROCESSED;
}
return ProcessMessageResult.FAILED;
}
Aggregations