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