public static void PollGoogleBase()
        {
            var myLock = new object();

            lock (myLock)
            {
                var date = DateTime.UtcNow;
                using (var repository = new TradelrRepository())
                {
                    // check for expired items
                    foreach (var sd in repository.GetSubDomains())
                    {
                        var products =
                            repository.GetProducts(sd.id).Where(x => x.gbase.HasValue);

                        foreach (var p in products)
                        {
                            var gb = new GoogleBaseExporter(sd.id, sd.ToHostName());
                            if (date > p.gbase_product.expirydate)
                            {
                                gb.InitValues(p);
#if !DEBUG
                                IEnumerable <Photo> productPhotos = repository.GetImages(PhotoType.PRODUCT, p.id).ToModel(Imgsize.LARGE);
                                gb.AddProductImages(productPhotos);
#endif
                                gb.AddToGoogleBase();

                                // delete old entry
                                gb.DeleteFromGoogleBase(p.gbase_product.externalid);

                                // update gbase entry
                                p.gbase_product.externalid   = gb.entry.Id.AbsoluteUri;
                                p.gbase_product.expirydate   = gb.entry.ExpirationDate;
                                p.gbase_product.externallink = NetworksGbase.URLFromEntry(gb.entry);
                            }
                            else
                            {
                                // get status
                                if (gb.GetFromGoogleBase(p.gbase_product.externalid))
                                {
                                    p.gbase_product.expirydate = gb.entry.ExpirationDate;

                                    if (gb.entry.IsDraft)
                                    {
                                        p.gbase_product.flags |= (int)InventoryItemFlag.DRAFT;
                                    }
                                    else
                                    {
                                        p.gbase_product.flags &= ~(int)InventoryItemFlag.DRAFT;
                                    }
                                }
                            }
                        }
                    }

                    repository.Save("PollGoogleBase");
                }
            }
        }
Beispiel #2
0
        public override void StartSynchronisation(bool?upload)
        {
            // pull items
            var gbase = new GoogleBaseExporter(subdomainid, hostName, sessionid);

            gbase.GetAllProducts();
            using (var repository = new TradelrRepository())
            {
                // create network location
                var inventoryLocation = new inventoryLocation
                {
                    name       = LOCATIONNAME_GBASE,
                    subdomain  = subdomainid,
                    lastUpdate = DateTime.UtcNow
                };
                locationid = repository.AddInventoryLocation(inventoryLocation, subdomainid);

                // init some settings
                locationid = repository.GetInventoryLocation(LOCATIONNAME_GBASE, subdomainid).id;
                currency   = repository.GetSubDomain(subdomainid).currency.ToCurrency();

                var products = repository.GetProducts(subdomainid);
                foreach (var entry in gbase.entries)
                {
                    ProductEntry entry1       = entry;
                    var          oldTypeEntry = products.SingleOrDefault(x => x.gbaseID == entry1.Id.AbsoluteUri);
                    if (oldTypeEntry != null)
                    {
                        // old type entry exists

                        // remove id
                        oldTypeEntry.gbaseID = null;

                        // create gbase entry, don't need to create a variant since one would already exist
                        oldTypeEntry.gbase_product = CreateGbaseInformationEntry(entry);
                    }
                    else
                    {
                        var newtypeEntry =
                            products.Where(x => x.gbase_product.externalid == entry1.Id.AbsoluteUri).Select(
                                x => x.gbase_product).SingleOrDefault();
                        if (newtypeEntry != null)
                        {
                            // new type entry exists
                            // update status
                            newtypeEntry.expirydate = entry.ExpirationDate;
                            if (entry.IsDraft)
                            {
                                newtypeEntry.flags |= (int)InventoryItemFlag.DRAFT;
                            }
                            else
                            {
                                newtypeEntry.flags &= ~(int)InventoryItemFlag.DRAFT;
                            }
                        }
                        else
                        {
                            // entry does not exist
                            // create and add entry to tradelr
                            var p = CreateProductFromEntry(entry);

                            // create variant
                            var variants = new List <product_variant>();

                            var variant = new product_variant()
                            {
                                sku =
                                    string.Concat("GBASE", entry.Id.AbsoluteUri.Substring(
                                                      entry.Id.AbsoluteUri.LastIndexOf('/') + 1))
                            };
                            variants.Add(variant);

                            // add gbase info
                            p.gbase_product = CreateGbaseInformationEntry(entry);
                            p.product_variants.AddRange(variants);
                            var pinfo = new ProductInfo()
                            {
                                p = p
                            };
                            repository.AddProduct(pinfo, subdomainid);

                            // images
                            foreach (var link in entry.AdditionalImageLinks)
                            {
                                var image = link.Value.ReadAndSaveProductImageFromUrl(subdomainid, sessionid, p.id);
                                p.thumb = image.id;
                            }
                        }
                    }
                }

                // upload to google base for each entry that does not have a special entry
                if (upload.HasValue && upload.Value)
                {
                    var newproducts =
                        repository.GetProducts(subdomainid).Where(
                            x => !x.gbase.HasValue && (x.flags & (int)ProductFlag.ARCHIVED) == 0);
                    foreach (var p in newproducts)
                    {
                        var gb = new GoogleBaseExporter(subdomainid, hostName, sessionid);
                        gb.InitValues(p);
#if !DEBUG
                        IEnumerable <Photo> productPhotos = repository.GetImages(PhotoType.PRODUCT, p.id).ToModel(Imgsize.LARGE);
                        gb.AddProductImages(productPhotos);
#endif
                        var worker = new GoogleBaseWorker(gb);
                        new Thread(worker.Post).Start();
                    }
                }

                // save
                repository.Save();
            }
        }