Search in sources :

Example 21 with Contact

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);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) ContactRefMember(com.zimbra.cs.mailbox.ContactGroup.ContactRefMember) GalRefMember(com.zimbra.cs.mailbox.ContactGroup.GalRefMember) Member(com.zimbra.cs.mailbox.ContactGroup.Member) InlineMember(com.zimbra.cs.mailbox.ContactGroup.InlineMember) Test(org.junit.Test)

Example 22 with Contact

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());
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ZContact(com.zimbra.client.ZContact) Contact(com.zimbra.cs.mailbox.Contact) ZContact(com.zimbra.client.ZContact) Test(org.junit.Test)

Example 23 with Contact

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);
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) List(java.util.List) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) TypedIdList(com.zimbra.cs.mailbox.util.TypedIdList) Contact(com.zimbra.cs.mailbox.Contact)

Example 24 with Contact

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;
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) DavException(com.zimbra.cs.dav.DavException) Contact(com.zimbra.cs.mailbox.Contact)

Example 25 with Contact

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();
}
Also used : Contact(com.zimbra.cs.mailbox.Contact)

Aggregations

Contact (com.zimbra.cs.mailbox.Contact)52 Mailbox (com.zimbra.cs.mailbox.Mailbox)32 ParsedContact (com.zimbra.cs.mime.ParsedContact)26 Test (org.junit.Test)23 OperationContext (com.zimbra.cs.mailbox.OperationContext)13 HashMap (java.util.HashMap)13 ZMailbox (com.zimbra.client.ZMailbox)10 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)10 MailItem (com.zimbra.cs.mailbox.MailItem)10 ItemId (com.zimbra.cs.service.util.ItemId)10 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)9 ZContact (com.zimbra.client.ZContact)8 Folder (com.zimbra.cs.mailbox.Folder)7 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)7 Term (org.apache.lucene.index.Term)7 Element (com.zimbra.common.soap.Element)6 Account (com.zimbra.cs.account.Account)6 Message (com.zimbra.cs.mailbox.Message)6 List (java.util.List)6