protected override void Process() { using (var unit = GetUnitOfWork()) { #region Communication var comms = (from o in unit.Scope.Repository <EdiCommunication>().GetAll() where !o.LastRun.HasValue || (o.NextRun.HasValue && DateTime.Now >= o.NextRun.Value) select o).ToList(); foreach (var comm in comms) { try { ediVendorID = comm.EdiFieldMappings.FirstOrDefault().EdiVendorID; string connection = comm.Connection; if (ConfigurationManager.ConnectionStrings[comm.Connection] != null) { connection = ConfigurationManager.ConnectionStrings[comm.Connection].ConnectionString; } EDICommunicationLayer eCom = new EDICommunicationLayer((EdiConnectionType)comm.EdiConnectionType, connection); var query = SetQueryParams(comm.Query, comm); var data = eCom.GetVendorData(query); var ediMapping = unit.Scope.Repository <EdiFieldMapping>().GetAll(x => x.EdiCommunicationID == comm.EdiCommunicationID).ToList(); var matchFields = (from m in ediMapping where m.MatchField group m by m.TableName into tableGroup select tableGroup).ToDictionary(x => x.Key, x => x); foreach (DataRow row in data.Tables[0].AsEnumerable()) { string tableName = matchFields.FirstOrDefault().Key; Type tableType = Type.GetType(tableName); var repoMethod = typeof(IScope).GetMethod("Repository").MakeGenericMethod(tableType); var repo = repoMethod.Invoke(unit.Scope, null); var table = repo.GetType().GetMethod("GetAllAsQueryable").Invoke(repo, new object[1] { null }); IQueryable result = null; var set = (table as IQueryable); var mField = matchFields[tableName].FirstOrDefault(); foreach (var matchField in matchFields[tableName]) { if (!String.IsNullOrEmpty(matchField.VendorFieldName)) { var value = row[matchField.VendorFieldName]; var propertyType = tableType.GetProperty(matchField.FieldName); LambdaExpression expression = null; if (propertyType.PropertyType == typeof(String)) { expression = System.Linq.Dynamic.DynamicExpression.ParseLambda(set.ElementType, typeof(bool), string.Format(string.Format(string.Format("({0}) == \"{1}\"", matchField.FieldName, value)))); } else { expression = System.Linq.Dynamic.DynamicExpression.ParseLambda(set.ElementType, typeof(bool), string.Format(string.Format("({0}) == \"{1}\"", matchField.FieldName, Convert.ChangeType(value, (TypeCode)matchField.VendorFieldType)))); } if (result == null) { result = set.Provider.CreateQuery ( Expression.Call(typeof(Queryable), "Where", new Type[] { set.ElementType }, set.Expression, Expression.Quote(expression))); } else { result = result.Provider.CreateQuery ( Expression.Call(typeof(Queryable), "Where", new Type[] { set.ElementType }, set.Expression, Expression.Quote(expression))); } } var count = ((IQueryable <EdiOrderResponse>)result.AsQueryable()).Count(); if (count == 1) { EdiOrderResponse resp = ((IQueryable <EdiOrderResponse>)result.AsQueryable()).FirstOrDefault(); var newRecord = GenerateResponse(unit.Scope, resp, data.Tables[0].AsEnumerable().Where(x => Convert.ChangeType(x.Field <object>(mField.VendorFieldName), (TypeCode)mField.VendorFieldType) == row[mField.VendorFieldName]), ediMapping); newRecord.EdiOrderResponseLines.ForEach((x, idx) => { x.EdiOrderLine.SetStatus(EdiOrderStatus.ReceiveShipmentNotificaiton, unit); x.EdiOrderLine.SetStatus(EdiOrderStatus.WaitForInvoiceNotification, unit); }); unit.Save(); } } } comm.LastRun = DateTime.Now; CronExpression exp = new CronExpression(comm.Schedule); comm.NextRun = exp.GetTimeAfter(DateTime.UtcNow); unit.Save(); } catch (Exception ex) { comm.Remark = string.Format("Communication rule failed: {0}", ex.Message); log.AuditError(string.Format("Communication rule failed (rule: {0})", comm.EdiCommunicationID), ex, "Communication EDI orders"); } } #endregion try { ediProcessor.GetCustomOrderResponses(unit, Config); unit.Save(); } catch (Exception ex) { log.AuditCritical("CustomOrderresponse failed processing", ex); } } }