Search in sources :

Example 16 with DbMailItem

use of com.zimbra.cs.db.DbMailItem 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 17 with DbMailItem

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

the class Message method createInternal.

static Message createInternal(int id, Folder folder, Conversation conv, ParsedMessage pm, StagedBlob staged, boolean unread, int flags, Tag.NormalizedTags ntags, DraftInfo dinfo, boolean noICal, ZVCalendar cal, CustomMetadataList extended, MessageCreateFactory fact) throws ServiceException {
    if (folder == null || !folder.canContain(Type.MESSAGE)) {
        throw MailServiceException.CANNOT_CONTAIN(folder, Type.MESSAGE);
    }
    if (!folder.canAccess(ACL.RIGHT_INSERT)) {
        throw ServiceException.PERM_DENIED("you do not have the required rights on the folder");
    }
    Mailbox mbox = folder.getMailbox();
    Account acct = mbox.getAccount();
    String sender = pm.getSenderEmail();
    List<Invite> components = null;
    String methodStr = null;
    // Skip calendar processing if message is being filed as spam or trash.
    if (cal != null && folder.getId() != Mailbox.ID_FOLDER_SPAM && folder.getId() != Mailbox.ID_FOLDER_TRASH) {
        // XXX: shouldn't we just be checking flags for Flag.FLAG_FROM_ME?
        // boolean sentByMe = (flags & Flag.FLAG_FROM_ME) != 0;
        boolean sentByMe = false;
        if (!Strings.isNullOrEmpty(sender)) {
            sentByMe = AccountUtil.addressMatchesAccountOrSendAs(acct, sender);
        }
        try {
            components = Invite.createFromCalendar(acct, pm.getFragment(acct.getLocale()), cal, sentByMe, mbox, id);
            methodStr = cal.getPropVal(ICalTok.METHOD, ICalTok.PUBLISH.toString()).toUpperCase();
            if (components != null) {
                flags |= Flag.BITMASK_INVITE;
                if (ICalTok.PUBLISH.toString().equals(methodStr) && !acct.getBooleanAttr(Provisioning.A_zimbraPrefCalendarAllowPublishMethodInvite, false)) {
                    // If method is PUBLISH, we don't process the calendar part.  Mark it as
                    // a regular attachment instead.
                    flags |= Flag.BITMASK_ATTACHED;
                    components = null;
                }
            }
        } catch (Exception e) {
            ZimbraLog.calendar.warn("Unable to process iCalendar attachment", e);
        }
    }
    // make sure the received date is not negative or in the future
    long date = pm.getReceivedDate(), now = System.currentTimeMillis();
    if (date < 0 || date > now) {
        date = now;
    }
    UnderlyingData data = new UnderlyingData();
    data.id = id;
    data.type = fact.getType().toByte();
    if (conv != null) {
        data.parentId = conv.getId();
    }
    data.folderId = folder.getId();
    data.indexId = !folder.inSpam() || acct.isJunkMessagesIndexingEnabled() ? IndexStatus.DEFERRED.id() : IndexStatus.DONE.id();
    data.locator = staged.getLocator();
    data.imapId = id;
    data.date = (int) (date / 1000);
    data.size = staged.getSize();
    data.setBlobDigest(staged.getDigest());
    data.setFlags(flags & (Flag.FLAGS_MESSAGE | Flag.FLAGS_GENERIC));
    data.setTags(ntags);
    data.setSubject(pm.getNormalizedSubject());
    data.metadata = encodeMetadata(DEFAULT_COLOR_RGB, 1, 1, extended, pm, pm.getFragment(acct.getLocale()), dinfo, null, null).toString();
    data.unreadCount = unread ? 1 : 0;
    data.contentChanged(mbox);
    ZimbraLog.mailop.info("Adding Message: id=%d, Message-ID=%s, parentId=%d, folderId=%d, folderName=%s acct=%s.", data.id, pm.getMessageID(), data.parentId, folder.getId(), folder.getName(), mbox.getAccountId());
    new DbMailItem(mbox).setSender(pm.getParsedSender().getSortString()).setRecipients(ParsedAddress.getSortString(pm.getParsedRecipients())).create(data);
    Message msg = fact.create(mbox, data);
    // process the components in this invite (must do this last so blob is created, etc)
    if (components != null) {
        try {
            msg.processInvitesAfterCreate(methodStr, folder.getId(), !noICal, pm, components);
        } catch (Exception e) {
            ZimbraLog.calendar.warn("Unable to process iCalendar attachment", e);
        }
    }
    msg.finishCreation(conv);
    return msg;
}
Also used : Account(com.zimbra.cs.account.Account) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) DbMailItem(com.zimbra.cs.db.DbMailItem) Invite(com.zimbra.cs.mailbox.calendar.Invite) MessagingException(javax.mail.MessagingException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException)

