001: package org.claros.intouch.contacts.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.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.claros.commons.auth.models.AuthProfile;
014: import org.claros.intouch.common.services.BaseService;
015: import org.claros.intouch.common.utility.Constants;
016: import org.claros.intouch.contacts.controllers.ContactsController;
017: import org.claros.intouch.contacts.controllers.GroupsController;
018: import org.claros.intouch.contacts.models.Contact;
019: import org.claros.intouch.contacts.models.ContactGroup;
020: import org.claros.intouch.contacts.utility.Utility;
021: import org.claros.intouch.preferences.controllers.UserPrefsController;
022:
023: public class GetListService extends BaseService {
024:
025: /**
026: *
027: */
028: private static final long serialVersionUID = 9052097028337237490L;
029: private static Log log = LogFactory.getLog(GetListService.class);
030:
031: /**
032: * The doGet method of the servlet. <br>
033: *
034: * This method is called when a form has its tag value method equals to get.
035: *
036: * @param request the request send by the client to the server
037: * @param response the response send by the server to the client
038: * @throws ServletException if an error occurred
039: * @throws IOException if an error occurred
040: */
041: public void doGet(HttpServletRequest request,
042: HttpServletResponse response) throws ServletException,
043: IOException {
044:
045: response.setHeader("Expires", "-1");
046: response.setHeader("Pragma", "no-cache");
047: response.setHeader("Cache-control", "no-cache");
048: response.setHeader("Content-Type", "text/html; charset=utf-8");
049:
050: PrintWriter out = response.getWriter();
051: // String charset = Constants.charset;
052:
053: String prefix = request.getParameter("prefix");
054: if (prefix == null || prefix.equals("")) {
055: prefix = "ALL";
056: } else {
057: /*
058: prefix = new String(prefix.getBytes(charset), "utf-8");
059: prefix = prefix.toUpperCase(loc);
060: */
061: prefix = prefix.toUpperCase();
062: }
063: AuthProfile auth = getAuthProfile(request);
064:
065: try {
066: List contacts = ContactsController.getContactsByNamePrefix(
067: auth, prefix);
068: List groups = GroupsController.getGroupsByUser(auth);
069:
070: String displayType = UserPrefsController.getUserSetting(
071: auth, "displayType");
072: if (displayType == null) {
073: displayType = Constants.DISPLAY_TYPE_NAME_FIRST;
074: }
075:
076: boolean nameFirst = true;
077: if (!displayType.equals(Constants.DISPLAY_TYPE_NAME_FIRST)) {
078: nameFirst = false;
079: }
080:
081: if (contacts != null) {
082: Contact tmp = null;
083: String img = null;
084: String fullName = null;
085: String email = null;
086: for (int i = 0; i < contacts.size(); i++) {
087: try {
088: tmp = (Contact) contacts.get(i);
089: if (tmp.getSex() == null
090: || tmp.getSex().equals("")) {
091: img = "unknown";
092: } else if (tmp.getSex().equals("M")) {
093: img = "male";
094: } else if (tmp.getSex().equals("F")) {
095: img = "female";
096: }
097: fullName = Utility.getFullName(tmp, nameFirst);
098: if (0 == fullName.length())
099: fullName = " ";
100: email = tmp.getEmailPrimary();
101: if (0 == email.length())
102: email = " ";
103:
104: out.print("<tr onclick='showContactDetails("
105: + tmp.getId() + ")' id='contact"
106: + tmp.getId() + "'>"
107: + "<td><img src='images/contact-" + img
108: + "-mini.png'></td>" + "<td>"
109: + fullName + "</td>" + "<td>" + email
110: + "</td>" + "</tr>");
111: } catch (Exception e) {
112: log.error("error while parsing contact", e);
113: }
114: }
115: }
116:
117: if (groups != null) {
118: ContactGroup tmp = null;
119: for (int i = 0; i < groups.size(); i++) {
120: tmp = (ContactGroup) groups.get(i);
121:
122: out
123: .print("<tr onclick='showGroupDetails("
124: + tmp.getId()
125: + ")'>"
126: + "<td><img src='images/contact-group-mini.png'></td>"
127: + "<td>" + tmp.getShortName()
128: + "</td>" + "<td> </td>"
129: + "</tr>");
130: }
131: }
132: } catch (Exception e) {
133: log.error("error while fetching contacts/groups", e);
134: }
135: }
136:
137: /**
138: *
139: */
140: public void doPost(HttpServletRequest request,
141: HttpServletResponse response) throws ServletException,
142: IOException {
143: doGet(request, response);
144: }
145:
146: }
|