private bool ValidateData(List<Instruction> data)
        {

            RealtyDb _db = new RealtyDb();

            var city = (from s in _db.Cities
                        select s).ToArray<City>();

            var district = (from s in _db.CityDistricts.Include("City")
                            select s).ToList<CityDisctict>();

            var propertyTypes = (from s in _db.PropertyTypes
                                 select s).ToList<PropertyType>();

            var propertyActions = (from s in _db.PropertyActions
                                   select s).ToList<PropertyAction>();

            var propertyObjects = (from s in _db.PropertyObjects
                                   select s).ToList<PropertyObject>();


            if (data == null || data.Count() == 0)
            {
                Console.WriteLine("Отсутствуют  инструкции на проверки, работа остановлена");
                return false;
            }
            int countErrors = 0;
            for (int i = 0; i < data.Count(); i++)
            {

                if (!string.IsNullOrEmpty(data[i].city.ToLower().Trim()))
                {
                    var cityObj = (from s in city
                                   where s.CityName.ToLower() == data[i].city.ToLower().Trim()//"Киев".ToLower()//
                                   select s).SingleOrDefault<City>();
                    if (cityObj == null)
                    {
                        Console.WriteLine("Не найден  город " + data[i].city);
                        countErrors++;
                    }

                    if (!string.IsNullOrEmpty(data[i].district.ToLower().Trim()))
                    {
                        var districtObj = (from s in district
                                           where s.District.ToLower() == data[i].district.ToLower().Trim()
                                           && s.City.Id == cityObj.Id
                                           select s).SingleOrDefault<CityDisctict>();

                        if (districtObj == null)
                        {
                            if (cityObj != null)
                                Console.WriteLine("Не найден  район " + data[i].district + " для города " + cityObj.CityName);
                            else
                                Console.WriteLine("Не найден  район " + data[i].district);
                            countErrors++;
                        }
                    }
                }

                var propertyTypeObj = (from s in propertyTypes
                                       where s.PropertyTypeName.ToLower() == data[i].propertyType.ToLower().Trim()//"Квартиры".ToLower()//
                                       select s).SingleOrDefault<PropertyType>();

                if (propertyTypeObj == null)
                {
                    Console.WriteLine("Не найден тип недвижимости" + data[i].propertyType);
                    countErrors++;
                }

                var propertyActionObj = (from s in propertyActions
                                         where s.PropertyActionName.ToLower() == data[i].propertyAction.ToLower().Trim()//"Продажа".ToLower()//
                                         && s.PropertyType.Id == propertyTypeObj.Id
                                         select s).SingleOrDefault<PropertyAction>();

                if (propertyActionObj == null)
                {
                    Console.WriteLine("Не найден тип действия с недвижимостью" + data[i].propertyAction);
                    countErrors++;
                }


            }

            _db.Dispose();
            _db = null;
            return countErrors == 0 ? true : false;
        }
        private void StoreEntity()
        {


            lock (dbLoaderLock)
            {
                Monitor.Wait(dbLoaderLock);
            }

            RealtyDb _db = new RealtyDb();
            bool stopLoop = false;
            Entity ent = null;
            int countStoredObjects = 0;

            var city = (from s in _db.Cities
                        select s).ToArray<City>();

            var district = (from s in _db.CityDistricts.Include("City")
                            select s).ToList<CityDisctict>();

            var propertyTypes = (from s in _db.PropertyTypes
                                 select s).ToList<PropertyType>();

            var propertyActions = (from s in _db.PropertyActions
                                   select s).ToList<PropertyAction>();

            var propertyObjects = (from s in _db.PropertyObjects
                                   select s).ToList<PropertyObject>();

            var periods = (from s in _db.Periods
                           select s).ToList<Periods>();

            var buildingTypes = (from s in _db.BuildingTypes
                                 select s).ToList<BuildingType>();

            var wcTypes = (from s in _db.WCTypes
                           select s).ToList<WCType>();

            var currencyTypes = (from s in _db.CurrencyTypes
                                 select s).ToList<CurrencyType>();

            var priceForTypes = (from s in _db.PriceForTypes
                                 select s).ToList<PriceForType>();

            var landCommunications = (from s in _db.LandCommunications
                                      select s).ToList<LandCommunication>();

            var landFunctions = (from s in _db.LandFunctions
                                 select s).ToList<LandFunction>();

            var commercialPropertyTypes = (from s in _db.CommercialPropertyTypes
                                           select s).ToList<CommercialPropertyType>();

            var serviceTypes = (from s in _db.ServiceTypes
                                select s).ToList<ServiceType>();


            _db.Dispose();
            _db = null;

            string connStr = "server=localhost; Port =3306; user id=root; password=mysqlPass; database=RealtyDb; pooling=true; CharSet=utf8; Connection Timeout=10000; Protocol=socket;";
            SqlObjectManipulation sql = new SqlObjectManipulation(connStr);
            WebRealty.Common.ImageProcessing imgHelper = new WebRealty.Common.ImageProcessing();

            DataValidator validate = new DataValidator();
            //int countInstructions = 0;
            //int countLinksToObject = 0;
            int countEntities = 0;
            int tmpInt = -1;
            do
            {
                lock (entitiesLocker)
                {
                    if (entities.Count > 0)
                        ent = entities.Dequeue();
                    countEntities = entities.Count;
                }

                //if (countEntities < minLimitList)
                //{
                //    lock (collectEntityLock)
                //    {
                //        Monitor.PulseAll(collectEntityLock);
                //    }
                //}



                //lock (instructionLocker)//получаем количество доступных инструкций
                //{
                //    countInstructions = instructions.Count();
                //}

                if (ent == null && t2.IsAlive)//засыпаем, пока нес не позовут
                {
                    eventLog.AddEventToLog("StoreEntity- сслыку на объект получить не удалось, поток собиратель объетов еще живой- засыпаем");
                    lock (dbLoaderLock)
                    {
                        Monitor.Wait(dbLoaderLock);
                    }
                    continue;
                }

                if (ent != null)
                {
                    var houseTypeObj = (from s in buildingTypes.AsEnumerable()
                                        where s.BuildingTypeName.ToLower() == ent.houseType.ToLower().Trim()
                                        select s).SingleOrDefault<BuildingType>();

                    var wcTypeObj = (from s in wcTypes.AsEnumerable()
                                     where s.WCTypeName.ToLower() == ent.wctype.ToLower().Trim()
                                     select s).SingleOrDefault<WCType>();

                    var currencyObj = (from s in currencyTypes
                                       where s.CurrencyTypeName.ToLower() == (ent.currency != null ? ent.currency.ToLower().Trim() : "у.е")
                                       select s).SingleOrDefault<CurrencyType>();

                    PriceForType priceForObj = null;
                    if (ent.priceFor != null)
                        priceForObj = (from s in priceForTypes
                                       where s.PriveForTypeName.ToLower().Replace(".", "") == ent.priceFor.ToLower().Trim().Replace(".", "")
                                       select s).FirstOrDefault<PriceForType>();

                    var cityObj = (from s in city
                                   where s.CityName.ToLower() == ent.City.ToLower().Trim()//"Киев".ToLower()//
                                   select s).SingleOrDefault<City>();

                    var districtObj = (from s in district
                                       where s.District.ToLower() == ent.District.ToLower().Trim()//"Голосеевский р-н.".ToLower()//
                                       && s.City.Id == cityObj.Id
                                       select s).SingleOrDefault<CityDisctict>();

                    var propertyTypeObj = (from s in propertyTypes
                                           where s.PropertyTypeName.ToLower() == ent.propertyType.ToLower().Trim()//"Квартиры".ToLower()//
                                           select s).SingleOrDefault<PropertyType>();

                    var propertyActionObj = (from s in propertyActions
                                             where s.PropertyActionName.ToLower() == ent.propertyAction.ToLower().Trim()//"Продажа".ToLower()//
                                             && s.PropertyType.Id == propertyTypeObj.Id
                                             select s).SingleOrDefault<PropertyAction>();

                    PropertyObject po = new PropertyObject()
                    {
                        BalconAvailable = ent.balconyAvailable,
                        BalconSpace = ent.balconySize,
                        BuildingTypeName = houseTypeObj,
                        City = cityObj,
                        CityDistrict = districtObj,
                        CommercialPropertyType = null,
                        ContactName = ent.contactName,
                        CountFloors = ent.countFloors, //???????
                        CountPhotos = (ent.photos != null ? ent.photos.Count() : 0),//ent.LinkToPhotos.Count(),
                        //CreatedDate=
                        Currency = currencyObj,
                        DistanceToCity = ent.distanceToCity,
                        Floor = ent.floor,
                        IsActive = true,
                        isBalconGlassed = ent.isBalconyGlassed,
                        IsNewBuilding = ent.isNewBuilding,
                        KitchenSpace = ent.kitchenSize,
                        LivingSpace = ent.livingSize,
                        NoCommission = ent.noComission,
                        //Periods=
                        Phone1 = ent.phone1,
                        Price = ent.price,
                        PriceForTypeName = priceForObj,
                        PropertyAction = propertyActionObj,
                        PropertyDescription = ent.description,
                        PropertyType = propertyTypeObj,
                        RoomCount = ent.roomsCount,
                        ServiceType = null,
                        Title = ent.title,
                        TotalSpace = ent.allSize,
                        WCType = wcTypeObj,
                        LinkOfObjectGrab = ent.linkToOriginalObject

                    };



                    if (po.CountFloors <= 0)
                        po.CountFloors = (int)ent.houseCountFloor;


                    if (po.LivingSpace <= 0)
                        po.LivingSpace = ent.houseGardenSize;

                    if (po.TotalSpace <= 0)
                        po.TotalSpace = ent.houseSize;

                    if (po.TotalSpace <= 0)
                        po.TotalSpace = ent.commercialObjectSize;




                    var datePart = ent.CreatedDate.Split('-');
                    if (datePart != null && datePart.Length > 1)
                    {
                        var dateSplitted = datePart[0].Split('.');
                        var timeSplitted = datePart[1].Split(':');
                        if (dateSplitted.Length == 3)
                        {
                            DateTime createdDate = new DateTime(Convert.ToInt32(dateSplitted[2]),
                                Convert.ToInt32(dateSplitted[1]),
                                Convert.ToInt32(dateSplitted[0]),
                                Convert.ToInt32(timeSplitted[0]),
                                Convert.ToInt32(timeSplitted[1]),
                                0, DateTimeKind.Local
                                );
                            po.CreatedDate = createdDate;
                        }
                    }

                    validate.ValidateAndFix(po);
                    try
                    {
                        int objectId = sql.AddPropertyObject(po);//uncomment it

                        for (int i = 0; i < ent.photos.Count(); i++)
                        {
                            if (ent.photos[i] != null && ent.photos[i].Length > 0)
                            {
                                //uncomment it
                                sql.Mysql_File_Save(objectId, 0, i.ToString() + ".jpg", "", 521, 521,
                                     ent.photos[i], imgHelper.ResizeImage(ent.photos[i], new System.Drawing.Size(120, 90)), false);
                            }
                        }
                        ent = null;
                        po = null;
                        Console.WriteLine("Stored objects : " + (++countStoredObjects).ToString());
                    }
                    catch (Exception ex)
                    {
                        eventLog.AddEventToLog("ОШИБКА сохранения объекта в БД: " + ex.Message.ToString() + " стэк ошибки: " + ex.StackTrace.ToString());
                    }
                }



                if (!t2.IsAlive && countEntities == 0)
                {
                    stopLoop = true;
                    linkLogger.TerminateLog();
                }

            } while (!stopLoop);

            Console.WriteLine("StoreEntity terminated");
            eventLog.AddEventToLog("StoreEntity завершает работу");
            return;

        }