Search in sources :

Example 1 with CreateContact

use of com.zimbra.cs.redolog.op.CreateContact 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 2 with CreateContact

use of com.zimbra.cs.redolog.op.CreateContact in project zm-mailbox by Zimbra.

the class Mailbox method createAutoContact.

/**
     * Creates new contacts in AUTO_CONTACTS folder.
     * Note: Its upto the caller to check whether the email addresses in the list pre-exist.
     *
     * @param octxt operation context
     * @param addrs email addresses
     * @return newly created contacts
     */
public List<Contact> createAutoContact(OperationContext octxt, Collection<InternetAddress> addrs) throws IOException {
    if (addrs.isEmpty()) {
        return Collections.emptyList();
    }
    List<Contact> result = new ArrayList<Contact>(addrs.size());
    String locale = octxt != null && octxt.getAuthenticatedUser() != null ? octxt.getAuthenticatedUser().getPrefLocale() : null;
    boolean nameFormatLastFirst = false;
    if (locale != null && locale.equals("ja")) {
        nameFormatLastFirst = true;
    }
    for (InternetAddress addr : addrs) {
        ZimbraLog.mailbox.debug("Auto-adding new contact addr=%s", addr);
        try {
            result.add(createContact(octxt, new ParsedContact(new ParsedAddress(addr, nameFormatLastFirst).getAttributes()), Mailbox.ID_FOLDER_AUTO_CONTACTS, null));
        } catch (ServiceException e) {
            if (e.getCode().equals(MailServiceException.TOO_MANY_CONTACTS)) {
                ZimbraLog.mailbox.warn("Aborting contact addition, " + "Failed to auto-add contact addr=%s", addr, e);
                return result;
            }
            ZimbraLog.mailbox.warn("Failed to auto-add contact addr=%s", addr, e);
        }
    }
    return result;
}
Also used : InternetAddress(com.zimbra.common.mime.InternetAddress) ParsedContact(com.zimbra.cs.mime.ParsedContact) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ModifyContact(com.zimbra.cs.redolog.op.ModifyContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) CreateContact(com.zimbra.cs.redolog.op.CreateContact) ParsedAddress(com.zimbra.cs.mime.ParsedAddress)

Aggregations

ParsedContact (com.zimbra.cs.mime.ParsedContact)2 CreateContact (com.zimbra.cs.redolog.op.CreateContact)2 ModifyContact (com.zimbra.cs.redolog.op.ModifyContact)2 InternetAddress (com.zimbra.common.mime.InternetAddress)1 Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)1 ServiceException (com.zimbra.common.service.ServiceException)1 CopyInputStream (com.zimbra.common.util.CopyInputStream)1 AccountServiceException (com.zimbra.cs.account.AccountServiceException)1 DbTag (com.zimbra.cs.db.DbTag)1 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)1 NormalizedTags (com.zimbra.cs.mailbox.Tag.NormalizedTags)1 ParsedAddress (com.zimbra.cs.mime.ParsedAddress)1 AlterItemTag (com.zimbra.cs.redolog.op.AlterItemTag)1 CreateMountpoint (com.zimbra.cs.redolog.op.CreateMountpoint)1 CreateTag (com.zimbra.cs.redolog.op.CreateTag)1 RefreshMountpoint (com.zimbra.cs.redolog.op.RefreshMountpoint)1 MailboxBlob (com.zimbra.cs.store.MailboxBlob)1 StagedBlob (com.zimbra.cs.store.StagedBlob)1 StoreManager (com.zimbra.cs.store.StoreManager)1 IOException (java.io.IOException)1