Quantcast
Channel: C1 CMS Foundation - Open Source on .NET
Viewing all articles
Browse latest Browse all 2540

New Post: Longtime operation in workflow

$
0
0
Yes, in this case i use SQL Data Provider
Code has try/catch block
        private void savePricesActivity_ExecuteCode(object sender, EventArgs e)
        {
            string filename = this.GetBinding<string>("Filename");
            UploadedFile uploadedFile = this.GetBinding<UploadedFile>("UploadedFile");
            string enc = this.GetBinding<string>("Encoding") ?? "UTF-8";
            Guid warehouseId = this.GetBinding<Guid>("SelectedWarehouse");
            Guid currencyId = this.GetBinding<Guid>("SelectedCurrency");
            bool clearWarehouse = this.GetBinding<bool>("ClearWarehouse");

            var warehouse = DataFacade.GetData<Warehouse>().FirstOrDefault(w => w.Id == warehouseId);
            if (warehouse == null)
            {
                this.ShowMessage(Composite.C1Console.Events.DialogType.Warning, "Import price", "Warehouse not found");
                this.LogMessage(Composite.Core.Logging.LogLevel.Warning, "Import price error. Warehouse not found.");
                return;
            }

            Dictionary<string, string> map = new Dictionary<string, string>();
            map.Add("Title", this.GetBinding<string>("MapNameField"));
            map.Add("Description", this.GetBinding<string>("MapDescField"));
            map.Add("Brand", this.GetBinding<string>("MapBrandField"));
            map.Add("Qty", this.GetBinding<string>("MapQtyField"));
            map.Add("Price1", this.GetBinding<string>("MapPrice1Field"));
            map.Add("Price2", this.GetBinding<string>("MapPrice2Field"));
            map.Add("Price3", this.GetBinding<string>("MapPrice3Field"));
            map.Add("Price4", this.GetBinding<string>("MapPrice4Field"));
            map.Add("Delivery", this.GetBinding<string>("MapDeliveryField"));

            uploadedFile.FileStream.Position = 0;
            var brandDict = new Dictionary<string, Guid>();

            var cnt = 0; var noName = 0; var skiped = 0; var t1 = DateTime.Now;
            try
            {
                using (TransactionScope transactionScope = TransactionsFacade.CreateNewScope(TimeSpan.FromHours(6.0)))
                {
                    if (clearWarehouse)
                    {
                        DataFacade.Delete<ProductElement>(p => p.Warehouse == warehouseId);
                    }

                    foreach (var item in CatalogFacade.ParseCSV(uploadedFile.FileStream, map, enc))
                    {
                        Guid? brandId = null;
                        var brandName = this.GetAttrValueSafe(item.Attribute("Brand"));
                        if (!string.IsNullOrEmpty(brandName))
                        {
                            var brandNameUpper = brandName.ToUpper();
                            if (brandDict.ContainsKey(brandNameUpper))
                            {
                                brandId = brandDict[brandNameUpper];
                            }
                            else
                            {
                                var brand = DataFacade.GetData<Brand>().FirstOrDefault(b => b.Code == brandNameUpper);
                                if (brand == null)
                                {
                                    brand = DataFacade.BuildNew<Brand>();
                                    brand.Id = Guid.NewGuid();
                                    brand.Code = brandNameUpper;
                                    brand.Title = brandName;
                                    DataFacade.AddNew<Brand>(brand);
                                }

                                brandDict.Add(brandNameUpper, brand.Id);
                                brandId = brand.Id;
                            }
                        }

                        string name = this.GetAttrValueSafe(item.Attribute("Title"));
                        if (!string.IsNullOrEmpty(name))
                        {
                            try
                            {
                                var elem = DataFacade.BuildNew<ProductElement>();
                                elem.Id = Guid.NewGuid();
                                elem.Title = name;
                                elem.Warehouse = warehouseId;
                                elem.Currency = currencyId;
                                elem.Brand = brandId;
                                elem.Description = this.GetAttrValueSafe(item.Attribute("Description"));
                                elem.Delivery = this.GetAttrValueSafe(item.Attribute("Delivery"));
                                elem.Qty = this.TryConvertQty(this.GetAttrValueSafe(item.Attribute("Qty")));
                                elem.Price1 = this.TryConvertPrice(this.GetAttrValueSafe(item.Attribute("Price1")));
                                elem.Price2 = this.TryConvertPrice(this.GetAttrValueSafe(item.Attribute("Price2")));
                                elem.Price3 = this.TryConvertPrice(this.GetAttrValueSafe(item.Attribute("Price3")));
                                elem.Price4 = this.TryConvertPrice(this.GetAttrValueSafe(item.Attribute("Price4")));

                                DataFacade.AddNew<ProductElement>(elem);
                            }
                            catch (Exception ex)
                            {
                                this.LogMessage(Composite.Core.Logging.LogLevel.Warning, string.Format("Import price: {0}\n Item {1} was skip.", ex.Message, name));
                                skiped++;
                            }
                        }
                        else
                        {
                            noName++;
                            skiped++;
                        }

                        cnt++;
                    }

                    warehouse.UpdateTime = DateTime.Now;
                    DataFacade.Update(warehouse);

                    transactionScope.Complete();
                }

                var finishMessage = new StringBuilder();
                finishMessage.AppendFormat("Plice list was loaded from file {0} to warehouse {1}.", filename, warehouse.Title);
                finishMessage.AppendLine();
                if (clearWarehouse)
                {
                    finishMessage.AppendLine("Existing item was deleted before loading.");
                }
                finishMessage.AppendFormat("LoadTime: {0}.", DateTime.Now - t1);
                finishMessage.AppendLine();
                finishMessage.AppendFormat("{0} items loade.", cnt);
                finishMessage.AppendLine();
                finishMessage.AppendFormat("{0} items skipped.", skiped);
                finishMessage.AppendLine();
                finishMessage.AppendFormat("include {0} items without name.", noName);

                this.Bindings.Add("FinishMessage", finishMessage.ToString());
                this.LogMessage(Composite.Core.Logging.LogLevel.Info, finishMessage.ToString());
            }
            catch (Exception ex)
            {
                this.Bindings.Add("FinishMessage", "Load error.");
                this.LogMessage(Composite.Core.Logging.LogLevel.Warning, string.Format("Load error: {0}", ex.Message));
            }
        }

Viewing all articles
Browse latest Browse all 2540

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>