Example 18 with DbMailItem

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

the class Message method reanalyze.

@Override
void reanalyze(Object data, long newSize) throws ServiceException {
    if (!(data instanceof ParsedMessage)) {
        throw ServiceException.FAILURE("cannot reanalyze non-ParsedMessage object", null);
    }
    Account acct = getAccount();
    ParsedMessage pm = (ParsedMessage) data;
    MailItem parent = getParent();
    // make sure the SUBJECT is correct
    if (!getSubject().equals(pm.getSubject())) {
        markItemModified(Change.SUBJECT);
    }
    rawSubject = pm.getSubject();
    mData.setSubject(pm.getNormalizedSubject());
    markItemModified(Change.METADATA);
    // recipients may have changed
    recipients = pm.getRecipients();
    // the fragment may have changed
    fragment = pm.getFragment(acct.getLocale());
    // make sure the "attachments" FLAG is correct
    boolean hadAttachment = mData.isSet(Flag.FlagInfo.ATTACHED);
    mData.unsetFlag(Flag.FlagInfo.ATTACHED);
    if (pm.hasAttachments()) {
        mData.setFlag(Flag.FlagInfo.ATTACHED);
    }
    if (hadAttachment != pm.hasAttachments()) {
        markItemModified(Change.FLAGS);
        parent.tagChanged(mMailbox.getFlagById(Flag.ID_ATTACHED), pm.hasAttachments());
    }
    // make sure the "urgency" FLAGs are correct
    int oldUrgency = mData.getFlags() & (Flag.BITMASK_HIGH_PRIORITY | Flag.BITMASK_LOW_PRIORITY);
    int urgency = pm.getPriorityBitmask();
    mData.unsetFlag(Flag.FlagInfo.HIGH_PRIORITY);
    mData.unsetFlag(Flag.FlagInfo.LOW_PRIORITY);
    mData.setFlags(mData.getFlags() | urgency);
    if (oldUrgency != urgency) {
        markItemModified(Change.FLAGS);
        if (urgency == Flag.BITMASK_HIGH_PRIORITY || oldUrgency == Flag.BITMASK_HIGH_PRIORITY) {
            parent.tagChanged(mMailbox.getFlagById(Flag.ID_HIGH_PRIORITY), urgency == Flag.BITMASK_HIGH_PRIORITY);
        }
        if (urgency == Flag.BITMASK_LOW_PRIORITY || oldUrgency == Flag.BITMASK_LOW_PRIORITY) {
            parent.tagChanged(mMailbox.getFlagById(Flag.ID_LOW_PRIORITY), urgency == Flag.BITMASK_LOW_PRIORITY);
        }
    }
    // update the SIZE and METADATA
    if (mData.size != newSize) {
        markItemModified(Change.SIZE);
        mMailbox.updateSize(newSize - mData.size, false);
        getFolder().updateSize(0, 0, newSize - mData.size);
        mData.size = newSize;
    }
    // rewrite the DB row to reflect our new view
    saveData(new DbMailItem(mMailbox), encodeMetadata(mRGBColor, mMetaVersion, mVersion, mExtendedData, pm, fragment, draftInfo, calendarItemInfos, calendarIntendedFor));
    if (parent instanceof VirtualConversation) {
        ((VirtualConversation) parent).recalculateMetadata(Collections.singletonList(this));
    }
}
Also used : Account(com.zimbra.cs.account.Account) DbMailItem(com.zimbra.cs.db.DbMailItem) DbMailItem(com.zimbra.cs.db.DbMailItem) ParsedMessage(com.zimbra.cs.mime.ParsedMessage)

Example 19 with DbMailItem

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

the class Mountpoint method create.

/**
 * Creates a new Mountpoint pointing at a remote item and persists it
 *  to the database.  A real nonnegative item ID must be supplied from
 *  a previous call to {@link Mailbox#getNextItemId(int)}.
 *
 * @param id        The id for the new mountpoint.
 * @param parent    The folder to place the new mountpoint in.
 * @param name      The new mountpoint's name.
 * @param ownerId   The remote mailbox's owner's <code>zimbraId</code>.
 * @param remoteId  The remote item's numeric id.
 * @param view      The (optional) default object type for the folder.
 * @param flags     Folder flags (e.g. {@link Flag#BITMASK_CHECKED}).
 * @param color     The new mountpoint's color.
 * @param reminderEnabled Whether calendar reminders are enabled
 * @param custom    An optional extra set of client-defined metadata.
 * @perms {@link ACL#RIGHT_INSERT} on the parent folder
 * @throws ServiceException   The following error codes are possible:<ul>
 *    <li><code>mail.CANNOT_CONTAIN</code> - if the target folder
 *        can't contain mountpoints
 *    <li><code>mail.ALREADY_EXISTS</code> - if a folder by that name
 *        already exists in the parent folder
 *    <li><code>mail.INVALID_NAME</code> - if the new folder's name is
 *        invalid
 *    <li><code>mail.INVALID_REQUEST</code> - if the <code>ownerId</code>
 *        or <code>remoteId</code> are invalid
 *    <li><code>service.FAILURE</code> - if there's a database failure
 *    <li><code>service.PERM_DENIED</code> - if you don't have
 *        sufficient permissions</ul>
 * @see #validateItemName(String)
 * @see #canContain(byte)
 */
