Search in sources :

Example 1 with Interval

use of com.zimbra.cs.fb.FreeBusy.Interval in project zm-mailbox by Zimbra.

the class ExchangeEWSMessage method encodeIntervals.

private void encodeIntervals(Iterable<Interval> fb, long startMonth, long endMonth, String type, ArrayList<String> months, ArrayList<byte[]> events, IntervalList consolidated) {
    HashMap<Long, LinkedList<Byte>> fbMap = new HashMap<Long, LinkedList<Byte>>();
    for (long i = startMonth; i <= endMonth; i++) fbMap.put(i, new LinkedList<Byte>());
    for (FreeBusy.Interval interval : fb) {
        String status = interval.getStatus();
        if (status.equals(type)) {
            LinkedList<Byte> buf = fbMap.get(millisToMonths(interval.getStart()));
            encodeFb(interval.getStart(), interval.getEnd(), buf);
            if (consolidated != null)
                consolidated.addInterval(new Interval(interval.getStart(), interval.getEnd(), IcalXmlStrMap.FBTYPE_BUSY));
        }
    }
    for (long m = startMonth; m <= endMonth; m++) {
        LinkedList<Byte> encodedList = fbMap.get(m);
        byte[] raw = {};
        if (encodedList.size() > 0) {
            try {
                raw = new byte[encodedList.size()];
                for (int i = 0; i < encodedList.size(); i++) raw[i] = encodedList.get(i).byteValue();
            } catch (Exception e) {
                ZimbraLog.fb.warn("error converting millis to minutes for month " + m, e);
                continue;
            }
        }
        if (0 != raw.length) {
            months.add(Long.toString(m));
            events.add(raw);
        }
    }
}
Also used : Interval(com.zimbra.cs.fb.FreeBusy.Interval) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Interval(com.zimbra.cs.fb.FreeBusy.Interval)

Example 2 with Interval

use of com.zimbra.cs.fb.FreeBusy.Interval in project zm-mailbox by Zimbra.

the class ExchangeMessage method encodeIntervals.

private void encodeIntervals(Iterable<Interval> fb, long startMonth, long endMonth, String type, Element months, Element events, IntervalList consolidated) {
    HashMap<Long, LinkedList<Byte>> fbMap = new HashMap<Long, LinkedList<Byte>>();
    for (long i = startMonth; i <= endMonth; i++) fbMap.put(i, new LinkedList<Byte>());
    for (FreeBusy.Interval interval : fb) {
        String status = interval.getStatus();
        if (status.equals(type)) {
            long start = interval.getStart();
            long end = interval.getEnd();
            long fbMonth = millisToMonths(start);
            LinkedList<Byte> buf = fbMap.get(fbMonth);
            encodeFb(start, end, buf);
            if (consolidated != null)
                consolidated.addInterval(new Interval(start, end, IcalXmlStrMap.FBTYPE_BUSY));
        }
    }
    for (long m = startMonth; m <= endMonth; m++) {
        String buf = "";
        LinkedList<Byte> encodedList = fbMap.get(m);
        if (encodedList.size() > 0) {
            try {
                byte[] raw = new byte[encodedList.size()];
                for (int i = 0; i < encodedList.size(); i++) raw[i] = encodedList.get(i).byteValue();
                byte[] encoded = Base64.encodeBase64(raw);
                buf = new String(encoded, "UTF-8");
            } catch (IOException e) {
                ZimbraLog.fb.warn("error converting millis to minutes for month " + m, e);
                continue;
            }
        }
        addElement(months, EL_V, Long.toString(m));
        addElement(events, EL_V, buf);
    }
}
Also used : Interval(com.zimbra.cs.fb.FreeBusy.Interval) HashMap(java.util.HashMap) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Interval(com.zimbra.cs.fb.FreeBusy.Interval)

Example 3 with Interval

use of com.zimbra.cs.fb.FreeBusy.Interval in project zm-mailbox by Zimbra.

the class LocalFreeBusyProvider method main.

