예제 #1
0
        public GetLeadAdapterResponse GetLeadAdapter(GetLeadAdapterRequest request)
        {
            GetLeadAdapterResponse response = new GetLeadAdapterResponse();

            Logger.Current.Verbose("Request to fetch LeadAdapter based on leadAdapterId" + request.Id);
            LeadAdapterAndAccountMap leadAdapter = leadAdaptersRepository.FindBy(request.Id);

            if (leadAdapter == null)
            {
                response.Exception = GetLeadAdapterNotFoundException();
            }
            else
            {
                if (leadAdapter.LeadAdapterTypeID != LeadAdapterTypes.Facebook)
                {
                    FTPAgent agent      = new FTPAgent();
                    var      ftpDetails = agent.GetFtpRegistration(leadAdapter.RequestGuid);
                    if (ftpDetails != null)
                    {
                        leadAdapter.Url       = ftpDetails.Host;
                        leadAdapter.UserName  = ftpDetails.UserName;
                        leadAdapter.Password  = ftpDetails.Password;
                        leadAdapter.EnableSsl = ftpDetails.EnableSsl;
                        leadAdapter.Port      = ftpDetails.Port;
                    }
                }
                LeadAdapterViewModel leadAdapterViewModel = Mapper.Map <LeadAdapterAndAccountMap, LeadAdapterViewModel>(leadAdapter);
                response.LeadAdapterViewModel = leadAdapterViewModel;
            }
            return(response);
        }
예제 #2
0
        public UpdateLeadAdapterResponse UpdateFacebookLeadAdapter(UpdateLeadAdapterRequest request)
        {
            Logger.Current.Verbose("Request received to update Facebook lead adapter.");

            try
            {
                LeadAdapterAndAccountMap leadAdapter = Mapper.Map <LeadAdapterViewModel, LeadAdapterAndAccountMap>(request.LeadAdapterViewModel);

                bool isDuplicate = leadAdaptersRepository.IsDuplicateFacebookAdapter(request.AccountId, leadAdapter.Id, leadAdapter.FacebookLeadAdapterName);
                if (isDuplicate)
                {
                    throw new UnsupportedOperationException("[|LeadAdapter with same name already exists.|]");
                }

                leadAdaptersRepository.Update(leadAdapter);
                unitOfWork.Commit();
                GetFacebookAppResponse fbResponse = this.GetFacebookApp(new GetFacebookAppRequest()
                {
                    AccountId = request.AccountId
                });
                string extendedToken = this.GetExtendedPageAccessToken(leadAdapter.FacebookLeadAdapter.UserAccessToken, leadAdapter.FacebookLeadAdapter.PageID, fbResponse.FacebookAppID, fbResponse.FacebookAppSecret);
                leadAdapter.FacebookLeadAdapter.PageAccessToken = !string.IsNullOrEmpty(extendedToken) ? extendedToken : leadAdapter.FacebookLeadAdapter.PageAccessToken;


                leadAdaptersRepository.UpdateFacebookLeadAdapter(leadAdapter.FacebookLeadAdapter);
            }
            catch (Exception ex)
            {
                Logger.Current.Error("An error occured while updating facebook lead adapter", ex);
            }
            Logger.Current.Informational("Leadadapter updated successfully.");
            return(new UpdateLeadAdapterResponse());
        }
