public void AddStatistics(User user, SearchResultMessageEntity messageEntity, string userAgent, string imei, string model, string osVersion)
        {
            var insertTime = DateTime.Now;

            int? userId = user != null ? user.Id : (int?) null;

            if (messageEntity.Products != null)
            {
                
                foreach (var product in messageEntity.Products)
                {
                    AddProductRequest(userId, product.Id, userAgent, imei, model, osVersion);   
                }
            }

            if (messageEntity.Ingredients != null)
            {
                
                foreach (var ingredient in messageEntity.Ingredients)
                {
                    AddIngredienRequest(userId, ingredient.Id, userAgent, imei, model, osVersion);
                }
            }

            if (messageEntity.Brands != null)
            {
                foreach (var brand in messageEntity.Brands)
                {
                    AddBrandRequest(userId, brand.Id, userAgent, imei, model, osVersion);
                }
            }

            if (messageEntity.Companies != null)
            {
                
                foreach (var company in messageEntity.Companies)
                {
                    AddCompanyRequest(userId, company.Id, userAgent, imei, model, osVersion);
                }
            }

            if (messageEntity.Countries != null)
            {

                foreach (var country in messageEntity.Countries)
                {
                    AddCountryRequest(userId, country.Id, userAgent, imei, model, osVersion);
                }
            }

            if (messageEntity.Concepts != null)
            {

                foreach (var concept in messageEntity.Concepts)
                {
                    AddConceptRequest(userId, concept.Id, userAgent, imei, model, osVersion);
                }
            }
        }
        protected override void Before_all_specs()
        {
            SetupDatabase(ShopGunSpecBase.Database.ShopGun, typeof(Base).Assembly);
            _statisticsDomainService = new StatisticsDomainService(new Repository<Brand>(GetNewDataContext()),
                                                                   new Repository<Company>(GetNewDataContext()),
                                                                   new Repository<Country>(GetNewDataContext()),
                                                                   new Repository<Product>(GetNewDataContext()),
                                                                   new Repository<Ingredient>(GetNewDataContext()),
                                                                   new Repository<Concept>(GetNewDataContext()),
                                                                   new Repository<AdviceBase>(GetNewDataContext()));

            _productBuilder = new ProductBuilder();
            _product = ProductBuilder.BuildProduct();
            _user = UserBuilder.BuildUser();
            using (var dataContext = GetNewDataContext())
            {
                dataContext.GetTable<Product>().InsertOnSubmit(_product);
                dataContext.GetTable<User>().InsertOnSubmit(_user);
                dataContext.SubmitChanges();
            }
            _searchResultMessageEntity = new SearchResultMessageEntity {Products = new List<Product> {_product}};
            base.Before_all_specs();
        }
        public SearchResultMessageEntity Search(string userSubcriptionToken, string queryString, IShopgunWebOperationContext shopgunWebOperationContext)
        {
            //TODO identify User
            User user = null;

            ////TODO Check for Subscription token, is valid. Maybe it this shall been done the webservice?
            
            SearchResultMessageEntity result;
            bool productExistsInDatabase = false;

            if (queryString.IsGtin())
            {
                Log.Debug("GTIN search for {0}", queryString);
                var product = _productApplicationService.FindProductByGtin(queryString, true);
                //var product = _productApplicationService.FindProductByGtinInOwnDatabase(queryString, true);

                result = new SearchResultMessageEntity();
                if (!string.IsNullOrEmpty(product.ProductName))
                {
                    result.SearchType = SearchResultMessageEntity.GtinSearch;
                    result.Products = new List<Product> {product};
                }
                else if (product.Brand != null)
                {
                    if (!string.IsNullOrEmpty(product.Brand.BrandName))
                    {
                        result.SearchType = SearchResultMessageEntity.FreeSearch;
                        result.Brands = new List<Brand> {product.Brand};
                    }
                    else if (product.Brand.Owner != null)
                    {
                        result.SearchType = SearchResultMessageEntity.FreeSearch;
                        result.Companies = new List<Company> {product.Brand.Owner};
                    }
                }
                if (product.Id > 0)
                {
                    productExistsInDatabase = true;
                }
            }
            else
            {
                Log.Debug("Freetext search for \"{0}\"", queryString);
                result =
                    new SearchResultMessageEntity
                        {
                            SearchType = SearchResultMessageEntity.FreeSearch
                            ,
                            //Brands = _brandApplicationService.FindBrands(queryString, true)
                            //,
                            //Companies = _companyApplicationService.FindCompanies(queryString, true)
                            //,
                            //Concepts = _conceptApplicationService.FindConcepts(queryString, true)
                            //,
                            //Countries = _countryApplicationService.FindCountries(queryString, true)
                            //,
                            //Ingredients = _ingredientApplicationService.FindIngredients(queryString, true, true)
                    };
                //Only exact match for now...
                var ingredient = _ingredientApplicationService.FindIngredient(queryString, true, true);
                if (ingredient != null)
                {
                    result.Ingredients = new List<Ingredient> { ingredient };
                }
                var company = _companyApplicationService.FindCompany(queryString, true);
                if(ingredient == null && company != null)
                {
                    result.Companies = new List<Company> { company } ;
                    result.SearchType = SearchResultMessageEntity.FreeSearch;
                }

                //if (!result.HasResults())
                //{
                //    result.Products = _productDomainService.FindProducts(queryString, true);
                //}
            }
            var resultFound = result.HasResults();

            if (shopgunWebOperationContext != null)
            {
                var userAgent = shopgunWebOperationContext.UserAgent;
                var imei = shopgunWebOperationContext.IMEI;
                var model = shopgunWebOperationContext.Model;
                var osVersion = shopgunWebOperationContext.OsVersion;

                _adviceSearchStatisticsDomainService.AddAdviceSearch(user, queryString, userAgent, imei, model, resultFound, osVersion);
            }

            if (result.SearchType != SearchResultMessageEntity.GtinSearch || productExistsInDatabase)
            {
                //_statisticsDomainService.AddStatistics(user, result, userAgent, imei, model, osVersion);
            }

            return result;
        }