Search in sources :

Example 16 with ParsedContact

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

the class Mailbox method createContact.

public Contact createContact(OperationContext octxt, ParsedContact pc, int folderId, String[] tags) throws ServiceException {
    StoreManager sm = StoreManager.getInstance();
    StagedBlob staged = null;
    if (pc.hasAttachment()) {
        // write the contact content directly to the mailbox's blob staging area
        InputStream is = null;
        try {
            staged = sm.stage(is = pc.getContentStream(), (int) pc.getSize(), this);
        } catch (IOException ioe) {
            throw ServiceException.FAILURE("could not save contact blob", ioe);
        } finally {
            ByteUtil.closeStream(is);
        }
    }
    CreateContact redoRecorder = new CreateContact(mId, folderId, pc, tags);
    boolean success = false;
    try {
        beginTransaction("createContact", octxt, redoRecorder);
        CreateContact redoPlayer = (CreateContact) currentChange().getRedoPlayer();
        boolean isRedo = redoPlayer != null;
        Tag.NormalizedTags ntags = new Tag.NormalizedTags(this, tags);
        int contactId = getNextItemId(isRedo ? redoPlayer.getContactId() : ID_AUTO_INCREMENT);
        MailboxBlob mblob = null;
        if (pc.hasAttachment()) {
            try {
                mblob = sm.renameTo(staged, this, contactId, getOperationChangeID());
                markOtherItemDirty(mblob);
            } catch (IOException ioe) {
                throw ServiceException.FAILURE("could not save contact blob", ioe);
            }
        }
        int flags = 0;
        Contact con = Contact.create(contactId, getFolderById(folderId), mblob, pc, flags, ntags, null);
        redoRecorder.setContactId(contactId);
        index.add(con);
        success = true;
        return con;
    } finally {
        endTransaction(success);
        sm.quietDelete(staged);
    }
}
Also used : StagedBlob(com.zimbra.cs.store.StagedBlob) NormalizedTags(com.zimbra.cs.mailbox.Tag.NormalizedTags) MailboxBlob(com.zimbra.cs.store.MailboxBlob) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) CopyInputStream(com.zimbra.common.util.CopyInputStream) InputStream(java.io.InputStream) NormalizedTags(com.zimbra.cs.mailbox.Tag.NormalizedTags) CreateContact(com.zimbra.cs.redolog.op.CreateContact) IOException(java.io.IOException) AlterItemTag(com.zimbra.cs.redolog.op.AlterItemTag) CreateTag(com.zimbra.cs.redolog.op.CreateTag) DbTag(com.zimbra.cs.db.DbTag) RefreshMountpoint(com.zimbra.cs.redolog.op.RefreshMountpoint) TargetConstraint(com.zimbra.cs.mailbox.MailItem.TargetConstraint) CreateMountpoint(com.zimbra.cs.redolog.op.CreateMountpoint) StoreManager(com.zimbra.cs.store.StoreManager) ModifyContact(com.zimbra.cs.redolog.op.ModifyContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) CreateContact(com.zimbra.cs.redolog.op.CreateContact)

Example 17 with ParsedContact

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

the class Mailbox method modifyContact.

public void modifyContact(OperationContext octxt, int contactId, ParsedContact pc) throws ServiceException {
    StoreManager sm = StoreManager.getInstance();
    StagedBlob staged = null;
    if (pc.hasAttachment()) {
        // write the contact content directly to the mailbox's blob staging area
        InputStream is = null;
        try {
            staged = sm.stage(is = pc.getContentStream(), pc.getSize(), this);
        } catch (IOException ioe) {
            throw ServiceException.FAILURE("could not save contact blob", ioe);
        } finally {
            ByteUtil.closeStream(is);
        }
    }
    ModifyContact redoRecorder = new ModifyContact(mId, contactId, pc);
    boolean success = false;
    try {
        beginTransaction("modifyContact", octxt, redoRecorder);
        Contact con = getContactById(contactId);
        if (!checkItemChangeID(con)) {
            throw MailServiceException.MODIFY_CONFLICT();
        }
        try {
            // setContent() calls reanalyze(), which also updates the contact fields even when there is no blob
            con.setContent(staged, pc);
        } catch (IOException ioe) {
            throw ServiceException.FAILURE("could not save contact blob", ioe);
        }
        index.add(con);
        success = true;
    } finally {
        endTransaction(success);
        sm.quietDelete(staged);
    }
}
Also used : ModifyContact(com.zimbra.cs.redolog.op.ModifyContact) StagedBlob(com.zimbra.cs.store.StagedBlob) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) CopyInputStream(com.zimbra.common.util.CopyInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) StoreManager(com.zimbra.cs.store.StoreManager) ModifyContact(com.zimbra.cs.redolog.op.ModifyContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) CreateContact(com.zimbra.cs.redolog.op.CreateContact)

