Search in sources :

Example 1 with FBInstance

use of com.zimbra.cs.fb.FreeBusy.FBInstance 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 2 with FBInstance

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

the class Appointment method getBusyTimesString.

private static String getBusyTimesString(OperationContext octxt, Mailbox mbox, List<Conflict> list, TimeZone tz, Locale lc, boolean hasMoreConflicts) throws ServiceException {
    StringBuilder sb = new StringBuilder();
    int conflictCount = 0;
    for (Conflict avail : list) {
        if (!avail.isBusy())
            continue;
        // List conflicting appointments and their organizers.
        FreeBusy fb = avail.getFreeBusy();
        List<FBInstance> instances = new ArrayList<FBInstance>();
        for (Iterator<Interval> iter = fb.iterator(); iter.hasNext(); ) {
            Interval interval = iter.next();
            // busy intervals only
            if (Conflict.isBusy(interval.getStatus())) {
                // busy appointments only
                for (FBInstance fbinst : interval.getInstances()) {
                    if (Conflict.isBusy(fbinst.getFreeBusy()))
                        instances.add(fbinst);
                }
            }
        }
        for (FBInstance instance : instances) {
            Date startDate = new Date(instance.getStartTime());
            Date endDate = new Date(instance.getEndTime());
            String start = CalendarMailSender.formatDateTime(startDate, tz, lc);
            sb.append(" * ").append(start);
            String end;
            if (DateTimeUtil.sameDay(startDate, endDate, tz)) {
                end = CalendarMailSender.formatTime(endDate, tz, lc);
                sb.append(" - ").append(end);
            } else {
                end = CalendarMailSender.formatDateTime(endDate, tz, lc);
                sb.append("\r\n   - ").append(end);
            }
            int apptId = instance.getApptId();
            long recurIdDt = instance.getRecurIdDt();
            CalendarItem appt = mbox.getCalendarItemById(octxt, apptId);
            Invite inv = appt.getInviteForRecurId(recurIdDt);
            if (inv != null && inv.hasOrganizer()) {
                ZOrganizer organizer = inv.getOrganizer();
                String orgDispName;
                if (organizer.hasCn())
                    orgDispName = organizer.getCn() + " <" + organizer.getAddress() + ">";
                else
                    orgDispName = organizer.getAddress();
                sb.append(L10nUtil.getMessage(MsgKey.calendarResourceConflictScheduledBy, lc, orgDispName));
            }
            sb.append("\r\n");
            conflictCount++;
        }
    }
    if (hasMoreConflicts)
        sb.append(" * ...\r\n");
    return sb.toString();
}
Also used : FreeBusy(com.zimbra.cs.fb.FreeBusy) ArrayList(java.util.ArrayList) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) Date(java.util.Date) FBInstance(com.zimbra.cs.fb.FreeBusy.FBInstance) Invite(com.zimbra.cs.mailbox.calendar.Invite) Interval(com.zimbra.cs.fb.FreeBusy.Interval)

Aggregations

FBInstance (com.zimbra.cs.fb.FreeBusy.FBInstance)2 Interval (com.zimbra.cs.fb.FreeBusy.Interval)2 AccessManager (com.zimbra.cs.account.AccessManager)1 FreeBusy (com.zimbra.cs.fb.FreeBusy)1 IntervalList (com.zimbra.cs.fb.FreeBusy.IntervalList)1 Folder (com.zimbra.cs.mailbox.Folder)1 Invite (com.zimbra.cs.mailbox.calendar.Invite)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 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1