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