Search in sources :

Example 11 with Contact

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

the class ZimbraQueryResultsImpl method getZimbraHit.

/**
 * We've got a {@link Mailbox}, a {@link DbSearch.Result} and (optionally) a Lucene Doc...
 * that's everything we need to build a real ZimbraHit.
 *
 * @param doc - Optional, only set if this search had a Lucene part
 */
ZimbraHit getZimbraHit(Mailbox mbox, DbSearch.Result sr, Document doc, DbSearch.FetchMode fetch) {
    MailItem item = null;
    ImapMessage i4msg = null;
    int modseq = -1, parentId = 0;
    switch(fetch) {
        case MAIL_ITEM:
            item = sr.getItem();
            break;
        case IMAP_MSG:
            i4msg = sr.getImapMessage();
            break;
        case MODSEQ:
            modseq = sr.getModSeq();
            break;
        case PARENT:
            parentId = sr.getParentId();
            break;
    }
    ZimbraHit result = null;
    switch(sr.getType()) {
        case CHAT:
        case MESSAGE:
            if (doc != null) {
                result = getMessagePartHit(mbox, sr.getId(), (Message) item, doc, sr.getSortValue());
            } else {
                result = getMessageHit(mbox, sr.getId(), (Message) item, null, sr.getSortValue());
            }
            break;
        case CONTACT:
            result = getContactHit(mbox, sr.getId(), (Contact) item, sr.getSortValue());
            break;
        case NOTE:
            result = getNoteHit(mbox, sr.getId(), (Note) item, sr.getSortValue());
            break;
        case APPOINTMENT:
            result = getAppointmentHit(mbox, sr.getId(), (CalendarItem) item, sr.getSortValue());
            break;
        case TASK:
            result = getTaskHit(mbox, sr.getId(), (Task) item, sr.getSortValue());
            break;
        case DOCUMENT:
        case WIKI:
            result = getDocumentHit(mbox, sr.getId(), (com.zimbra.cs.mailbox.Document) item, doc, sr.getSortValue());
            break;
        default:
            assert (false);
    }
    if (i4msg != null) {
        result.cacheImapMessage(i4msg);
    }
    if (modseq > 0) {
        result.cacheModifiedSequence(modseq);
    }
    if (parentId != 0) {
        result.cacheParentId(parentId);
    }
    return result;
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) MailItem(com.zimbra.cs.mailbox.MailItem) Task(com.zimbra.cs.mailbox.Task) ImapMessage(com.zimbra.cs.imap.ImapMessage) Message(com.zimbra.cs.mailbox.Message) ImapMessage(com.zimbra.cs.imap.ImapMessage) Note(com.zimbra.cs.mailbox.Note) Document(org.apache.lucene.document.Document) Contact(com.zimbra.cs.mailbox.Contact)

Example 12 with Contact

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

