use of com.zimbra.cs.fb.FreeBusy in project zm-mailbox by Zimbra.
the class ToXML method encodeFreeBusy.
public static Element encodeFreeBusy(Element parent, FreeBusy fb) {
Element resp = parent.addElement(MailConstants.E_FREEBUSY_USER);
resp.addAttribute(MailConstants.A_ID, fb.getName());
for (Iterator<FreeBusy.Interval> iter = fb.iterator(); iter.hasNext(); ) {
FreeBusy.Interval cur = iter.next();
String status = cur.getStatus();
Element elt;
if (status.equals(IcalXmlStrMap.FBTYPE_FREE)) {
elt = resp.addElement(MailConstants.E_FREEBUSY_FREE);
} else if (status.equals(IcalXmlStrMap.FBTYPE_BUSY)) {
elt = resp.addElement(MailConstants.E_FREEBUSY_BUSY);
} else if (status.equals(IcalXmlStrMap.FBTYPE_BUSY_TENTATIVE)) {
elt = resp.addElement(MailConstants.E_FREEBUSY_BUSY_TENTATIVE);
} else if (status.equals(IcalXmlStrMap.FBTYPE_BUSY_UNAVAILABLE)) {
elt = resp.addElement(MailConstants.E_FREEBUSY_BUSY_UNAVAILABLE);
} else if (status.equals(IcalXmlStrMap.FBTYPE_NODATA)) {
elt = resp.addElement(MailConstants.E_FREEBUSY_NODATA);
} else {
assert (false);
elt = null;
}
if (elt != null) {
elt.addAttribute(MailConstants.A_CAL_START_TIME, cur.getStart());
elt.addAttribute(MailConstants.A_CAL_END_TIME, cur.getEnd());
}
}
return resp;
}
use of com.zimbra.cs.fb.FreeBusy in project zm-mailbox by Zimbra.
the class CheckRecurConflicts method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Account authAcct = getAuthenticatedAccount(zsc);
Element response = getResponseElement(zsc);
long rangeStart = request.getAttributeLong(MailConstants.A_CAL_START_TIME, 0);
if (rangeStart == 0)
rangeStart = System.currentTimeMillis();
long rangeEnd = request.getAttributeLong(MailConstants.A_CAL_END_TIME, 0);
if (rangeEnd == 0)
rangeEnd = Long.MAX_VALUE;
boolean allInstances = request.getAttributeBool(MailConstants.A_CAL_ALL, false);
String exApptUid = request.getAttribute(MailConstants.A_APPT_FREEBUSY_EXCLUDE_UID, null);
// Parse and expand the recurrence.
TimeZoneMap tzmap = new TimeZoneMap(Util.getAccountTimeZone(authAcct));
ParsedRecurrence parsed = parseRecur(request, tzmap);
List<Instance> instances = getInstances(parsed, rangeStart, rangeEnd);
if (instances == null || instances.isEmpty())
return response;
// Find the range covered by the instances.
long rangeStartActual = rangeEnd, rangeEndActual = rangeStart;
for (Instance inst : instances) {
if (inst.hasStart() && inst.hasEnd()) {
rangeStartActual = Math.min(rangeStartActual, inst.getStart());
rangeEndActual = Math.max(rangeEndActual, inst.getEnd());
}
}
if (rangeStartActual >= rangeEndActual)
return response;
// Run free/busy search on the users.
FreeBusyQuery fbQuery = new FreeBusyQuery((HttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST), zsc, authAcct, rangeStartActual, rangeEndActual, exApptUid);
for (Iterator<Element> usrIter = request.elementIterator(MailConstants.E_FREEBUSY_USER); usrIter.hasNext(); ) {
Element usrElem = usrIter.next();
int folderId = (int) usrElem.getAttributeLong(MailConstants.A_FOLDER, FreeBusyQuery.CALENDAR_FOLDER_ALL);
if (folderId == Mailbox.ID_FOLDER_USER_ROOT || folderId == 0)
folderId = FreeBusyQuery.CALENDAR_FOLDER_ALL;
String id = usrElem.getAttribute(MailConstants.A_ID, null);
if (id != null)
fbQuery.addAccountId(id, folderId);
String name = usrElem.getAttribute(MailConstants.A_NAME, null);
if (name != null)
fbQuery.addEmailAddress(name, folderId);
}
Collection<FreeBusy> fbResults = fbQuery.getResults();
List<UserConflicts> conflicts = new ArrayList<UserConflicts>();
for (FreeBusy fb : fbResults) {
UserConflicts ucon = getConflicts(fb, instances);
conflicts.add(ucon);
}
// Find conflicts for each instance.
for (Instance inst : instances) {
Element instElem = addInstance(response, inst);
int numConflicts = 0;
for (UserConflicts ucon : conflicts) {
String fbStatus = ucon.get(inst);
if (fbStatus != null) {
++numConflicts;
String username = ucon.getUsername();
Element usrElem = instElem.addElement(MailConstants.E_FREEBUSY_USER);
usrElem.addAttribute(MailConstants.A_NAME, username);
usrElem.addAttribute(MailConstants.A_APPT_FREEBUSY, fbStatus);
}
}
if (numConflicts == 0 && !allInstances)
instElem.detach();
}
return response;
}
use of com.zimbra.cs.fb.FreeBusy 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();
}
use of com.zimbra.cs.fb.FreeBusy in project zm-mailbox by Zimbra.
the class IfbFormatter method formatCallback.
@Override
public void formatCallback(UserServletContext context) throws IOException, ServiceException, UserServletException {
context.resp.setCharacterEncoding("UTF-8");
context.resp.setContentType(MimeConstants.CT_TEXT_CALENDAR);
long rangeStart = context.getStartTime();
long rangeEnd = context.getEndTime();
if (rangeEnd < rangeStart)
throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "End time must be after Start time");
long days = (rangeEnd - rangeStart) / Constants.MILLIS_PER_DAY;
long maxDays = LC.calendar_freebusy_max_days.longValueWithinRange(0, 36600);
if (days > maxDays)
throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "Requested range is too large (Maximum " + maxDays + " days)");
String acctName = null;
FreeBusy fb = null;
if (context.targetMailbox != null) {
String exuid = context.params.get(UserServlet.QP_EXUID);
Appointment exAppt = null;
if (exuid != null) {
CalendarItem ci = context.targetMailbox.getCalendarItemByUid(context.opContext, exuid);
if (ci instanceof Appointment)
exAppt = (Appointment) ci;
}
acctName = context.targetMailbox.getAccount().getName();
fb = context.targetMailbox.getFreeBusy(context.opContext, acctName, rangeStart, rangeEnd, context.getFreeBusyCalendar(), exAppt);
} else {
// Unknown mailbox. Fake an always-free response, to avoid harvest attacks.
acctName = fixupAccountName(context.accountPath);
fb = FreeBusy.emptyFreeBusy(acctName, rangeStart, rangeEnd);
}
String fbFormat = context.params.get(UserServlet.QP_FBFORMAT);
String fbMsg;
if ("event".equalsIgnoreCase(fbFormat)) {
fbMsg = fb.toVCalendarAsVEvents();
} else {
String url = context.req.getRequestURL() + "?" + context.req.getQueryString();
fbMsg = fb.toVCalendar(FreeBusy.Method.PUBLISH, acctName, null, url);
}
context.resp.getOutputStream().write(fbMsg.getBytes("UTF-8"));
}
Aggregations