//------------------------------ Glass Info Splitters----------------------------------------------

        private bool CheckGlassJsonInfoModelAndAddToDb(GlassJsonInfoModel glassInfoModel)
        {
            bool added = false;

            if (!IsGlassAlreadyAdded(glassInfoModel.EuroCode,
                                     glassInfoModel.MaterialNumber,
                                     glassInfoModel.LocalCode,
                                     glassInfoModel.IndustryCode))
            {
                // Get car IDs or create ones and all related entities
                int makeId      = this.CheckAndGetMakeId(glassInfoModel.Make);
                int?modelId     = this.CheckAndGetModelId(glassInfoModel.Model);
                var bodyTypeIds = this.CheckAndGetBodyTypeId(glassInfoModel.BodyTypes);
                var vehicles    = this.CheckAndGetVehicleIds(makeId, modelId, bodyTypeIds);

                var characteristics     = this.HandleCharacteristics(glassInfoModel.Characteristics);
                var images              = this.HandleImages(glassInfoModel.Images);
                var interchaneableParts = this.HandleInterchaneableParts(glassInfoModel.InterchangeableParts);
                var superceeds          = this.HandleAccessories(glassInfoModel.Accessories);
                var accessories         = this.HandleSuperceeds(glassInfoModel.Superceeds);

                this.CreateNewGlassWithAllRelations(glassInfoModel, vehicles, characteristics, images, interchaneableParts, accessories, superceeds);

                added = true;
            }

            return(added);
        }
        private void AddArrayOfJsonsFromFileToDb(string passedFile)
        {
            string fileToUse = defaultJsonFilePathToRead;

            if (!string.IsNullOrEmpty(passedFile))
            {
                fileToUse = passedFile;
            }

            string jsonInfoString = this.Reader.Read(fileToUse);
            JArray glassesJArray  = this.ParseStringToJarray(jsonInfoString);

            for (int i = 0; i < glassesJArray.Count; i++)
            {
                var glassItemJson           = glassesJArray[i];
                GlassJsonInfoModel newGlass = new GlassJsonInfoModel();
                try
                {
                    newGlass = glassItemJson.ToObject <GlassJsonInfoModel>();
                    this.AddInDbAndLogInfo(newGlass);
                }
                catch (DbEntityValidationException dbEx)
                {
                    ValidationExceptionCatcher(dbEx, newGlass, i);
                }
                catch (Exception e)
                {
                    // uncomment
                    this.Logger.LogError($"Error while parsing jObject --{glassItemJson.ToString()}-- under index ({i}) to Glass: {e.Message}",
                                         errorsfilePathToWrite);
                }
            }
        }
        private void AddInDbAndLogInfo(GlassJsonInfoModel glass)
        {
            bool added = this.CheckGlassJsonInfoModelAndAddToDb(glass);

            if (added)
            {
                // uncomment
                //this.Logger.LogInfo(glass.ToString(), parsedGlassesInfofilePath);
                //this.Logger.LogInfo("=======================================", parsedGlassesInfofilePath);
            }
        }
 private void ValidationExceptionCatcher(DbEntityValidationException dbEx, GlassJsonInfoModel glass, int index)
 {
     foreach (var validationErrors in dbEx.EntityValidationErrors)
     {
         foreach (var validationError in validationErrors.ValidationErrors)
         {
             string info = string.Format("Property: [{0}] Error: [{1}]",
                                         validationError.PropertyName,
                                         validationError.ErrorMessage);
             Trace.TraceInformation(info);
             // uncomment
             this.Logger.LogError($"Validation Error: jObject with Eurocode --{glass.EuroCode}-- index ({index}) to Glass, DB exception: {info}",
                                  errorsfilePathToWrite);
         }
     }
 }
        private void CreateNewGlassWithAllRelations(GlassJsonInfoModel glassInfoModel,
                                                    List <Vehicle> vehicles, List <VehicleGlassCharacteristic> characteristics,
                                                    List <VehicleGlassImage> images, List <VehicleGlassInterchangeablePart> interchaneableParts,
                                                    List <VehicleGlassSuperceed> superceeds, List <VehicleGlassAccessory> accessories)
        {
            VehicleGlass glass = this.Mapper.Map <VehicleGlass>(glassInfoModel);

            this.Glasses.Add(glass);

            // -------------- for optimization
            var uniqueCode = this.Glasses.GetCode(glass);

            this.GlassesCodesProjectionFromDb.Add(uniqueCode);
            // -------------- for optimization

            foreach (var vehicle in vehicles)
            {
                vehicle.VehicleGlasses.Add(glass);
            }
            foreach (var characteristic in characteristics)
            {
                characteristic.VehicleGlasses.Add(glass);
            }
            foreach (var image in images)
            {
                image.VehicleGlasses.Add(glass);
            }
            foreach (var interchangeablePart in interchaneableParts)
            {
                interchangeablePart.VehicleGlasses.Add(glass);
            }
            foreach (var supereed in superceeds)
            {
                supereed.VehicleGlasses.Add(glass);
            }
            foreach (var accessory in accessories)
            {
                accessory.VehicleGlasses.Add(glass);
            }
        }