Example 18 with ParsedContact

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

the class TestContactGroup method derefContact.

@Test
public void derefContact() throws Exception {
    Account account = Provisioning.getInstance().get(AccountBy.name, TestUtil.getAddress("user1"));
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    //TODO
    OperationContext octxt = null;
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_fileAs, ContactConstants.FA_FIRST_LAST);
    fields.put(ContactConstants.A_firstName, "test");
    fields.put(ContactConstants.A_email, "test1@zimbra.com");
    fields.put(ContactConstants.A_workEmail1, "test2@zimbra.com");
    Contact contact = mbox.createContact(octxt, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    ContactGroup contactGroup = createContactGroup(new MemberData[] { new MemberData(Member.Type.CONTACT_REF, "" + contact.getId()), new MemberData(Member.Type.INLINE, "aaa@test.com"), new MemberData(Member.Type.INLINE, "zzz@test.com") });
    contactGroup.derefAllMembers(mbox, octxt);
    boolean gotContactRefMember = false;
    String prevMemberKey = null;
    for (Member member : contactGroup.getDerefedMembers()) {
        String memberKey = member.getDerefedKey();
        if (prevMemberKey != null) {
            assertTrue(prevMemberKey.compareTo(memberKey) < 0);
        }
        prevMemberKey = memberKey;
        Member.Type type = member.getType();
        if (type == Member.Type.CONTACT_REF) {
            assertEquals("test", memberKey);
            gotContactRefMember = true;
        }
    // System.out.println(memberKey);
    }
    List<String> emailAddrs = contactGroup.getEmailAddresses(false, mbox, octxt, false);
    assertEquals(4, emailAddrs.size());
    assertTrue(emailAddrs.contains("test1@zimbra.com"));
    assertTrue(emailAddrs.contains("test2@zimbra.com"));
    assertTrue(emailAddrs.contains("aaa@test.com"));
    assertTrue(emailAddrs.contains("zzz@test.com"));
    assertTrue(gotContactRefMember);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) 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 19 with ParsedContact

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

the class TestContactGroup method returnMembersAsDlist.

@Test
public void returnMembersAsDlist() throws Exception {
    Account account = Provisioning.getInstance().get(AccountBy.name, TestUtil.getAddress("user1"));
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    //TODO
    OperationContext octxt = null;
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_fileAs, ContactConstants.FA_FIRST_LAST);
    fields.put(ContactConstants.A_firstName, "test");
    fields.put(ContactConstants.A_email, "test1@zimbra.com");
    fields.put(ContactConstants.A_workEmail1, "test2@zimbra.com");
    Contact contact = mbox.createContact(octxt, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    Account galMember = Provisioning.getInstance().get(AccountBy.name, TestUtil.getAddress("user2"));
    LdapAccount ldapAccount = (LdapAccount) galMember;
    String dn = ldapAccount.getDN();
    String galEntryEmail = galMember.getName();
    ContactGroup contactGroup = createContactGroup(new MemberData[] { new MemberData(Member.Type.CONTACT_REF, "" + contact.getId()), new MemberData(Member.Type.GAL_REF, dn), new MemberData(Member.Type.INLINE, "aaa@test.com"), new MemberData(Member.Type.INLINE, "zzz@test.com") });
    contactGroup.derefAllMembers(mbox, octxt);
    for (Member member : contactGroup.getDerefedMembers()) {
        String memberKey = member.getDerefedKey();
        System.out.println(memberKey);
    }
    String dlist = contactGroup.migrateToDlist(mbox, octxt);
    // should be in member order
    assertEquals("test1@zimbra.com, test2@zimbra.com, " + galEntryEmail + ", aaa@test.com, zzz@test.com", dlist);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) 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 20 with ParsedContact

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

the class ModifyContact method redo.

@Override
public void redo() throws ServiceException {
    Mailbox mailbox = MailboxManager.getInstance().getMailboxById(getMailboxId());
    InputStream in = null;
    try {
        in = getAdditionalDataStream();
        ParsedContact pc = new ParsedContact(mFields, in);
        mailbox.modifyContact(getOperationContext(), mId, pc);
    } catch (IOException e) {
        throw ServiceException.FAILURE("Redo error", e);
    } finally {
        ByteUtil.closeStream(in);
    }
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) InputStream(java.io.InputStream) IOException(java.io.IOException)

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