001: package org.claros.intouch.webmail.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.List;
006:
007: import javax.servlet.ServletException;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010:
011: import org.claros.intouch.common.services.BaseService;
012: import org.claros.intouch.common.utility.Constants;
013: import org.claros.intouch.contacts.controllers.ContactsController;
014: import org.claros.intouch.contacts.models.Contact;
015: import org.claros.intouch.contacts.utility.Utility;
016: import org.claros.intouch.preferences.controllers.UserPrefsController;
017:
018: public class AutoCompleteService extends BaseService {
019:
020: /**
021: *
022: */
023: private static final long serialVersionUID = -3418640902654581164L;
024:
025: /**
026: * The doGet method of the servlet. <br>
027: *
028: * This method is called when a form has its tag value method equals to get.
029: *
030: * @param request the request send by the client to the server
031: * @param response the response send by the server to the client
032: * @throws ServletException if an error occurred
033: * @throws IOException if an error occurred
034: */
035: public void doGet(HttpServletRequest request,
036: HttpServletResponse response) throws ServletException,
037: IOException {
038:
039: response.setHeader("Expires", "-1");
040: response.setHeader("Pragma", "no-cache");
041: response.setHeader("Cache-control", "no-cache");
042: response.setHeader("Content-Type", "text/html; charset=utf-8");
043: PrintWriter out = response.getWriter();
044:
045: String name = new String(request.getParameter("query")
046: .getBytes(Constants.charset), "utf-8");
047:
048: try {
049:
050: String displayType = UserPrefsController.getUserSetting(
051: getAuthProfile(request), "displayType");
052: if (displayType == null) {
053: displayType = Constants.DISPLAY_TYPE_NAME_FIRST;
054: }
055: boolean nameFirst = true;
056: if (!displayType.equals(Constants.DISPLAY_TYPE_NAME_FIRST)) {
057: nameFirst = false;
058: }
059:
060: List contacts = ContactsController
061: .searchContactsByNameEmailNick(getAuthProfile(
062: request).getUsername(), name, true);
063: if (contacts != null) {
064: String fn = null;
065: Contact tmp = null;
066: for (int i = 0; i < contacts.size(); i++) {
067: tmp = (Contact) contacts.get(i);
068: fn = Utility.getFullName(tmp, nameFirst);
069: fn = org.claros.commons.utility.Utility
070: .replaceAllOccurances(fn, ",", "");
071: fn = org.claros.commons.utility.Utility
072: .replaceAllOccurances(fn, ";", "");
073: if (fn == null || fn.length() == 0) {
074: out.println(tmp.getEmailPrimary());
075: } else {
076: if (tmp.getSex() == null) {
077: tmp.setSex("N");
078: }
079: out.println(fn + " <" + tmp.getEmailPrimary()
080: + ">\t" + tmp.getSex());
081: }
082: }
083: }
084: } catch (Exception e) {
085: e.printStackTrace();
086: }
087: }
088:
089: /**
090: * The doPost method of the servlet. <br>
091: *
092: * This method is called when a form has its tag value method equals to post.
093: *
094: * @param request the request send by the client to the server
095: * @param response the response send by the server to the client
096: * @throws ServletException if an error occurred
097: * @throws IOException if an error occurred
098: */
099: public void doPost(HttpServletRequest request,
100: HttpServletResponse response) throws ServletException,
101: IOException {
102: doGet(request, response);
103: }
104:
105: }
|