public void AddProductKeyword(int productId, string keyword)
        {
            ProductEntity p = new ProductEntity(productId);
            KeywordCollection keywords = new KeywordCollection();
            keywords.GetMulti(null);

            if (keywords.Any(pr => pr.Keyword == keyword))
            {
                int keywordId = keywords.Where<KeywordEntity>(f => f.Keyword == keyword).FirstOrDefault().Id;

                ProductKeywordEntity productKeyword = new ProductKeywordEntity();
                productKeyword.KeywordId = keywordId;
                productKeyword.ProductId = productId;
                productKeyword.Save();
            }
            else
            {
                KeywordEntity k = new KeywordEntity();
                k.Keyword = keyword;
                k.Save();

                ProductKeywordEntity newProductKeyword = new ProductKeywordEntity();
                newProductKeyword.KeywordId = k.Id;
                newProductKeyword.ProductId = productId;
                newProductKeyword.Save();

            }
        }
        public ProductEntity AddProduct(string title, double price, string redirectUrl, int catId, int brandId, string detail, bool newItemStatus, bool freeShippingStatus, int storeId)
        {
            ProductEntity product = new ProductEntity();
            product.Title = title;
            product.Price = price;
            product.CategoryId = catId;
            product.BrandId = brandId;
            product.DetailDescription = detail;
            product.Status = false;
            product.StoreId = storeId;
            product.IsNewItem = newItemStatus;
            product.IsSliderProduct = false;
            product.IsSpeacialProduct = false;
            product.IsFreeShipping = freeShippingStatus;
            product.AddedDate = DateTime.Now;
            product.RedirectUrl = redirectUrl;

            product.Save();

            return product;
        }
 /// <summary> setups the sync logic for member _product</summary>
 /// <param name="relatedEntity">Instance to set as the related entity of type entityType</param>
 private void SetupSyncProduct(IEntity relatedEntity)
 {
     if(_product!=relatedEntity)
     {
         DesetupSyncProduct(true, true);
         _product = (ProductEntity)relatedEntity;
         this.PerformSetupSyncRelatedEntity( _product, new PropertyChangedEventHandler( OnProductPropertyChanged ), "Product", ProductSearchEngine.RelationClasses.StaticProductPhotoRelations.ProductEntityUsingProductIdStatic, true, ref _alreadyFetchedProduct, new string[] {  } );
     }
 }
 /// <summary> Removes the sync logic for member _product</summary>
 /// <param name="signalRelatedEntity">If set to true, it will call the related entity's UnsetRelatedEntity method</param>
 /// <param name="resetFKFields">if set to true it will also reset the FK fields pointing to the related entity</param>
 private void DesetupSyncProduct(bool signalRelatedEntity, bool resetFKFields)
 {
     this.PerformDesetupSyncRelatedEntity( _product, new PropertyChangedEventHandler( OnProductPropertyChanged ), "Product", ProductSearchEngine.RelationClasses.StaticProductPhotoRelations.ProductEntityUsingProductIdStatic, true, signalRelatedEntity, "ProductPhotos", resetFKFields, new int[] { (int)ProductPhotoFieldIndex.ProductId } );
     _product = null;
 }
 /// <summary> Retrieves the related entity of type 'ProductEntity', using a relation of type 'n:1'</summary>
 /// <param name="forceFetch">if true, it will discard any changes currently in the currently loaded related entity and will refetch the entity from the persistent storage</param>
 /// <returns>A fetched entity of type 'ProductEntity' which is related to this entity.</returns>
 public virtual ProductEntity GetSingleProduct(bool forceFetch)
 {
     if( ( !_alreadyFetchedProduct || forceFetch || _alwaysFetchProduct) && !this.IsSerializing && !this.IsDeserializing  && !this.InDesignMode)
     {
         bool performLazyLoading = this.CheckIfLazyLoadingShouldOccur(Relations.ProductEntityUsingProductId);
         ProductEntity newEntity = new ProductEntity();
         bool fetchResult = false;
         if(performLazyLoading)
         {
             AddToTransactionIfNecessary(newEntity);
             fetchResult = newEntity.FetchUsingPK(this.ProductId);
         }
         if(fetchResult)
         {
             newEntity = (ProductEntity)GetFromActiveContext(newEntity);
         }
         else
         {
             if(!_productReturnsNewIfNotFound)
             {
                 RemoveFromTransactionIfNecessary(newEntity);
                 newEntity = null;
             }
         }
         this.Product = newEntity;
         _alreadyFetchedProduct = fetchResult;
     }
     return _product;
 }
 /// <summary>Private CTor for deserialization</summary>
 /// <param name="info"></param>
 /// <param name="context"></param>
 protected ProductPhotoEntity(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _product = (ProductEntity)info.GetValue("_product", typeof(ProductEntity));
     if(_product!=null)
     {
         _product.AfterSave+=new EventHandler(OnEntityAfterSave);
     }
     _productReturnsNewIfNotFound = info.GetBoolean("_productReturnsNewIfNotFound");
     _alwaysFetchProduct = info.GetBoolean("_alwaysFetchProduct");
     _alreadyFetchedProduct = info.GetBoolean("_alreadyFetchedProduct");
     this.FixupDeserialization(FieldInfoProviderSingleton.GetInstance(), PersistenceInfoProviderSingleton.GetInstance());
     // __LLBLGENPRO_USER_CODE_REGION_START DeserializationConstructor
     // __LLBLGENPRO_USER_CODE_REGION_END
 }
        /// <summary>Creates a new, empty ProductEntity object.</summary>
        /// <returns>A new, empty ProductEntity object.</returns>
        public override IEntity Create()
        {
            IEntity toReturn = new ProductEntity();

            // __LLBLGENPRO_USER_CODE_REGION_START CreateNewProduct
            // __LLBLGENPRO_USER_CODE_REGION_END
            return toReturn;
        }
 public void UpdateProduct(int productId, string title, double price, string redirectUrl, int catId, int brandId, string detail, bool status, bool slideStatus
     , bool specialStatus, bool newItemStatus, bool freeShippingStatus)
 {
     ProductEntity product = new ProductEntity(productId);
     product.Title = title;
     product.Price = price;
     product.RedirectUrl = redirectUrl;
     product.CategoryId = catId;
     product.BrandId = brandId;
     product.DetailDescription = detail;
     product.Status = status;
     product.IsSliderProduct = slideStatus;
     product.IsSpeacialProduct = specialStatus;
     product.IsNewItem = newItemStatus;
     product.IsFreeShipping = freeShippingStatus;
     product.Save();
 }
        public ProductCollection GetRelatedProducts(int pid)
        {
            ProductEntity product = new ProductEntity(pid);

            ProductKeywordCollection productKeywords = new ProductKeywordCollection();
            productKeywords.GetMulti(null);
            List<int> keywordIds = new List<int>();

            foreach (ProductKeywordEntity item in product.ProductKeywords)
            {
                keywordIds.Add(item.KeywordId);
            }
            //var relatedProductIds = from rp in productKeywords
            //                        group rp by new { ProductID = rp.ProductId, KeywordID = rp.KeywordId } into g
            //                        where keywordIds.Contains(g.Key.KeywordID) && g.Key.KeywordID != 2
            //                        select new
            //                        {
            //                            Key = g.Key
            //                            ,
            //                            Count = g.Select(p => p.ProductId).Count()
            //                        }; // into g  //select new {RCount=g.Key //where keywordIds.Contains(rp.KeywordId) && rp.ProductId != 2 select new { rp.ProductId };

            var relatedProductIds = productKeywords.Where(p => keywordIds.Contains(p.KeywordId) && p.ProductId != pid).GroupBy(g => g.ProductId).Select(gr => new
            {
                ProductId = gr.Key,
                RelatedKeywordCount = gr.Count()
            }).OrderByDescending(f => f.RelatedKeywordCount).Take(5);

            ArrayList ids = new ArrayList();
            foreach (var item in relatedProductIds)
            {
                ids.Add(item.ProductId);
            }

            //Related Products
            ProductCollection productList = new ProductCollection();
            PredicateExpression filter = new PredicateExpression();
            filter.Add(new FieldCompareRangePredicate(ProductFields.Id, ids));
            filter.Add(ProductFields.Status == true);
            productList.GetMulti(filter);

            return productList;
        }
 public ProductCollection GetDetailedProduct(int pid)
 {
     IPrefetchPath prefatchPath = new PrefetchPath(EntityType.ProductEntity);
     prefatchPath.Add(ProductEntity.PrefetchPathComments).Filter.Add(CommentFields.Status == true);
     prefatchPath.Add(ProductEntity.PrefetchPathProductKeywords);
     prefatchPath.Add(ProductEntity.PrefetchPathProductSpecifications);
     prefatchPath.Add(ProductEntity.PrefetchPathProductPhotos);
     ProductEntity p = new ProductEntity(pid, prefatchPath);
     ProductCollection col = new ProductCollection();
     col.Add(p);
     return col;
 }
        public bool DeleteProductSpecification(int productId, string SpecificationName)
        {
            ProductEntity product = new ProductEntity(productId);

            return product.ProductSpecifications.Where<ProductSpecificationEntity>(p => p.ProductId == productId && p.SpecificationName == SpecificationName).FirstOrDefault().Delete();
        }
        static void Main(string[] args)
        {
            /*Get Main Categories*/

            //CategoryCollection categories = new CategoryCollection();
            //PredicateExpression filter = new PredicateExpression();
            //filter.Add(new FieldCompareNullPredicate(CategoryFields.BaseCategoryId));
            //categories.GetMulti(filter);

            //Console.WriteLine(categories.Count);

            //foreach (var item in categories)
            //{
            //    Console.WriteLine(item.Name);
            //}

            /*Get Categories with Sub Cotegories*/

            //CategoryCollection categories = new CategoryCollection();
            //PredicateExpression filter = new PredicateExpression();
            //filter.Add(new FieldCompareNullPredicate(CategoryFields.BaseCategoryId));
            //categories.GetMulti(filter);

            //foreach (var item in categories)
            //{
            //    Console.WriteLine(item.Name);
            //    Console.WriteLine("--Sub Categories--");
            //    foreach (var subCategory in item.Categories)
            //    {
            //        Console.WriteLine(subCategory.Name);
            //    }
            //    Console.WriteLine("**************************");
            //}

            /*Get Sub Categories with Main CategoryId*/

            //CategoryCollection subCategories = new CategoryCollection();
            //PredicateExpression filter = new PredicateExpression();
            //IPrefetchPath prefetchPath = new PrefetchPath((int)EntityType.CategoryEntity);
            //prefetchPath.Add(CategoryEntity.PrefetchPathCategories);
            //filter.Add(CategoryFields.BaseCategoryId == 1);
            //subCategories.GetMulti(filter, prefetchPath);

            //foreach (var item in subCategories)
            //{
            //    Console.WriteLine(item.Name);
            //    foreach (var sub in item.Categories)
            //    {
            //        Console.WriteLine(sub.Name);
            //    }
            //}

            /*Get Slider Product With Rating,Price,Title,Desc with SubString,Image.*/

            //ProductCollection sliderProducts = new ProductCollection();
            //PredicateExpression filter = new PredicateExpression();
            //IPrefetchPath prefetchPath = new PrefetchPath((int)EntityType.ProductEntity);
            //IPrefetchPathElement path = prefetchPath.Add(ProductEntity.PrefetchPathComments);
            //path.SubPath.Add(CommentEntity.PrefetchPathRate);

            //filter.Add(ProductFields.IsSliderProduct == true);
            //sliderProducts.GetMulti(filter, prefetchPath);

            //foreach (var item in sliderProducts)
            //{
            //    Console.WriteLine(item.Title);
            //    Console.WriteLine(item.Price + "TL");
            //    Console.WriteLine(item.DetailDescription.Substring(0,item.DetailDescription.Length/5)+"...");
            //    int rateCount = 0;
            //    int totalRate = 0;
            //    foreach (var itemComments in item.Comments)
            //    {
            //        rateCount++;
            //        totalRate += (int)itemComments.Rate.Value;
            //    }
            //    if(rateCount>0)
            //    Console.WriteLine("Rate :"+totalRate / rateCount);
            //    //Console.WriteLine("Photo :"+item.ProductPhotos.FirstOrDefault().UniqueName);
            //}

            /* Add Product Photos */
            //for (int i = 0; i < 4; i++)
            //{
            //    ProductEntity product = new ProductEntity(6);

            //    ProductPhotoEntity productPhotos = new ProductPhotoEntity();
            //    productPhotos.Product = product;

            //    productPhotos.UniqueName = Guid.NewGuid() + ".jpg";

            //    productPhotos.Save();

            //}

            /*Slider Products Photos*/
            //ProductPhotoCollection productPhotos = new ProductPhotoCollection();
            //RelationCollnection relationsToUse = new RelationCollection();
            //relationsToUse.Add(ProductPhotoEntity.Relations.ProductEntityUsingProductId);

            //IPredicateExpression selectFilter = new PredicateExpression();
            //selectFilter.Add(ProductFields.IsSliderProduct==true);
            //productPhotos.GetMulti(selectFilter, 0, null, relationsToUse);

            //var productPhotosDistinct = productPhotos.GroupBy(p => p.ProductId).Select(g => g.First());

            //foreach (var item in productPhotosDistinct)
            //{
            //    Console.WriteLine(item.ProductId +"  "+ item.UniqueName);
            //    Console.WriteLine("-------------------");
            //}

            /*Searching by Keywords*/
            //string keyword = "laptop";
            //ProductCollection productList = new ProductCollection();
            //IPredicateExpression filter = new PredicateExpression();
            //RelationCollection relations = new RelationCollection();
            //relations.Add(ProductEntity.Relations.ProductKeywordEntityUsingProductId);
            //relations.Add(KeywordEntity.Relations.ProductKeywordEntityUsingKeywordId);
            //filter.Add(new FieldLikePredicate(KeywordFields.Keyword,null,"%"+keyword+"%"));

            //productList.GetMulti(filter,0,null,relations);

            //foreach (var item in productList)
            //{
            //    Console.WriteLine(item.Title);
            //}

            /*Get Product with Category Id*/
            //int categoryId = 13;

            //ProductCollection productList = new ProductCollection();
            //IPredicateExpression filter = new PredicateExpression();
            //IPrefetchPath prefetchPath = new PrefetchPath((int)EntityType.ProductEntity);
            //prefetchPath.Add(ProductEntity.PrefetchPathCategory);
            //RelationCollection relation = new RelationCollection();
            //relation.Add(ProductEntity.Relations.CategoryEntityUsingCategoryId);
            //filter.Add(CategoryFields.Id == categoryId);

            //productList.GetMulti(filter, 0, null, relation,prefetchPath);

            //foreach (var item in productList)
            //{
            //    Console.WriteLine(item.Title);
            //    Console.WriteLine("Kategorisi: {0}",item.Category.Name);
            //}

            /*Get Product with BaseCategoryId*/
            //int baseCategoryId = 8; //Computer
            //ProductCollection productList = new ProductCollection();
            //IPredicateExpression filter = new PredicateExpression();
            //IPrefetchPath prefetchPath = new PrefetchPath((int)EntityType.ProductEntity);
            //prefetchPath.Add(ProductEntity.PrefetchPathCategory);
            //RelationCollection relation = new RelationCollection();
            //relation.Add(ProductEntity.Relations.CategoryEntityUsingCategoryId);
            //filter.Add(CategoryFields.BaseCategoryId == baseCategoryId);

            //productList.GetMulti(filter, 0, null, relation, prefetchPath);

            //foreach (var item in productList)
            //{
            //    Console.WriteLine(item.Title);
            //    Console.WriteLine("Kategorisi: {0}", item.Category.Name);
            //}

            /*Get Product with Keywrod and BaseCategoryId*/
            //int baseCategoryId = 8;
            //string keyword = "Laptop";
            //ProductCollection productList = new ProductCollection();
            //IPrefetchPath prefetchCategory = new
            //    PrefetchPath((int)EntityType.ProductEntity);
            //prefetchCategory.Add(ProductEntity.PrefetchPathCategory);
            //RelationCollection relations = new RelationCollection();
            //relations.Add(ProductEntity.Relations.CategoryEntityUsingCategoryId);
            //relations.Add(ProductEntity.Relations.ProductKeywordEntityUsingProductId);
            //relations.Add(KeywordEntity.Relations.ProductKeywordEntityUsingKeywordId);

            //IPredicateExpression filter = new
            //    PredicateExpression();

            //filter.Add(new FieldLikePredicate(KeywordFields.Keyword,null,"%"+keyword+"%"));
            //filter.Add(CategoryFields.BaseCategoryId == baseCategoryId);

            //productList.GetMulti(filter, 0, null, relations, prefetchCategory);

            ///* City Id'ye göre Düzenleme*/
            //List<ProductEntity> products = productList.Where(p => p.Store.Membership.StoreInformation.CityId == 7).ToList<ProductEntity>();
            //foreach (var item in products)
            //{
            //    Console.WriteLine(item.Title);
            //    Console.WriteLine("KAtegorisi : {0}",item.Category.Name);
            //    Console.WriteLine("Base Categoryisi {0}",
            //        item.Category.Category.Name);
            //}

            /*Get Related Products*/

            ProductEntity product = new ProductEntity(2);

            ProductKeywordCollection productKeywords = new ProductKeywordCollection();
            productKeywords.GetMulti(null);
            List<int> keywordIds = new List<int>();

            foreach (ProductKeywordEntity item in product.ProductKeywords)
            {
                keywordIds.Add(item.KeywordId);
            }
            //var relatedProductIds = from rp in productKeywords
            //                        group rp by new { ProductID = rp.ProductId, KeywordID = rp.KeywordId } into g
            //                        where keywordIds.Contains(g.Key.KeywordID) && g.Key.KeywordID != 2
            //                        select new
            //                        {
            //                            Key = g.Key
            //                            ,
            //                            Count = g.Select(p => p.ProductId).Count()
            //                        }; // into g  //select new {RCount=g.Key //where keywordIds.Contains(rp.KeywordId) && rp.ProductId != 2 select new { rp.ProductId };

            var relatedProductIds = productKeywords.Where(p => keywordIds.Contains(p.KeywordId) && p.ProductId != 2).GroupBy(g => g.ProductId).Select(gr => new
            {
                ProductId = gr.Key,
                RelatedKeywordCount = gr.Count()
            }).OrderByDescending(f => f.RelatedKeywordCount).Take(5);

            ArrayList ids = new ArrayList();
            foreach (var item in relatedProductIds)
            {
                ids.Add(item.ProductId);
            }

            //Related Products
            ProductCollection productList = new ProductCollection();
            PredicateExpression filter = new PredicateExpression();
            filter.Add(new FieldCompareRangePredicate(ProductFields.Id, ids));

            productList.GetMulti(filter);

            /*Get Product List by Brand Id*/
            //ProductCollection productList = new ProductCollection();
            //RelationCollection relation = new RelationCollection();
            //relation.Add(ProductEntity.Relations.BrandEntityUsingBrandId);
            //IPrefetchPath prefatchPath = new PrefetchPath((int)EntityType.ProductEntity);
            //prefatchPath.Add(ProductEntity.PrefetchPathBrand);
            //PredicateExpression filter = new PredicateExpression();
            //filter.Add(BrandFields.Id == 2);

            //productList.GetMulti(filter,0,null,relation,prefatchPath);

            /*Get Last 3 Added Products*/
            //ProductCollection productList = new ProductCollection();
            //SortExpression sorter = new SortExpression();
            //sorter.Add(ProductFields.AddedDate | SortOperator.Descending);
            //productList.GetMulti(null, 3, sorter);

            /*Get Product Tags*/

            /* Get Detailed Product*/
            //IPrefetchPath prefatchPath = new PrefetchPath(EntityType.ProductEntity);
            //prefatchPath.Add(ProductEntity.PrefetchPathComments);
            //prefatchPath.Add(ProductEntity.PrefetchPathProductKeywords);
            //prefatchPath.Add(ProductEntity.PrefetchPathProductSpecifications);
            //prefatchPath.Add(ProductEntity.PrefetchPathProductPhotos);
            //ProductEntity p = new ProductEntity(2, prefatchPath);

            /*Sort Özellikleri*/
            //Sort By A-Z

            //ProductCollection productList = new ProductCollection();

            //RelationCollection relation = new RelationCollection();
            //relation.Add(ProductEntity.Relations.BrandEntityUsingBrandId);
            //relation.Add(ProductEntity.Relations.CommentEntityUsingProductId);
            //relation.Add(CommentEntity.Relations.RateEntityUsingRateId);

            ////IPrefetchPath prefatchPath = new PrefetchPath((int)EntityType.ProductEntity);
            ////prefatchPath.Add(ProductEntity.PrefetchPathBrand);
            ////prefatchPath.Add(ProductEntity.PrefetchPathComments).SubPath.Add(CommentEntity.PrefetchPathRate);
            //PredicateExpression filter = new PredicateExpression();
            //filter.Add(BrandFields.Id == 2);
            //productList.GetMulti(filter, 0, null, relation);
            //foreach (var item in productList)
            //{
            //    Console.WriteLine(item.Id + " " + item.Title);
            //}

            //productList.Sort((int)ProductFieldIndex.Price, System.ComponentModel.ListSortDirection.Ascending);

            ////SortExpression sorter = new SortExpression();
            ////sorter.Add(ProductFields.Id | SortOperator.Descending);
            ////productList.GetMulti(filter, 0, sorter, relation);

            //Console.WriteLine("***********************************");
            //foreach (var item in productList)
            //{
            //    Console.WriteLine(item.Id + " " + item.Title);

            //}

            /*Get Keywords by searching text*/
            //KeywordCollection keywords = new KeywordCollection();
            //PredicateExpression filter = new PredicateExpression();
            //string keyword = "ap";
            //filter.Add(new FieldLikePredicate(KeywordFields.Keyword,null,keyword+"%"));

            //keywords.GetMulti(filter);

            //foreach (var item in keywords)
            //{
            //    Console.WriteLine(item.Keyword);
            //}
            //Console.Read();

            /*Get Store Products*/

            //MembershipEntity member = new MembershipEntity(3);
            //ProductCollection products = new ProductCollection();
            //RelationCollection relations = new RelationCollection();
            //relations.Add(ProductEntity.Relations.StoreEntityUsingStoreId);
            //relations.Add(StoreEntity.Relations.MembershipEntityUsingMembershipId);

            //IPrefetchPath prefatchPath = new PrefetchPath((int)EntityType.ProductEntity);
            //prefatchPath.Add(ProductEntity.PrefetchPathStore).SubPath.Add(StoreEntity.PrefetchPathMembership);

            //PredicateExpression filter = new PredicateExpression();

            //filter.Add(MembershipFields.Id == member.Id);

            //SortExpression sorter = new SortExpression();

            //sorter.Add(ProductFields.AddedDate | SortOperator.Descending);
            //products.GetMulti(filter, 5,sorter,relations,prefatchPath);

            //ProductEntity product = new ProductEntity(6);

            //product.ProductSpecifications.Where<ProductSpecificationEntity>(p => p.ProductId == 6 && p.SpecificationName == "Color").FirstOrDefault().Delete();

            //ResultsetFields fields = new ResultsetFields(2);
            //fields.DefineField(WebTrackerFields.IpAddress, 0, "Id");
            //fields.DefineField(WebTrackerFields.Id, 1, "Amount");
            //fields[1].AggregateFunctionToApply = AggregateFunction.CountDistinct;

            //DataTable results = new DataTable();
            //TypedListDAO dao = new TypedListDAO();
            //dao.GetMultiAsDataTable(fields, results, 0, null, null, null, true, null, null, 0, 0);

            SHA1 sha = new SHA1CryptoServiceProvider();

            string SifrelenecekVeri = "123456";

            string SifrelenmisVeri = Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(SifrelenecekVeri)));

            Console.WriteLine(SifrelenmisVeri);

            Console.ReadLine();
        }