001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.lead;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Properties;
023: import java.util.Vector;
024:
025: import javax.mail.Message;
026: import javax.mail.Session;
027: import javax.mail.Transport;
028: import javax.mail.internet.InternetAddress;
029: import javax.mail.internet.MimeMessage;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.base.util.UtilCache;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.base.util.UtilValidate;
035: import org.ofbiz.entity.GenericDelegator;
036: import org.ofbiz.entity.GenericEntityException;
037: import org.ofbiz.entity.GenericPK;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.entity.condition.EntityComparisonOperator;
040: import org.ofbiz.entity.condition.EntityConditionList;
041: import org.ofbiz.entity.condition.EntityExpr;
042: import org.ofbiz.entity.condition.EntityOperator;
043:
044: import com.sourcetap.sfa.security.SecurityLinkInfo;
045: import com.sourcetap.sfa.security.SecurityWrapper;
046: import com.sourcetap.sfa.util.EmailProperties;
047: import com.sourcetap.sfa.util.SimpleExpression;
048: import com.sourcetap.sfa.util.StringHelper;
049: import com.sourcetap.sfa.util.UserInfo;
050:
051: /**
052: *
053: *
054: *
055: */
056: public class LeadHelper {
057: private static LeadHelper globalLeadHelper = null;
058: public static final String module = LeadHelper.class.getName();
059:
060: private GenericDelegator delegator = null;
061:
062: /** Hashtable to cache lead assignment rules.
063: * For each PartyAttributePK there is a String in the cache specifying the preference value.
064: * In this way the cache speeds things up whether or not the user has a particular preference.
065: */
066: public UtilCache leadAssignemtRuleCache = new UtilCache(
067: "LeadAssignmentRuleCache");
068:
069: public LeadHelper() {
070: }
071:
072: public LeadHelper(GenericDelegator delegator) {
073: this .delegator = delegator;
074: }
075:
076: /**
077: * DOCUMENT ME!
078: *
079: * @param emailTo
080: * @param smsTo
081: * @param leadGV
082: *
083: * @return
084: */
085: public static String sendLeadNotifyEmail(String emailTo,
086: String smsTo, GenericValue leadGV) {
087: try {
088: EmailProperties emailProperties = EmailProperties
089: .getInstance();
090:
091: String SMTP_SERVER = emailProperties.relayHost;
092: boolean LEAD_NOTIFY_SMTP_DEBUG = emailProperties.smtpDebug;
093: String LEAD_NOTIFY_LINK_URL = emailProperties.notifyLink;
094: boolean LEAD_NOTIFY_EMAIL_SEND = emailProperties.sendEmail;
095: String LEAD_NOTIFY_EMAIL_FROM = emailProperties.sendEmailFrom;
096: String LEAD_NOTIFY_EMAIL_CC = "";
097: boolean LEAD_NOTIFY_SMS_SEND = emailProperties.sendSMS;
098: String LEAD_NOTIFY_SMS_FROM = emailProperties.sendSMSFrom;
099: String LEAD_NOTIFY_SMS_CC = "";
100:
101: /* final String SMTP_SERVER = UtilProperties.getPropertyValue(sfaPropertiesUrl, "smtp.relay.host");
102: final String LEAD_NOTIFY_SMTP_DEBUG = UtilProperties.getPropertyValue(sfaPropertiesUrl, "lead.notify.smtp.debug");
103: final String LEAD_NOTIFY_LINK_URL = UtilProperties.getPropertyValue(sfaPropertiesUrl, "lead.notify.link.url");
104: final String LEAD_NOTIFY_EMAIL_SEND = UtilProperties.getPropertyValue(sfaPropertiesUrl, "lead.notify.email.send");
105: final String LEAD_NOTIFY_EMAIL_FROM = UtilProperties.getPropertyValue(sfaPropertiesUrl, "lead.notify.email.from");
106: final String LEAD_NOTIFY_EMAIL_CC = UtilProperties.getPropertyValue(sfaPropertiesUrl, "lead.notify.email.cc");
107: final String LEAD_NOTIFY_SMS_SEND = UtilProperties.getPropertyValue(sfaPropertiesUrl, "lead.notify.sms.send");
108: final String LEAD_NOTIFY_SMS_FROM = UtilProperties.getPropertyValue(sfaPropertiesUrl, "lead.notify.sms.from");
109: final String LEAD_NOTIFY_SMS_CC = UtilProperties.getPropertyValue(sfaPropertiesUrl, "lead.notify.sms.cc");
110: */
111: String leadId = (leadGV.getString("leadId") == null) ? ""
112: : leadGV.getString("leadId");
113: String companyName = (leadGV.getString("companyName") == null) ? ""
114: : leadGV.getString("companyName");
115: String firstName = (leadGV.getString("firstName") == null) ? ""
116: : leadGV.getString("firstName");
117: String lastName = (leadGV.getString("lastName") == null) ? ""
118: : leadGV.getString("lastName");
119:
120: try {
121: Properties props = new Properties();
122:
123: props.put("mail.smtp.host", SMTP_SERVER);
124:
125: Session session = Session.getInstance(props, null);
126:
127: if (LEAD_NOTIFY_SMTP_DEBUG) {
128: session.setDebug(true);
129: } else {
130: session.setDebug(false);
131: }
132:
133: if (LEAD_NOTIFY_EMAIL_SEND && !emailTo.equals("")) {
134: try {
135: StringBuffer emailContent = new StringBuffer();
136: emailContent
137: .append("A new sales lead has been assigned to you.<BR>\n");
138: emailContent.append("<BR>\n");
139:
140: if (UtilValidate
141: .isNotEmpty(LEAD_NOTIFY_LINK_URL)) {
142: emailContent
143: .append("Click the link below to view the lead.<BR>\n");
144: emailContent.append("<BR>\n");
145: emailContent.append("<A HREF=\""
146: + LEAD_NOTIFY_LINK_URL + leadId
147: + "\">");
148: }
149:
150: emailContent.append(firstName + " " + lastName);
151:
152: if (!companyName.equals("")) {
153: emailContent.append(" (" + companyName
154: + ")");
155: }
156:
157: if (UtilValidate
158: .isNotEmpty(LEAD_NOTIFY_LINK_URL)) {
159: emailContent.append("</A>\n");
160: }
161:
162: MimeMessage emailMessage = new MimeMessage(
163: session);
164: emailMessage.setFrom(new InternetAddress(
165: LEAD_NOTIFY_EMAIL_FROM));
166: emailMessage.addRecipients(
167: Message.RecipientType.TO, emailTo);
168:
169: if (UtilValidate
170: .isNotEmpty(LEAD_NOTIFY_EMAIL_CC)) {
171: emailMessage.addRecipients(
172: Message.RecipientType.CC,
173: LEAD_NOTIFY_EMAIL_CC);
174: }
175:
176: emailMessage.setSubject("New Sales Lead - "
177: + firstName + " " + lastName);
178: emailMessage
179: .addHeaderLine("MIME-Version: 1.0\nContent-type: text/html; charset=us-ascii\n");
180: emailMessage.setContent(
181: emailContent.toString(), "text/html");
182: Transport.send(emailMessage);
183: } catch (Exception e) {
184: Debug.logError(
185: "Error e-mailing lead notification: "
186: + e.getLocalizedMessage(),
187: "sendLeadNotifyEmail");
188:
189: return "Error e-mailing lead notification: "
190: + e.getLocalizedMessage();
191: }
192: }
193:
194: if (LEAD_NOTIFY_SMS_SEND && !smsTo.equals("")) {
195: try {
196: StringBuffer smsContent = new StringBuffer();
197: smsContent.append(firstName + " " + lastName);
198:
199: if (!companyName.equals("")) {
200: smsContent.append(" (" + companyName + ")");
201: }
202:
203: MimeMessage smsMessage = new MimeMessage(
204: session);
205: smsMessage.setFrom(new InternetAddress(
206: LEAD_NOTIFY_SMS_FROM));
207:
208: if (UtilValidate.isNotEmpty(LEAD_NOTIFY_SMS_CC)) {
209: smsMessage.addRecipients(
210: Message.RecipientType.CC,
211: LEAD_NOTIFY_SMS_CC);
212: }
213:
214: smsMessage.addRecipients(
215: Message.RecipientType.TO, smsTo);
216: smsMessage.setSubject("New Sales Lead");
217: smsMessage.setContent(smsContent.toString(),
218: "text/plain");
219: Transport.send(smsMessage);
220: } catch (Exception e) {
221: Debug.logError(
222: "Error sending sms lead notification: "
223: + e.getLocalizedMessage(),
224: "sendLeadNotifyEmail");
225:
226: return "Error sending sms lead notification: "
227: + e.getLocalizedMessage();
228: }
229: }
230: } catch (Exception e) {
231: Debug.logError(
232: "Error preparing to send lead notification: "
233: + e.getLocalizedMessage(),
234: "sendLeadNotifyEmail");
235:
236: return "Error preparing to send lead notification: "
237: + e.getLocalizedMessage();
238: }
239: } catch (RuntimeException e) {
240: Debug.logError("Error e-mailing lead notification: "
241: + e.getLocalizedMessage(), "sendLeadNotifyEmail");
242:
243: return "Error e-mailing lead notification: "
244: + e.getLocalizedMessage();
245: } catch (Error e) {
246: Debug.logError("Error e-mailing lead notification: "
247: + e.getLocalizedMessage(), module);
248:
249: return "Error e-mailing lead notification: "
250: + e.getLocalizedMessage();
251: }
252:
253: return "success";
254: }
255:
256: /**
257: * DOCUMENT ME!
258: *
259: * @return
260: */
261: public GenericDelegator getDelegator() {
262: return delegator;
263: }
264:
265: /**
266: * DOCUMENT ME!
267: *
268: * @param delegator
269: */
270: public void setDelegator(GenericDelegator delegator) {
271: this .delegator = delegator;
272: }
273:
274: /**
275: * DOCUMENT ME!
276: *
277: * @param delegator
278: *
279: * @return
280: */
281: public static LeadHelper getInstance(GenericDelegator delegator) {
282: if (globalLeadHelper == null) //don't want to block here
283: {
284: synchronized (LeadHelper.class) {
285: //must check if null again as one of the blocked threads can still enter
286: if (globalLeadHelper == null) {
287: globalLeadHelper = new LeadHelper(delegator);
288: }
289: }
290: }
291:
292: return globalLeadHelper;
293: }
294:
295: public List findMatchingAccounts(String leadCompany,
296: UserInfo userInfo) {
297:
298: try {
299: List accountList = SecurityWrapper.findByAnd("Account",
300: UtilMisc
301: .toList(new EntityExpr("accountName",
302: EntityComparisonOperator.LIKE,
303: leadCompany)), UtilMisc
304: .toList("companyName"), userInfo,
305: new SecurityLinkInfo("Account", "accountId", true),
306: delegator);
307:
308: if (accountList.size() < 1) {
309: String companyNameCanon = StringHelper.replaceAll(
310: leadCompany, ",", "");
311: companyNameCanon = StringHelper.replaceAll(
312: companyNameCanon, ".", "");
313: companyNameCanon = companyNameCanon.toUpperCase();
314:
315: String removeStrings[] = { " CORP", " INC" };
316: for (int i = 0; i < removeStrings.length; i++) {
317: int pos = companyNameCanon
318: .lastIndexOf(removeStrings[i]);
319: if (pos > 0)
320: companyNameCanon = companyNameCanon.substring(
321: 0, pos);
322: }
323: companyNameCanon += "%";
324:
325: accountList = SecurityWrapper.findByAnd("Account",
326: UtilMisc.toList(new EntityExpr("accountName",
327: EntityComparisonOperator.LIKE,
328: companyNameCanon)), UtilMisc
329: .toList("companyName"), userInfo,
330: new SecurityLinkInfo("Account", "accountId",
331: true), delegator);
332: }
333:
334: return accountList;
335: } catch (Exception e) {
336: Debug.logError("Error in findMatchingAccounts: "
337: + e.getMessage(), module);
338: return null;
339: }
340: }
341:
342: public List findMatchingContacts(String leadFirstName,
343: String leadLastName, UserInfo userInfo) {
344:
345: try {
346: List contactList = delegator.findByCondition("Contact",
347: new EntityConditionList(UtilMisc.toList(
348: new EntityExpr("lastName",
349: EntityComparisonOperator.LIKE,
350: leadLastName), new EntityExpr(
351: "firstName",
352: EntityComparisonOperator.LIKE,
353: leadFirstName + "%")),
354: EntityOperator.AND), null, UtilMisc
355: .toList("lastName"));
356:
357: return contactList;
358: } catch (Exception e) {
359: Debug.logError("Error in findMatchingAccounts: "
360: + e.getMessage(), module);
361: return null;
362: }
363: }
364:
365: public List findMatchingContacts(String accountId,
366: String leadFirstName, String leadLastName, UserInfo userInfo) {
367:
368: try {
369: List contactList = delegator
370: .findByCondition(
371: "Contact",
372: new EntityConditionList(
373: UtilMisc
374: .toList(
375: new EntityExpr(
376: "accountId",
377: EntityComparisonOperator.EQUALS,
378: accountId),
379: new EntityExpr(
380: "lastName",
381: EntityComparisonOperator.LIKE,
382: leadLastName),
383: new EntityExpr(
384: "firstName",
385: EntityComparisonOperator.LIKE,
386: leadFirstName)),
387: EntityOperator.AND), null, UtilMisc
388: .toList("lastName"));
389:
390: return contactList;
391: } catch (Exception e) {
392: Debug.logError("Error in findMatchingAccounts: "
393: + e.getMessage(), module);
394: return null;
395: }
396: }
397:
398: /**
399: * DOCUMENT ME!
400: *
401: * @param dataVector
402: * @param userInfo
403: *
404: * @return
405: */
406: public String getLeadAssignment(Vector dataVector, UserInfo userInfo) {
407: String assignmentPartyId = null;
408:
409: ArrayList leadRules = getAssignmentRules(userInfo
410: .getAccountId());
411:
412: if ((leadRules == null) || leadRules.isEmpty()) {
413: return null;
414: }
415:
416: Iterator ruleIterator = leadRules.iterator();
417:
418: while (ruleIterator.hasNext()) {
419: LeadAssignmentRule rule = (LeadAssignmentRule) ruleIterator
420: .next();
421:
422: List ruleCriteriaList = rule.getAssignmentCriteria();
423: GenericValue ruleInfo = rule.getAssignmentRule();
424:
425: if ((ruleCriteriaList == null)
426: || (ruleCriteriaList.isEmpty())) {
427: continue;
428: }
429:
430: Debug.logVerbose("checking rule: "
431: + ruleInfo.getString("name"), module);
432:
433: String assignTo = null;
434:
435: if (ruleInfo != null) {
436: assignTo = ruleInfo.getString("assignToPartyId");
437: }
438:
439: Iterator criteriaIterator = ruleCriteriaList.iterator();
440:
441: boolean mismatch = false;
442:
443: while (criteriaIterator.hasNext()) {
444: GenericValue criteria = (GenericValue) criteriaIterator
445: .next();
446:
447: String criteriaName = criteria.getString("name");
448: String fieldName = criteria.getString("fieldName");
449: String operator = criteria.getString("operator");
450: String value = criteria.getString("value");
451:
452: Debug.logVerbose("checking criteria: " + criteriaName
453: + " -> " + fieldName + " " + operator + " "
454: + value, module);
455:
456: for (int i = 0; i < dataVector.size(); i++) {
457: GenericValue gv = (GenericValue) dataVector.get(i);
458:
459: if (gv.getModelEntity().getField(fieldName) != null) {
460: String fieldValue = gv.getString(fieldName);
461:
462: Debug.logVerbose("fieldValue = " + fieldValue,
463: module);
464:
465: if (!SimpleExpression.compare(fieldValue,
466: operator, value)) {
467:
468: Debug.logVerbose("criteria match failed",
469: module);
470:
471: mismatch = true;
472: }
473:
474: break;
475: }
476: }
477:
478: if (mismatch) {
479: break;
480: }
481: }
482:
483: if (!mismatch) {
484: assignmentPartyId = assignTo;
485:
486: break;
487: }
488: }
489:
490: return assignmentPartyId;
491: }
492:
493: /**
494: * DOCUMENT ME!
495: *
496: * @param partyId
497: *
498: * @return
499: */
500: public ArrayList getAssignmentRules(String partyId) {
501: Debug.logVerbose("[getAssignmentRules] partyId: " + partyId,
502: module);
503: Debug.logVerbose(
504: "[getAssignmentRules] delegator.getDelegatorName(): "
505: + delegator.getDelegatorName(), module);
506:
507: GenericPK partyPK = delegator.makePK("Party", UtilMisc.toMap(
508: "partyId", partyId));
509: ArrayList leadRules = (ArrayList) leadAssignemtRuleCache
510: .get(partyPK);
511:
512: if (leadRules == null) {
513: try {
514: List ruleList = delegator.findByAndCache(
515: "LeadAssignmentRule", UtilMisc.toMap(
516: "accountId", partyId), UtilMisc
517: .toList("priority"));
518:
519: if ((ruleList != null) && (!ruleList.isEmpty())) {
520: leadRules = new ArrayList();
521:
522: Iterator i = ruleList.iterator();
523:
524: while (i.hasNext()) {
525: GenericValue leadAssignmentRule = (GenericValue) i
526: .next();
527: List assignmentCriteria = getAssignmentCriteria(leadAssignmentRule
528: .getString("leadAssignmentRuleId"));
529: leadRules
530: .add(new LeadAssignmentRule(
531: leadAssignmentRule,
532: assignmentCriteria));
533: }
534:
535: leadAssignemtRuleCache.put(partyPK, leadRules);
536: } else {
537: Debug.logVerbose(
538: "[getAssignmentRules] Did not find Assignment rules for party."
539: + partyId, module);
540: }
541: } catch (GenericEntityException e) {
542: Debug.logError(e, module);
543: }
544: }
545:
546: return leadRules;
547: }
548:
549: /**
550: * DOCUMENT ME!
551: *
552: * @param ruleId
553: *
554: * @return
555: */
556: public List getAssignmentCriteria(String ruleId) {
557: Debug.logVerbose("[getAssignmentCriteria] ruleId: " + ruleId,
558: module);
559:
560: List criteriaList = null;
561:
562: try {
563: criteriaList = delegator.findByAndCache(
564: "LeadAssignmentCriteria", UtilMisc.toMap(
565: "leadAssignmentRuleId", ruleId), UtilMisc
566: .toList("criteriaSequence"));
567: } catch (GenericEntityException e) {
568: Debug.logError(e, module);
569: }
570:
571: return criteriaList;
572: }
573:
574: /**
575: * DOCUMENT ME!
576: */
577: public void clearCache() {
578: leadAssignemtRuleCache.clear();
579: }
580:
581: /**
582: * DOCUMENT ME!
583: *
584: */
585: public class LeadAssignmentRule {
586: GenericValue assignmentRule_ = null;
587: List assignmentCriteria_ = null;
588:
589: public LeadAssignmentRule(GenericValue assignmentRule,
590: List assignmentCriteria) {
591: assignmentRule_ = assignmentRule;
592: assignmentCriteria_ = assignmentCriteria;
593: }
594:
595: /**
596: * DOCUMENT ME!
597: *
598: * @return
599: */
600: public List getAssignmentCriteria() {
601: return assignmentCriteria_;
602: }
603:
604: /**
605: * DOCUMENT ME!
606: *
607: * @return
608: */
609: public GenericValue getAssignmentRule() {
610: return assignmentRule_;
611: }
612:
613: /**
614: * DOCUMENT ME!
615: *
616: * @param assignmentCriteria
617: */
618: public void setAssignmentCriteria(List assignmentCriteria) {
619: assignmentCriteria_ = assignmentCriteria;
620: }
621:
622: /**
623: * DOCUMENT ME!
624: *
625: * @param assignmentRule
626: */
627: public void setAssginmentRule(GenericValue assignmentRule) {
628: assignmentRule_ = assignmentRule;
629: }
630: }
631: }
|