use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.
the class GalSearchControl method doLocalGalAccountSync.
private void doLocalGalAccountSync(GalSearchResultCallback callback, Mailbox mbox, OperationContext octxt, int changeId, Set<Integer> folderIds, String syncToken, int limit, String filterAttr, String filterValue, boolean getCount) throws ServiceException {
ZimbraLog.gal.info("Using limit %d for gal account sync", limit);
Pair<List<Integer>, TypedIdList> changed = mbox.getModifiedItems(octxt, changeId, 0, MailItem.Type.CONTACT, folderIds, -1, limit);
if (getCount) {
int remain = 0;
if (limit != 0) {
int total = mbox.getModifiedItemsCount(octxt, changeId, 0, MailItem.Type.CONTACT, folderIds);
remain = total > limit ? total - limit : 0;
ZimbraLog.gal.debug("totalCount: %d", total);
}
callback.setRemain(remain);
ZimbraLog.gal.debug("remain: %d, limit: %d", remain, limit);
}
int count = 0;
boolean hasMore = false;
for (int itemId : changed.getFirst()) {
try {
MailItem item = mbox.getItemById(octxt, itemId, MailItem.Type.CONTACT);
if (item instanceof Contact) {
Contact c = (Contact) item;
if (filterAttr != null && !filterValue.equals(c.get(filterAttr))) {
continue;
}
callback.handleContact(c);
count++;
if (count % 100 == 0) {
ZimbraLog.gal.trace("processing #%s", count);
}
changeId = item.getModifiedSequence();
if (count == limit) {
hasMore = true;
break;
}
}
} catch (MailServiceException mse) {
if (MailServiceException.NO_SUCH_ITEM.equals(mse.getId())) {
ZimbraLog.gal.warn("skipping item %d due to no such item; probably deleted during sync", itemId, mse);
} else {
throw mse;
}
}
}
GalSyncToken newToken = new GalSyncToken(syncToken, mbox.getAccountId(), changeId);
ZimbraLog.gal.debug("computing new sync token for %s:%s", mbox.getAccountId(), newToken);
callback.setNewToken(newToken);
callback.setHasMoreResult(hasMore);
}
use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.
the class ZimbraQueryTest method searchResultMode.
@Test
public void searchResultMode() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
Map<String, Object> fields = new HashMap<String, Object>();
fields.put(ContactConstants.A_email, "test1@zimbra.com");
Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
MailboxTestUtil.index(mbox);
SearchParams params = new SearchParams();
params.setQueryString("contact:test");
params.setSortBy(SortBy.NONE);
params.setTypes(EnumSet.of(MailItem.Type.CONTACT));
params.setFetchMode(SearchParams.Fetch.IDS);
ZimbraQuery query = new ZimbraQuery(new OperationContext(mbox), SoapProtocol.Soap12, mbox, params);
ZimbraQueryResults result = query.execute();
Assert.assertTrue(result.hasNext());
Assert.assertEquals(contact.getId(), result.getNext().getItemId());
IOUtil.closeQuietly(result);
}
use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.
the class AddressObject method constructContactGroupFromAppleXProps.
private static void constructContactGroupFromAppleXProps(DavContext ctxt, Account ownerAccount, VCard vcard, Contact existingContact, int folderId) {
ListMultimap<String, VCardParamsAndValue> xprops = Contact.decodeUnknownVCardProps(vcard.fields.get(ContactConstants.A_vCardXProps));
String kind = VCardParamsAndValue.getFirstValue(XABSKIND, xprops);
if (kind != null && kind.compareTo("group") == 0) {
ContactGroup contactGroup;
List<VCardParamsAndValue> xabsmembers = xprops.get(XABSMEMBER);
try {
if (existingContact == null) {
// create
contactGroup = ContactGroup.init();
} else {
// modify
contactGroup = ContactGroup.init(existingContact, true);
// remove all the contacts of type CONTACT_REF that belong to the collection same as the group
ArrayList<Member> membersToRemove = new ArrayList<Member>();
for (Member member : contactGroup.getMembers()) {
if (Member.Type.CONTACT_REF.equals(member.getType())) {
ItemId itemId = new ItemId(member.getValue(), existingContact.getAccount().getId());
if (itemId.belongsTo(existingContact.getAccount())) {
// make sure member belongs to the same collection as the group.
Contact c = getContactByUID(ctxt, itemId.toString(), existingContact.getAccount(), folderId);
if (c != null) {
membersToRemove.add(member);
}
}
}
}
for (Member member : membersToRemove) {
contactGroup.removeMember(member.getType(), member.getValue());
}
}
for (VCardParamsAndValue memberProp : xabsmembers) {
String member = memberProp.getValue();
if (member.startsWith("urn:uuid:")) {
member = member.substring(9);
}
Contact c = getContactByUID(ctxt, member, ownerAccount, folderId);
if (c != null) {
// add to the group as a CONTACT_REF
ItemId itemId = new ItemId(c);
contactGroup.addMember(Member.Type.CONTACT_REF, itemId.toString());
}
}
vcard.fields.put(ContactConstants.A_type, ContactConstants.TYPE_GROUP);
vcard.fields.put(ContactConstants.A_groupMember, contactGroup.encode());
// remove the Apple x-props and preserve the rest.
xprops.removeAll(XABSKIND);
xprops.removeAll(XABSMEMBER);
vcard.fields.put(ContactConstants.A_vCardXProps, Contact.encodeUnknownVCardProps(xprops));
} catch (ServiceException e) {
ZimbraLog.dav.debug("can't parse xprop %s", xabsmembers, e);
}
}
}
use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.
the class AddressObject method toVCard.
public String toVCard(DavContext ctxt) throws ServiceException, DavException {
Contact contact = (Contact) getMailItem(ctxt);
populateContactGroupAppleXProps(ctxt, contact);
return VCard.formatContact(contact, null, true, false).getFormatted();
}
use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.
the class UrlNamespace method getResourceFromMailItem.
/* Returns DavResource for the MailItem. */
public static DavResource getResourceFromMailItem(DavContext ctxt, MailItem item) throws DavException {
DavResource resource = null;
if (item == null) {
return resource;
}
MailItem.Type itemType = item.getType();
try {
MailItem.Type viewType;
switch(itemType) {
case MOUNTPOINT:
Mountpoint mp = (Mountpoint) item;
viewType = mp.getDefaultView();
// don't expose mounted calendars when using iCal style delegation model.
if (!ctxt.useIcalDelegation() && (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK)) {
resource = new RemoteCalendarCollection(ctxt, mp);
} else if (viewType == MailItem.Type.CONTACT) {
resource = new RemoteAddressbookCollection(ctxt, mp);
} else {
resource = new RemoteCollection(ctxt, mp);
}
break;
case FOLDER:
Folder f = (Folder) item;
viewType = f.getDefaultView();
if (f.getId() == Mailbox.ID_FOLDER_INBOX && DavResource.isSchedulingEnabled()) {
resource = new ScheduleInbox(ctxt, f);
} else if (f.getId() == Mailbox.ID_FOLDER_SENT && DavResource.isSchedulingEnabled()) {
resource = new ScheduleOutbox(ctxt, f);
} else if (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK) {
resource = getCalendarCollection(ctxt, f);
} else if (viewType == MailItem.Type.CONTACT) {
resource = new AddressbookCollection(ctxt, f);
} else {
resource = new Collection(ctxt, f);
}
break;
case DOCUMENT:
resource = new Notebook(ctxt, (Document) item);
break;
case APPOINTMENT:
case TASK:
resource = new CalendarObject.LocalCalendarObject(ctxt, (CalendarItem) item);
break;
case MESSAGE:
resource = getCalendarItemForMessage(ctxt, (Message) item);
break;
case CONTACT:
resource = new AddressObject(ctxt, (Contact) item);
break;
default:
break;
}
} catch (ServiceException e) {
ZimbraLog.dav.info("cannot create DavResource", e);
}
return resource;
}
Aggregations