the class CreateContact method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    boolean verbose = request.getAttributeBool(MailConstants.A_VERBOSE, true);
    boolean wantImapUid = request.getAttributeBool(MailConstants.A_WANT_IMAP_UID, false);
    boolean wantModSeq = request.getAttributeBool(MailConstants.A_WANT_MODIFIED_SEQUENCE, false);
    Element cn = request.getElement(MailConstants.E_CONTACT);
    ItemId iidFolder = new ItemId(cn.getAttribute(MailConstants.A_FOLDER, DEFAULT_FOLDER), zsc);
    String[] tags = TagUtil.parseTags(cn, mbox, octxt);
    Element vcard = cn.getOptionalElement(MailConstants.E_VCARD);
    List<ParsedContact> pclist;
    if (vcard != null) {
        pclist = parseAttachedVCard(zsc, octxt, mbox, vcard);
    } else {
        pclist = new ArrayList<ParsedContact>(1);
        Pair<Map<String, Object>, List<Attachment>> cdata = parseContact(cn, zsc, octxt);
        pclist.add(new ParsedContact(cdata.getFirst(), cdata.getSecond()));
    }
    if (needToMigrateDlist(zsc)) {
        for (ParsedContact pc : pclist) {
            migrateFromDlist(pc);
        }
    }
    List<Contact> contacts = createContacts(octxt, mbox, iidFolder, pclist, tags);
    Contact con = null;
    if (contacts.size() > 0)
        con = contacts.get(0);
    Element response = zsc.createElement(MailConstants.CREATE_CONTACT_RESPONSE);
    if (con != null) {
        if (verbose) {
            int fields = ToXML.NOTIFY_FIELDS;
            if (wantImapUid) {
                fields |= Change.IMAP_UID;
            }
            if (wantModSeq) {
                fields |= Change.MODSEQ;
            }
            ToXML.encodeContact(response, ifmt, octxt, con, (ContactGroup) null, (Collection<String>) null, /* memberAttrFilter */
            true, /* summary */
            (Collection<String>) null, /* attrFilter */
            fields, (String) null, /* migratedDList */
            false, /* returnHiddenAttrs */
            GetContacts.NO_LIMIT_MAX_MEMBERS, true);
        } else {
            Element contct = response.addNonUniqueElement(MailConstants.E_CONTACT);
            contct.addAttribute(MailConstants.A_ID, con.getId());
            if (wantImapUid) {
                contct.addAttribute(MailConstants.A_IMAP_UID, con.getImapUid());
            }
            if (wantModSeq) {
                contct.addAttribute(MailConstants.A_MODIFIED_SEQUENCE, con.getModifiedSequence());
            }
        }
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) ItemId(com.zimbra.cs.service.util.ItemId) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with Contact

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

the class CreateContact method createContacts.

public static List<Contact> createContacts(OperationContext oc, Mailbox mbox, ItemId iidFolder, List<ParsedContact> list, String[] tags) throws ServiceException {
    List<Contact> toRet = new ArrayList<Contact>();
    mbox.lock.lock();
    try {
        for (ParsedContact pc : list) {
            toRet.add(mbox.createContact(oc, pc, iidFolder.getId(), tags));
        }
    } finally {
        mbox.lock.release();
    }
    return toRet;
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) ArrayList(java.util.ArrayList) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact)

Example 14 with Contact

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

the class CreateContact method parseAttachment.