예제 #3
0
        public UpdateLeadAdapterResponse UpdateLeadAdapter(UpdateLeadAdapterRequest request)
        {
            Logger.Current.Verbose("Request received to update a lead adapter.");
            FTPAgent agent = new FTPAgent();

            LeadAdapterAndAccountMap leadAdapter = Mapper.Map <LeadAdapterViewModel, LeadAdapterAndAccountMap>(request.LeadAdapterViewModel);
            bool isDuplicate = leadAdaptersRepository.IsDuplicateLeadAdapter(leadAdapter.LeadAdapterTypeID, leadAdapter.AccountID, leadAdapter.Id);

            if (isDuplicate)
            {
                throw new UnsupportedOperationException("[|LeadAdapter already exists.|]");
            }
            string leadAdapterPhysicalPath = ConfigurationManager.AppSettings["LEADADAPTER_PHYSICAL_PATH"].ToString();

            request.LeadAdapterViewModel.ArchivePath = Path.Combine(leadAdapterPhysicalPath, request.LeadAdapterViewModel.AccountID.ToString(),
                                                                    request.LeadAdapterViewModel.LeadAdapterType.ToString(), "Archive");
            request.LeadAdapterViewModel.LocalFilePath = Path.Combine(leadAdapterPhysicalPath, request.LeadAdapterViewModel.AccountID.ToString(),
                                                                      request.LeadAdapterViewModel.LeadAdapterType.ToString(), "Local");
            if (agent.UpdateFtpRegistration(request.LeadAdapterViewModel.UserName, request.LeadAdapterViewModel.Password, request.LeadAdapterViewModel.Url,
                                            request.LeadAdapterViewModel.Port, request.LeadAdapterViewModel.EnableSSL, request.LeadAdapterViewModel.RequestGuid) && leadAdapter != null)
            {
                leadAdaptersRepository.Update(leadAdapter);
                unitOfWork.Commit();
                Logger.Current.Informational("Leadadapter updated successfully.");
            }
            return(new UpdateLeadAdapterResponse());
        }
        private int InsertJobLog(LeadAdapterAndAccountMap accountMap)
        {
            Logger.Current.Informational("Inserting joblog for account id : " + accountMap.AccountID);
            var jobLog = new LeadAdapterJobLogs();

            jobLog.LeadAdapterAndAccountMapID = accountMap.Id;
            jobLog.LeadAdapterJobStatusID     = LeadAdapterJobStatus.Undefined;
            jobLog.Remarks  = string.Empty;
            jobLog.FileName = "lead_feed.xml";
            List <LeadAdapterJobLogDetails> details = new List <LeadAdapterJobLogDetails>();

            jobLog.LeadAdapterJobLogDetails = details;
            return(importDataRepository.InsertLeadAdapterjob(jobLog, new Guid(), false, false, accountMap.AccountID, accountMap.CreatedBy, 1, accountMap.CreatedBy, true));
        }
예제 #5
0
        void isValidLeadAdapter(LeadAdapterAndAccountMap leadAdapter)
        {
            IEnumerable <BusinessRule> brokenRules = leadAdapter.GetBrokenRules();

            if (brokenRules.Any())
            {
                StringBuilder brokenRulesBuilder = new StringBuilder();
                foreach (BusinessRule rule in brokenRules.Distinct())
                {
                    brokenRulesBuilder.AppendLine(rule.RuleDescription);
                }
                throw new Exception(brokenRulesBuilder.ToString());
            }
        }
 public BaseLeadAdapterProvider(int accountId, int leadAdapterAndAccountMapID, LeadAdapterTypes leadAdapterType
                                , ILeadAdaptersRepository leadAdaptersRepository, IImportDataRepository importDataRepository, ISearchService <Contact> searchService,
                                IUnitOfWork unitOfWork, ICustomFieldService customFieldService, ICachingService cacheService, IServiceProviderRepository serviceProviderRepository, IMailGunService mailGunService,
                                IContactService contactService)
 {
     this.AccountID  = accountId;
     this.repository = leadAdaptersRepository;
     this.serviceProviderRepository = serviceProviderRepository;
     this.importDataRepository      = importDataRepository;
     this.customFieldService        = customFieldService;
     this.unitOfWork          = unitOfWork;
     this.mailGunService      = mailGunService;
     this.contactService      = contactService;
     this.leadAdapterType     = leadAdapterType;
     this.cacheService        = cacheService;
     LeadAdapterAccountMapID  = leadAdapterAndAccountMapID;
     leadAdapterAndAccountMap = repository.GetLeadAdapterByID(LeadAdapterAccountMapID);
     _fieldMappings           = GetFieldMappings();
 }
