use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.
the class TestContactGroup method returnMembersAsDlist.
@Test
public void returnMembersAsDlist() throws Exception {
Account account = Provisioning.getInstance().get(AccountBy.name, TestUtil.getAddress("user1"));
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
// TODO
OperationContext octxt = null;
Map<String, Object> fields = new HashMap<String, Object>();
fields.put(ContactConstants.A_fileAs, ContactConstants.FA_FIRST_LAST);
fields.put(ContactConstants.A_firstName, "test");
fields.put(ContactConstants.A_email, "test1@zimbra.com");
fields.put(ContactConstants.A_workEmail1, "test2@zimbra.com");
Contact contact = mbox.createContact(octxt, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
Account galMember = Provisioning.getInstance().get(AccountBy.name, TestUtil.getAddress("user2"));
LdapAccount ldapAccount = (LdapAccount) galMember;
String dn = ldapAccount.getDN();
String galEntryEmail = galMember.getName();
ContactGroup contactGroup = createContactGroup(new MemberData[] { new MemberData(Member.Type.CONTACT_REF, "" + contact.getId()), new MemberData(Member.Type.GAL_REF, dn), new MemberData(Member.Type.INLINE, "aaa@test.com"), new MemberData(Member.Type.INLINE, "zzz@test.com") });
contactGroup.derefAllMembers(mbox, octxt);
for (Member member : contactGroup.getDerefedMembers()) {
String memberKey = member.getDerefedKey();
System.out.println(memberKey);
}
String dlist = contactGroup.migrateToDlist(mbox, octxt);
// should be in member order
assertEquals("test1@zimbra.com, test2@zimbra.com, " + galEntryEmail + ", aaa@test.com, zzz@test.com", dlist);
}
use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.
the class TestZClient method testZMailboxGetContactsForFolder.
@Test
public void testZMailboxGetContactsForFolder() throws ServiceException {
Mailbox mbox = TestUtil.getMailbox(USER_NAME);
ZMailbox zmbox = TestUtil.getZMailbox(USER_NAME);
Contact contact = TestUtil.createContactInDefaultFolder(mbox, "testzclient2@example.com");
List<ZContact> allZContacts = zmbox.getContactsForFolder(ZFolder.ID_CONTACTS, null, /* ids */
(ContactSortBy) null, true, /* sync */
null);
assertNotNull("zmbox.getAllContacts result should not be null", allZContacts);
assertEquals("zmbox.getAllContacts result should have 1 entry", 1, allZContacts.size());
ZContact zcontact = allZContacts.get(0);
assertEquals("GetAllContacts:Expected=Contact IMAP UID Actual=ZContact IMAP UID", contact.getImapUid(), zcontact.getImapUid());
assertEquals("GetAllContacts:Expected=Contact ModifiedSequence Actual=ZContact ModifiedSequence", contact.getModifiedSequence(), zcontact.getModifiedSequence());
}
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) 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);
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 AddressObject method getContactByUID.
/**
* @param preferredBaseName If more than one item matches the UID, prefer one matching this name - can be null
*/
private static Contact getContactByUID(DavContext ctxt, String uid, Account account, int folderId, String preferredBaseName) throws ServiceException {
Contact item = null;
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
if (uid.endsWith(AddressObject.VCARD_EXTENSION)) {
uid = uid.substring(0, uid.length() - AddressObject.VCARD_EXTENSION.length());
// Unescape the name (It was encoded in DavContext intentionally)
uid = HttpUtil.urlUnescape(uid);
}
// first check whether the UID is an encoding of the Contact ID
int id = 0;
int index = uid.indexOf(':');
if (index > 0) {
String accountId = uid.substring(0, index);
try {
if (accountId.equals(account.getId()))
id = Integer.parseInt(uid.substring(index + 1));
} catch (NumberFormatException e) {
}
}
if (id > 0) {
item = mbox.getContactById(ctxt.getOperationContext(), Integer.parseInt(uid.substring(index + 1)));
} else {
StringBuilder query = new StringBuilder();
query.append("#").append(ContactConstants.A_vCardUID).append(":");
// escape the double quotes in uid and surround with double quotes
query.append("\"").append(uid.replace("\"", "\\\"")).append("\"");
query.append(" OR ").append("#").append(ContactConstants.A_vCardURL).append(":");
query.append("\"").append(uid.replace("\"", "\\\"")).append("\"");
ZimbraLog.dav.debug("query %s", query.toString());
try (ZimbraQueryResults zqr = mbox.index.search(ctxt.getOperationContext(), query.toString(), EnumSet.of(MailItem.Type.CONTACT), SortBy.NAME_ASC, 10)) {
// There could be multiple contacts with the same UID from different collections.
item = getMatchingHit(zqr, folderId, preferredBaseName);
} catch (Exception e) {
ZimbraLog.dav.error("can't search for: uid=%s", uid, e);
}
}
if ((item != null) && (folderId >= 0) && (item.getFolderId() != folderId))
item = null;
return item;
}
use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.
the class AddressObject method toVCard.
public String toVCard(DavContext ctxt, java.util.Collection<String> attrs) throws ServiceException, DavException {
if (attrs == null || attrs.isEmpty())
return toVCard(ctxt);
Contact contact = (Contact) getMailItem(ctxt);
populateContactGroupAppleXProps(ctxt, contact);
return VCard.formatContact(contact, attrs, true).getFormatted();
}
Aggregations