001: package org.claros.intouch.contacts.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005:
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: import org.claros.intouch.common.services.BaseService;
011: import org.claros.intouch.common.utility.Constants;
012: import org.claros.intouch.contacts.controllers.ContactsController;
013: import org.claros.intouch.contacts.models.Contact;
014: import org.claros.intouch.contacts.utility.Utility;
015:
016: public class ExportVcardService extends BaseService {
017:
018: /**
019: *
020: */
021: private static final long serialVersionUID = 4818273833984599431L;
022:
023: /**
024: * The doGet method of the servlet. <br>
025: *
026: * This method is called when a form has its tag value method equals to get.
027: *
028: * @param request the request send by the client to the server
029: * @param response the response send by the server to the client
030: * @throws ServletException if an error occurred
031: * @throws IOException if an error occurred
032: */
033: public void doGet(HttpServletRequest request,
034: HttpServletResponse response) throws ServletException,
035: IOException {
036:
037: response.setHeader("Expires", "-1");
038: response.setHeader("Pragma", "no-cache");
039: response.setHeader("Cache-control", "no-cache");
040: response.setHeader("Content-Type", "text/x-vcard; charset="
041: + Constants.charset);
042:
043: String sId = request.getParameter("id");
044: PrintWriter out = response.getWriter();
045:
046: try {
047: Contact contact = ContactsController.getContactById(
048: getAuthProfile(request), new Long(sId));
049:
050: String fullName = Utility.getFullName(contact, true);
051: String fullNameHeader = fullName.replace(' ', '_');
052: response.setHeader("Content-disposition",
053: "attachment; filename=" + fullNameHeader + ".vcf");
054:
055: out.println("BEGIN:VCARD");
056: out.println("VERSION:3.0");
057: out.println("FN:" + fullName);
058:
059: String middleName = contact.getMiddleName();
060: if (middleName != null && !middleName.trim().equals("")) {
061: middleName = " " + middleName;
062: } else {
063: middleName = "";
064: }
065:
066: out.println("N:" + contact.getFirstName() + middleName
067: + ";" + contact.getLastName());
068: out.println("ORG:" + getInfo(contact.getWorkCompany()));
069: out.println("ADR;TYPE=WORK,POSTAL,PARCEL:;;"
070: + getInfo(contact.getWorkAddress()) + ";"
071: + getInfo(contact.getWorkCity()) + ";"
072: + getInfo(contact.getWorkProvince()) + ";"
073: + getInfo(contact.getWorkZip()) + ";"
074: + getInfo(contact.getWorkCountry()));
075: out.println("ADR;TYPE=HOME,POSTAL,PARCEL:;;"
076: + getInfo(contact.getHomeAddress()) + ";"
077: + getInfo(contact.getHomeCity()) + ";"
078: + getInfo(contact.getHomeProvince()) + ";"
079: + getInfo(contact.getHomeZip()) + ";"
080: + getInfo(contact.getHomeCountry()));
081: out.println("TEL;TYPE=VOICE,MSG,WORK:"
082: + getInfo(contact.getWorkPhone()));
083: out.println("TEL;TYPE=VOICE,MSG,HOME:"
084: + getInfo(contact.getHomePhone()));
085: out.println("TEL;TYPE=FAX,WORK:"
086: + getInfo(contact.getWorkFaks()));
087: out.println("TEL;TYPE=FAX,HOME:"
088: + getInfo(contact.getHomeFaks()));
089: out.println("EMAIL;TYPE=INTERNET,PREF:"
090: + getInfo(contact.getEmailPrimary()));
091: out.println("EMAIL;TYPE=INTERNET:"
092: + getInfo(contact.getEmailAlternate()));
093: out.println("URL:" + contact.getWebPage());
094: out.println("END:VCARD");
095:
096: } catch (Exception e) {
097: e.printStackTrace();
098: }
099:
100: /*
101: BEGIN:VCARD
102: VERSION:3.0
103: FN:Frank Dawson
104: N:Frank;Dawson
105: ORG:Lotus Development Corporation
106: ADR;TYPE=WORK,POSTAL,PARCEL:;;6544 Battleford Drive
107: ;Raleigh;NC;27613-3502;U.S.A.
108: TEL;TYPE=VOICE,MSG,WORK:+1-919-676-9515
109: TEL;TYPE=FAX,WORK:+1-919-676-9564
110: EMAIL;TYPE=INTERNET,PREF:Frank_Dawson@Lotus.com
111: EMAIL;TYPE=INTERNET:fdawson@earthlink.net
112: URL:http://home.earthlink.net/~fdawson
113: END:VCARD
114: */
115: }
116:
117: private static String getInfo(String str) {
118: if (str == null)
119: return " ";
120: if (str.equals(""))
121: return " ";
122: return str;
123: }
124:
125: }
|