Search in sources :

Example 1 with Member

use of com.zimbra.cs.mailbox.ContactGroup.Member 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 2 with Member

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

the class ContactMemberOfMap method getMemberOfMap.

public static Map<String, Set<String>> getMemberOfMap(Mailbox mbox, OperationContext octxt) {
    long start = System.currentTimeMillis();
    SearchParams params = new SearchParams();
    params.setQueryString("#type:group");
    Set<MailItem.Type> types = Sets.newHashSetWithExpectedSize(1);
    types.add(MailItem.Type.CONTACT);
    params.setTypes(types);
    params.setSortBy(SortBy.NONE);
    OperationContext opCtxt;
    try {
        opCtxt = octxt != null ? octxt : new OperationContext(mbox.getAccount());
    } catch (ServiceException se) {
        ZimbraLog.contact.debug("Problem creating OperationContext for getMemberOfMap", se);
        return Collections.emptyMap();
    }
    try (ZimbraQueryResults results = mbox.index.search(SoapProtocol.Soap12, opCtxt, params)) {
        Map<String, Set<String>> memberOfMap = Maps.newHashMap();
        while (results.hasNext()) {
            ZimbraHit hit = results.getNext();
            if (hit instanceof ContactHit) {
                Contact contact = ((ContactHit) hit).getContact();
                ContactGroup contactGroup = null;
                try {
                    contactGroup = ContactGroup.init(contact.get(ContactConstants.A_groupMember));
                    List<Member> members = contactGroup.getMembers();
                    if (members == null) {
                        continue;
                    }
                    for (Member member : members) {
                        if (!Type.CONTACT_REF.equals(member.getType())) {
                            continue;
                        }
                        String memberId = member.getValue();
                        if (!memberOfMap.containsKey(memberId)) {
                            memberOfMap.put(memberId, Sets.newHashSet());
                        }
                        memberOfMap.get(memberId).add(hit.getParsedItemID().toString());
                    }
                } catch (ServiceException e) {
                    ZimbraLog.contact.debug("can't get group members for Contact %d", contact.getId(), e);
                }
            }
        }
        ZimbraLog.contact.debug("getMemberOfMap for %s - %s members %s", mbox.getAccount().getName(), memberOfMap.size(), ZimbraLog.elapsedTime(start, System.currentTimeMillis()));
        return memberOfMap;
    } catch (ServiceException | IOException e) {
        ZimbraLog.contact.debug("Problem creating MemberOf map", e);
    }
    return Collections.emptyMap();
}
Also used : ZimbraHit(com.zimbra.cs.index.ZimbraHit) SearchParams(com.zimbra.cs.index.SearchParams) Set(java.util.Set) IOException(java.io.IOException) Type(com.zimbra.cs.mailbox.ContactGroup.Member.Type) ServiceException(com.zimbra.common.service.ServiceException) ContactHit(com.zimbra.cs.index.ContactHit) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Member(com.zimbra.cs.mailbox.ContactGroup.Member)

Example 3 with Member

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

the class CreateContact method parseContactMergeMode.

