Search in sources :

Example 1 with IcalTranslator

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;
    }
}
Also used : CoreEventInfo(org.bedework.calcorei.CoreEventInfo) EventInfo(org.bedework.calfacade.svc.EventInfo) Icalendar(org.bedework.icalendar.Icalendar) StringReader(java.io.StringReader) BwEvent(org.bedework.calfacade.BwEvent) IcalTranslator(org.bedework.icalendar.IcalTranslator) BwAlarm(org.bedework.calfacade.BwAlarm) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 2 with IcalTranslator

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;
}
Also used : Response(org.bedework.calfacade.responses.Response) IcalTranslator(org.bedework.icalendar.IcalTranslator)

Example 3 with IcalTranslator

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;
}
Also used : CalSvcFactoryDefault(org.bedework.calsvci.CalSvcFactoryDefault) CalSvcIPars(org.bedework.calsvci.CalSvcIPars) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) IcalTranslator(org.bedework.icalendar.IcalTranslator)

Example 4 with IcalTranslator

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;
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) HashMap(java.util.HashMap) BwCalendar(org.bedework.calfacade.BwCalendar) Calendar(net.fortuna.ical4j.model.Calendar) ArrayList(java.util.ArrayList) BwEvent(org.bedework.calfacade.BwEvent) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) IcalTranslator(org.bedework.icalendar.IcalTranslator)

Aggregations

IcalTranslator (org.bedework.icalendar.IcalTranslator)4 BwEvent (org.bedework.calfacade.BwEvent)2 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 EventInfo (org.bedework.calfacade.svc.EventInfo)2 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Calendar (net.fortuna.ical4j.model.Calendar)1 CoreEventInfo (org.bedework.calcorei.CoreEventInfo)1 BwAlarm (org.bedework.calfacade.BwAlarm)1 BwCalendar (org.bedework.calfacade.BwCalendar)1 Response (org.bedework.calfacade.responses.Response)1 CalSvcFactoryDefault (org.bedework.calsvci.CalSvcFactoryDefault)1 CalSvcIPars (org.bedework.calsvci.CalSvcIPars)1 Icalendar (org.bedework.icalendar.Icalendar)1 WebdavException (org.bedework.webdav.servlet.shared.WebdavException)1