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;
}
Aggregations