static ParsedContact parseContactMergeMode(Element cn, ZimbraSoapContext zsc, OperationContext octxt, Contact existing) throws ServiceException {
    Mailbox mbox = getRequestedMailbox(zsc);
    ParsedContact.FieldDeltaList deltaList = new ParsedContact.FieldDeltaList();
    List<Attachment> attachments = new ArrayList<Attachment>();
    boolean isContactGroup = false;
    for (Element elt : cn.listElements(MailConstants.E_ATTRIBUTE)) {
        String name = elt.getAttribute(MailConstants.A_ATTRIBUTE_NAME);
        if (name.trim().equals(""))
            throw ServiceException.INVALID_REQUEST("at least one contact field name is blank", null);
        Attachment attach = parseAttachment(elt, name, zsc, octxt, existing);
        if (attach == null) {
            String opStr = elt.getAttribute(MailConstants.A_OPERATION, null);
            ParsedContact.FieldDelta.Op op = FieldDelta.Op.fromString(opStr);
            String value = elt.getText();
            deltaList.addAttrDelta(name, value, op);
            if (ContactConstants.A_type.equals(name) && ContactConstants.TYPE_GROUP.equals(value) && ParsedContact.FieldDelta.Op.REMOVE != op) {
                isContactGroup = true;
            }
        } else {
            attachments.add(attach);
        }
    }
    boolean discardExistingMembers = false;
    for (Element elt : cn.listElements(MailConstants.E_CONTACT_GROUP_MEMBER)) {
        if (!isContactGroup && !existing.isGroup()) {
            throw ServiceException.INVALID_REQUEST(MailConstants.E_CONTACT_GROUP_MEMBER + " is only allowed for contact group", null);
        }
        String opStr = elt.getAttribute(MailConstants.A_OPERATION);
        ModifyGroupMemberOperation groupMemberOp = ModifyGroupMemberOperation.fromString(opStr);
        if (ModifyGroupMemberOperation.RESET.equals(groupMemberOp)) {
            discardExistingMembers = true;
        } else {
            ParsedContact.FieldDelta.Op op = FieldDelta.Op.fromString(opStr);
            ContactGroup.Member.Type memberType = ContactGroup.Member.Type.fromSoap(elt.getAttribute(MailConstants.A_CONTACT_GROUP_MEMBER_TYPE, null));
            String memberValue = elt.getAttribute(MailConstants.A_CONTACT_GROUP_MEMBER_VALUE, null);
            if (memberType == null) {
                throw ServiceException.INVALID_REQUEST("missing member type", null);
            }
            if (StringUtil.isNullOrEmpty(memberValue)) {
                throw ServiceException.INVALID_REQUEST("missing member value", null);
            }
            // bug 98526: remove account ID from item ID when it references the local account
            String contactId = memberValue;
            if (memberType == ContactGroup.Member.Type.CONTACT_REF) {
                ItemId iid = new ItemId(memberValue, mbox.getAccountId());
                if (!iid.getAccountId().equals(mbox.getAccountId())) {
                    contactId = memberValue;
                } else {
                    contactId = String.valueOf(iid.getId());
                }
            }
            deltaList.addGroupMemberDelta(memberType, contactId, op);
        }
    }
    return new ParsedContact(existing).modify(deltaList, attachments, discardExistingMembers);
}
Also used : Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ItemId(com.zimbra.cs.service.util.ItemId) FieldDelta(com.zimbra.cs.mime.ParsedContact.FieldDelta) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ModifyGroupMemberOperation(com.zimbra.soap.mail.type.ModifyGroupMemberOperation) Member(com.zimbra.cs.mailbox.ContactGroup.Member)

Example 4 with Member

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

the class TestContactGroup method membersIterator.

@Test
public void membersIterator() 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) });
    boolean caughtException = false;
    try {
        for (Iterator<Member> iter = contactGroup.getMembers().iterator(); iter.hasNext(); ) {
            Member member = iter.next();
            if (member.getType() == Member.Type.GAL_REF || member.getType() == Member.Type.INLINE) {
                // not allowed
                iter.remove();
            }
        }
    } catch (UnsupportedOperationException e) {
        caughtException = true;
    }
    assertTrue(caughtException);
}
Also used : 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 5 with Member

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

the class TestContactGroup method unmodifiableList.

@Test
public void unmodifiableList() 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) });
    boolean caughtException = false;
    try {
        List<Member> members = contactGroup.getMembers();
        for (Member member : members) {
            // not allowed
            members.remove(member);
        }
    } catch (UnsupportedOperationException e) {
        caughtException = true;
    }
    assertTrue(caughtException);
}
Also used : 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

Member (com.zimbra.cs.mailbox.ContactGroup.Member)17 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)15 ContactRefMember (com.zimbra.cs.mailbox.ContactGroup.ContactRefMember)12 GalRefMember (com.zimbra.cs.mailbox.ContactGroup.GalRefMember)12 InlineMember (com.zimbra.cs.mailbox.ContactGroup.InlineMember)12 Test (org.junit.Test)12 ServiceException (com.zimbra.common.service.ServiceException)5 Contact (com.zimbra.cs.mailbox.Contact)5 ZMailbox (com.zimbra.client.ZMailbox)4 Account (com.zimbra.cs.account.Account)4 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 ItemId (com.zimbra.cs.service.util.ItemId)4 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)3 OperationContext (com.zimbra.cs.mailbox.OperationContext)3 ParsedContact (com.zimbra.cs.mime.ParsedContact)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Element (com.zimbra.common.soap.Element)2 VCardParamsAndValue (com.zimbra.cs.mailbox.VCardParamsAndValue)2 ZContact (com.zimbra.client.ZContact)1