Search in sources :

Example 1 with ContactGroup

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

the class ContactCSV method toCSV.

public void toCSV(String format, String locale, Character separator, Iterator<? extends MailItem> contacts, StringBuilder sb) throws ParseException, ServiceException {
    if (knownFormats == null) {
        return;
    }
    CsvFormat fmt = getFormat(format, locale);
    if (separator != null) {
        fieldSeparator = separator;
    } else {
        String delimKey = fmt.key();
        Character formatDefaultDelim = delimiterInfo.get(delimKey);
        if (formatDefaultDelim != null) {
            LOG.debug("toCSV choosing %c from <delimiter> matching %s", formatDefaultDelim, delimKey);
            fieldSeparator = formatDefaultDelim;
        }
    }
    LOG.debug("toCSV Requested=[format=\"%s\" locale=\"%s\" delim=\"%c\"] Actual=[%s delim=\"%c\"]", format, locale, separator, fmt, fieldSeparator);
    if (fmt == null) {
        return;
    }
    if (fmt.allFields()) {
        ArrayList<Map<String, String>> allContacts = new ArrayList<Map<String, String>>();
        HashSet<String> fields = new HashSet<String>();
        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());
                }
            }
        }
        ArrayList<String> allFields = new ArrayList<String>();
        allFields.addAll(fields);
        Collections.sort(allFields);
        addFieldDef(allFields, sb);
        for (Map<String, String> contactMap : allContacts) {
            toCSVContact(allFields, contactMap, sb);
        }
        return;
    }
    if (!fmt.hasNoHeaders()) {
        addFieldDef(fmt, sb);
    }
    while (contacts.hasNext()) {
        Object c = contacts.next();
        if (c instanceof Contact) {
            toCSVContact(fmt, (Contact) c, sb);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Contact(com.zimbra.cs.mailbox.Contact) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with ContactGroup

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

the class ParsedContact method modify.

public ParsedContact modify(FieldDeltaList fieldDeltaList, List<Attachment> attachDelta, boolean discardExistingMembers) throws ServiceException {
    if (attachDelta != null && !attachDelta.isEmpty()) {
        try {
            validateImageAttachments(attachDelta);
        } catch (IOException ioe) {
            throw MailServiceException.MESSAGE_PARSE_ERROR(ioe);
        }
        for (Attachment attach : attachDelta) {
            // make sure we don't have anything referenced in both fieldDelta and attachDelta
            fieldDeltaList.removeAllAttrDeltaByName(attach.getName());
            // add the new attachments to the contact
            removeAttachment(attach.getName());
            if (contactAttachments == null) {
                contactAttachments = new ArrayList<Attachment>(attachDelta.size());
            }
            contactAttachments.add(attach);
        }
    }
    ContactGroup contactGroup = null;
    String encodedContactGroup = contactFields.get(ContactConstants.A_groupMember);
    contactGroup = encodedContactGroup == null ? ContactGroup.init() : ContactGroup.init(encodedContactGroup);
    boolean contactGroupMemberChanged = false;
    if (discardExistingMembers && contactGroup.hasMembers()) {
        contactGroup.removeAllMembers();
        contactGroupMemberChanged = true;
    }
    for (FieldDelta delta : fieldDeltaList.getDeltaList()) {
        if (delta instanceof AttrDelta) {
            processAttrDelta((AttrDelta) delta);
        } else if (delta instanceof GroupMemberDelta) {
            processGroupMemberDelta((GroupMemberDelta) delta, contactGroup);
            contactGroupMemberChanged = true;
        }
    }
    if (contactFields.isEmpty())
        throw ServiceException.INVALID_REQUEST("contact must have fields", null);
    if (contactGroupMemberChanged) {
        contactFields.put(ContactConstants.A_groupMember, contactGroup.encode());
    }
    digest = null;
    indexDocs = null;
    if (contactAttachments != null) {
        try {
            mimeMessage = generateMimeMessage(contactAttachments);
            // Original stream is now stale.
            ByteUtil.closeStream(sharedStream);
            sharedStream = null;
            initializeSizeAndDigest();
        } catch (MessagingException me) {
            throw MailServiceException.MESSAGE_PARSE_ERROR(me);
        } catch (IOException e) {
            throw MailServiceException.MESSAGE_PARSE_ERROR(e);
        }
    } else {
        // No attachments.  Wipe out any previous reference to a blob.
        ByteUtil.closeStream(sharedStream);
        sharedStream = null;
        mimeMessage = null;
        size = 0;
        digest = null;
    }
    return this;
}
Also used : MessagingException(javax.mail.MessagingException) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) IOException(java.io.IOException) ContactGroup(com.zimbra.cs.mailbox.ContactGroup)

Example 3 with ContactGroup

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

the class ParsedContact method init.

private void init(Map<String, ? extends Object> fields, InputStream in) throws ServiceException {
    if (fields == null) {
        throw ServiceException.INVALID_REQUEST("contact must have fields", null);
    }
    // Initialized shared stream.
    try {
        if (in instanceof SharedInputStream) {
            sharedStream = in;
        } else if (in != null) {
            byte[] content = ByteUtil.getContent(in, 1024);
            sharedStream = new SharedByteArrayInputStream(content);
        }
    } catch (IOException e) {
        throw MailServiceException.MESSAGE_PARSE_ERROR(e);
    }
    // Initialize fields.
    Map<String, String> map = new HashMap<String, String>();
    for (Map.Entry<String, ? extends Object> entry : fields.entrySet()) {
        String key = StringUtil.stripControlCharacters(entry.getKey());
        String value = null;
        if (entry.getValue() instanceof String[]) {
            // encode multi value attributes as JSONObject
            try {
                value = Contact.encodeMultiValueAttr((String[]) entry.getValue());
            } catch (JSONException e) {
                ZimbraLog.index.warn("Error encoding multi valued attribute " + key, e);
            }
        } else if (entry.getValue() instanceof String) {
            value = StringUtil.stripControlCharacters((String) entry.getValue());
        } else if (entry.getValue() instanceof ContactGroup) {
            value = ((ContactGroup) entry.getValue()).encode();
        }
        if (key != null && !key.trim().isEmpty() && !Strings.isNullOrEmpty(value)) {
            if (key.length() > ContactConstants.MAX_FIELD_NAME_LENGTH) {
                throw ServiceException.INVALID_REQUEST("too big filed name", null);
            } else if (value.length() > ContactConstants.MAX_FIELD_VALUE_LENGTH) {
                throw MailServiceException.CONTACT_TOO_BIG(ContactConstants.MAX_FIELD_VALUE_LENGTH, value.length());
            }
            map.put(key, value);
        }
    }
    if (map.isEmpty()) {
        throw ServiceException.INVALID_REQUEST("contact must have fields", null);
    } else if (map.size() > ContactConstants.MAX_FIELD_COUNT) {
        throw ServiceException.INVALID_REQUEST("too many fields", null);
    }
    contactFields = map;
    // Initialize attachments.
    if (sharedStream != null) {
        InputStream contentStream = null;
        try {
            // Parse attachments.
            contentStream = getContentStream();
            contactAttachments = parseBlob(contentStream);
            for (Attachment attach : contactAttachments) {
                contactFields.remove(attach.getName());
            }
            initializeSizeAndDigest();
        } catch (MessagingException me) {
            throw MailServiceException.MESSAGE_PARSE_ERROR(me);
        } catch (IOException ioe) {
            throw MailServiceException.MESSAGE_PARSE_ERROR(ioe);
        } finally {
            ByteUtil.closeStream(contentStream);
        }
    }
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) SharedInputStream(javax.mail.internet.SharedInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) JSONException(org.json.JSONException) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) IOException(java.io.IOException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) SharedInputStream(javax.mail.internet.SharedInputStream) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ContactGroup

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

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

