コード例 #1
0
        /// <summary>
        /// Prepare list of used images
        /// Many be associated with products, as thumbnail, icon, or std. images
        /// or as additional images
        /// or to option swathces
        /// </summary>
        private void InitAssociatedImageUrls()
        {
            associatedImages = new List <string>();

            // ADD PRODUCT IMAGES
            ICriteria criteria = NHibernateHelper.CreateCriteria <Product>();

            criteria.Add(Restrictions.Eq("Store", AbleContext.Current.Store));
            criteria.Add(Restrictions.Disjunction()
                         .Add(Restrictions.Like("ImageUrl", "~/Assets/ProductImages/%"))
                         .Add(Restrictions.Like("ThumbnailUrl", "~/Assets/ProductImages/%"))
                         .Add(Restrictions.Like("IconUrl", "~/Assets/ProductImages/%")));

            IList <Product> products = ProductDataSource.LoadForCriteria(criteria);

            foreach (Product product in products)
            {
                if (product.ImageUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(product.ImageUrl);
                }
                if (product.ThumbnailUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(product.ThumbnailUrl);
                }
                if (product.IconUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(product.IconUrl);
                }
            }

            // ADDITIONAL IMAGES
            ICriteria imageCriteria = NHibernateHelper.CreateCriteria <ProductImage>();

            imageCriteria.Add(Restrictions.Like("ImageUrl", "~/Assets/ProductImages/%"));
            IList <ProductImage> images = ProductImageDataSource.LoadForCriteria(imageCriteria);

            foreach (ProductImage image in images)
            {
                associatedImages.Add(image.ImageUrl);
            }

            // OPTION SWATCHES
            ICriteria choicesCriteria = NHibernateHelper.CreateCriteria <OptionChoice>();

            choicesCriteria.Add(Restrictions.Disjunction()
                                .Add(Restrictions.Like("ImageUrl", "~/Assets/ProductImages/%"))
                                .Add(Restrictions.Like("ThumbnailUrl", "~/Assets/ProductImages/%")));

            IList <OptionChoice> choices = OptionChoiceDataSource.LoadForCriteria(choicesCriteria);

            foreach (OptionChoice choice in choices)
            {
                if (choice.ImageUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(choice.ImageUrl);
                }
                if (choice.ThumbnailUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(choice.ThumbnailUrl);
                }
            }

            // ADD CATEGORY IMAGES
            ICriteria categoryCriteria = NHibernateHelper.CreateCriteria <Category>();

            categoryCriteria.Add(Restrictions.Eq("Store", AbleContext.Current.Store));
            categoryCriteria.Add(Restrictions.Like("ThumbnailUrl", "~/Assets/ProductImages/%"));
            IList <Category> categories = CategoryDataSource.LoadForCriteria(categoryCriteria);

            foreach (Category category in categories)
            {
                if (category.ThumbnailUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(category.ThumbnailUrl);
                }
            }

            // ADD LINK IMAGES
            ICriteria linksCriteria = NHibernateHelper.CreateCriteria <Link>();

            linksCriteria.Add(Restrictions.Eq("Store", AbleContext.Current.Store));
            linksCriteria.Add(Restrictions.Like("ThumbnailUrl", "~/Assets/ProductImages/%"));
            IList <Link> links = LinkDataSource.LoadForCriteria(linksCriteria);

            foreach (Link link in links)
            {
                if (link.ThumbnailUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(link.ThumbnailUrl);
                }
            }

            // ADD GIFT WRAPING IMAGES
            ICriteria wrapstylesCriteria = NHibernateHelper.CreateCriteria <WrapStyle>();

            wrapstylesCriteria.Add(Restrictions.Like("ImageUrl", "~/Assets/ProductImages/%"));

            IList <WrapStyle> wrapStyles = WrapStyleDataSource.LoadForCriteria(wrapstylesCriteria);

            foreach (WrapStyle ws in wrapStyles)
            {
                if (ws.ImageUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(ws.ImageUrl);
                }
                if (ws.ThumbnailUrl.StartsWith("~/Assets/ProductImages/"))
                {
                    associatedImages.Add(ws.ThumbnailUrl);
                }
            }

            // ADD VARIANT IMAGES
            ICriteria variantsCriteria = NHibernateHelper.CreateCriteria <ProductVariant>();

            criteria.Add(Restrictions.Disjunction()
                         .Add(Restrictions.Like("ImageUrl", "~/Assets/ProductImages/%"))
                         .Add(Restrictions.Like("ThumbnailUrl", "~/Assets/ProductImages/%"))
                         .Add(Restrictions.Like("IconUrl", "~/Assets/ProductImages/%")));

            int variantsCount      = ProductVariantDataSource.CountForCriteria(variantsCriteria.Clone() as ICriteria);
            int maxVariantsToCache = 500;

            // avoid loading all variants at same time
            for (int index = 0; index < variantsCount; index += maxVariantsToCache)
            {
                variantsCriteria.SetMaxResults(maxVariantsToCache);
                variantsCriteria.SetFirstResult(index);
                IList <ProductVariant> productVariants = ProductVariantDataSource.LoadForCriteria(variantsCriteria);
                foreach (ProductVariant productVariant in productVariants)
                {
                    if (productVariant.ImageUrl.StartsWith("~/Assets/ProductImages/"))
                    {
                        associatedImages.Add(productVariant.ImageUrl);
                    }
                    if (productVariant.ThumbnailUrl.StartsWith("~/Assets/ProductImages/"))
                    {
                        associatedImages.Add(productVariant.ThumbnailUrl);
                    }
                    if (productVariant.IconUrl.StartsWith("~/Assets/ProductImages/"))
                    {
                        associatedImages.Add(productVariant.IconUrl);
                    }
                }
            }
        }