001: package com.sourcetap.sfa.lead;
002:
003: import java.sql.Timestamp;
004: import java.util.ArrayList;
005: import java.util.Calendar;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Vector;
010:
011: import org.ofbiz.base.util.Debug;
012: import org.ofbiz.base.util.UtilFormatOut;
013: import org.ofbiz.base.util.UtilMisc;
014: import org.ofbiz.entity.GenericDelegator;
015: import org.ofbiz.entity.GenericEntityException;
016: import org.ofbiz.entity.GenericPK;
017: import org.ofbiz.entity.GenericValue;
018:
019: import com.sourcetap.sfa.account.AccountEventProcessor;
020: import com.sourcetap.sfa.contact.ContactEventProcessor;
021: import com.sourcetap.sfa.event.DataMatrix;
022: import com.sourcetap.sfa.event.GenericEventProcessor;
023: import com.sourcetap.sfa.opportunity.OpportunityEventProcessor;
024: import com.sourcetap.sfa.ui.UICache;
025: import com.sourcetap.sfa.ui.UIWebScreenSection;
026: import com.sourcetap.sfa.ui.UIWebUtility;
027: import com.sourcetap.sfa.util.UserInfo;
028:
029: public class LeadConverter {
030: public static final String module = LeadConverter.class.getName();
031:
032: public List fieldMapping;
033: public HashMap screenSections;
034: public HashMap sourceData;
035:
036: public String accountId = null;
037: public String contactId = null;
038: public String opportunityId = null;
039: public String opportunityName = null;
040: public String opportunityStageId = null;
041: public boolean createContact = true;
042: public boolean createOpportunity = true;
043: protected GenericDelegator delegator = null;
044: protected UICache uiCache = null;
045: protected UIWebScreenSection uiWebScreenSection = null;
046: protected UserInfo userInfo = null;
047: protected LeadEventProcessor leadEventProcessor = null;
048:
049: public LeadConverter(GenericDelegator delegator, UICache uiCache,
050: UserInfo userInfo, LeadEventProcessor leadEventProcessor) {
051: fieldMapping = new ArrayList();
052: sourceData = new HashMap();
053: initializeFieldMappings();
054: this .delegator = delegator;
055: this .uiCache = uiCache;
056: this .userInfo = userInfo;
057: this .leadEventProcessor = leadEventProcessor;
058:
059: try {
060: this .uiWebScreenSection = UIWebScreenSection
061: .getUiWebScreenSection(userInfo, "LEAD",
062: "LeadHeader", delegator, uiCache);
063: } catch (GenericEntityException e) {
064: Debug.logError("Error creating leadConverter: "
065: + e.getMessage(), module);
066: throw new IllegalArgumentException(
067: "Unable to load screen section LEAD.LeadHeader in LeadConverter");
068: }
069: }
070:
071: public void initializeFieldMappings() {
072: addMapping("Lead", "leadOwnerId", "ACCOUNT", "AccountHeader",
073: "Account", "accountOwnerId");
074: addMapping("Lead", "companyName", "ACCOUNT", "AccountHeader",
075: "Account", "accountName");
076: addMapping("Lead", "businessPhone", "ACCOUNT", "AccountHeader",
077: "Account", "phone");
078: addMapping("Lead", "faxPhone", "ACCOUNT", "AccountHeader",
079: "Account", "fax");
080: addMapping("Address", "mailingAddress", "ACCOUNT",
081: "AccountHeader", "Address", "mailingAddress");
082: addMapping("Address", "city", "ACCOUNT", "AccountHeader",
083: "Address", "city");
084: addMapping("Address", "state", "ACCOUNT", "AccountHeader",
085: "Address", "state");
086: addMapping("Address", "zip", "ACCOUNT", "AccountHeader",
087: "Address", "zip");
088: addMapping("Address", "country", "ACCOUNT", "AccountHeader",
089: "Address", "country");
090: addMapping("Lead", "stockSymbol", "ACCOUNT", "AccountHeader",
091: "Account", "stockSymbol");
092: addMapping("Lead", "annualRevenue", "ACCOUNT", "AccountHeader",
093: "Account", "annualRevenue");
094: addMapping("Lead", "sicCodeId", "ACCOUNT", "AccountHeader",
095: "Account", "sicCodeId");
096: addMapping("Lead", "industry", "ACCOUNT", "AccountHeader",
097: "Account", "industry");
098: addMapping("Lead", "numberOfEmployees", "ACCOUNT",
099: "AccountHeader", "Account", "numberOfEmployees");
100: addMapping("Lead", "dunsNumber", "ACCOUNT", "AccountHeader",
101: "Account", "dunsNumber");
102: addMapping("Lead", "websiteUrl", "ACCOUNT", "AccountHeader",
103: "Account", "websiteUrl");
104: addMapping("Lead", "businessDescription", "ACCOUNT",
105: "AccountHeader", "Account", "businessDescription");
106: //AccountHeader_Account_accountTypeId_0
107: //AccountHeader_Account_classInfo_0
108: //AccountHeader_Account_statusId_0
109: // addMapping( "Lead", "statusId", );
110: // addMapping( "Lead", "activeFlag", );
111: // addMapping( "Lead", "validatedFlag", );
112: // addMapping( "Lead", "leadSourceDetails", );
113: // addMapping( "Lead", "ownershipTypeId", "AccountHeader", "Account", );
114:
115: addMapping("Lead", "leadOwnerId", "CONTACT", "ContactHeader",
116: "Contact", "contactOwnerId");
117: addMapping("Lead", "firstName", "CONTACT", "ContactHeader",
118: "Contact", "firstName");
119: addMapping("Lead", "lastName", "CONTACT", "ContactHeader",
120: "Contact", "lastName");
121: addMapping("Lead", "title", "CONTACT", "ContactHeader",
122: "Contact", "title");
123: addMapping("Lead", "email", "CONTACT", "ContactHeader",
124: "Contact", "email");
125: addMapping("Lead", "businessPhone", "CONTACT", "ContactHeader",
126: "Contact", "businessPhone");
127: addMapping("Lead", "homePhone", "CONTACT", "ContactHeader",
128: "Contact", "homePhone");
129: addMapping("Lead", "faxPhone", "CONTACT", "ContactHeader",
130: "Contact", "faxPhone");
131: addMapping("Lead", "mobilePhone", "CONTACT", "ContactHeader",
132: "Contact", "mobilePhone");
133: addMapping("Lead", "pager", "CONTACT", "ContactHeader",
134: "Contact", "pager");
135: addMapping("Address", "mailingAddress", "CONTACT",
136: "ContactHeader", "Address", "mailingAddress");
137: addMapping("Address", "city", "CONTACT", "ContactHeader",
138: "Address", "city");
139: addMapping("Address", "state", "CONTACT", "ContactHeader",
140: "Address", "state");
141: addMapping("Address", "zip", "CONTACT", "ContactHeader",
142: "Address", "zip");
143: addMapping("Address", "country", "CONTACT", "ContactHeader",
144: "Address", "country");
145: //ContactHeader_Contact_ssn_0
146: //ContactHeader_Contact_statusId_0
147: //ContactHeader_Contact_accountId_0
148:
149: addMapping("Lead", "targetAmount", "OPPORTUNITIES",
150: "OpportunityHeader", "Deal", "amount");
151: addMapping("Lead", "targetRatingId", "OPPORTUNITIES",
152: "OpportunityHeader", "Deal", "ratingId");
153: addMapping("Lead", "targetDescription", "OPPORTUNITIES",
154: "OpportunityHeader", "Deal", "description");
155: addMapping("Lead", "leadSourceId", "OPPORTUNITIES",
156: "OpportunityHeader", "Deal", "leadSourceId");
157: addMapping("Lead", "leadOwnerId", "OPPORTUNITIES",
158: "OpportunityHeader", "Deal", "ownerId");
159: //OpportunityHeader_Deal_dealName_0
160: //OpportunityHeader_Deal_accountId_0
161: //OpportunityHeader_Deal_dealTypeId_0
162: //OpportunityHeader_Deal_projectedCloseDate_0
163: //OpportunityHeader_Deal_actualCloseDate_0
164: //OpportunityHeader_Deal_stageId_0
165: //OpportunityHeader_Deal_dealStatusId_0
166: //OpportunityHeader_Deal_isInForecast_0
167: }
168:
169: public void addData(String entityName, GenericValue value) {
170: sourceData.put("entityName", value);
171: }
172:
173: public void addMapping(String fromEntity, String fromAttribute,
174: String toScreenName, String toScreenSection,
175: String toEntity, String toAttribute) {
176: fieldMapping.add(new MappingInfo(fromEntity, fromAttribute,
177: toScreenName, toScreenSection, toEntity, toAttribute));
178: }
179:
180: public boolean convert(String leadId) {
181: DataMatrix dataMatrix = DataMatrix.fillFromDB(delegator,
182: userInfo, uiWebScreenSection, leadEventProcessor,
183: new GenericPK(delegator.getModelEntity("Lead"),
184: UtilMisc.toMap("leadId", leadId)));
185:
186: return convert(delegator, dataMatrix, userInfo, uiCache);
187:
188: }
189:
190: public boolean convert(GenericDelegator delegator,
191: DataMatrix leadData, UserInfo userInfo, UICache uiCache) {
192: try {
193: HashMap dataMatrixMap = new HashMap();
194:
195: Iterator mappingIter = fieldMapping.iterator();
196: while (mappingIter.hasNext()) {
197: MappingInfo mapInfo = (MappingInfo) mappingIter.next();
198: String screenId = mapInfo.screenName + "."
199: + mapInfo.screenSection;
200:
201: DataInfo dataInfo = (DataInfo) dataMatrixMap
202: .get(screenId);
203:
204: if (dataInfo == null) {
205: UIWebScreenSection uiWebScreenSection = UIWebScreenSection
206: .getUiWebScreenSection(userInfo,
207: mapInfo.screenName,
208: mapInfo.screenSection, delegator,
209: uiCache);
210:
211: dataInfo = new DataInfo(delegator,
212: uiWebScreenSection);
213: dataMatrixMap.put(screenId, dataInfo);
214: }
215:
216: String field = mapInfo.toParamName;
217: GenericValue sourceGV = leadData.getCurrentBuffer()
218: .getGenericValue(0, mapInfo.fromEntity, false);
219: if (sourceGV != null) {
220: String value = sourceGV
221: .getString(mapInfo.fromAttribute);
222: dataInfo.add(mapInfo.toParamName, value);
223: } else {
224: System.out.println("Unable to get source entity"
225: + mapInfo.fromEntity);
226: }
227:
228: }
229:
230: GenericValue leadGV = leadData.getCurrentBuffer()
231: .getGenericValue(0, "Lead", false);
232: GenericValue addressGV = leadData.getCurrentBuffer()
233: .getGenericValue(0, "Address", false);
234:
235: boolean hasAccountInfo = false;
236: String accountName = UtilFormatOut.checkNull(leadGV
237: .getString("companyName"));
238:
239: if (((accountId != null) && (accountId.length() > 0))
240: || (accountName.length() > 0))
241: hasAccountInfo = true;
242:
243: if (hasAccountInfo) {
244: DataInfo dataInfo = (DataInfo) dataMatrixMap
245: .get("ACCOUNT.AccountHeader");
246: GenericEventProcessor eventProcessor = new AccountEventProcessor();
247:
248: if ((accountId != null) && (accountId.length() > 0)) {
249: dataInfo.retrieveFromDB(userInfo, eventProcessor,
250: new GenericPK(delegator
251: .getModelEntity("Account"),
252: UtilMisc.toMap("accountId",
253: accountId)));
254: dataInfo.processUpdate(userInfo, delegator,
255: eventProcessor);
256: } else {
257: dataInfo.loadData(userInfo, eventProcessor);
258: dataInfo.processInsert(userInfo, delegator,
259: eventProcessor);
260: }
261:
262: GenericValue accountGV = dataInfo.dataMatrix
263: .getCurrentBuffer().getGenericValue(0,
264: "Account", false);
265: accountId = accountGV.getString("accountId");
266: accountName = accountGV.getString("accountName");
267: }
268:
269: if (getCreateContact()) {
270: DataInfo dataInfo = (DataInfo) dataMatrixMap
271: .get("CONTACT.ContactHeader");
272: GenericEventProcessor eventProcessor = new ContactEventProcessor();
273:
274: if ((contactId != null) && (contactId.length() > 0)) {
275: dataInfo.retrieveFromDB(userInfo, eventProcessor,
276: new GenericPK(delegator
277: .getModelEntity("Contact"),
278: UtilMisc.toMap("contactId",
279: contactId)));
280: if (hasAccountInfo) {
281: GenericValue contactGV = dataInfo.dataMatrix
282: .getCurrentBuffer().getGenericValue(0,
283: "Contact", false);
284: String accountIdOrig = contactGV
285: .getString("accountId");
286: if ((accountIdOrig == null)
287: || (accountIdOrig.length() < 1))
288: contactGV.set("accountId", accountId);
289: }
290: dataInfo.processUpdate(userInfo, delegator,
291: eventProcessor);
292: } else {
293: dataInfo.loadData(userInfo, eventProcessor);
294: if (hasAccountInfo)
295: dataInfo.dataMatrix.getCurrentBuffer()
296: .getGenericValue(0, "Contact", false)
297: .set("accountId", accountId);
298: dataInfo.processInsert(userInfo, delegator,
299: eventProcessor);
300: }
301:
302: GenericValue contactGV = dataInfo.dataMatrix
303: .getCurrentBuffer().getGenericValue(0,
304: "Contact", false);
305: contactId = contactGV.getString("contactId");
306:
307: // Add ContactEmploymentInfo if there is an account tied to this record
308: if ((accountId != null) && (accountId.length() > 0)) {
309: GenericValue employmentGV = delegator
310: .findByPrimaryKey("ContactEmployment",
311: UtilMisc.toMap(
312: "contactEmploymentId",
313: contactId + "-E"));
314: if (employmentGV == null) {
315: employmentGV = new GenericValue(delegator
316: .getModelEntity("ContactEmployment"));
317: employmentGV.setDelegator(delegator);
318: employmentGV.set("contactEmploymentId",
319: contactId + "-E");
320: employmentGV.set("contactId", contactId);
321: employmentGV.set("title", leadGV
322: .getString("title"));
323: employmentGV.set("companyName", accountName);
324: if (addressGV != null) {
325: employmentGV
326: .set(
327: "mailingAddress",
328: addressGV
329: .getString("mailingAddress"));
330: employmentGV.set("city", addressGV
331: .getString("city"));
332: employmentGV.set("state", addressGV
333: .getString("state"));
334: employmentGV.set("zip", addressGV
335: .getString("zip"));
336: }
337:
338: employmentGV.create();
339: }
340: }
341: }
342:
343: if (getCreateOpportunity() && hasAccountInfo) {
344: DataInfo dataInfo = (DataInfo) dataMatrixMap
345: .get("OPPORTUNITIES.OpportunityHeader");
346: GenericEventProcessor eventProcessor = new OpportunityEventProcessor();
347: DataMatrix dataMatrix = dataInfo.loadData(userInfo,
348: eventProcessor);
349: GenericValue dealGV = dataMatrix.getCurrentBuffer()
350: .getGenericValue(0, "Deal", false);
351: dealGV.set("accountId", accountId);
352:
353: if (opportunityName != null)
354: dealGV.set("dealName", opportunityName);
355: else
356: dealGV.set("dealName", "Opportunity for "
357: + accountName);
358:
359: if (opportunityStageId != null)
360: dealGV.set("stageId", opportunityStageId);
361:
362: dealGV.set("dealTypeId", "1");
363: dataInfo.processInsert(userInfo, delegator,
364: eventProcessor);
365:
366: opportunityId = dealGV.getString("dealId");
367: }
368:
369: // move the related lead data to the contact or account
370: String leadId = leadGV.getString("leadId");
371:
372: //PartyIdentifiers are moved to the account and contact
373: List partyIdentifiers = delegator.findByAnd(
374: "PartyIdentifier", UtilMisc
375: .toMap("partyId", leadId), null);
376: String partyId = contactId;
377: String partyType = "Contact";
378: if (hasAccountInfo) {
379: partyType = "Account";
380: partyId = accountId;
381:
382: Iterator identIter = partyIdentifiers.iterator();
383: while (identIter.hasNext()) {
384: GenericValue gv = (GenericValue) identIter.next();
385: gv.set("partyId", accountId);
386: }
387: delegator.storeAll(partyIdentifiers);
388: }
389: if (getCreateContact()) {
390: Iterator identIter = partyIdentifiers.iterator();
391: while (identIter.hasNext()) {
392: GenericValue gv = (GenericValue) identIter.next();
393: gv.set("partyId", contactId);
394: }
395: delegator.storeAll(partyIdentifiers);
396: }
397:
398: delegator.removeByAnd("PartyIdentifier", UtilMisc.toMap(
399: "partyId", leadId));
400:
401: //Activities are mapped to the contact and account
402: List activities = delegator.findByAnd("Activity", UtilMisc
403: .toMap("leadId", leadId), null);
404: Iterator actIter = activities.iterator();
405: while (actIter.hasNext()) {
406: GenericValue gv = (GenericValue) actIter.next();
407: gv.set("accountId", accountId);
408: gv.set("contactId", contactId);
409: }
410: delegator.storeAll(activities);
411:
412: //Notes are mapped to the account
413: List notes = delegator.findByAnd("NoteLink",
414: UtilMisc.toMap("noteLinkType", "Lead",
415: "noteLinkId", leadId), null);
416: delegator.removeByAnd("NoteLink", UtilMisc.toMap(
417: "noteLinkType", "Lead", "noteLinkId", leadId));
418: Iterator noteIter = notes.iterator();
419: while (noteIter.hasNext()) {
420: GenericValue gv = (GenericValue) noteIter.next();
421: gv.set("noteLinkType", partyType);
422: gv.set("noteLinkId", partyId);
423: }
424: delegator.storeAll(notes);
425:
426: //File attachments are mapped to account and contact
427: List leadFiles = delegator.findByAnd("LeadFile", UtilMisc
428: .toMap("leadId", leadId), null);
429: delegator.removeByAnd("LeadFile", UtilMisc.toMap("leadId",
430: leadId));
431: Iterator fileIter = leadFiles.iterator();
432: while (fileIter.hasNext()) {
433: GenericValue gv = (GenericValue) fileIter.next();
434:
435: if (hasAccountInfo) {
436: GenericValue accountFile = new GenericValue(
437: delegator.getModelEntity("AccountFile"));
438: accountFile.setDelegator(delegator);
439: accountFile.set("accountId", accountId);
440: accountFile.set("fileId", gv.get("fileId"));
441: accountFile.set("createdBy", gv.get("createdBy"));
442: accountFile.set("modifiedBy", gv.get("modifiedBy"));
443: accountFile.set("createdDate", gv
444: .get("createdDate"));
445: accountFile.set("modifiedDate", gv
446: .get("modifiedDate"));
447: accountFile.create();
448: }
449:
450: if (getCreateContact()) {
451: GenericValue contactFile = new GenericValue(
452: delegator.getModelEntity("ContactFile"));
453: contactFile.setDelegator(delegator);
454: contactFile.set("contactId", contactId);
455: contactFile.set("fileId", gv.get("fileId"));
456: contactFile.set("createdBy", gv.get("createdBy"));
457: contactFile.set("modifiedBy", gv.get("modifiedBy"));
458: contactFile.set("createdDate", gv
459: .get("createdDate"));
460: contactFile.set("modifiedDate", gv
461: .get("modifiedDate"));
462: contactFile.create();
463: }
464: }
465:
466: // update the converted date on the lead.
467: Timestamp now = new Timestamp(Calendar.getInstance()
468: .getTime().getTime());
469: leadGV.set("convertedDate", now);
470: leadGV.set("convertedAccountId", accountId);
471: leadGV.set("convertedContactId", contactId);
472: delegator.store(leadGV);
473:
474: return true;
475:
476: } catch (Exception e) {
477: Debug.logError(e, module);
478: return false;
479: }
480: }
481:
482: public class MappingInfo {
483: public String fromEntity;
484: public String fromAttribute;
485: public String toEntity;
486: public String toAttribute;
487: public String toParamName;
488: public String screenName;
489: public String screenSection;
490:
491: public MappingInfo(String fromEntityStr,
492: String fromAttributeStr, String screenNameStr,
493: String screenSectionStr, String toEntityStr,
494: String toAttributeStr) {
495: fromEntity = fromEntityStr;
496: fromAttribute = fromAttributeStr;
497: toEntity = toEntityStr;
498: toAttribute = toAttributeStr;
499: screenName = screenNameStr;
500: screenSection = screenSectionStr;
501: toParamName = UIWebUtility.getParamName("", screenSection,
502: toEntity, toAttribute, 0);
503: }
504: }
505:
506: public class DataInfo {
507: UIWebScreenSection uiWebScreenSection;
508: DataMatrix dataMatrix;
509: Vector values;
510: Vector fields;
511: GenericDelegator delegator;
512: String[] valuesA;
513:
514: public DataInfo(GenericDelegator delegator_,
515: UIWebScreenSection uiWebScreenSection_) {
516: delegator = delegator_;
517: uiWebScreenSection = uiWebScreenSection_;
518: dataMatrix = new DataMatrix(delegator, uiWebScreenSection
519: .getEntityParamVector());
520: values = new Vector();
521: fields = new Vector();
522: }
523:
524: public void add(String field, String value) {
525: fields.add(field);
526: values.add(value);
527: }
528:
529: public DataMatrix retrieveFromDB(UserInfo userInfo,
530: GenericEventProcessor eventProcessor,
531: GenericPK primaryKey) throws GenericEntityException {
532: dataMatrix = DataMatrix.fillFromDB(delegator, userInfo,
533: uiWebScreenSection, eventProcessor, primaryKey);
534: dataMatrix.addRowFromArray(0, values, fields,
535: uiWebScreenSection);
536: return dataMatrix;
537: }
538:
539: public DataMatrix loadData(UserInfo userInfo,
540: GenericEventProcessor eventProcessor)
541: throws GenericEntityException {
542: eventProcessor.processCreate(userInfo, uiWebScreenSection
543: .getEntityNameList(), delegator, dataMatrix);
544: dataMatrix.addRowFromArray(0, values, fields,
545: uiWebScreenSection);
546: return dataMatrix;
547: }
548:
549: public void processInsert(UserInfo userInfo,
550: GenericDelegator delegator,
551: GenericEventProcessor eventProcessor)
552: throws GenericEntityException {
553: eventProcessor.processInsert(userInfo, delegator,
554: dataMatrix);
555: }
556:
557: public void processUpdate(UserInfo userInfo,
558: GenericDelegator delegator,
559: GenericEventProcessor eventProcessor)
560: throws GenericEntityException {
561: eventProcessor.processUpdate(userInfo, delegator,
562: dataMatrix);
563: }
564: }
565:
566: /**
567: * @return
568: */
569: public String getAccountId() {
570: return accountId;
571: }
572:
573: /**
574: * @return
575: */
576: public String getContactId() {
577: return contactId;
578: }
579:
580: /**
581: * @return
582: */
583: public String getOpportunityName() {
584: return opportunityName;
585: }
586:
587: /**
588: * @return
589: */
590: public String getOpportunityStageId() {
591: return opportunityStageId;
592: }
593:
594: /**
595: * @param string
596: */
597: public void setAccountId(String string) {
598: accountId = string;
599: }
600:
601: /**
602: * @param string
603: */
604: public void setContactId(String string) {
605: contactId = string;
606: }
607:
608: /**
609: * @param string
610: */
611: public void setOpportunityName(String string) {
612: opportunityName = string;
613: }
614:
615: /**
616: * @param string
617: */
618: public void setOpportunityStageId(String string) {
619: opportunityStageId = string;
620: }
621:
622: /**
623: * @return
624: */
625: public String getOpportunityId() {
626: return opportunityId;
627: }
628:
629: /**
630: * @param string
631: */
632: public void setOpportunityId(String string) {
633: opportunityId = string;
634: }
635:
636: /**
637: * @return
638: */
639: public boolean getCreateContact() {
640: return createContact;
641: }
642:
643: /**
644: * @return
645: */
646: public boolean getCreateOpportunity() {
647: return createOpportunity;
648: }
649:
650: /**
651: * @param b
652: */
653: public void setCreateContact(boolean b) {
654: createContact = b;
655: }
656:
657: /**
658: * @param b
659: */
660: public void setCreateOpportunity(boolean b) {
661: createOpportunity = b;
662: }
663:
664: }
|