public ShippingRate[] GetShippingRatesSyncParallel(decimal weight, string originZipCode, string destinationZipCode) { // create object that will store results List<ShippingRate> rates = new List<ShippingRate>(); // launch asynchronous requests, which will complete in parallel WebRequest fedExRequest = CreateFedExRequest(weight, originZipCode, destinationZipCode); IAsyncResult fedExResult = fedExRequest.BeginGetResponse(null, null); WebRequest upsRequest = CreateUpsRequest(weight, originZipCode, destinationZipCode); IAsyncResult upsResult = upsRequest.BeginGetResponse(null, null); WebRequest uspsRequest = CreateUspsRequest(weight, originZipCode, destinationZipCode); IAsyncResult uspsResult = uspsRequest.BeginGetResponse(null, null); // wait for all requests; each EndGetResponse will block if the request hasn't completed using (WebResponse response = fedExRequest.EndGetResponse(fedExResult)) rates.AddRange(GetFedExRates(response)); using (WebResponse response = upsRequest.EndGetResponse(upsResult)) rates.AddRange(GetUpsRates(response)); using (WebResponse response = uspsRequest.EndGetResponse(uspsResult)) rates.AddRange(GetUspsRates(response)); return rates.ToArray(); }
public ShippingRate[] GetShippingRatesSync(decimal weight, string originZipCode, string destinationZipCode) { // create object that will store results List<ShippingRate> rates = new List<ShippingRate>(); // launch requests serially, waiting for each one using (WebResponse response = CreateFedExRequest(weight, originZipCode, destinationZipCode).GetResponse()) rates.AddRange(GetFedExRates(response)); using (WebResponse response = CreateUpsRequest(weight, originZipCode, destinationZipCode).GetResponse()) rates.AddRange(GetUpsRates(response)); using (WebResponse response = CreateUspsRequest(weight, originZipCode, destinationZipCode).GetResponse()) rates.AddRange(GetUspsRates(response)); return rates.ToArray(); }
private ShoppingList CreateShoppingList(WeeklyMenu weeklyMenu, Guid? userId) { var listOfamounts = new List<Amount>(); var shoppingList = new ShoppingList { Id = Guid.NewGuid(), UserId = userId, WeeklyMenuId = weeklyMenu.Id, ShoppingListItems = new List<ShoppingListItem>(), Recipes = weeklyMenu.Recipes, WeekNumber = weeklyMenu.WeekNumber }; weeklyMenu.Recipes.ForEach(_=> listOfamounts.AddRange(_.Amounts)); var viewModel = AddToShoppingList(shoppingList, listOfamounts); _unitOfWork.ShoppingLists.Add(shoppingList); _unitOfWork.Commit(); return viewModel; }
public void CreateGraph(int nodeCount, int edgeCount) { _f8.TabulaRasa (); var creationDate = DateHelper.ConvertDateTime (DateTime.Now); var vertexIDs = new List<Int32> (); var prng = new Random (); if (nodeCount < _numberOfToBeTestedVertices) { _numberOfToBeTestedVertices = nodeCount; } _toBeBenchenVertices = new List<VertexModel> (_numberOfToBeTestedVertices); for (var i = 0; i < nodeCount; i++) { vertexIDs.Add (_f8.CreateVertex (creationDate).Id); } if (edgeCount != 0) { foreach (var aVertexId in vertexIDs) { var targetVertices = new HashSet<Int32> (); do { targetVertices.Add (vertexIDs [prng.Next (0, vertexIDs.Count)]); } while (targetVertices.Count < edgeCount); foreach (var aTargetVertex in targetVertices) { _f8.CreateEdge (aVertexId, 0, aTargetVertex, creationDate); } } _toBeBenchenVertices.AddRange (PickInterestingIDs (vertexIDs, prng) .Select (aId => { VertexModel v = null; _f8.TryGetVertex (out v, aId); return v; })); } }
/// <summary> /// Captures all moderations. /// </summary> public void CaptureModeration() { lock (Container.lockModeratedClientList) { var entries = new List<LogEntry>(); var lastModerated = DateTime.MinValue; lock (Repository.Container.lockDatabase) { using (var command = new SQLiteCommand(this.Container.DatabaseConnection)) { command.CommandText = "SELECT MAX(CreationDate) FROM Moderate"; var result = command.ExecuteScalar(); if (result is Int32 && (Int32)result > 0) lastModerated = ((Int32)result).ToDateTime(); if (result is Int64 && (Int64)result > 0) lastModerated = ((Int64)result).ToDateTime(); } } if (lastModerated == DateTime.MinValue) LogService.Debug("Start capturing previous Moderation data. This may take some time..."); uint? index = null; ushort length = 10; try { while (true) { var logEntriesResult = index.HasValue ? QueryRunner.GetLogEntries(length, false, index) : QueryRunner.GetLogEntries(length, false); entries.AddRange(logEntriesResult.LogEntries.Where(m => m.LogLevel == LogLevel.Info && m.LogCategory == "VirtualServer" && m.TimeStamp > lastModerated)); index = logEntriesResult.LogPositionToContinueReading; if (index == 0) break; if (!logEntriesResult.LogEntries.Any() || logEntriesResult.LogEntries.Min(l => l.TimeStamp) < lastModerated) break; if (entries.Count > 10000) break; length = 100; } } catch (ArgumentException) { } foreach (var logEntry in entries) { var entity = ModeratedClientEntity.Parse(logEntry); if (entity.HasValue) { lock (Repository.Container.lockDatabase) { using (var command = new SQLiteCommand(this.Container.DatabaseConnection)) { command.CommandText = string.Format("INSERT INTO Moderate(ClientDatabaseId, ModeratorDatabaseId, ServerGroupId, Type, CreationDate) VALUES({0}, {1}, {2}, {3}, {4})", entity.Value.User, entity.Value.Moderator, entity.Value.ServerGroup, (int)entity.Value.Type, entity.Value.Moderated.ToTimeStamp()); command.ExecuteNonQuery(); } } } } if (lastModerated == DateTime.MinValue) LogService.Debug("Finished capturing Moderation data."); } }
public override void OnExecute() { base.OnExecute(); DateTime now = DateTime.Now; List<ONotification> notifications = TablesLogic.tNotification.LoadList( TablesLogic.tNotification.NextNotificationDateTime < now); foreach (ONotification notification in notifications) { try { // Load up the object and test if the expected // field is null. // OActivity activity = TablesLogic.tActivity.Load(notification.ActivityID); ONotificationProcess notificationProcess = TablesLogic.tNotificationProcess.Load(notification.NotificationProcessID); ONotificationMilestones milestones = notificationProcess.NotificationMilestones; // 2010.06.30 // Kim Foong // Checks if any of the objects are null, if so, we terminate the // notification. // if (activity == null || notificationProcess == null || milestones == null) { using (Connection c = new Connection()) { notification.NextNotificationDateTime = null; notification.Save(); c.Commit(); } continue; } Type type = typeof(TablesLogic).Assembly.GetType("LogicLayer." + activity.ObjectTypeName); LogicLayerPersistentObject obj = PersistentObject.LoadObject(type, activity.AttachedObjectID.Value) as LogicLayerPersistentObject; if (obj == null) { // 2010.06.30 // Kim Foong // Terminate the notification. // using (Connection c = new Connection()) { notification.NextNotificationDateTime = null; notification.Save(); c.Commit(); } continue; } int milestoneNumber = notification.NextNotificationMilestone.Value; int notificationLevel = notification.NextNotificationLevel.Value; string expectedField = (string)milestones.DataRow["ExpectedField" + milestoneNumber]; object value = DataFrameworkBinder.GetValue(obj, expectedField, false); using (Connection c = new Connection()) { if (value == null) { // Now since this value is null, we must send a notification. // // But we need to first find out the list of all the recipients // configured to receive the notification. // ONotificationHierarchyLevel notificationHierarchyLevel = notificationProcess.NotificationHierarchy.FindNotificationHierarchyLevelByLevel(notificationLevel); StringBuilder email = new StringBuilder(); StringBuilder cellphone = new StringBuilder(); // Assign users/roles to the task. // StringBuilder emails = new StringBuilder(); StringBuilder cellphones = new StringBuilder(); if (notificationHierarchyLevel != null) { List<OUser> notifyUsers = new List<OUser>(); List<OPosition> notifyPositions = new List<OPosition>(); // Here we assigned the users // foreach (OUser user in notificationHierarchyLevel.Users) notifyUsers.Add(user); // Then we assign the positions. // foreach (OPosition position in notificationHierarchyLevel.Positions) notifyPositions.Add(position); // Then we assign the positions through the roles. // List<string> roleCodes = new List<string>(); foreach (ORole role in notificationHierarchyLevel.Roles) roleCodes.Add(role.RoleCode); List<OPosition> assignedPositions = OPosition.GetPositionsByRoleCodesAndObject(obj, roleCodes.ToArray()); notifyPositions.AddRange(assignedPositions); // Then we get a distinct list of users who will be // the recipients to our notification. // List<OUser> users = TablesLogic.tUser.LoadList( TablesLogic.tUser.ObjectID.In(notifyUsers) | TablesLogic.tUser.Positions.ObjectID.In(notifyPositions)); // Construct the list of email and SMS recipients. // string emailRecipients = ""; string smsRecipients = ""; foreach (OUser user in users) { if (user.UserBase.Email != null && user.UserBase.Email.Trim() != "") emailRecipients += user.UserBase.Email.Trim() + ";"; if (user.UserBase.Cellphone != null && user.UserBase.Cellphone.Trim() != "") smsRecipients += user.UserBase.Cellphone.Trim() + ";"; } // Generate and the send message to the users. // OMessageTemplate messageTemplate = null; object messageTemplateId = notificationProcess.DataRow["MessageTemplate" + milestoneNumber + "ID"]; if (messageTemplateId != null && messageTemplateId != DBNull.Value) messageTemplate = TablesLogic.tMessageTemplate.Load((Guid)messageTemplateId); if (messageTemplate != null) messageTemplate.GenerateAndSendMessage(obj, emailRecipients, smsRecipients); } } // Now, determine the next notification date. // // Find out when is the next date/time the next // notification should occur. // DateTime? nextNotificationDateTime = null; int? nextNotificationLevel = null; int? nextNotificationMilestone = null; notificationProcess.GetNextNotificationDateTime(obj, notification.NextNotificationDateTime, ref nextNotificationDateTime, ref nextNotificationMilestone, ref nextNotificationLevel); notification.NextNotificationDateTime = nextNotificationDateTime; notification.NextNotificationLevel = nextNotificationLevel; notification.NextNotificationMilestone = nextNotificationMilestone; notification.Save(); c.Commit(); } } catch (Exception ex) { LogEvent("An error occured while performing notification ObjectID = '" + notification.ObjectID + "' " + ex.Message + "\n" + ex.StackTrace); } } }