Search in sources :

Example 36 with ParsedContact

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

the class ContactQueryTest method search.

@Test
public void search() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_firstName, "Michael");
    fields.put(ContactConstants.A_lastName, "Smith");
    fields.put(ContactConstants.A_email, "michael.smith@zimbra.com");
    mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    fields.put(ContactConstants.A_firstName, "Jonathan");
    fields.put(ContactConstants.A_lastName, "Smith");
    fields.put(ContactConstants.A_email, "jonathan.smith@zimbra.com");
    Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    ZimbraQueryResults results = mbox.index.search(new OperationContext(mbox), "contact:\"Jon Smith\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue("Expected some hits", results.hasNext());
    Assert.assertEquals("Hit ItemId not as expected", contact.getId(), results.getNext().getItemId());
    results.close();
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 37 with ParsedContact

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

the class ContactQueryTest method wildcard.

@Test
public void wildcard() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_firstName, "First*");
    fields.put(ContactConstants.A_lastName, "Las*t");
    fields.put(ContactConstants.A_email, "first.last@zimbra.com");
    Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    ZimbraQueryResults results = mbox.index.search(new OperationContext(mbox), "contact:\"First\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"First*\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"Las*\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"Las*t\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 38 with ParsedContact

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

the class CreateContact method redo.

@Override
public void redo() throws Exception {
    int mboxId = getMailboxId();
    Mailbox mbox = MailboxManager.getInstance().getMailboxById(mboxId);
    OperationContext octxt = getOperationContext();
    if (mTags == null && mTagIds != null) {
        mTags = TagUtil.tagIdStringToNames(mbox, octxt, mTagIds);
    }
    InputStream in = null;
    try {
        in = getAdditionalDataStream();
        ParsedContact pc = new ParsedContact(mFields, in);
        mbox.createContact(octxt, pc, mFolderId, mTags);
    } catch (ServiceException e) {
        String code = e.getCode();
        if (code.equals(MailServiceException.ALREADY_EXISTS)) {
            mLog.info("Contact %d already exists in mailbox %d", mId, mboxId);
        } else {
            throw e;
        }
    } finally {
        ByteUtil.closeStream(in);
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) InputStream(java.io.InputStream)

Example 39 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)

Example 40 with ParsedContact

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

the class MetadataTest method legacyContact.

@Test
public void legacyContact() throws ServiceException {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, Object> fields = new HashMap<String, Object>();
    String f1 = "First1";
    String l1 = "Last1";
    fields.put(ContactConstants.A_firstName, f1);
    fields.put(ContactConstants.A_lastName, l1);
    Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    Assert.assertEquals(f1, contact.get(ContactConstants.A_firstName));
    Assert.assertEquals(l1, contact.get(ContactConstants.A_lastName));
    Metadata metadata = new Metadata();
    String mailAddr = "test@email.net";
    String f2 = "First2";
    metadata.put(ContactConstants.A_email, mailAddr);
    metadata.put(ContactConstants.A_firstName, f2);
    contact.decodeMetadata(metadata);
    Assert.assertEquals(mailAddr, contact.get(ContactConstants.A_email));
    Assert.assertEquals(f2, contact.get(ContactConstants.A_firstName));
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) HashMap(java.util.HashMap) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

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