예제 #7
0
        public InsertLeadAdapterResponse InsertLeadAdapter(InsertLeadAdapterRequest request)
        {
            Logger.Current.Verbose("Request received to insert a lead adapter.");
            InsertLeadAdapterResponse response = new InsertLeadAdapterResponse();
            FTPAgent agent = new FTPAgent();

            LeadAdapterViewModel vm = request.LeadAdapterViewModel;
            int  AccountID          = vm.AccountID;
            bool isDuplicate        = leadAdaptersRepository.IsDuplicateLeadAdapter(vm.LeadAdapterType, AccountID,
                                                                                    vm.LeadAdapterAndAccountMapId);

            if (isDuplicate)
            {
                throw new UnsupportedOperationException("[|LeadAdapter already exists.|]");
            }

            string leadAdapterPhysicalPath = ConfigurationManager.AppSettings["LEADADAPTER_PHYSICAL_PATH"].ToString();

            vm.ArchivePath   = Path.Combine(leadAdapterPhysicalPath, AccountID.ToString(), vm.LeadAdapterType.ToString(), "Archive");
            vm.LocalFilePath = Path.Combine(leadAdapterPhysicalPath, AccountID.ToString(), vm.LeadAdapterType.ToString(), "Local");
            vm.RequestGuid   = agent.FTPRegistration(vm.UserName, vm.Password, vm.Url, vm.Port, vm.EnableSSL, vm.RequestGuid);

            //Create folders
            if (!Directory.Exists(vm.LocalFilePath))
            {
                Directory.CreateDirectory(vm.LocalFilePath);
            }
            if (!Directory.Exists(vm.ArchivePath))
            {
                Directory.CreateDirectory(vm.ArchivePath);
            }

            LeadAdapterAndAccountMap leadAdapter          = Mapper.Map <LeadAdapterViewModel, LeadAdapterAndAccountMap>(vm);
            bool isLeadAdapterAlreadyConfiguredForAccount = leadAdaptersRepository.isLeadAdapterAlreadyConfigured(AccountID, vm.LeadAdapterType);

            leadAdaptersRepository.Insert(leadAdapter);
            LeadAdapterAndAccountMap newLeadAdapter = unitOfWork.Commit() as LeadAdapterAndAccountMap;

            if (!isLeadAdapterAlreadyConfiguredForAccount)
            {
                CustomFieldTab customfieldtab = customfieldRepository.GetLeadAdapterCustomFieldTab(AccountID);
                if (customfieldtab == null)
                {
                    InsertCustomFieldTabRequest      customfieldtabrequest = new InsertCustomFieldTabRequest();
                    CustomFieldTabViewModel          tab     = new CustomFieldTabViewModel();
                    CustomFieldSectionViewModel      section = new CustomFieldSectionViewModel();
                    GetLeadAdapterCustomFieldRequest getleadadaptercustomfieldsrequest = new GetLeadAdapterCustomFieldRequest();
                    getleadadaptercustomfieldsrequest.AccountId       = AccountID;
                    getleadadaptercustomfieldsrequest.LeadAdapterType = vm.LeadAdapterType;
                    GetLeadAdapterCustomFieldResponse getleadadaptercustomfieldsresponse = customfieldService.GetLeadAdapterCustomFieldsByType(getleadadaptercustomfieldsrequest);
                    section.CustomFields = getleadadaptercustomfieldsresponse.CustomFields.ToList();
                    section.Name         = vm.LeadAdapterType.ToString();
                    section.StatusId     = CustomFieldSectionStatus.Active;

                    tab.AccountId        = AccountID;
                    tab.Name             = "Lead Adapter Custom Fields";
                    tab.IsLeadAdapterTab = true;
                    tab.Sections         = new List <CustomFieldSectionViewModel>();
                    tab.Sections.Add(section);
                    tab.StatusId = CustomFieldTabStatus.Active;
                    customfieldtabrequest.CustomFieldTabViewModel = tab;
                    customfieldService.InsertCustomFieldTab(customfieldtabrequest);
                }
                else
                {
                    UpdateCustomFieldTabRequest      customfieldtabrequest = new UpdateCustomFieldTabRequest();
                    CustomFieldTabViewModel          tab     = Mapper.Map <CustomFieldTab, CustomFieldTabViewModel>(customfieldtab);
                    CustomFieldSectionViewModel      section = new CustomFieldSectionViewModel();
                    GetLeadAdapterCustomFieldRequest getleadadaptercustomfieldsrequest = new GetLeadAdapterCustomFieldRequest();
                    getleadadaptercustomfieldsrequest.AccountId       = AccountID;
                    getleadadaptercustomfieldsrequest.LeadAdapterType = vm.LeadAdapterType;
                    GetLeadAdapterCustomFieldResponse getleadadaptercustomfieldsresponse = customfieldService.GetLeadAdapterCustomFieldsByType(getleadadaptercustomfieldsrequest);
                    section.CustomFields = getleadadaptercustomfieldsresponse.CustomFields.ToList();
                    section.Name         = vm.LeadAdapterType.ToString();
                    section.StatusId     = CustomFieldSectionStatus.Active;
                    tab.Sections.Add(section);
                    tab.StatusId = CustomFieldTabStatus.Active;
                    customfieldtabrequest.CustomFieldTabViewModel = tab;
                    customfieldService.UpdateCustomFieldTab(customfieldtabrequest);
                }
            }
            foreach (Tag tag in leadAdapter.Tags.Where(t => t.Id == 0))
            {
                Tag savedTag = tagRepository.FindBy(tag.TagName, leadAdapter.AccountID);
                indexingService.IndexTag(savedTag);
                accountRepository.ScheduleAnalyticsRefresh(savedTag.Id, (byte)IndexType.Tags);
            }
            response.LeadAdapterViewModel = Mapper.Map <LeadAdapterAndAccountMap, LeadAdapterViewModel>(newLeadAdapter);
            Logger.Current.Informational("Leadadapter inserted successfully.");
            return(new InsertLeadAdapterResponse());
        }
