Search in sources :

Example 1 with DbMailItem

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

the class SearchFolder method create.

/**
 * Creates a new SearchFolder 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 search folder.
 * @param parent  The parent folder to place the new folder in.
 * @param name    The new folder's name.
 * @param query   The query associated with the search folder.
 * @param types   The (optional) set of item types the search returns.
 * @param sort    The (optional) order the results are returned in.
 * @param color   The new folder's color.
 * @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 search folders
 *    <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 supplied query
 *        string is blank or missing
 *    <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 #validateQuery(String)
 * @see #canContain(byte)
 */
static SearchFolder create(int id, String uuid, Folder parent, String name, String query, String types, String sort, int flags, Color color, CustomMetadata custom) throws ServiceException {
    if (parent == null || !parent.canContain(Type.SEARCHFOLDER)) {
        throw MailServiceException.CANNOT_CONTAIN();
    }
    if (!parent.canAccess(ACL.RIGHT_INSERT)) {
        throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the parent folder");
    }
    name = validateItemName(name);
    query = validateQuery(query);
    if (parent.findSubfolder(name) != null) {
        throw MailServiceException.ALREADY_EXISTS(name);
    }
    if (types != null && types.trim().equals("")) {
        types = null;
    }
    if (sort != null && sort.trim().equals("")) {
        sort = null;
    }
    Mailbox mbox = parent.getMailbox();
    UnderlyingData data = new UnderlyingData();
    data.uuid = uuid;
    data.id = id;
    data.type = Type.SEARCHFOLDER.toByte();
    data.folderId = parent.getId();
    data.parentId = parent.getId();
    data.date = mbox.getOperationTimestamp();
    data.setFlags((flags | Flag.toBitmask(mbox.getAccount().getDefaultFolderFlags())) & Flag.FLAGS_FOLDER);
    data.name = name;
    data.setSubject(name);
    data.metadata = encodeMetadata(color, 1, 1, custom, query, types, sort);
    data.contentChanged(mbox);
    ZimbraLog.mailop.info("Adding SearchFolder %s: id=%d, parentId=%d, parentName=%s.", name, data.id, parent.getId(), parent.getName());
    new DbMailItem(mbox).create(data);
    SearchFolder search = new SearchFolder(mbox, data);
    search.finishCreation(parent);
    return search;
}
Also used : DbMailItem(com.zimbra.cs.db.DbMailItem)

Example 2 with DbMailItem

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

the class WikiItem method create.

static WikiItem create(int id, String uuid, Folder folder, String wikiword, ParsedDocument pd, CustomMetadata custom) throws ServiceException {
    Metadata meta = new Metadata();
    UnderlyingData data = prepareCreate(Type.WIKI, id, uuid, folder, wikiword, WIKI_CONTENT_TYPE, pd, meta, custom, 0);
    Mailbox mbox = folder.getMailbox();
    data.contentChanged(mbox);
    ZimbraLog.mailop.info("Adding WikiItem %s: id=%d, folderId=%d, folderName=%s.", wikiword, data.id, folder.getId(), folder.getName());
    new DbMailItem(mbox).create(data);
    WikiItem wiki = new WikiItem(mbox, data);
    wiki.finishCreation(null);
    pd.setVersion(wiki.getVersion());
    return wiki;
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) DbMailItem(com.zimbra.cs.db.DbMailItem)

Example 3 with DbMailItem

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

the class Note method create.

/**
 * Creates a new Note 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 note.
 * @param folder    The {@link Folder} to create the note in.
 * @param content   The note's body.
 * @param location  The note's onscreen bounding box.
 * @param color     The note's color.
 * @param custom    An optional extra set of client-defined metadata.
 * @perms {@link ACL#RIGHT_INSERT} on the folder
 * @throws ServiceException   The following error codes are possible:<ul>
 *    <li><code>mail.CANNOT_CONTAIN</code> - if the target folder
 *        can't hold notes
 *    <li><code>mail.INVALID_REQUEST</code> - if the note has no content
 *    <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 Folder#canContain(byte)
 */
static Note create(int id, Folder folder, String content, Rectangle location, Color color, CustomMetadata custom) throws ServiceException {
    if (folder == null || !folder.canContain(Type.NOTE)) {
        throw MailServiceException.CANNOT_CONTAIN();
    }
    if (!folder.canAccess(ACL.RIGHT_INSERT)) {
        throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the folder");
    }
    content = StringUtil.stripControlCharacters(content);
    if (content == null || content.equals("")) {
        throw ServiceException.INVALID_REQUEST("notes may not be empty", null);
    }
    if (location == null) {
        location = new Rectangle();
    }
    Mailbox mbox = folder.getMailbox();
    UnderlyingData data = new UnderlyingData();
    data.id = id;
    data.type = Type.NOTE.toByte();
    data.folderId = folder.getId();
    if (!folder.inSpam() || mbox.getAccount().getBooleanAttr(Provisioning.A_zimbraJunkMessagesIndexingEnabled, false)) {
        data.indexId = IndexStatus.DEFERRED.id();
    }
    data.date = mbox.getOperationTimestamp();
    data.setSubject(content);
    data.metadata = encodeMetadata(color, 1, 1, custom, location);
    data.contentChanged(mbox);
    ZimbraLog.mailop.info("Adding Note: id=%d, folderId=%d, folderName=%s.", data.id, folder.getId(), folder.getName());
    new DbMailItem(mbox).create(data);
    Note note = new Note(mbox, data);
    note.finishCreation(null);
    return note;
}
Also used : DbMailItem(com.zimbra.cs.db.DbMailItem)

