Search in sources :

Example 6 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, 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);
}
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 7 with Contact

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);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 8 with Contact

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);
        }
    }
}
Also used : VCardParamsAndValue(com.zimbra.cs.mailbox.VCardParamsAndValue) ServiceException(com.zimbra.common.service.ServiceException) ArrayList(java.util.ArrayList) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) Member(com.zimbra.cs.mailbox.ContactGroup.Member) ItemId(com.zimbra.cs.service.util.ItemId) Contact(com.zimbra.cs.mailbox.Contact)

Example 9 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) throws ServiceException, DavException {
    Contact contact = (Contact) getMailItem(ctxt);
    populateContactGroupAppleXProps(ctxt, contact);
    return VCard.formatContact(contact, null, true, false).getFormatted();
}
Also used : Contact(com.zimbra.cs.mailbox.Contact)

Example 10 with Contact

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;
}
Also used : Message(com.zimbra.cs.mailbox.Message) Folder(com.zimbra.cs.mailbox.Folder) Document(com.zimbra.cs.mailbox.Document) Contact(com.zimbra.cs.mailbox.Contact) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) MailItem(com.zimbra.cs.mailbox.MailItem) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

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