예제 #8
0
        public InsertLeadAdapterResponse InsertFacebookLeadAdapter(InsertLeadAdapterRequest request)
        {
            Logger.Current.Verbose("Request received to insert Facebook lead adapter.");
            InsertLeadAdapterResponse response = new InsertLeadAdapterResponse();
            int newLeadAdapterID = 0;

            try
            {
                LeadAdapterViewModel vm = request.LeadAdapterViewModel;
                int AccountID           = vm.AccountID;

                bool isDuplicate = leadAdaptersRepository.IsDuplicateFacebookAdapter(AccountID, vm.LeadAdapterAndAccountMapId, vm.FacebookLeadAdapterName);
                if (isDuplicate)
                {
                    throw new UnsupportedOperationException("[|LeadAdapter with same name already exists.|]");
                }


                LeadAdapterAndAccountMap leadAdapter = Mapper.Map <LeadAdapterViewModel, LeadAdapterAndAccountMap>(vm);
                leadAdaptersRepository.Insert(leadAdapter);
                LeadAdapterAndAccountMap newLeadAdapter = unitOfWork.Commit() as LeadAdapterAndAccountMap;
                newLeadAdapterID = newLeadAdapter.Id;
                leadAdapter.FacebookLeadAdapter.LeadAdapterAndAccountMapID = newLeadAdapter.Id;

                GetFacebookAppResponse fbResponse = this.GetFacebookApp(new GetFacebookAppRequest()
                {
                    AccountId = AccountID
                });
                string extendedToken = this.GetExtendedPageAccessToken(leadAdapter.FacebookLeadAdapter.UserAccessToken, leadAdapter.FacebookLeadAdapter.PageID, fbResponse.FacebookAppID, fbResponse.FacebookAppSecret);
                leadAdapter.FacebookLeadAdapter.PageAccessToken = !string.IsNullOrEmpty(extendedToken) ? extendedToken : leadAdapter.FacebookLeadAdapter.PageAccessToken;

                leadAdaptersRepository.InsertFacebookLeadAdapter(leadAdapter.FacebookLeadAdapter);

                var hasFacebookFields = leadAdaptersRepository.HasFacebookFields(AccountID);
                if (!hasFacebookFields)
                {
                    CustomFieldTab customfieldtab = customfieldRepository.GetLeadAdapterCustomFieldTab(AccountID);
                    if (customfieldtab == null)
                    {
                        InsertCustomFieldTabRequest      customfieldtabrequest = new InsertCustomFieldTabRequest();
                        CustomFieldTabViewModel          tab     = new CustomFieldTabViewModel();
                        CustomFieldSectionViewModel      section = new CustomFieldSectionViewModel();
                        GetLeadAdapterCustomFieldRequest getleadadaptercustomfieldsrequest = new GetLeadAdapterCustomFieldRequest();
                        getleadadaptercustomfieldsrequest.AccountId       = AccountID;
                        getleadadaptercustomfieldsrequest.LeadAdapterType = vm.LeadAdapterType;
                        GetLeadAdapterCustomFieldResponse getleadadaptercustomfieldsresponse = customfieldService.GetLeadAdapterCustomFieldsByType(getleadadaptercustomfieldsrequest);
                        section.CustomFields = getleadadaptercustomfieldsresponse.CustomFields.ToList();
                        section.Name         = vm.LeadAdapterType.ToString();
                        section.StatusId     = CustomFieldSectionStatus.Active;

                        tab.AccountId        = AccountID;
                        tab.Name             = "Lead Adapter Custom Fields";
                        tab.IsLeadAdapterTab = true;
                        tab.Sections         = new List <CustomFieldSectionViewModel>();
                        tab.Sections.Add(section);
                        tab.StatusId = CustomFieldTabStatus.Active;
                        customfieldtabrequest.CustomFieldTabViewModel = tab;
                        customfieldService.InsertCustomFieldTab(customfieldtabrequest);
                    }
                    else
                    {
                        UpdateCustomFieldTabRequest      customfieldtabrequest = new UpdateCustomFieldTabRequest();
                        CustomFieldTabViewModel          tab     = Mapper.Map <CustomFieldTab, CustomFieldTabViewModel>(customfieldtab);
                        CustomFieldSectionViewModel      section = new CustomFieldSectionViewModel();
                        GetLeadAdapterCustomFieldRequest getleadadaptercustomfieldsrequest = new GetLeadAdapterCustomFieldRequest();
                        getleadadaptercustomfieldsrequest.AccountId       = AccountID;
                        getleadadaptercustomfieldsrequest.LeadAdapterType = vm.LeadAdapterType;
                        GetLeadAdapterCustomFieldResponse getleadadaptercustomfieldsresponse = customfieldService.GetLeadAdapterCustomFieldsByType(getleadadaptercustomfieldsrequest);
                        section.CustomFields = getleadadaptercustomfieldsresponse.CustomFields.ToList();
                        section.Name         = vm.LeadAdapterType.ToString();
                        section.StatusId     = CustomFieldSectionStatus.Active;
                        tab.Sections.Add(section);
                        tab.StatusId = CustomFieldTabStatus.Active;
                        customfieldtabrequest.CustomFieldTabViewModel = tab;
                        customfieldService.UpdateCustomFieldTab(customfieldtabrequest);
                    }
                }

                foreach (Tag tag in leadAdapter.Tags.Where(t => t.Id == 0))
                {
                    Tag savedTag = tagRepository.FindBy(tag.TagName, leadAdapter.AccountID);
                    indexingService.IndexTag(savedTag);
                    accountRepository.ScheduleAnalyticsRefresh(savedTag.Id, (byte)IndexType.Tags);
                }
                response.LeadAdapterViewModel = Mapper.Map <LeadAdapterAndAccountMap, LeadAdapterViewModel>(newLeadAdapter);
            }
            catch (Exception ex)
            {
                Logger.Current.Error("An error occured while creating Facebook LeadAdapter", ex);
                if (newLeadAdapterID != 0)
                {
                    this.DeleteLeadAdapter(new DeleteLeadAdapterRequest(newLeadAdapterID));
                }
                throw;
            }

            Logger.Current.Informational("Facebook Leadadapter inserted successfully.");
            return(new InsertLeadAdapterResponse());
        }
        /*In this method we will get all the files for the lead adapter and we will download the file to our machine
         * in to the local folder of the lead adapter and we will get all the data that is embeded in the file
         * and we will validate each record and also checking the duplicate status and adding the particular recrod
         * and finally we will upload all the contacts and the joblogs and then we will move the file from local folder
         * to archive folder and we will delete the file from the ftp location.
         */
        public virtual void Process()
        {
            try
            {
                leadAdapterAndAccountMap = repository.GetLeadAdapterByID(LeadAdapterAccountMapID);
                ServiceProvider ServiceProviders = serviceProviderRepository
                                                   .GetServiceProviders(1, CommunicationType.Mail, MailType.TransactionalEmail);

                #region Read FTP
                var ftpManager = new FtpService();
                ftpManager.OnServiceFailure += ftpManager_OnServiceFailure;
                var filesList = ftpManager.GetFiles(leadAdapterAndAccountMap.RequestGuid, string.Empty, ServiceProviders.LoginToken,
                                                    leadAdapterAndAccountMap.LeadAdapterTypeID.ToString(), leadAdapterAndAccountMap.AccountName);

                foreach (var fileName in filesList)
                {
                    /*
                     * Read XML file from local path for Builder Number
                     * Get accounts related to builder number
                     * Copy file to all those accounts
                     * **/
                    try
                    {
                        var localFilePath = Path.Combine(leadAdapterAndAccountMap.LocalFilePath, fileName);
                        ftpManager.Download(leadAdapterAndAccountMap.RequestGuid, string.Empty, fileName, localFilePath);
                        List <LeadAdapterAndAccountMap> leadData = new List <LeadAdapterAndAccountMap>();

                        leadData.Add(leadAdapterAndAccountMap);

                        var nodes = GetNodes(localFilePath);
                        foreach (var node in nodes)
                        {
                            var attributes    = node.GetAllAttributesAndElements();
                            var builderNumber = attributes[_fieldMappings.GetOrDefault("BuilderNumber")].Value;
                            IEnumerable <Guid> matchedGuids = ftpManager.FindMatchGuids(leadAdapterAndAccountMap.RequestGuid);
                            leadData.AddRange(repository.GetEmptyCommunities(builderNumber, leadAdapterAndAccountMap.LeadAdapterTypeID, matchedGuids));
                        }
                        if (File.Exists(localFilePath))
                        {
                            File.Delete(localFilePath);
                        }

                        foreach (LeadAdapterAndAccountMap leadAdapter in leadData.GroupBy(g => g.Id).Select(s => s.First()))
                        {
                            Logger.Current.Informational("Inserting joblog for account id : " + leadAdapter.AccountID);
                            var jobLog = new LeadAdapterJobLogs();
                            jobLog.LeadAdapterAndAccountMapID = leadAdapter.Id;
                            jobLog.LeadAdapterJobStatusID     = LeadAdapterJobStatus.Undefined;
                            jobLog.Remarks  = string.Empty;
                            jobLog.FileName = fileName;
                            List <LeadAdapterJobLogDetails> details = new List <LeadAdapterJobLogDetails>();
                            jobLog.LeadAdapterJobLogDetails = details;
                            var leadAdapterJobLogID = importDataRepository.InsertLeadAdapterjob(jobLog, new Guid(), false, false, leadAdapter.AccountID, leadAdapter.CreatedBy, 1, leadAdapter.CreatedBy, true, 0);

                            var filePath        = Path.Combine(leadAdapter.LocalFilePath, fileName);
                            var fileName_rename = Path.GetFileNameWithoutExtension(filePath) + "~" + leadAdapterJobLogID + Path.GetExtension(filePath);
                            localFilePath = Path.Combine(leadAdapter.LocalFilePath, fileName_rename);

                            ftpManager.Download(leadAdapter.RequestGuid, string.Empty, fileName, localFilePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Current.Error("An error occured while accessing file downloading it to matched accounts, filename : " + fileName, ex);
                        continue;
                    }
                }
                ;
                #endregion

                #region Read Local Files
                readLocalFiles(ftpManager);
                #endregion
            }
            catch (IndexOutOfRangeException indexEx)
            {
                Logger.Current.Error("Index out of range exception, account name : " + leadAdapterAndAccountMap.AccountName, indexEx);
            }
            catch (Exception ex)
            {
                Logger.Current.Error("An error occured when uploading the contacts in the lead adapter, account name : " + leadAdapterAndAccountMap.AccountName, ex);
                SendEmail("Exception details" + ex, "Exception while processing data from " + leadAdapterAndAccountMap.LeadAdapterTypeID.ToString() + " LeadAdapter");
            }
            finally
            {
                repository.UpdateProcessedDate(LeadAdapterAccountMapID);
            }
        }