static Mountpoint create(int id, String uuid, Folder parent, String name, String ownerId, int remoteId, String remoteUuid, Type view, int flags, Color color, boolean reminderEnabled, CustomMetadata custom) throws ServiceException {
    if (parent == null || ownerId == null || remoteId <= 0) {
        throw ServiceException.INVALID_REQUEST("invalid parameters when creating mountpoint", null);
    }
    if (!parent.canAccess(ACL.RIGHT_INSERT)) {
        throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the parent folder");
    }
    if (!parent.canContain(Type.MOUNTPOINT)) {
        throw MailServiceException.CANNOT_CONTAIN();
    }
    name = validateItemName(name);
    if (parent.findSubfolder(name) != null) {
        throw MailServiceException.ALREADY_EXISTS(name);
    }
    Mailbox mbox = parent.getMailbox();
    UnderlyingData data = new UnderlyingData();
    data.uuid = uuid;
    data.id = id;
    data.type = Type.MOUNTPOINT.toByte();
    data.folderId = parent.getId();
    data.parentId = data.folderId;
    data.date = mbox.getOperationTimestamp();
    data.setFlags(flags & Flag.FLAGS_FOLDER);
    data.name = name;
    data.setSubject(name);
    data.metadata = encodeMetadata(color, 1, 1, custom, view, ownerId, remoteId, remoteUuid, reminderEnabled);
    data.contentChanged(mbox);
    ZimbraLog.mailop.info("Adding Mountpoint %s: id=%d, parentId=%d, parentName=%s.", name, data.id, parent.getId(), parent.getName());
    new DbMailItem(mbox).create(data);
    Mountpoint mpt = new Mountpoint(mbox, data);
    mpt.finishCreation(parent);
    return mpt;
}
Also used : DbMailItem(com.zimbra.cs.db.DbMailItem)

Example 20 with DbMailItem

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

the class Note method setContent.

void setContent(String content) throws ServiceException {
    if (!isMutable()) {
        throw MailServiceException.IMMUTABLE_OBJECT(mId);
    }
    if (!canAccess(ACL.RIGHT_WRITE)) {
        throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the note");
    }
    content = StringUtil.stripControlCharacters(content);
    if (Strings.isNullOrEmpty(content)) {
        throw ServiceException.INVALID_REQUEST("notes may not be empty", null);
    }
    if (content.equals(mData.getSubject())) {
        return;
    }
    addRevision(false);
    markItemModified(Change.CONTENT | Change.DATE);
    // XXX: should probably update both mData.size and the Mailbox's size
    mData.setSubject(content);
    mData.date = mMailbox.getOperationTimestamp();
    saveData(new DbMailItem(mMailbox));
}
Also used : DbMailItem(com.zimbra.cs.db.DbMailItem)

Aggregations

DbMailItem (com.zimbra.cs.db.DbMailItem)20 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)4 Account (com.zimbra.cs.account.Account)3 DbTag (com.zimbra.cs.db.DbTag)3 Invite (com.zimbra.cs.mailbox.calendar.Invite)3 ZimbraMailItem (com.zimbra.common.mailbox.ZimbraMailItem)2 ParsedContact (com.zimbra.cs.mime.ParsedContact)2 ParsedDocument (com.zimbra.cs.mime.ParsedDocument)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ConcurrentLinkedHashMap (com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap)1 ZFolder (com.zimbra.client.ZFolder)1 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)1 TimeZoneMap (com.zimbra.common.calendar.TimeZoneMap)1 ServiceException (com.zimbra.common.service.ServiceException)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1 IndexDocument (com.zimbra.cs.index.IndexDocument)1 CustomMetadataList (com.zimbra.cs.mailbox.MailItem.CustomMetadata.CustomMetadataList)1 UnderlyingData (com.zimbra.cs.mailbox.MailItem.UnderlyingData)1 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)1