private static string getSettingsJson(ProductSectionEditViewModel section)
 {
     if (section.Type == ProductSectionType.Text)
     {
         var settings = new ProductSectionTextSettings {Text = section.Text};
         return JsonConvert.SerializeObject(settings);
     }
     return null;
 }
Exemplo n.º 2
0
 private static string getSettingsJson(ProductSectionEditViewModel section)
 {
     if (section.Type == ProductSectionType.Text)
     {
         var settings = new ProductSectionTextSettings {
             Text = section.Text
         };
         return(JsonConvert.SerializeObject(settings));
     }
     return(null);
 }
Exemplo n.º 3
0
        public ActionResult ProductsJson(HttpPostedFileBase file)
        {
            if (file.ContentLength <= 0) return View();

            var rootPath = Server.MapPath("~/Import");
            var guid = Guid.NewGuid().ToString();
            var path = Path.Combine(rootPath, guid);
            Directory.CreateDirectory(path);
            file.SaveAs(Path.Combine(rootPath, guid + ".zip"));
            using (var zip = new ZipFile(Path.Combine(rootPath, guid + ".zip")))
            {
                zip.FlattenFoldersOnExtract = true;
                zip.ExtractAll(path);
            }
            System.IO.File.Delete(Path.Combine(rootPath, guid + ".zip"));

            var imgIdMap = new Dictionary<Guid, Guid>();
            foreach (var imgFile in Directory.GetFiles(path, "*.jpg"))
            {
                var imgId = Guid.Parse(Path.GetFileNameWithoutExtension(imgFile));
                var dbUpl = db.Uploads.FirstOrDefault(u => u.Id == imgId);
                if (dbUpl == null)
                {
                    dbUpl = new Upload { Type = UploadType.ProductImage };
                    db.Uploads.Add(dbUpl);
                    db.SaveChanges();

                    System.IO.File.Copy(imgFile, Path.Combine(Server.MapPath("~/Storage"), dbUpl.Id.ToString()), true);
                }
                imgIdMap.Add(imgId, dbUpl.Id);
            }

            if (System.IO.File.Exists(Path.Combine(path, "export.json")))
            {
                // TODO: Use text reader for very large jsons
                // Import json
                /*
                using (var sr = new StreamReader(Path.Combine(path, "export.json")))
                using (var reader = new JsonTextReader(sr))
                {
                    while (reader.Read())
                    {
                        if ()
                    }
                }
                */

                dynamic json = JObject.Parse(System.IO.File.ReadAllText(Path.Combine(path, "export.json")));

                var categoryidMap = new Dictionary<int, int>();
                var optCategoryidMap = new Dictionary<int, int>();
                var optMap = new Dictionary<int, int>();
                var prodMap = new Dictionary<int, int>();

                foreach (var category in json.categories)
                {
                    int? parentId = category.parentId;
                    if (parentId != null) parentId = categoryidMap[(int)category.parentId];
                    string name = category.name;
                    var dbCategory = categoryService.FindAll()
                        .FirstOrDefault(c => c.Name == name && c.ParentId == parentId);
                    if (dbCategory == null)
                    {
                        var categoryModel = new CategoryEditViewModel
                                            {
                                                Name = category.name,
                                                Description = category.description,
                                                IsVisible = category.isVisible ?? true,
                                                SortOrder = category.sortOrder ?? 0
                                            };
                        if (category.parentId != null)
                            categoryModel.ParentId = categoryidMap[(int)category.parentId];
                        dbCategory = categoryService.AddOrUpdate(categoryModel);
                    }
                    categoryidMap.Add((int)category.id, dbCategory.Id);
                }

                foreach (var optCategory in json.optionCategories)
                {
                    string name = optCategory.name;
                    var dbOptCategory = db.OptionCategories.FirstOrDefault(c => c.Name == name);
                    if (dbOptCategory == null)
                    {
                        dbOptCategory = new OptionCategory
                                            {
                                                Name = optCategory.name,
                                                Description = optCategory.description,
                                                Type = optCategory.type,
                                                IncludeInFilters = optCategory.includeInFilters
                                            };
                        db.OptionCategories.Add(dbOptCategory);
                        db.SaveChanges();
                    }
                    optCategoryidMap.Add((int)optCategory.id, dbOptCategory.Id);
                }

                foreach (var option in json.options)
                {
                    string name = option.name;
                    int catId = optCategoryidMap[(int)option.optionCategoryId];
                    var dbOpt = db.Options.FirstOrDefault(o => o.Name == name && o.OptionCategoryId == catId);
                    if (dbOpt == null)
                    {
                        dbOpt = new Option
                                {
                                    Name = option.name,
                                    Description = option.description,
                                    OptionCategoryId = catId,
                                };
                        db.Options.Add(dbOpt);
                        db.SaveChanges();
                    }
                    optMap.Add((int)option.id, dbOpt.Id);
                }

                foreach (var product in json.products)
                {
                    string name = product.name;
                    var dbProd = productFinder.FindAll().FirstOrDefault(p => p.Name == name);
                    if (dbProd == null)
                    {
                        var prodModel = new ProductEditViewModel();
                        prodModel.Sku = product.sku;
                        prodModel.Name = product.name;
                        prodModel.Description = product.description;
                        prodModel.Price = product.price ?? 0;
                        prodModel.RetailPrice = product.retailPrice;
                        prodModel.CostPrice = product.costPrice;
                        prodModel.SalePrice = product.salePrice;
                        prodModel.IsFeatured = product.isFeatured;
                        prodModel.IsVisible = product.isVisible;
                        prodModel.CategoryIds = string.Join(",", ((JArray)product.categories).Select(
                            i => categoryidMap[(int)i].ToString()));
                        prodModel.Keywords = product.keywords;
                        prodModel.Quantity = product.quantity;
                        prodModel.TaxClassId = product.taxClassid;
                        prodModel.Weight = product.weight ?? 0;
                        prodModel.OptionIds = string.Join(",", ((JArray)product.options).Select(
                            i => optMap[(int)i].ToString()));
                        if (product.sections != null)
                        {
                            foreach (var sect in product.sections)
                            {
                                var sectModel = new ProductSectionEditViewModel
                                                {
                                                    Title = sect.title,
                                                    Type = sect.type,
                                                    Position = sect.position,
                                                    Settings = sect.settings,
                                                    Priority = sect.priority,
                                                    Text = sect.text
                                                };
                                prodModel.Sections.Add(sectModel);
                            }
                        }
                        if (product.uploads != null)
                        {
                            foreach (var upl in product.uploads)
                            {
                                if (prodModel.UploadIds == null)
                                    prodModel.UploadIds = "";
                                else
                                    prodModel.UploadIds += ",";
                                prodModel.UploadIds += imgIdMap[(Guid)upl.id];
                            }
                        }
                        if (product.skus != null)
                        {
                            foreach (var sku in product.skus)
                            {
                                var optIds = new List<int>();
                                if (sku.options != null)
                                {
                                    foreach (int optId in sku.options)
                                    {
                                        optIds.Add(optMap[optId]);
                                    }
                                }
                                var uploadIds = new List<Guid>();
                                if (sku.uploads != null)
                                {
                                    foreach (var uplId in sku.uploads)
                                    {
                                        uploadIds.Add(imgIdMap[(Guid)uplId]);
                                    }
                                }
                                prodModel.Skus.Skus.Add(
                                    new ProductSkuEditViewModel
                                    {
                                        Sku = sku.sku,
                                        Price = sku.price,
                                        Quantity = sku.quantity,
                                        UPC = sku.upc,
                                        Weight = sku.weight,
                                        OptionIds = JsonConvert.SerializeObject(optIds.ToArray()),
                                        UploadIds = JsonConvert.SerializeObject(uploadIds.ToArray())
                                    });
                            }
                        }
                        dbProd = productService.CreateOrUpdate(prodModel);
                    }
                    prodMap.Add((int)product.id, dbProd.Id);
                }
            }

            return View().WithInfo(
                "Data import has been initiated. You will receive notification as soon as the import is completed".TA());
        }