Example 4 with DbMailItem

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

the class CalendarItem method create.

static CalendarItem create(int id, Folder folder, int flags, Tag.NormalizedTags ntags, String uid, ParsedMessage pm, Invite firstInvite, long nextAlarm, CustomMetadata custom) throws ServiceException {
    firstInvite.sanitize(false);
    if (!folder.canAccess(ACL.RIGHT_INSERT)) {
        throw ServiceException.PERM_DENIED("you do not have the required rights on the folder");
    }
    if (!firstInvite.isPublic() && !folder.canAccess(ACL.RIGHT_PRIVATE)) {
        throw ServiceException.PERM_DENIED("you do not have permission to create private calendar item in this folder");
    }
    Mailbox mbox = folder.getMailbox();
    if (pm != null && pm.hasAttachments()) {
        firstInvite.setHasAttachment(true);
        flags |= Flag.BITMASK_ATTACHED;
    } else {
        firstInvite.setHasAttachment(false);
        flags &= ~Flag.BITMASK_ATTACHED;
    }
    if (firstInvite.isDraft()) {
        flags |= Flag.BITMASK_DRAFT;
    } else {
        flags &= ~Flag.BITMASK_DRAFT;
    }
    if (firstInvite.isHighPriority()) {
        flags |= Flag.BITMASK_HIGH_PRIORITY;
    } else {
        flags &= ~Flag.BITMASK_HIGH_PRIORITY;
    }
    if (firstInvite.isLowPriority()) {
        flags |= Flag.BITMASK_LOW_PRIORITY;
    } else {
        flags &= ~Flag.BITMASK_LOW_PRIORITY;
    }
    MailItem.Type type = firstInvite.isEvent() ? Type.APPOINTMENT : Type.TASK;
    String sender = null;
    ZOrganizer org = firstInvite.getOrganizer();
    if (org != null) {
        sender = org.getIndexString();
    }
    sender = Strings.nullToEmpty(sender);
    String subject = Strings.nullToEmpty(firstInvite.getName());
    List<Invite> invites = new ArrayList<Invite>();
    invites.add(firstInvite);
    Recurrence.IRecurrence recur = firstInvite.getRecurrence();
    long startTime, endTime;
    if (recur != null) {
        ParsedDateTime dtStart = recur.getStartTime();
        startTime = dtStart != null ? dtStart.getUtcTime() : 0;
        ParsedDateTime dtEnd = recur.getEndTime();
        endTime = dtEnd != null ? dtEnd.getUtcTime() : 0;
    } else {
        ParsedDateTime dtStart = firstInvite.getStartTime();
        startTime = dtStart != null ? dtStart.getUtcTime() : 0;
        ParsedDateTime dtEnd = firstInvite.getEffectiveEndTime();
        endTime = dtEnd != null ? dtEnd.getUtcTime() : startTime;
    }
    Account account = mbox.getAccount();
    firstInvite.updateMyPartStat(account, firstInvite.getPartStat());
    UnderlyingData data = new UnderlyingData();
    data.id = id;
    data.type = type.toByte();
    data.folderId = folder.getId();
    if (!folder.inSpam() || mbox.getAccount().getBooleanAttr(Provisioning.A_zimbraJunkMessagesIndexingEnabled, false)) {
        data.indexId = IndexStatus.DEFERRED.id();
    }
    data.imapId = id;
    data.date = mbox.getOperationTimestamp();
    data.setFlags(flags & (Flag.FLAGS_CALITEM | Flag.FLAGS_GENERIC));
    data.setTags(ntags);
    data.setSubject(subject);
    data.metadata = encodeMetadata(DEFAULT_COLOR_RGB, 1, 1, custom, uid, startTime, endTime, recur, invites, firstInvite.getTimeZoneMap(), new ReplyList(), null);
    data.contentChanged(mbox, false);
    if (!firstInvite.hasRecurId()) {
        ZimbraLog.calendar.info("Adding CalendarItem: id=%d, Message-ID=\"%s\", folderId=%d, subject=\"%s\", UID=%s", data.id, pm != null ? pm.getMessageID() : "(none)", folder.getId(), firstInvite.isPublic() ? firstInvite.getName() : "(private)", firstInvite.getUid());
    } else {
        ZimbraLog.calendar.info("Adding CalendarItem: id=%d, Message-ID=\"%s\", folderId=%d, subject=\"%s\", UID=%s, recurId=%s", data.id, pm != null ? pm.getMessageID() : "(none)", folder.getId(), firstInvite.isPublic() ? firstInvite.getName() : "(private)", firstInvite.getUid(), firstInvite.getRecurId().getDtZ());
    }
    new DbMailItem(mbox).setSender(sender).create(data);
    CalendarItem item = type == Type.APPOINTMENT ? new Appointment(mbox, data) : new Task(mbox, data);
    Invite defInvite = item.getDefaultInviteOrNull();
    if (defInvite != null) {
        Collection<Instance> instances = item.expandInstances(CalendarUtils.MICROSOFT_EPOC_START_MS_SINCE_EPOC, Long.MAX_VALUE, false);
        if (instances.isEmpty()) {
            ZimbraLog.calendar.info("CalendarItem has effectively zero instances: id=%d, folderId=%d, subject=\"%s\", UID=%s ", data.id, folder.getId(), firstInvite.isPublic() ? firstInvite.getName() : "(private)", firstInvite.getUid());
            item.delete();
            throw ServiceException.FORBIDDEN("Recurring series has effectively zero instances");
        }
    }
    // If we're creating an invite during email delivery, always default to NEEDS_ACTION state.
    // If not email delivery, we assume the requesting client knows what it's doing and has set the
    // correct partstat in the invite.
    String defaultPartStat;
    if (mbox.getOperationContext() == null) {
        // octxt == null implies we're in email delivery.  (There needs to be better way to determine this...)
        defaultPartStat = IcalXmlStrMap.PARTSTAT_NEEDS_ACTION;
    } else {
        defaultPartStat = firstInvite.getPartStat();
    }
    item.processPartStat(firstInvite, pm != null ? pm.getMimeMessage() : null, true, defaultPartStat);
    item.finishCreation(null);
    folder.updateHighestMODSEQ();
    if (pm != null) {
        item.createBlob(pm, firstInvite);
    }
    item.mEndTime = item.recomputeRecurrenceEndTime(item.mEndTime);
    if (firstInvite.hasAlarm()) {
        item.recomputeNextAlarm(nextAlarm, false, false);
        item.saveMetadata();
        AlarmData alarmData = item.getAlarmData();
        if (alarmData != null) {
            long newNextAlarm = alarmData.getNextAtBase();
            if (newNextAlarm > 0 && newNextAlarm < item.mStartTime) {
                item.mStartTime = newNextAlarm;
            }
        }
    }
    DbMailItem.addToCalendarItemTable(item);
    Callback cb = getCallback();
    if (cb != null) {
        cb.created(item);
    }
    return item;
}
Also used : IRecurrence(com.zimbra.cs.mailbox.calendar.Recurrence.IRecurrence) Recurrence(com.zimbra.cs.mailbox.calendar.Recurrence) Account(com.zimbra.cs.account.Account) IRecurrence(com.zimbra.cs.mailbox.calendar.Recurrence.IRecurrence) DbMailItem(com.zimbra.cs.db.DbMailItem) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) ArrayList(java.util.ArrayList) DbMailItem(com.zimbra.cs.db.DbMailItem) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 5 with DbMailItem

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

