Search in sources :

Example 16 with ContactGroup

use of com.zimbra.cs.mailbox.ContactGroup in project zm-mailbox by Zimbra.

the class ToXML method encodeContactGroup.

private static void encodeContactGroup(Element elem, ContactGroup contactGroup, Collection<String> memberAttrFilter, ItemIdFormatter ifmt, OperationContext octxt, boolean summary, int fields) throws ServiceException {
    for (ContactGroup.Member member : contactGroup.getMembers(true)) {
        Element eMember = elem.addNonUniqueElement(MailConstants.E_CONTACT_GROUP_MEMBER);
        encodeContactGroupMemberBasic(eMember, member);
        Object derefedMember = member.getDerefedObj();
        if (derefedMember != null) {
            if (derefedMember instanceof String) {
            // inline member, do nothing
            } else if (derefedMember instanceof Contact) {
                // only expand one level for now.
                // If this member is a group, do not create/pass down a ContactGroup object from the member.
                encodeContact(eMember, ifmt, octxt, (Contact) derefedMember, summary, memberAttrFilter, fields);
            } else if (derefedMember instanceof GalContact) {
                encodeGalContact(eMember, (GalContact) derefedMember, memberAttrFilter);
            } else if (derefedMember instanceof Element) {
                // proxied GAL or Contact entry
                Element eContact = (Element) derefedMember;
                if (memberAttrFilter != null) {
                    for (Element eAttr : eContact.listElements()) {
                        String name = eAttr.getAttribute(MailConstants.A_ATTRIBUTE_NAME, null);
                        if (!memberAttrFilter.contains(name)) {
                            eAttr.detach();
                        }
                    }
                }
                eMember.addNonUniqueElement(eContact);
            }
        }
    }
}
Also used : GalContact(com.zimbra.cs.account.GalContact) Element(com.zimbra.common.soap.Element) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) GalContact(com.zimbra.cs.account.GalContact) Contact(com.zimbra.cs.mailbox.Contact)

Example 17 with ContactGroup

use of com.zimbra.cs.mailbox.ContactGroup in project zm-mailbox by Zimbra.

the class UserServletUtil method populateContactFields.

