private bool MessagesExist() { var nameInventoryPrice = KCMSMQQueueHelper.GetSyncName(SyncType.InventoryPrice); var nameInventoryQuantity = KCMSMQQueueHelper.GetSyncName(SyncType.InventoryQuantity); var nameVendorQuantity = KCMSMQQueueHelper.GetSyncName(SyncType.VendorQuantity); string pnAddressInventoryPrice = Environment.MachineName + @"\private$\" + KCMSMQQueueHelper.GetSyncQueueName(SyncType.InventoryPrice); string pnAddressInventoryQuantity = Environment.MachineName + @"\private$\" + KCMSMQQueueHelper.GetSyncQueueName(SyncType.InventoryQuantity); string pnAddressVendorQuantity = Environment.MachineName + @"\private$\" + KCMSMQQueueHelper.GetSyncQueueName(SyncType.VendorQuantity); KCMSMQueueReader queueInventoryPrice = new KCMSMQueueReader(pnAddressInventoryPrice); KCMSMQueueReader queueInventoryQuantity = new KCMSMQueueReader(pnAddressInventoryQuantity); KCMSMQueueReader queueVendorQuantity = new KCMSMQueueReader(pnAddressVendorQuantity); bool queueInventoryPriceExists = queueInventoryPrice.IsQueueExists; bool queueInventoryQuantityExists = queueInventoryQuantity.IsQueueExists; bool queueVendorQuantityExists = queueVendorQuantity.IsQueueExists; if (queueInventoryPriceExists && queueInventoryPrice.GetMessagesCount() > 0) { return(true); } else if (queueInventoryQuantityExists && queueInventoryQuantity.GetMessagesCount() > 0) { return(true); } else if (queueVendorQuantityExists && queueVendorQuantity.GetMessagesCount() > 0) { return(true); } return(false); }
private bool IsPreviousMessage(SyncType syncType, string inventoryId, Message msg, string warehouse) { bool result = false; var reader = new StreamReader(msg.BodyStream); try { var json = reader.ReadToEnd(); var msgObj = JsonConvert.DeserializeObject <KCMSMQMessage>(json); if (KCMSMQQueueHelper.ParseSyncName(msgObj.Query) == syncType) { //should be only one inserted var firstInserted = (JToken)msgObj.Inserted.FirstOrDefault(); if (firstInserted != null && firstInserted.SelectToken(INVENTORY_ID)?.ToString() == inventoryId && firstInserted.SelectToken("WarehouseID")?.ToString() == warehouse) { result = true; } } } finally { reader.Dispose(); } return(result); }
public virtual IEnumerable messages() { Messages.Cache.Clear(); PushNotificationsHook pricePN = PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.InventoryPrice)); PushNotificationsHook quantityPN = PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.InventoryQuantity)); PushNotificationsHook vendorPN = PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.VendorQuantity)); if (pricePN == null || quantityPN == null || vendorPN == null) { throw new PXException(KCMessages.MSMQShouldBeInitialized); } KCMSMQueueReader price = null; KCMSMQueueReader quantity = null; KCMSMQueueReader vendor = null; try { price = new KCMSMQueueReader(pricePN.Address); quantity = new KCMSMQueueReader(quantityPN.Address); vendor = new KCMSMQueueReader(vendorPN.Address); if (price.IsQueueExists && quantity.IsQueueExists && vendor.IsQueueExists) { price.CleanEmptyMessages(); quantity.CleanEmptyMessages(); vendor.CleanEmptyMessages(); } SetMessages(price); SetMessages(quantity); SetMessages(vendor); } catch { throw new PXException(KCMessages.MSMQShouldBeInitialized); } finally { price?.Dispose(); quantity?.Dispose(); vendor?.Dispose(); } return(Messages.Cache.Inserted); }
private KCMSMQueueReader CreateQueue(SyncType syncType) { var pnName = KCMSMQQueueHelper.GetSyncName(syncType); string pnAddress = Environment.MachineName + @"\private$\" + KCMSMQQueueHelper.GetSyncQueueName(syncType); KCMSMQueueReader queue = new KCMSMQueueReader(pnAddress); bool queueExists = queue.IsQueueExists; if (queueExists) { queue.DeleteQueue(); } queue.CreateQueue(); string activity = KCMSMQConstants.createQueue(pnName); string comments = KCMSMQConstants.createQueueSuccess(pnName); InsertInitializationActivity(activity, comments); return(queue); }
private void CreatePN(SyncType syncType) { var pnName = KCMSMQQueueHelper.GetSyncName(syncType); PushNotificationsHook notification = PushNotifications.SelectSingle(pnName); if (notification != null) { PushNotifications.Delete(notification); PushNotifications.Cache.PersistDeleted(notification); foreach (var item in PushNotifocationSource.Select(notification.HookId)) { var obj = item.GetItem <PushNotificationsSource>(); PushNotifocationSource.Delete(obj); PushNotifocationSource.Cache.PersistDeleted(obj); } foreach (var item in PushNotifocationErrors.Select(notification.HookId)) { var obj = item.GetItem <PushNotificationsErrors>(); PushNotifocationErrors.Delete(obj); PushNotifocationErrors.Cache.PersistDeleted(obj); } foreach (var item in PushNotifocationFailed.Select(notification.HookId)) { var obj = item.GetItem <PushNotificationsFailedToSend>(); PushNotifocationFailed.Delete(obj); PushNotifocationFailed.Cache.PersistDeleted(obj); } Persist(); } notification = new PushNotificationsHook { Name = pnName, Type = "KCMQ", Active = true, Address = Environment.MachineName + @"\private$\" + KCMSMQQueueHelper.GetSyncQueueName(syncType), HookId = Guid.NewGuid() }; PushNotifications.Insert(notification); GIDesign design = Design.SelectSingle(pnName); PushNotificationsSource source = new PushNotificationsSource { HookId = notification.HookId, DesignID = design.DesignID, SourceType = "GI" }; PushNotifocationSource.Insert(source); InsertInitializationActivity(KCMSMQConstants.createPN(pnName), KCMSMQConstants.createPNSuccess(pnName)); Persist(); }
//16.05.19 VG: NB!!! Do not change method signature public void PostNotification(NotificationResultWrapper results, CancellationToken cancellationToken) { if (!MessageQueue.Exists(Address)) { return; } //23.09.19 KA: Getting the type of synchrnization var syncType = KCMSMQQueueHelper.ParseSyncName(results.Result.Query); if (syncType == SyncType.Unknown) { return; } var jObj = JObject.Parse(results.SerializedResult); var insertedObjects = jObj.SelectToken(INSERTED); if (insertedObjects?.HasValues != true) { //return; } var deletedObjects = jObj.SelectToken(DELETED); var baseMessage = JObject.Parse(results.SerializedResult); baseMessage.SelectToken(INSERTED)?.Parent.Remove(); baseMessage.SelectToken(DELETED)?.Parent.Remove(); var insertedList = insertedObjects.ToArray(); var deletedList = deletedObjects.ToArray(); using (new PXConnectionScope()) { var graph = PXGraph.CreateInstance <KCInventoryManagementMaint>(); for (var x = 0; x < insertedList.Length; x++) { var inserted = insertedList[x]; var inventoryCD = inserted.SelectToken(INVENTORY_ID)?.ToString(); var warehouseID = inserted.SelectToken(WAREHOUSE_ID)?.ToString(); if (string.IsNullOrWhiteSpace(inventoryCD)) { continue; } JToken deleted = null;// deletedObjects?.FirstOrDefault(z => z.SelectToken(INVENTORY_ID)?.ToString() == inventoryCD); if (syncType == SyncType.InventoryPrice && IsPriceChanged(inserted, x)) { FixNullPrices(inserted); SetRetailPrice(inserted); SetDefPrice(inserted); SendUpdateMessage(syncType, inventoryCD, baseMessage, inserted, deleted); } else if (syncType == SyncType.InventoryQuantity || syncType == SyncType.VendorQuantity) { if (syncType == SyncType.InventoryQuantity) { var rule = graph.InventoryTrackingRule.SelectSingle().InventoryTrackingRule; if (rule == KCInventoryTrackingRulesConstants.Consolidate) { SendInventoryUpdateMessage(syncType, inventoryCD, baseMessage, inserted, deleted, warehouseID); } else { var warehouse = inserted.SelectToken("INSite_siteID")?.ToString(); if (!string.IsNullOrWhiteSpace(warehouse)) { SendInventoryUpdateMessage(syncType, inventoryCD, baseMessage, inserted, deleted); } } } else if (syncType == SyncType.VendorQuantity && graph.HasAnyMappedVendors()) { SendInventoryUpdateMessage(syncType, inventoryCD, baseMessage, inserted, deleted); } } } for (var x = 0; x < deletedList.Length; x++) { if (deletedList.Length == 1 && insertedList.Length == 0) { var del = deletedList[x]; var inventoryCD = del.SelectToken(INVENTORY_ID)?.ToString(); if (string.IsNullOrWhiteSpace(inventoryCD)) { continue; } var deleted = deletedObjects?.FirstOrDefault(z => z.SelectToken(INVENTORY_ID)?.ToString() == inventoryCD); JToken inserted = deleted; FixNullPrices(deleted); SetRetailPrice(deleted); SendUpdateMessage(syncType, inventoryCD, baseMessage, inserted, deleted); } } } }
public void ProcessMessageAPI(KCPriceAndInventoryMessage message) { KCDataExchangeMaint masterGraph = PXGraph.CreateInstance <KCDataExchangeMaint>(); KCSiteMaster connection = masterGraph.Connection.SelectSingle(); KCARestClient client = new KCARestClient(connection); KCInventoryItemAPIHelper helper = new KCInventoryItemAPIHelper(client, logger.LoggerProperties); KCPriceAndInventoryMaint graph = PXGraph.CreateInstance <KCPriceAndInventoryMaint>(); var syncType = KCMSMQQueueHelper.ParseSyncQueueName(message.Address); var syncName = KCMSMQQueueHelper.GetSyncName(syncType); PushNotificationsHook pnHook = graph.PushNotification.SelectSingle(syncName); if (syncType == SyncType.InventoryQuantity) { KCMSMQueueReader quantity = null; try { quantity = new KCMSMQueueReader(pnHook.Address); if (quantity.TryReceiveMessage(message.MessageID, out var msg)) { foreach (object product in msg.Inserted) { KCMSMQInventoryQuantity quantityProduct = JsonConvert.DeserializeObject <KCMSMQInventoryQuantity>(product.ToString()); UpdateSiteQuantity(masterGraph, quantityProduct, helper); } } } finally { quantity?.Dispose(); } } else if (syncType == SyncType.InventoryPrice) { KCMSMQueueReader price = null; try { price = new KCMSMQueueReader(pnHook.Address); if (price.TryReceiveMessage(message.MessageID, out var msg)) { foreach (object product in msg.Inserted) { KCMSMQInventoryPrice priceProduct = JsonConvert.DeserializeObject <KCMSMQInventoryPrice>(product?.ToString()); UpdatePrice(masterGraph, priceProduct, helper); } } } finally { price?.Dispose(); } } else if (syncType == SyncType.VendorQuantity) { KCMSMQueueReader vendor = null; try { vendor = new KCMSMQueueReader(pnHook.Address); if (vendor.TryReceiveMessage(message.MessageID, out var msg)) { foreach (object product in msg.Inserted) { KCMSMQInventoryQuantity quantityProduct = JsonConvert.DeserializeObject <KCMSMQInventoryQuantity>(product.ToString()); UpdateSiteQuantity(masterGraph, quantityProduct, helper); } } } finally { vendor?.Dispose(); } } }
public void ProcessMessageFTP(List <KCPriceAndInventoryMessage> messages, CancellationToken cancellationToken) { KCDataExchangeMaint masterGraph = PXGraph.CreateInstance <KCDataExchangeMaint>(); KCPriceAndInventoryMaint graph = PXGraph.CreateInstance <KCPriceAndInventoryMaint>(); var MSMQQuantityUpdates = new Dictionary <string, List <KCAPIQuantity> >(); var MSMQPrices = new Dictionary <string, KCAPIInventoryItem>(); var productsForExportPrice = new List <KeyValuePair <string, InventoryItem> >(); var productsForExportQuantity = new Dictionary <string, KeyValuePair <string, InventoryItem> >(); PushNotificationsHook pricePN = graph.PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.InventoryPrice)); PushNotificationsHook quantityPN = graph.PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.InventoryQuantity)); PushNotificationsHook vendorPN = graph.PushNotification.SelectSingle(KCMSMQQueueHelper.GetSyncName(SyncType.VendorQuantity)); KCMSMQueueReader price = null; KCMSMQueueReader quantity = null; KCMSMQueueReader vendor = null; try { price = new KCMSMQueueReader(pricePN.Address); quantity = new KCMSMQueueReader(quantityPN.Address); vendor = new KCMSMQueueReader(vendorPN.Address); foreach (KCPriceAndInventoryMessage msg in messages) { var syncType = KCMSMQQueueHelper.ParseSyncQueueName(msg.Address); if (syncType == SyncType.InventoryQuantity || syncType == SyncType.VendorQuantity) { List <object> insertedMessages = null; if (syncType == SyncType.InventoryQuantity && quantity.TryReceiveMessage(msg.MessageID, out var invMessage)) { insertedMessages = invMessage.Inserted; } if (syncType == SyncType.VendorQuantity && vendor.TryReceiveMessage(msg.MessageID, out var vendorMessage)) { insertedMessages = vendorMessage.Inserted; } if (insertedMessages != null) { foreach (object item in insertedMessages) { KCMSMQInventoryQuantity quantityProduct = JsonConvert.DeserializeObject <KCMSMQInventoryQuantity>(item.ToString()); InventoryItem inventoryItem = masterGraph.ProductByInvCd.Select(quantityProduct.InventoryID.Trim()); KNSIKCInventoryItem kcProduct = masterGraph.KCInventoryItem.SelectSingle(inventoryItem.InventoryID); if (kcProduct.UsrKCCAID != null) { var key = inventoryItem.InventoryCD; if (!MSMQQuantityUpdates.ContainsKey(key)) { MSMQQuantityUpdates.Add(key, quantityProduct.Updates); } if (!productsForExportQuantity.ContainsKey(key)) { InventoryItemPCExt inventoryItemPCExt = inventoryItem.GetExtension <InventoryItemPCExt>(); productsForExportQuantity.Add(inventoryItem.InventoryCD, new KeyValuePair <string, InventoryItem>(inventoryItemPCExt.UsrKNCompositeType, inventoryItem)); } } } } } else if (syncType == SyncType.InventoryPrice) { if (price.TryReceiveMessage(msg.MessageID, out var message)) { foreach (object item in message.Inserted) { KCMSMQInventoryPrice priceProduct = JsonConvert.DeserializeObject <KCMSMQInventoryPrice>(item.ToString()); InventoryItem inventoryItem = masterGraph.ProductByInvCd.Select(priceProduct.InventoryID.Trim()); InventoryItemPCExt inventoryItemPCExt = inventoryItem.GetExtension <InventoryItemPCExt>(); productsForExportPrice.Add(new KeyValuePair <string, InventoryItem>(inventoryItemPCExt.UsrKNCompositeType, inventoryItem)); MSMQPrices.Add(inventoryItem.InventoryCD, KCMapInventoryItem.GetAPIMSMQInventoryPrice(priceProduct)); } } } } } finally { price?.Dispose(); quantity?.Dispose(); vendor?.Dispose(); } Export(masterGraph, productsForExportPrice, cancellationToken, MSMQPrices); Export(masterGraph, productsForExportQuantity.Values.ToList(), cancellationToken, null, MSMQQuantityUpdates); }