the class Conversation method recalculateMetadata.

SenderList recalculateMetadata(List<Message> msgs) throws ServiceException {
    Collections.sort(msgs, new Message.SortDateAscending());
    markItemModified(RECALCULATE_CHANGE_MASK);
    mEncodedSenders = null;
    mSenderList = new SenderList(msgs);
    mData.size = msgs.size();
    Set<String> tags = new HashSet<String>();
    mData.unreadCount = 0;
    mData.setFlags(0);
    mExtendedData = null;
    for (Message msg : msgs) {
        super.addChild(msg);
        mData.unreadCount += (msg.isUnread() ? 1 : 0);
        mData.setFlags(mData.getFlags() | msg.getInternalFlagBitmask());
        for (String tag : msg.mData.getTags()) {
            tags.add(tag);
        }
        mExtendedData = MetadataCallback.duringConversationAdd(mExtendedData, msg);
    }
    mData.setTags(new Tag.NormalizedTags(tags));
    // need to rewrite the overview metadata
    ZimbraLog.mailbox.debug("resetting metadata: cid=%d,size was=%d is=%d", mId, mData.size, mSenderList.size());
    saveData(new DbMailItem(mMailbox));
    return mSenderList;
}
Also used : ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DbMailItem(com.zimbra.cs.db.DbMailItem) DbTag(com.zimbra.cs.db.DbTag) HashSet(java.util.HashSet)

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