001: package org.claros.intouch.contacts.controllers;
002:
003: import java.util.List;
004: import java.util.Locale;
005:
006: import javax.mail.internet.InternetAddress;
007:
008: import org.claros.commons.auth.models.AuthProfile;
009: import org.claros.commons.exception.NoPermissionException;
010: import org.claros.intouch.common.utility.Constants;
011: import org.claros.intouch.common.utility.Utility;
012: import org.claros.intouch.contacts.models.Contact;
013: import org.claros.intouch.preferences.controllers.UserPrefsController;
014:
015: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
016: import com.jenkov.mrpersister.itf.IGenericDao;
017: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
018: import com.jenkov.mrpersister.util.JdbcUtil;
019:
020: /**
021: * @author Umut Gokbayrak
022: */
023: public class ContactsController {
024:
025: /**
026: *
027: * @param auth
028: * @param prefix
029: * @return
030: * @throws Exception
031: */
032: public static List getContactsByNamePrefix(AuthProfile auth,
033: String prefix) throws Exception {
034: IGenericDao dao = null;
035: List contacts = null;
036: try {
037: dao = Utility.getDbConnection();
038: String username = auth.getUsername();
039:
040: String displayType = UserPrefsController.getUserSetting(
041: auth, "displayType");
042: String sql = null;
043: if (displayType == null) {
044: displayType = Constants.DISPLAY_TYPE_NAME_FIRST;
045: }
046:
047: if (prefix.equals("ALL")) {
048: prefix = "";
049: }
050: if (displayType == null
051: || displayType
052: .equals(Constants.DISPLAY_TYPE_NAME_FIRST)) {
053: sql = "SELECT * FROM CONTACTS WHERE USERNAME=? AND UPPER(FIRST_NAME) LIKE '"
054: + prefix + "%'";
055: } else if (displayType
056: .equals(Constants.DISPLAY_TYPE_SURNAME_FIRST)) {
057: sql = "SELECT * FROM CONTACTS WHERE USERNAME=? AND UPPER(LAST_NAME) LIKE '"
058: + prefix + "%'";
059: }
060: contacts = dao.readList(Contact.class, sql,
061: new Object[] { username });
062: contacts = org.claros.intouch.contacts.utility.Utility
063: .sortContacts(contacts, displayType
064: .equals(Constants.DISPLAY_TYPE_NAME_FIRST));
065: } finally {
066: JdbcUtil.close(dao);
067: dao = null;
068: }
069: return contacts;
070: }
071:
072: /**
073: * @param auth
074: * @param contact
075: */
076: public static void saveContact(AuthProfile auth, Contact contact)
077: throws Exception {
078: IGenericDao dao = null;
079: try {
080: dao = Utility.getDbConnection();
081:
082: Long id = contact.getId();
083: if (id == null) {
084: // it is an insert
085: IObjectMappingKey myObj = Constants.persistMan
086: .getObjectMappingFactory().createInstance(
087: Contact.class,
088: new AutoGeneratedColumnsMapper(true));
089: dao.insert(myObj, contact);
090: } else {
091: // it is an update
092: Contact tmp = getContactById(auth, contact.getId());
093: if (!tmp.getUsername().equals(auth.getUsername())) {
094: throw new NoPermissionException();
095: }
096: dao.update(contact);
097: }
098: } finally {
099: JdbcUtil.close(dao);
100: dao = null;
101: }
102: }
103:
104: /**
105: *
106: * @param auth
107: * @param id
108: * @return
109: * @throws Exception
110: */
111: public static Contact getContactById(AuthProfile auth, Long id)
112: throws Exception {
113: IGenericDao dao = null;
114: Contact result = null;
115: try {
116: dao = Utility.getDbConnection();
117: result = (Contact) dao.readByPrimaryKey(Contact.class, id);
118:
119: if (!result.getUsername().equals(auth.getUsername())) {
120: throw new NoPermissionException();
121: }
122: } finally {
123: JdbcUtil.close(dao);
124: dao = null;
125: }
126: return result;
127: }
128:
129: /**
130: *
131: * @param auth
132: * @param id
133: * @throws Exception
134: */
135: public static void deleteContact(AuthProfile auth, Long id)
136: throws Exception {
137: Contact tmp = getContactById(auth, id);
138: if (!tmp.getUsername().equals(auth.getUsername())) {
139: throw new NoPermissionException();
140: }
141:
142: IGenericDao dao = null;
143: try {
144: dao = Utility.getDbConnection();
145: dao.deleteByPrimaryKey(Contact.class, id);
146: } catch (Exception e) {
147: // do nothing sier
148: } finally {
149: JdbcUtil.close(dao);
150: dao = null;
151: }
152: }
153:
154: /**
155: * @param auth
156: * @param address
157: * @param company
158: * @param email
159: * @param name
160: * @param phone
161: * @return
162: */
163: public static List searchContacts(AuthProfile auth, String address,
164: String company, String email, String name, String phone)
165: throws Exception {
166: IGenericDao dao = null;
167: List contacts = null;
168: try {
169: dao = Utility.getDbConnection();
170: String username = auth.getUsername();
171:
172: String sql = "SELECT * FROM CONTACTS WHERE USERNAME=? ";
173:
174: // address conditions
175: if (address != null && address.trim().length() != 0) {
176: sql += "AND (" + " upper(HOME_ADDRESS) like '%"
177: + address + "%' or "
178: + " upper(HOME_CITY) like '%" + address
179: + "%' or " + " upper(HOME_PROVINCE) like '%"
180: + address + "%' or " + " HOME_ZIP like '%"
181: + address + "%' or "
182: + " upper(HOME_COUNTRY) like '%" + address
183: + "%' or " + " upper(WORK_ADDRESS) like '%"
184: + address + "%' or "
185: + " upper(WORK_CITY) like '%" + address
186: + "%' or " + " upper(WORK_PROVINCE) like '%"
187: + address + "%' or " + " WORK_ZIP like '%"
188: + address + "%' or "
189: + " upper(WORK_COUNTRY) like '%" + address
190: + "%') ";
191: }
192:
193: // company criteria
194: if (company != null && company.trim().length() != 0) {
195: sql += "or upper(WORK_COMPANY) like '%" + company
196: + "%' ";
197: }
198:
199: // search emails
200: if (email != null && email.trim().length() != 0) {
201: sql += "or (" + " upper(EMAIL_PRIMARY) like '%" + email
202: + "%' or " + " upper(EMAIL_ALTERNATE) like '%"
203: + email + "%') ";
204: }
205:
206: // search names
207: if (name != null && name.trim().length() != 0) {
208: sql += "or (" + " upper(FIRST_NAME) like '%" + name
209: + "%' or " + " upper(MIDDLE_NAME) like '%"
210: + name + "%' or " + " upper(LAST_NAME) like '%"
211: + name + "%') ";
212: }
213:
214: // search phones
215: if (phone != null && phone.trim().length() != 0) {
216: sql += "or (" + " GSM_NO_PRIMARY like '%" + phone
217: + "%' or " + " GSM_NO_ALTERNATE like '%"
218: + phone + "%' or " + " HOME_PHONE like '%"
219: + phone + "%' or " + " WORK_PHONE like '%"
220: + phone + "%') ";
221: }
222:
223: contacts = dao.readList(Contact.class, sql,
224: new Object[] { username });
225:
226: String displayType = UserPrefsController.getUserSetting(
227: auth, "displayType");
228: if (displayType == null) {
229: displayType = Constants.DISPLAY_TYPE_NAME_FIRST;
230: }
231:
232: contacts = org.claros.intouch.contacts.utility.Utility
233: .sortContacts(contacts, displayType
234: .equals(Constants.DISPLAY_TYPE_NAME_FIRST));
235: } finally {
236: JdbcUtil.close(dao);
237: dao = null;
238: }
239: return contacts;
240: }
241:
242: /**
243: * @param auth
244: * @param address
245: * @param company
246: * @param email
247: * @param name
248: * @param phone
249: * @return
250: */
251: public static List searchContactsByNameEmailNick(String username,
252: String str, boolean onlyWithEmail) throws Exception {
253: IGenericDao dao = null;
254: List contacts = null;
255: try {
256: dao = Utility.getDbConnection();
257:
258: String sql = "SELECT * FROM CONTACTS WHERE USERNAME=? ";
259:
260: // search emails
261: if (str != null && str.trim().length() != 0) {
262: str = str.toUpperCase();
263:
264: sql += "and (" + " upper(EMAIL_PRIMARY) like '%" + str
265: + "%' or " + " upper(EMAIL_ALTERNATE) like '%"
266: + str + "%' or " + " upper(FIRST_NAME) like '%"
267: + str + "%' or "
268: + " upper(MIDDLE_NAME) like '%" + str
269: + "%' or " + " upper(NICK_NAME) like '%" + str
270: + "%' or " + " upper(LAST_NAME) like '%" + str
271: + "%' " + " ) ";
272: }
273: if (onlyWithEmail) {
274: sql += "and EMAIL_PRIMARY like '%@%' ";
275: }
276: contacts = dao.readList(Contact.class, sql,
277: new Object[] { username });
278: contacts = org.claros.intouch.contacts.utility.Utility
279: .sortContacts(contacts, true);
280: } finally {
281: JdbcUtil.close(dao);
282: dao = null;
283: }
284: return contacts;
285: }
286:
287: /**
288: * @param auth
289: * @param address
290: * @param company
291: * @param email
292: * @param name
293: * @param phone
294: * @return
295: */
296: public static Contact searchContactByNick(String username,
297: String str) throws Exception {
298: IGenericDao dao = null;
299: List contacts = null;
300: Contact contact = null;
301: try {
302: dao = Utility.getDbConnection();
303: String sql = "SELECT * FROM CONTACTS WHERE USERNAME=? AND upper(NICK_NAME) = ? ";
304: contacts = dao
305: .readList(Contact.class, sql, new Object[] {
306: username,
307: str.toUpperCase(new Locale("en", "US")) });
308: if (contacts.size() == 1) {
309: contact = (Contact) contacts.get(0);
310: }
311: } finally {
312: JdbcUtil.close(dao);
313: dao = null;
314: }
315: return contact;
316: }
317:
318: /**
319: *
320: * @param username
321: * @param email
322: * @return
323: * @throws Exception
324: */
325: public static Contact searchContactByEmail(String username,
326: String email) throws Exception {
327: IGenericDao dao = null;
328: List contacts = null;
329: Contact contact = null;
330: try {
331: dao = Utility.getDbConnection();
332: String sql = "SELECT * FROM CONTACTS WHERE USERNAME=? AND ("
333: + " upper(EMAIL_PRIMARY) = ? or "
334: + " upper(EMAIL_ALTERNATE) = ?)";
335: email = email.toUpperCase(new Locale("en", "US"));
336: contacts = dao.readList(Contact.class, sql, new Object[] {
337: username, email, email });
338: if (contacts.size() == 1) {
339: contact = (Contact) contacts.get(0);
340: }
341: } finally {
342: JdbcUtil.close(dao);
343: dao = null;
344: }
345: return contact;
346: }
347:
348: /**
349: *
350: * @param username
351: * @param emailAddress
352: * @return
353: * @throws Exception
354: */
355: public static boolean searchContactExistsByEmail(String username,
356: String emailAddress) throws Exception {
357: IGenericDao dao = null;
358: List contacts = null;
359: try {
360: dao = Utility.getDbConnection();
361: String sql = "SELECT * FROM CONTACTS WHERE USERNAME=? AND ("
362: + " lower(EMAIL_PRIMARY) = ? or "
363: + " lower(EMAIL_ALTERNATE) = ?)";
364: String tmpEmail = emailAddress.toLowerCase(new Locale("en",
365: "US"));
366: contacts = dao.readList(Contact.class, sql, new Object[] {
367: username, tmpEmail, tmpEmail });
368: } finally {
369: JdbcUtil.close(dao);
370: dao = null;
371: }
372:
373: if (contacts == null || contacts.size() == 0) {
374: return false;
375: } else {
376: return true;
377: }
378: }
379:
380: /**
381: *
382: * @param auth
383: * @param adr
384: * @throws Exception
385: */
386: public static void saveSenderFromAddr(AuthProfile auth,
387: InternetAddress adr) throws Exception {
388:
389: String fullName = org.claros.commons.utility.Utility
390: .doCharsetCorrections(adr.getPersonal());
391: String emailAddress = org.claros.commons.utility.Utility
392: .doCharsetCorrections(adr.getAddress());
393:
394: // if this e-mail previously exists omit it. so let's search for it first.
395: if (!searchContactExistsByEmail(auth.getUsername(),
396: emailAddress)) {
397: // save it.
398: if (fullName != null) {
399: fullName = fullName.trim();
400: }
401:
402: int firstPos = fullName.indexOf(" ");
403: String firstName = fullName;
404: if (firstPos > 0) {
405: firstName = fullName.substring(0, firstPos).trim();
406: }
407:
408: int lastPos = fullName.lastIndexOf(" ");
409: String lastName = "";
410: if (lastPos > 0) {
411: lastName = fullName.substring(lastPos).trim();
412: }
413: String middleName = "";
414: if (firstPos > 0 && lastPos > 0 && firstPos != lastPos) {
415: middleName = fullName.substring(firstPos, lastPos)
416: .trim();
417: }
418: if (firstName == null || firstName.equals("")) {
419: firstName = emailAddress.substring(0, emailAddress
420: .indexOf("@"));
421: }
422:
423: Contact contact = new Contact();
424: contact.setFirstName(firstName);
425: contact.setMiddleName(middleName);
426: contact.setLastName(lastName);
427: contact.setEmailPrimary(emailAddress);
428: contact.setUsername(auth.getUsername());
429: ContactsController.saveContact(auth, contact);
430: }
431: }
432: }
|