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); } }