public static void main(String[] args) {
    IntervalList l = new IntervalList(0, 100);
    Interval toAdd;
    System.out.println("List: " + l.toString());
    toAdd = new Interval(50, 60, IcalXmlStrMap.FBTYPE_BUSY);
    l.addInterval(toAdd);
    System.out.println("Added: " + toAdd + l.toString());
    toAdd = new Interval(10, 20, IcalXmlStrMap.FBTYPE_BUSY_TENTATIVE);
    l.addInterval(toAdd);
    System.out.println("Added: " + toAdd + l.toString());
    toAdd = new Interval(20, 30, IcalXmlStrMap.FBTYPE_BUSY_UNAVAILABLE);
    l.addInterval(toAdd);
    System.out.println("Added: " + toAdd + l.toString());
    toAdd = new Interval(15, 35, IcalXmlStrMap.FBTYPE_BUSY);
    l.addInterval(toAdd);
    System.out.println("Added: " + toAdd + l.toString());
    try {
        Mailbox mbox = MailboxManager.getInstance().getMailboxById(1);
        FreeBusy fb = getFreeBusyList(mbox.getAccount(), false, mbox, mbox.getAccount().getName(), 0, Long.MAX_VALUE, FreeBusyQuery.CALENDAR_FOLDER_ALL, null);
        System.out.println(fb.toString());
    } catch (ServiceException e) {
        System.out.println("EXCEPTION: " + e);
        e.printStackTrace();
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) IntervalList(com.zimbra.cs.fb.FreeBusy.IntervalList) Interval(com.zimbra.cs.fb.FreeBusy.Interval)

Example 4 with Interval

use of com.zimbra.cs.fb.FreeBusy.Interval in project zm-mailbox by Zimbra.

the class LocalFreeBusyProvider method getFreeBusyList.

/**
     *
     * @param mbox
     * @param start
     * @param end
     * @param folder folder to run free/busy search on; FreeBusyQuery.CALENDAR_FOLDER_ALL (-1) for all folders
     * @param exAppt appointment to exclude; calculate free/busy assuming
     *               the specified appointment wasn't there
     * @return
     * @throws ServiceException
     */
public static FreeBusy getFreeBusyList(Account authAcct, boolean asAdmin, Mailbox mbox, String name, long start, long end, int folder, Appointment exAppt) throws ServiceException {
    AccessManager accessMgr = AccessManager.getInstance();
    boolean accountAceAllowed = accessMgr.canDo(authAcct, mbox.getAccount(), User.R_viewFreeBusy, asAdmin);
    int numAllowedFolders = 0;
    int exApptId = exAppt == null ? -1 : exAppt.getId();
    IntervalList intervals = new IntervalList(start, end);
    List<CalendarDataResult> calDataResultList;
    if (folder == FreeBusyQuery.CALENDAR_FOLDER_ALL) {
        calDataResultList = mbox.getAllCalendarsSummaryForRange(null, MailItem.Type.APPOINTMENT, start, end);
    } else {
        calDataResultList = new ArrayList<CalendarDataResult>(1);
        calDataResultList.add(mbox.getCalendarSummaryForRange(null, folder, MailItem.Type.APPOINTMENT, start, end));
    }
    for (CalendarDataResult result : calDataResultList) {
        int folderId = result.data.getFolderId();
        Folder f = mbox.getFolderById(null, folderId);
        if ((f.getFlagBitmask() & Flag.BITMASK_EXCLUDE_FREEBUSY) != 0) {
            ZimbraLog.fb.debug("Calendar '%s' id=%s ignored - has EXCLUDE_FREEBUSY flag set", f.getName(), folderId);
            continue;
        }
        // Free/busy must be allowed by folder or at account-level.
        boolean folderFBAllowed = CalendarItem.allowFreeBusyAccess(f, authAcct, asAdmin);
        if (folderFBAllowed)
            ++numAllowedFolders;
        if (!folderFBAllowed && !accountAceAllowed) {
            ZimbraLog.fb.debug("Calendar '%s' id=%s ignored - folderFBAllowed=%s accountAceAllowed=%s", f.getName(), folderId, folderFBAllowed, accountAceAllowed);
            continue;
        }
        for (Iterator<CalendarItemData> iter = result.data.calendarItemIterator(); iter.hasNext(); ) {
            CalendarItemData appt = iter.next();
            int apptId = appt.getCalItemId();
            if (apptId == exApptId)
                continue;
            FullInstanceData defaultInstance = appt.getDefaultData();
            if (defaultInstance == null)
                continue;
            boolean isTransparent = false;
            String transp = defaultInstance.getTransparency();
            isTransparent = IcalXmlStrMap.TRANSP_TRANSPARENT.equals(transp);
            long defaultDuration = 0;
            if (defaultInstance.getDuration() != null)
                defaultDuration = defaultInstance.getDuration().longValue();
            String defaultFreeBusy = defaultInstance.getFreeBusyActual();
            for (Iterator<InstanceData> instIter = appt.instanceIterator(); instIter.hasNext(); ) {
                InstanceData instance = instIter.next();
                long instStart = instance.getDtStart() != null ? instance.getDtStart().longValue() : 0;
                // Skip instances that are outside the time range but were returned due to alarm being in range.
                if (instStart >= end)
                    continue;
                long dur = defaultDuration;
                if (instance.getDuration() != null)
                    dur = instance.getDuration().longValue();
                if (// Only consider instances with non-zero, positive duration.
                dur <= 0)
                    continue;
                long instEnd = instStart + dur;
                long recurIdDt = 0;
                // Skip if instance is TRANSPARENT to free/busy searches.
                if (instance instanceof FullInstanceData) {
                    FullInstanceData fullInst = (FullInstanceData) instance;
                    String transpInst = fullInst.getTransparency();
                    recurIdDt = fullInst.getRecurrenceId();
                    if (IcalXmlStrMap.TRANSP_TRANSPARENT.equals(transpInst))
                        continue;
                } else if (isTransparent) {
                    continue;
                }
                String freeBusy = instance.getFreeBusyActual();
                if (freeBusy == null)
                    freeBusy = defaultFreeBusy;
                if (!IcalXmlStrMap.FBTYPE_FREE.equals(freeBusy)) {
                    FBInstance fbInst = new FBInstance(freeBusy, instStart, instEnd, apptId, recurIdDt);
                    Interval ival = new Interval(instStart, instEnd, freeBusy, fbInst);
                    intervals.addInterval(ival);
                }
            }
        }
    }
    if (!accountAceAllowed && numAllowedFolders == 0 && !LC.freebusy_disable_nodata_status.booleanValue()) {
        Interval nodata = new Interval(start, end, IcalXmlStrMap.FBTYPE_NODATA);
        intervals.addInterval(nodata);
    }
    return new FreeBusy(name, intervals, start, end);
}
Also used : AccessManager(com.zimbra.cs.account.AccessManager) FullInstanceData(com.zimbra.cs.mailbox.calendar.cache.FullInstanceData) CalendarDataResult(com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache.CalendarDataResult) Folder(com.zimbra.cs.mailbox.Folder) InstanceData(com.zimbra.cs.mailbox.calendar.cache.InstanceData) FullInstanceData(com.zimbra.cs.mailbox.calendar.cache.FullInstanceData) IntervalList(com.zimbra.cs.fb.FreeBusy.IntervalList) CalendarItemData(com.zimbra.cs.mailbox.calendar.cache.CalendarItemData) FBInstance(com.zimbra.cs.fb.FreeBusy.FBInstance) Interval(com.zimbra.cs.fb.FreeBusy.Interval)

Example 5 with Interval

use of com.zimbra.cs.fb.FreeBusy.Interval in project zm-mailbox by Zimbra.

the class WorkingHours method getWorkingHours.

public static FreeBusy getWorkingHours(Account authAcct, boolean asAdmin, Account account, String name, long start, long end) throws ServiceException {
    // If free/busy viewing is blocked, so is viewing working hours.
    AccessManager accessMgr = AccessManager.getInstance();
    boolean accountAceAllowed = accessMgr.canDo(authAcct, account, User.R_viewFreeBusy, asAdmin);
    if (!accountAceAllowed)
        return FreeBusy.nodataFreeBusy(account.getName(), start, end);
    // Get the working hours preference and parse it.
    String workingHoursPref = account.getPrefCalendarWorkingHours();
    HoursByDay workingHoursByDay = new HoursByDay(workingHoursPref);
    // Build a recurrence rule for each day of the week and expand over the time range.
    IntervalList intervals = new IntervalList(start, end);
    ICalTimeZone tz = Util.getAccountTimeZone(account);
    TimeZoneMap tzmap = new TimeZoneMap(tz);
    StartSpec startSpec = new StartSpec(start, tz);
    for (int day = 1; day <= 7; ++day) {
        TimeRange timeRange = workingHoursByDay.getHoursForDay(day);
        if (timeRange.enabled) {
            IRecurrence rrule = getRecurrenceForDay(day, startSpec, timeRange, tz, tzmap);
            List<Instance> instances = rrule.expandInstances(0, start, end);
            for (Instance inst : instances) {
                Interval ival = new Interval(inst.getStart(), inst.getEnd(), IcalXmlStrMap.FBTYPE_BUSY_UNAVAILABLE);
                intervals.addInterval(ival);
            }
        }
    }
    // hours are shown as out-of-office.
    for (Iterator<Interval> iter = intervals.iterator(); iter.hasNext(); ) {
        Interval interval = iter.next();
        String status = interval.getStatus();
        interval.setStatus(invertStatus(status));
    }
    return new FreeBusy(name, intervals, start, end);
}
Also used : AccessManager(com.zimbra.cs.account.AccessManager) Instance(com.zimbra.cs.mailbox.CalendarItem.Instance) IRecurrence(com.zimbra.cs.mailbox.calendar.Recurrence.IRecurrence) IntervalList(com.zimbra.cs.fb.FreeBusy.IntervalList) TimeZoneMap(com.zimbra.common.calendar.TimeZoneMap) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) Interval(com.zimbra.cs.fb.FreeBusy.Interval)

Aggregations

Interval (com.zimbra.cs.fb.FreeBusy.Interval)7 IntervalList (com.zimbra.cs.fb.FreeBusy.IntervalList)3 AccessManager (com.zimbra.cs.account.AccessManager)2 FBInstance (com.zimbra.cs.fb.FreeBusy.FBInstance)2 Instance (com.zimbra.cs.mailbox.CalendarItem.Instance)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)1 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)1 ServiceException (com.zimbra.common.service.ServiceException)1 FreeBusy (com.zimbra.cs.fb.FreeBusy)1 Folder (com.zimbra.cs.mailbox.Folder)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 Invite (com.zimbra.cs.mailbox.calendar.Invite)1 IRecurrence (com.zimbra.cs.mailbox.calendar.Recurrence.IRecurrence)1 ZOrganizer (com.zimbra.cs.mailbox.calendar.ZOrganizer)1 CalendarDataResult (com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache.CalendarDataResult)1 CalendarItemData (com.zimbra.cs.mailbox.calendar.cache.CalendarItemData)1 FullInstanceData (com.zimbra.cs.mailbox.calendar.cache.FullInstanceData)1 InstanceData (com.zimbra.cs.mailbox.calendar.cache.InstanceData)1