Search in sources :

Example 41 with ParsedContact

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

the class Contact method create.

/**
     * Creates a new {@link Contact} and persists it to the database.
     * <p>
     * A real nonnegative item ID must be supplied from a previous call to {@link Mailbox#getNextItemId(int)}.
     *
     * @param id      The id for the new contact.
     * @param folder  The {@link Folder} to create the contact in.
     * @param mblob   The stored blob containing contact attachments.
     * @param pc      The contact's fields and values, plus attachments.
     * @param flags   Initial flagset
     * @param ntags    A serialized version of all {@link Tag}s to apply.
     * @perms {@link ACL#RIGHT_INSERT} on the folder
     * @throws ServiceException   The following error codes are possible:<ul>
     *    <li><tt>mail.CANNOT_CONTAIN</tt> - if the target folder can't
     *        contain contacts
     *    <li><tt>service.INVALID_REQUEST</tt> - if no fields are specified
     *        for the contact
     *    <li><tt>service.FAILURE</tt> - if there's a database failure
     *    <li><tt>service.PERM_DENIED</tt> - if you don't have sufficient
     *        permissions</ul>
     * @see #canContain(byte)
     */
static Contact create(int id, Folder folder, MailboxBlob mblob, ParsedContact pc, int flags, Tag.NormalizedTags ntags, CustomMetadata custom) throws ServiceException {
    if (folder == null || !folder.canContain(Type.CONTACT)) {
        throw MailServiceException.CANNOT_CONTAIN();
    }
    if (!folder.canAccess(ACL.RIGHT_INSERT)) {
        throw ServiceException.PERM_DENIED("you do not have the required rights on the folder");
    }
    Mailbox mbox = folder.getMailbox();
    mbox.updateContactCount(1);
    UnderlyingData data = new UnderlyingData();
    data.id = id;
    data.type = Type.CONTACT.toByte();
    data.folderId = folder.getId();
    if (!folder.inSpam() || mbox.getAccount().getBooleanAttr(Provisioning.A_zimbraJunkMessagesIndexingEnabled, false)) {
        data.indexId = IndexStatus.DEFERRED.id();
    }
    data.imapId = id;
    data.locator = mblob == null ? null : mblob.getLocator();
    data.setBlobDigest(pc.getDigest());
    data.size = pc.getSize();
    data.date = mbox.getOperationTimestamp();
    data.setFlags(flags | (pc.hasAttachment() ? Flag.BITMASK_ATTACHED : 0));
    data.setTags(ntags);
    data.metadata = encodeMetadata(DEFAULT_COLOR_RGB, 1, 1, custom, pc.getFields(), pc.getAttachments());
    data.contentChanged(mbox);
    if (ZimbraLog.mailop.isInfoEnabled()) {
        String email = "null";
        if (pc.getFields() != null) {
            email = pc.getFields().get(ContactConstants.A_email);
        }
        ZimbraLog.mailop.info("adding contact %s: id=%d, folderId=%d, folderName=%s.", email, data.id, folder.getId(), folder.getName());
    }
    new DbMailItem(mbox).setSender(getFileAsString(pc.getFields())).create(data);
    Contact contact = new Contact(mbox, data);
    contact.finishCreation(null);
    if (contact.fields.isEmpty()) {
        throw ServiceException.INVALID_REQUEST("contact must have fields", null);
    }
    return contact;
}
Also used : DbMailItem(com.zimbra.cs.db.DbMailItem) ParsedContact(com.zimbra.cs.mime.ParsedContact)

Aggregations

ParsedContact (com.zimbra.cs.mime.ParsedContact)41 Test (org.junit.Test)24 HashMap (java.util.HashMap)23 Mailbox (com.zimbra.cs.mailbox.Mailbox)15 Contact (com.zimbra.cs.mailbox.Contact)12 OperationContext (com.zimbra.cs.mailbox.OperationContext)12 ItemId (com.zimbra.cs.service.util.ItemId)7 ServiceException (com.zimbra.common.service.ServiceException)6 Element (com.zimbra.common.soap.Element)6 ArrayList (java.util.ArrayList)6 Account (com.zimbra.cs.account.Account)5 Attachment (com.zimbra.cs.mailbox.Contact.Attachment)5 IOException (java.io.IOException)5 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)4 InputStream (java.io.InputStream)4 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)3 Member (com.zimbra.cs.mailbox.ContactGroup.Member)3 CreateContact (com.zimbra.cs.redolog.op.CreateContact)3 ModifyContact (com.zimbra.cs.redolog.op.ModifyContact)3 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)3