use of com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache in project zm-mailbox by Zimbra.
the class Search method runSimpleAppointmentQuery.
private static void runSimpleAppointmentQuery(Element parent, SearchParams params, OperationContext octxt, ZimbraSoapContext zsc, Account authAcct, Mailbox mbox, List<String> folderIdStrs) throws ServiceException {
Set<MailItem.Type> types = params.getTypes();
MailItem.Type type = types.size() == 1 ? Iterables.getOnlyElement(types) : MailItem.Type.APPOINTMENT;
if (params.getSortBy() != null) {
parent.addAttribute(MailConstants.A_SORTBY, params.getSortBy().toString());
}
parent.addAttribute(MailConstants.A_QUERY_OFFSET, params.getOffset());
parent.addAttribute(MailConstants.A_QUERY_MORE, false);
List<ItemId> folderIids = new ArrayList<ItemId>(folderIdStrs.size());
for (String folderIdStr : folderIdStrs) {
folderIids.add(new ItemId(folderIdStr, zsc));
}
Provisioning prov = Provisioning.getInstance();
MailboxManager mboxMgr = MailboxManager.getInstance();
Server localServer = prov.getLocalServer();
Map<Server, Map<String, List<Integer>>> /* folder ids */
groupedByServer = groupByServer(ItemId.groupFoldersByAccount(octxt, mbox, folderIids));
// Look up in calendar cache first.
if (LC.calendar_cache_enabled.booleanValue()) {
CalSummaryCache calCache = CalendarCacheManager.getInstance().getSummaryCache();
long rangeStart = params.getCalItemExpandStart();
long rangeEnd = params.getCalItemExpandEnd();
for (Iterator<Map.Entry<Server, Map<String, List<Integer>>>> serverIter = groupedByServer.entrySet().iterator(); serverIter.hasNext(); ) {
Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry = serverIter.next();
Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
// for each account
for (Iterator<Map.Entry<String, List<Integer>>> acctIter = accountFolders.entrySet().iterator(); acctIter.hasNext(); ) {
Map.Entry<String, List<Integer>> acctEntry = acctIter.next();
String acctId = acctEntry.getKey();
List<Integer> folderIds = acctEntry.getValue();
// Setup ItemIdFormatter appropriate for this folder which might not be in the authed account
// but also take note of presense of <noqualify/> in SOAP context
ItemIdFormatter ifmt = new ItemIdFormatter(authAcct.getId(), acctId, zsc.wantsUnqualifiedIds());
// for each folder
for (Iterator<Integer> iterFolderId = folderIds.iterator(); iterFolderId.hasNext(); ) {
int folderId = iterFolderId.next();
try {
CalendarDataResult result = calCache.getCalendarSummary(octxt, acctId, folderId, type, rangeStart, rangeEnd, true);
if (result != null) {
// Found data in cache.
iterFolderId.remove();
addCalendarDataToResponse(parent, zsc, ifmt, result.data, result.allowPrivateAccess);
}
} catch (ServiceException e) {
String ecode = e.getCode();
if (ecode.equals(ServiceException.PERM_DENIED)) {
// share permission was revoked
ZimbraLog.calendar.warn("Ignoring permission error during calendar search of folder %s", ifmt.formatItemId(folderId), e);
} else if (ecode.equals(MailServiceException.NO_SUCH_FOLDER)) {
// shared calendar folder was deleted by the owner
ZimbraLog.calendar.warn("Ignoring deleted calendar folder %s", ifmt.formatItemId(folderId));
} else {
throw e;
}
iterFolderId.remove();
}
}
if (folderIds.isEmpty())
acctIter.remove();
}
if (accountFolders.isEmpty())
serverIter.remove();
}
}
// For any remaining calendars, we have to get the data the hard way.
for (Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry : groupedByServer.entrySet()) {
Server server = serverMapEntry.getKey();
Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
if (server.equals(localServer)) {
// local server
for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
String acctId = entry.getKey();
List<Integer> folderIds = entry.getValue();
if (folderIds.isEmpty())
continue;
Account targetAcct = prov.get(AccountBy.id, acctId);
if (targetAcct == null) {
ZimbraLog.calendar.warn("Skipping unknown account " + acctId + " during calendar search");
continue;
}
Mailbox targetMbox = mboxMgr.getMailboxByAccount(targetAcct);
searchLocalAccountCalendars(parent, params, octxt, zsc, authAcct, targetMbox, folderIds, type);
}
} else {
// remote server
searchRemoteAccountCalendars(parent, params, zsc, authAcct, accountFolders);
}
}
}
use of com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache in project zm-mailbox by Zimbra.
the class GetMiniCal method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Mailbox mbox = getRequestedMailbox(zsc);
Account authAcct = getAuthenticatedAccount(zsc);
OperationContext octxt = getOperationContext(zsc, context);
Element response = getResponseElement(zsc);
long rangeStart = request.getAttributeLong(MailConstants.A_CAL_START_TIME);
long rangeEnd = request.getAttributeLong(MailConstants.A_CAL_END_TIME);
List<ItemId> folderIids = new ArrayList<ItemId>();
for (Iterator<Element> foldersIter = request.elementIterator(MailConstants.E_FOLDER); foldersIter.hasNext(); ) {
Element fElem = foldersIter.next();
ItemId iidFolder = new ItemId(fElem.getAttribute(MailConstants.A_ID), zsc);
folderIids.add(iidFolder);
}
ICalTimeZone tz = parseTimeZone(request);
if (tz == null)
// requestor's time zone, not mailbox owner's
tz = Util.getAccountTimeZone(authAcct);
TreeSet<String> busyDates = new TreeSet<String>();
Provisioning prov = Provisioning.getInstance();
MailboxManager mboxMgr = MailboxManager.getInstance();
Server localServer = prov.getLocalServer();
ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
Map<ItemId, Resolved> resolved = resolveMountpoints(octxt, mbox, folderIids);
Map<ItemId, ItemId> /* requested iid */
reverseMap = new HashMap<ItemId, ItemId>();
for (Map.Entry<ItemId, Resolved> entry : resolved.entrySet()) {
ItemId requestedIid = entry.getKey();
Resolved res = entry.getValue();
if (res.error == null) {
reverseMap.put(res.iid, requestedIid);
} else {
addError(response, ifmt.formatItemId(requestedIid), res.error.getCode(), res.error.getMessage());
}
}
Map<Server, Map<String, List<Integer>>> /* folder ids */
groupedByServer = Search.groupByServer(groupFoldersByAccount(resolved));
// Look up in calendar cache first.
if (LC.calendar_cache_enabled.booleanValue()) {
CalSummaryCache calCache = CalendarCacheManager.getInstance().getSummaryCache();
Calendar cal = new GregorianCalendar(tz);
for (Iterator<Map.Entry<Server, Map<String, List<Integer>>>> serverIter = groupedByServer.entrySet().iterator(); serverIter.hasNext(); ) {
Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry = serverIter.next();
Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
// for each account
for (Iterator<Map.Entry<String, List<Integer>>> acctIter = accountFolders.entrySet().iterator(); acctIter.hasNext(); ) {
Map.Entry<String, List<Integer>> acctEntry = acctIter.next();
String acctId = acctEntry.getKey();
List<Integer> folderIds = acctEntry.getValue();
// for each folder
for (Iterator<Integer> iterFolderId = folderIds.iterator(); iterFolderId.hasNext(); ) {
int folderId = iterFolderId.next();
try {
CalendarDataResult result = calCache.getCalendarSummary(octxt, acctId, folderId, MailItem.Type.APPOINTMENT, rangeStart, rangeEnd, true);
if (result != null) {
// Found data in cache.
iterFolderId.remove();
addBusyDates(cal, result.data, rangeStart, rangeEnd, busyDates);
}
} catch (ServiceException e) {
iterFolderId.remove();
ItemId iid = new ItemId(acctId, folderId);
// Error must mention folder id requested by client.
ItemId reqIid = reverseMap.get(iid);
if (reqIid != null) {
ZimbraLog.calendar.warn("Error accessing calendar folder " + ifmt.formatItemId(reqIid), e);
addError(response, ifmt.formatItemId(reqIid), e.getCode(), e.getMessage());
} else {
ZimbraLog.calendar.warn("Error accessing calendar folder; resolved id=" + ifmt.formatItemId(iid) + " (missing reverse mapping)", e);
addError(response, ifmt.formatItemId(iid), e.getCode(), e.getMessage());
}
}
}
if (folderIds.isEmpty())
acctIter.remove();
}
if (accountFolders.isEmpty())
serverIter.remove();
}
}
// For any remaining calendars, we have to get the data the hard way.
for (Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry : groupedByServer.entrySet()) {
Server server = serverMapEntry.getKey();
Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
if (server.equals(localServer)) {
// local server
for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
String acctId = entry.getKey();
List<Integer> folderIds = entry.getValue();
Account targetAcct = prov.get(AccountBy.id, acctId);
if (targetAcct == null) {
ZimbraLog.calendar.warn("Skipping unknown account " + acctId + " during minical search");
continue;
}
Mailbox targetMbox = mboxMgr.getMailboxByAccount(targetAcct);
for (int folderId : folderIds) {
try {
doLocalFolder(octxt, tz, targetMbox, folderId, rangeStart, rangeEnd, busyDates);
} catch (ServiceException e) {
ItemId iid = new ItemId(acctId, folderId);
// Error must mention folder id requested by client.
ItemId reqIid = reverseMap.get(iid);
if (reqIid != null) {
ZimbraLog.calendar.warn("Error accessing calendar folder " + ifmt.formatItemId(reqIid), e);
addError(response, ifmt.formatItemId(reqIid), e.getCode(), e.getMessage());
} else {
ZimbraLog.calendar.warn("Error accessing calendar folder; resolved id=" + ifmt.formatItemId(iid) + " (missing reverse mapping)", e);
addError(response, ifmt.formatItemId(iid), e.getCode(), e.getMessage());
}
}
}
}
} else {
// remote server
// mail service soap requests want to see a target account
String nominalTargetAcctId = null;
List<String> folderList = new ArrayList<String>();
for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
String acctId = entry.getKey();
if (nominalTargetAcctId == null)
nominalTargetAcctId = acctId;
ItemIdFormatter ifmtRemote = new ItemIdFormatter(authAcct.getId(), acctId, false);
List<Integer> folderIds = entry.getValue();
for (int folderId : folderIds) {
folderList.add(ifmtRemote.formatItemId(folderId));
}
}
doRemoteFolders(zsc, nominalTargetAcctId, folderList, rangeStart, rangeEnd, busyDates, response, reverseMap, ifmt);
}
}
for (String datestamp : busyDates) {
Element dateElem = response.addElement(MailConstants.E_CAL_MINICAL_DATE);
dateElem.setText(datestamp);
}
return response;
}
Aggregations