public static void populateContactFields(Iterator<? extends MailItem> contacts, Mailbox mbox, OperationContext octxt, ArrayList<Map<String, String>> allContacts, HashSet<String> fields) throws ServiceException {
    while (contacts.hasNext()) {
        Object obj = contacts.next();
        if (obj instanceof Contact) {
            Contact c = (Contact) obj;
            if (c.isContactGroup()) {
                HashMap<String, String> nContacts = new HashMap<String, String>();
                // first add all the fields and values
                nContacts.putAll(c.getFields());
                // remove groupMemeber
                nContacts.remove(ContactConstants.A_groupMember);
                // then re-calculate the dlist as in 7.X
                ContactGroup cg = ContactGroup.init(c, false);
                String strs = cg.migrateToDlist(mbox, octxt);
                nContacts.put(ContactConstants.A_dlist, strs);
                allContacts.add(nContacts);
                fields.addAll(nContacts.keySet());
            } else {
                allContacts.add(c.getFields());
                fields.addAll(c.getFields().keySet());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) Contact(com.zimbra.cs.mailbox.Contact)

Example 18 with ContactGroup

use of com.zimbra.cs.mailbox.ContactGroup in project zm-mailbox by Zimbra.

the class CreateContact method migrateFromDlist.

static void migrateFromDlist(ParsedContact pc) throws ServiceException {
    /*
         * replace groupMember with dlist data
         *
         * Note: if the user had also used new clients to manipulate group members
         *       all ref members will be lost, since all dlist members will be
         *       migrated as INLINE members.
         *       if dlist is an empty string, the group will become an empty group.
         *
         *       This is the expected behavior.
         */
    Map<String, String> fields = pc.getFields();
    String dlist = fields.get(ContactConstants.A_dlist);
    if (dlist != null) {
        try {
            ContactGroup contactGroup = ContactGroup.init();
            contactGroup.migrateFromDlist(dlist);
            fields.put(ContactConstants.A_groupMember, contactGroup.encode());
            fields.remove(ContactConstants.A_dlist);
        } catch (Exception e) {
            ZimbraLog.contact.info("skipped migrating contact group, dlist=[%s]", dlist, e);
        }
    }
}
Also used : ContactGroup(com.zimbra.cs.mailbox.ContactGroup) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 19 with ContactGroup

use of com.zimbra.cs.mailbox.ContactGroup in project zm-mailbox by Zimbra.

the class GetContacts method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    Account mRequestedAcct = getRequestedAccount(zsc);
    Account mAuthedAcct = getAuthenticatedAccount(zsc);
    GetContactsRequest req = zsc.elementToJaxb(request);
    boolean sync = req.getSync() == null ? false : req.getSync();
    boolean derefContactGroupMember = req.getDerefGroupMember();
    String folderIdStr = req.getFolderId();
    int folderId = ALL_FOLDERS;
    if (folderIdStr != null) {
        ItemId iidFolder = new ItemId(folderIdStr, zsc);
        if (iidFolder.belongsTo(mbox))
            folderId = iidFolder.getId();
        else
            throw ServiceException.FAILURE("Got remote folderId: " + folderIdStr + " but did not proxy", null);
    }
    SortBy sort = SortBy.of(req.getSortBy());
    if (sort == null) {
        sort = SortBy.NONE;
    }
    ArrayList<String> attrs = null;
    ArrayList<String> memberAttrs = null;
    ArrayList<ItemId> ids = null;
    // MailConstants.A_ATTRIBUTE_NAME
    List<AttributeName> reqAttrs = req.getAttributes();
    if (reqAttrs != null && reqAttrs.size() > 0) {
        attrs = new ArrayList<String>();
        for (AttributeName attrName : reqAttrs) {
            attrs.add(attrName.getName());
        }
    }
    // MailConstants.E_CONTACT_GROUP_MEMBER_ATTRIBUTE
    reqAttrs = req.getMemberAttributes();
    if (reqAttrs != null && reqAttrs.size() > 0) {
        memberAttrs = new ArrayList<String>();
        for (AttributeName attrName : reqAttrs) {
            memberAttrs.add(attrName.getName());
        }
    }
    // MailConstants.E_CONTACT
    List<Id> contactIds = req.getContacts();
    if (contactIds != null && contactIds.size() > 0) {
        ids = new ArrayList<ItemId>();
        for (Id target : contactIds) {
            String idStr = target.getId();
            if (idStr.indexOf(",") > 0) {
                // comma-separated IDs. TODO: deprecate this use-case
                String[] toks = idStr.split(",");
                for (int i = 0; i < toks.length; i++) {
                    ids.add(new ItemId(toks[i], zsc));
                }
            } else {
                ids.add(new ItemId(idStr, zsc));
            }
        }
    }
    long maxMembers = DEFAULT_MAX_MEMBERS;
    boolean returnHiddenAttrs = false;
    if (attrs == null) {
        returnHiddenAttrs = req.getReturnHiddenAttrs();
        maxMembers = (req.getMaxMembers() == null) ? DEFAULT_MAX_MEMBERS : req.getMaxMembers();
    }
    boolean returnCertInfo = req.getReturnCertInfo();
    Map<String, Set<String>> memberOfMap = null;
    if (req.getIncludeMemberOf()) {
        memberOfMap = ContactMemberOfMap.getMemberOfMap(mbox, octxt);
    }
    Element response = zsc.createElement(MailConstants.GET_CONTACTS_RESPONSE);
    // want to return modified date only on sync-related requests
    int fields = ToXML.NOTIFY_FIELDS;
    if (sync) {
        fields |= Change.CONFLICT;
    }
    if (req.getWantImapUid()) {
        fields |= Change.IMAP_UID;
    }
    // for perf reason, derefContactGroupMember is not supported in this mode
    if (derefContactGroupMember) {
        if (ids == null) {
            throw ServiceException.INVALID_REQUEST(MailConstants.A_DEREF_CONTACT_GROUP_MEMBER + " is supported only when specific contact ids are specified", null);
        }
    }
    if (ids != null) {
        ArrayList<Integer> local = new ArrayList<Integer>();
        HashMap<String, StringBuffer> remote = new HashMap<String, StringBuffer>();
        partitionItems(zsc, ids, local, remote);
        if (remote.size() > 0) {
            if (folderId > 0)
                throw ServiceException.INVALID_REQUEST("Cannot specify a folder with mixed-mailbox items", null);
            List<Element> responses = proxyRemote(request, remote, context);
            for (Element e : responses) response.addNonUniqueElement(e);
        }
        if (local.size() > 0) {
            boolean migrateDlist = CreateContact.needToMigrateDlist(zsc);
            for (int id : local) {
                Contact con = mbox.getContactById(octxt, id);
                if (con != null && (folderId == ALL_FOLDERS || folderId == con.getFolderId())) {
                    ContactGroup contactGroup = null;
                    String migratedDlist = null;
                    if (migrateDlist) {
                        ContactGroup cg = ContactGroup.init(con, false);
                        if (cg != null) {
                            migratedDlist = cg.migrateToDlist(con.getMailbox(), octxt);
                        }
                    } else if (derefContactGroupMember) {
                        contactGroup = ContactGroup.init(con, false);
                        if (contactGroup != null) {
                            contactGroup.derefAllMembers(con.getMailbox(), octxt, zsc.getResponseProtocol());
                        }
                    }
                    ToXML.encodeContact(response, ifmt, octxt, con, contactGroup, memberAttrs, false, /* summary */
                    attrs, fields, migratedDlist, returnHiddenAttrs, maxMembers, returnCertInfo, ContactMemberOfMap.setOfMemberOf(zsc.getRequestedAccountId(), id, memberOfMap), mRequestedAcct, mAuthedAcct);
                }
            }
        }
    } else {
        for (Contact con : mbox.getContactList(octxt, folderId, sort)) {
            if (con != null) {
                ToXML.encodeContact(response, ifmt, octxt, con, null, null, false, /* summary */
                attrs, fields, null, returnHiddenAttrs, maxMembers, returnCertInfo, ContactMemberOfMap.setOfMemberOf(zsc.getRequestedAccountId(), con.getId(), memberOfMap));
            }
        }
    }
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Set(java.util.Set) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) HashMap(java.util.HashMap) SortBy(com.zimbra.cs.index.SortBy) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) ItemId(com.zimbra.cs.service.util.ItemId) GetContactsRequest(com.zimbra.soap.mail.message.GetContactsRequest) Mailbox(com.zimbra.cs.mailbox.Mailbox) AttributeName(com.zimbra.soap.type.AttributeName) OperationContext(com.zimbra.cs.mailbox.OperationContext) Contact(com.zimbra.cs.mailbox.Contact) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Id(com.zimbra.soap.type.Id) ItemId(com.zimbra.cs.service.util.ItemId) ContactGroup(com.zimbra.cs.mailbox.ContactGroup)

Example 20 with ContactGroup

use of com.zimbra.cs.mailbox.ContactGroup in project zm-mailbox by Zimbra.

the class TestContactGroup method getMembers.

@Test
public void getMembers() throws Exception {
    ContactGroup contactGroup = createContactGroup(new MemberData[] { new MemberData(Member.Type.CONTACT_REF, CONTACT_REF_VALUE), new MemberData(Member.Type.GAL_REF, GAL_REF_VALUE), new MemberData(Member.Type.INLINE, INLINE_VALUE) });
    List<Member> members = contactGroup.getMembers();
    assertEquals(3, members.size());
    assertTrue(members.get(0) instanceof ContactRefMember);
    assertEquals(CONTACT_REF_VALUE, members.get(0).getValue());
    assertTrue(members.get(1) instanceof GalRefMember);
    assertEquals(GAL_REF_VALUE, members.get(1).getValue());
    assertTrue(members.get(2) instanceof InlineMember);
    assertEquals(INLINE_VALUE, members.get(2).getValue());
}
Also used : ContactRefMember(com.zimbra.cs.mailbox.ContactGroup.ContactRefMember) GalRefMember(com.zimbra.cs.mailbox.ContactGroup.GalRefMember) InlineMember(com.zimbra.cs.mailbox.ContactGroup.InlineMember) 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)

Aggregations

ContactGroup (com.zimbra.cs.mailbox.ContactGroup)25 Member (com.zimbra.cs.mailbox.ContactGroup.Member)16 Test (org.junit.Test)13 ContactRefMember (com.zimbra.cs.mailbox.ContactGroup.ContactRefMember)12 GalRefMember (com.zimbra.cs.mailbox.ContactGroup.GalRefMember)12 InlineMember (com.zimbra.cs.mailbox.ContactGroup.InlineMember)12 Contact (com.zimbra.cs.mailbox.Contact)9 HashMap (java.util.HashMap)8 ServiceException (com.zimbra.common.service.ServiceException)7 Account (com.zimbra.cs.account.Account)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)5 ItemId (com.zimbra.cs.service.util.ItemId)5 ArrayList (java.util.ArrayList)5 ZMailbox (com.zimbra.client.ZMailbox)4 Element (com.zimbra.common.soap.Element)4 OperationContext (com.zimbra.cs.mailbox.OperationContext)4 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)3 Attachment (com.zimbra.cs.mailbox.Contact.Attachment)3 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)2 VCardParamsAndValue (com.zimbra.cs.mailbox.VCardParamsAndValue)2