the class CreateContact method parseContact.

/*
     * for CreateContact and ModifyContact replace mode
     */
static Pair<Map<String, Object>, List<Attachment>> parseContact(Element cn, ZimbraSoapContext zsc, OperationContext octxt, Contact existing) throws ServiceException {
    Map<String, Object> fields = new HashMap<String, Object>();
    List<Attachment> attachments = new ArrayList<Attachment>();
    boolean isContactGroup = false;
    Mailbox mbox = getRequestedMailbox(zsc);
    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);
        }
        // do not allow specifying groupMember as attribute directly
        disallowGroupMemberAttr(name);
        Attachment attach = parseAttachment(elt, name, zsc, octxt, existing);
        if (attach == null) {
            disallowOperation(elt);
            String value = elt.getText();
            StringUtil.addToMultiMap(fields, name, value);
            if (ContactConstants.A_type.equals(name) && ContactConstants.TYPE_GROUP.equals(value)) {
                isContactGroup = true;
            }
        } else {
            attachments.add(attach);
        }
    }
    // parse contact group members
    ContactGroup contactGroup = null;
    for (Element elt : cn.listElements(MailConstants.E_CONTACT_GROUP_MEMBER)) {
        if (!isContactGroup) {
            // do not check existing contact, because this is replace mode or creating
            throw ServiceException.INVALID_REQUEST(MailConstants.E_CONTACT_GROUP_MEMBER + " is only allowed for contact group", null);
        }
        disallowOperation(elt);
        if (contactGroup == null) {
            contactGroup = ContactGroup.init(existing, true);
            if (existing != null) {
                contactGroup.removeAllMembers();
            }
        }
        String memberType = elt.getAttribute(MailConstants.A_CONTACT_GROUP_MEMBER_TYPE);
        String memberValue = elt.getAttribute(MailConstants.A_CONTACT_GROUP_MEMBER_VALUE);
        Member.Type type = Member.Type.fromSoap(memberType);
        // bug 98526: remove account ID from item ID when it references the local account
        String contactId = memberValue;
        if (type == Member.Type.CONTACT_REF) {
            ItemId iid = new ItemId(memberValue, mbox.getAccountId());
            if (iid.getAccountId().equals(mbox.getAccountId())) {
                contactId = String.valueOf(iid.getId());
            }
        }
        contactGroup.addMember(type, contactId);
    }
    if (contactGroup != null) {
        fields.put(ContactConstants.A_groupMember, contactGroup);
    }
    return new Pair<Map<String, Object>, List<Attachment>>(fields, attachments);
}
Also used : HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) Member(com.zimbra.cs.mailbox.ContactGroup.Member) Pair(com.zimbra.common.util.Pair)

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