private static Attachment parseAttachment(Element elt, String name, ZimbraSoapContext zsc, OperationContext octxt, Contact existing) throws ServiceException {
    // check for uploaded attachment
    String attachId = elt.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
    if (attachId != null) {
        if (Contact.isSMIMECertField(name)) {
            elt.setText(parseCertificate(elt, name, zsc, octxt, existing));
            return null;
        } else {
            Upload up = FileUploadServlet.fetchUpload(zsc.getAuthtokenAccountId(), attachId, zsc.getAuthToken());
            UploadDataSource uds = new UploadDataSource(up);
            return new Attachment(new DataHandler(uds), name, (int) up.getSize());
        }
    }
    int itemId = (int) elt.getAttributeLong(MailConstants.A_ID, -1);
    String part = elt.getAttribute(MailConstants.A_PART, null);
    if (itemId != -1 || (part != null && existing != null)) {
        MailItem item = itemId == -1 ? existing : getRequestedMailbox(zsc).getItemById(octxt, itemId, MailItem.Type.UNKNOWN);
        try {
            if (item instanceof Contact) {
                Contact contact = (Contact) item;
                if (part != null && !part.equals("")) {
                    try {
                        int partNum = Integer.parseInt(part) - 1;
                        if (partNum >= 0 && partNum < contact.getAttachments().size()) {
                            Attachment att = contact.getAttachments().get(partNum);
                            return new Attachment(att.getDataHandler(), name, att.getSize());
                        }
                    } catch (NumberFormatException nfe) {
                    }
                    throw ServiceException.INVALID_REQUEST("invalid contact part number: " + part, null);
                } else {
                    VCard vcf = VCard.formatContact(contact);
                    return new Attachment(vcf.getFormatted().getBytes("utf-8"), "text/x-vcard; charset=utf-8", name, vcf.fn + ".vcf");
                }
            } else if (item instanceof Message) {
                Message msg = (Message) item;
                if (part != null && !part.equals("")) {
                    try {
                        MimePart mp = Mime.getMimePart(msg.getMimeMessage(), part);
                        if (mp == null) {
                            throw MailServiceException.NO_SUCH_PART(part);
                        }
                        DataSource ds = new MimePartDataSource(mp);
                        return new Attachment(new DataHandler(ds), name);
                    } catch (MessagingException me) {
                        throw ServiceException.FAILURE("error parsing blob", me);
                    }
                } else {
                    DataSource ds = new MessageDataSource(msg);
                    return new Attachment(new DataHandler(ds), name, (int) msg.getSize());
                }
            } else if (item instanceof Document) {
                Document doc = (Document) item;
                if (part != null && !part.equals("")) {
                    throw MailServiceException.NO_SUCH_PART(part);
                }
                DataSource ds = new DocumentDataSource(doc);
                return new Attachment(new DataHandler(ds), name, (int) doc.getSize());
            }
        } catch (IOException ioe) {
            throw ServiceException.FAILURE("error attaching existing item data", ioe);
        } catch (MessagingException e) {
            throw ServiceException.FAILURE("error attaching existing item data", e);
        }
    }
    return null;
}
Also used : Message(com.zimbra.cs.mailbox.Message) MessagingException(javax.mail.MessagingException) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) Document(com.zimbra.cs.mailbox.Document) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) MimePartDataSource(javax.mail.internet.MimePartDataSource) UploadDataSource(com.zimbra.cs.service.UploadDataSource) MessageDataSource(com.zimbra.cs.mailbox.MessageDataSource) DocumentDataSource(com.zimbra.cs.mailbox.DocumentDataSource) DataSource(javax.activation.DataSource) MailItem(com.zimbra.cs.mailbox.MailItem) MessageDataSource(com.zimbra.cs.mailbox.MessageDataSource) MimePartDataSource(javax.mail.internet.MimePartDataSource) DocumentDataSource(com.zimbra.cs.mailbox.DocumentDataSource) MimePart(javax.mail.internet.MimePart) UploadDataSource(com.zimbra.cs.service.UploadDataSource) VCard(com.zimbra.cs.service.formatter.VCard)

Example 15 with Contact

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

the class ImportContacts method ImportCsvContacts.

public static List<ItemId> ImportCsvContacts(OperationContext oc, Mailbox mbox, ItemId iidFolder, List<Map<String, String>> csvContacts) throws ServiceException {
    List<ItemId> createdIds = new LinkedList<ItemId>();
    for (Map<String, String> contact : csvContacts) {
        String[] tags = TagUtil.decodeTags(ContactCSV.getTags(contact));
        Contact c = mbox.createContact(oc, new ParsedContact(contact), iidFolder.getId(), tags);
        createdIds.add(new ItemId(c));
        ContactGroup.MigrateContactGroup mcg = new ContactGroup.MigrateContactGroup(mbox);
        mcg.migrate(c);
    }
    return createdIds;
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) ContactGroup(com.zimbra.cs.mailbox.ContactGroup) ItemId(com.zimbra.cs.service.util.ItemId) LinkedList(java.util.LinkedList) ImportContact(com.zimbra.soap.mail.type.ImportContact) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact)

Aggregations

Contact (com.zimbra.cs.mailbox.Contact)52 Mailbox (com.zimbra.cs.mailbox.Mailbox)32 ParsedContact (com.zimbra.cs.mime.ParsedContact)26 Test (org.junit.Test)23 OperationContext (com.zimbra.cs.mailbox.OperationContext)13 HashMap (java.util.HashMap)13 ZMailbox (com.zimbra.client.ZMailbox)10 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)10 MailItem (com.zimbra.cs.mailbox.MailItem)10 ItemId (com.zimbra.cs.service.util.ItemId)10 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)9 ZContact (com.zimbra.client.ZContact)8 Folder (com.zimbra.cs.mailbox.Folder)7 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)7 Term (org.apache.lucene.index.Term)7 Element (com.zimbra.common.soap.Element)6 Account (com.zimbra.cs.account.Account)6 Message (com.zimbra.cs.mailbox.Message)6 List (java.util.List)6