public static IEmailSyncProvider GetExchanger(EMailSyncServer server, EMailSyncPolicy policy) { if (server == null || String.IsNullOrEmpty(server.ServerType) || !_exchangers.ContainsKey(server.ServerType)) { throw new PXException(Messages.EmailExchangeProviderNotFound); } IEmailSyncProvider prov = (IEmailSyncProvider)Activator.CreateInstance(_exchangers[server.ServerType], server, policy); return(prov); }
private static Tuple <EMailSyncServer, EMailSyncPolicy, PXSyncMailbox> GetConfig(int emailAccountID) { PXGraph graph = new PXGraph(); foreach (PXResult <EMailSyncAccount, EMailSyncServer, EMailAccount, EPEmployee, Contact> row in PXSelectJoin <EMailSyncAccount, InnerJoin <EMailSyncServer, On <EMailSyncServer.accountID, Equal <EMailSyncAccount.serverID> >, InnerJoin <EMailAccount, On <EMailAccount.emailAccountID, Equal <EMailSyncAccount.emailAccountID> >, LeftJoin <EPEmployee, On <EMailSyncAccount.employeeID, Equal <EPEmployee.bAccountID> >, LeftJoin <Contact, On <EPEmployee.defContactID, Equal <Contact.contactID>, And <EPEmployee.parentBAccountID, Equal <Contact.bAccountID> > > > > > >, Where <EMailSyncAccount.emailAccountID, Equal <Required <EMailSyncAccount.emailAccountID> > >, OrderBy <Asc <EMailSyncAccount.serverID, Asc <EMailSyncAccount.employeeID> > > > .SelectSingleBound(graph, null, emailAccountID)) { EMailSyncServer server = row; EMailSyncAccount account = row; Contact contact = row; EMailAccount eMailAccount = row; if (server == null || account == null || String.IsNullOrEmpty(account.Address)) { throw new PXException(Messages.EmailExchangeAccountNotFound); } if (server.IsActive != true) { throw new PXException(Messages.EmailExchangeAccountNotEnabled); } string address = (contact != null ? contact.EMail : null) ?? account.Address; PXSyncMailbox mailbox = new PXSyncMailbox(address, account.EmployeeID.Value, emailAccountID, new PXSyncMailboxPreset(null, null), new PXSyncMailboxPreset(null, null), eMailAccount.IncomingProcessing ?? false); string policyName = account.PolicyName ?? server.DefaultPolicyName; EMailSyncPolicy policy = PXSelect <EMailSyncPolicy, Where <EMailSyncPolicy.policyName, Equal <Required <EMailSyncPolicy.policyName> > > > .SelectSingleBound(graph, null, policyName); if (policy == null) { throw new PXException(Messages.EmailExchangePolicyNotFound, account.Address); } if (String.IsNullOrEmpty(server.ServerType) || !_exchangers.ContainsKey(server.ServerType)) { throw new PXException(Messages.EmailExchangeProviderNotFound); } return(Tuple.Create(server, policy, mailbox)); } throw new PXException(Messages.EmailExchangeAccountNotFound); }
//protected List<PXExchangeEvent> log = new List<PXExchangeEvent>(); //protected void FlushLog() //{ // if (log == null || log.Count <= 0) return; // foreach (PXExchangeEvent occasion in log) // { // SaveEvent(occasion); // } // log = new List<PXExchangeEvent>(); //} #endregion #endregion protected BaseExchangeSyncProvider(EMailSyncServer account, EMailSyncPolicy policy) { if (account == null) { throw new ArgumentNullException("account"); } if (policy == null) { throw new ArgumentNullException("policy"); } Account = account; Policy = policy; Cache = new PXSyncCache(); LogVerbose(null, Messages.EmailExchangeProviderInitialised, Policy.PolicyName); }
public MicrosoftExchangeSyncProvider(EMailSyncServer server, EMailSyncPolicy policy) : base(server, policy) { }
protected void ProcessInternal(ProcessingContext context) { Dictionary <int, List <int> > processing = new Dictionary <int, List <int> >( ); foreach (EMailSyncAccount account in context.Accounts) { List <int> list; if (!processing.TryGetValue(account.ServerID.Value, out list)) { processing[account.ServerID.Value] = list = new List <int>(); } list.Add(account.EmployeeID.Value); } foreach (int serverID in processing.Keys) { Dictionary <string, ProcessingBox> buckets = new Dictionary <string, ProcessingBox>(); EMailSyncServer server = null; foreach (PXResult <EMailSyncAccount, EMailSyncServer, EMailAccount, EPEmployee> row in PXSelectJoin <EMailSyncAccount, InnerJoin <EMailSyncServer, On <EMailSyncServer.accountID, Equal <EMailSyncAccount.serverID> >, InnerJoin <EMailAccount, On <EMailAccount.emailAccountID, Equal <EMailSyncAccount.emailAccountID> >, LeftJoin <EPEmployee, On <EMailSyncAccount.employeeID, Equal <EPEmployee.bAccountID> > > > >, Where <EMailSyncServer.accountID, Equal <Required <EMailSyncServer.accountID> >, And <EMailSyncAccount.address, IsNotNull> >, OrderBy <Asc <EMailSyncAccount.serverID, Asc <EMailSyncAccount.employeeID> > > > .Select(this, serverID)) { server = (EMailSyncServer)row; EMailSyncAccount account = (EMailSyncAccount)row; EMailAccount eMailAccount = (EMailAccount)row; if (!processing.ContainsKey(serverID) || !processing[serverID].Contains(account.EmployeeID.Value)) { continue; } string address = account.Address; EMailSyncPolicy policy = null; if (!String.IsNullOrEmpty(account.PolicyName)) { policy = context.Policies[account.PolicyName]; } if (policy == null && !String.IsNullOrEmpty(server.DefaultPolicyName)) { policy = context.Policies[server.DefaultPolicyName]; } if (policy == null) { throw new PXException(Messages.EmailExchangePolicyNotFound, account.Address); } ProcessingBox bucket; if (!buckets.TryGetValue(policy.PolicyName, out bucket)) { buckets[policy.PolicyName] = bucket = new ProcessingBox(policy); } if (policy.ContactsSync ?? false) { bucket.Contacts.Add( new PXSyncMailbox(address, account.EmployeeID.Value, account.EmailAccountID, new PXSyncMailboxPreset(account.ContactsExportDate, account.ContactsExportFolder), new PXSyncMailboxPreset(account.ContactsImportDate, account.ContactsImportFolder), eMailAccount.IncomingProcessing ?? false) { Reinitialize = account.ToReinitialize == true, IsReset = account.IsReset == true }); } if (policy.EmailsSync ?? false) { bucket.Emails.Add( new PXSyncMailbox(address, account.EmployeeID.Value, account.EmailAccountID, new PXSyncMailboxPreset(account.EmailsExportDate, account.EmailsExportFolder), new PXSyncMailboxPreset(account.EmailsImportDate, account.EmailsImportFolder), eMailAccount.IncomingProcessing ?? false) { Reinitialize = account.ToReinitialize == true, IsReset = account.IsReset == true }); } if (policy.TasksSync ?? false) { bucket.Tasks.Add( new PXSyncMailbox(address, account.EmployeeID.Value, account.EmailAccountID, new PXSyncMailboxPreset(account.TasksExportDate, account.TasksExportFolder), new PXSyncMailboxPreset(account.TasksImportDate, account.TasksImportFolder), eMailAccount.IncomingProcessing ?? false) { Reinitialize = account.ToReinitialize == true, IsReset = account.IsReset == true }); } if (policy.EventsSync ?? false) { bucket.Events.Add( new PXSyncMailbox(address, account.EmployeeID.Value, account.EmailAccountID, new PXSyncMailboxPreset(account.EventsExportDate, account.EventsExportFolder), new PXSyncMailboxPreset(account.EventsImportDate, account.EventsImportFolder), eMailAccount.IncomingProcessing ?? false) { Reinitialize = account.ToReinitialize == true, IsReset = account.IsReset == true }); } } if (server == null) { continue; } List <Exception> errors = new List <Exception>(); foreach (string policy in buckets.Keys) { ProcessingBox bucket = buckets[policy]; using (IEmailSyncProvider provider = PXEmailSyncHelper.GetExchanger(server, bucket.Policy)) { foreach (PXEmailSyncOperation.Operations operation in Enum.GetValues(typeof(PXEmailSyncOperation.Operations))) //do one time for all existing sync types { try { switch (operation) { case PXEmailSyncOperation.Operations.Emails: if (bucket.EmailsPending) { provider.EmailsSync(bucket.Policy, PXEmailSyncDirection.Parse(bucket.Policy.EmailsDirection), bucket.Emails); } break; case PXEmailSyncOperation.Operations.Contacts: if (bucket.ContactsPending) { provider.ContactsSync(bucket.Policy, PXEmailSyncDirection.Parse(bucket.Policy.ContactsDirection), bucket.Contacts); } break; case PXEmailSyncOperation.Operations.Tasks: if (bucket.TasksPending) { provider.TasksSync(bucket.Policy, PXEmailSyncDirection.Parse(bucket.Policy.TasksDirection), bucket.Tasks); } break; case PXEmailSyncOperation.Operations.Events: if (bucket.EventsPending) { provider.EventsSync(bucket.Policy, PXEmailSyncDirection.Parse(bucket.Policy.EventsDirection), bucket.Events); } break; } } catch (PXExchangeSyncItemsException ex) { if (bucket.Policy.SkipError == true) { continue; } if (ex.Errors.Count > 0) { foreach (string address in ex.Errors.Keys) { string message = String.Join(Environment.NewLine, ex.Errors[address].ToArray()); context.StoreError(serverID, address, message); } } } catch (PXExchangeSyncFatalException ex) { if (bucket.Policy.SkipError == true) { continue; } errors.Add(new PXException(Messages.EmailExchangeSyncOperationError, operation.ToString())); if (!String.IsNullOrEmpty(ex.Mailbox)) { context.StoreError(serverID, ex.Mailbox, ex.InnerMessage); } else { errors.Add(ex); } } catch (Exception ex) { if (bucket.Policy.SkipError == true) { continue; } errors.Add(new PXException(Messages.EmailExchangeSyncOperationError, operation.ToString())); errors.Add(ex); } } } } if (errors.Count > 0) { throw new PXException(String.Join(Environment.NewLine, errors.Select(e => e.Message).ToArray())); } } for (int index = 0; index < context.Accounts.Count; index++) { PXProcessing.SetInfo(index, ActionsMessages.RecordProcessed); } //handle exceptions if (context.Exceptions.Count > 0) { foreach (int index in context.Exceptions.Keys) { List <string> errors = context.Exceptions[index]; if (errors == null || errors.Count < 0) { continue; } PXProcessing.SetError(index, String.Join(Environment.NewLine, errors.ToArray( ))); } throw new PXException(Messages.EmailExchangeSyncFailed); } }