Exemple #1
0
        private void PersistVehicleCharacteristics(VehicleCharacteristics chars)
        {
            VehicleCharacteristicsEntity entity =
                VehicleCharacteristicsAssembler.FromDomainObjectToEntity(chars);

            using (VTSDatabase database = new VTSDatabase())
            {
                database.VehicleCharacteristics.Add(entity);
                try
                {
                    database.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    string result = String.Empty;
                    foreach (DbEntityValidationResult eve in e.EntityValidationErrors)
                    {
                        string entityTypename = eve.Entry.Entity.GetType().Name;
                        string errorString    = String.Empty;
                        foreach (DbValidationError error in eve.ValidationErrors)
                        {
                            errorString = error.PropertyName + " : " + error.ErrorMessage;
                        }
                        result = String.Format("{0} | {1}", result, entityTypename + " -> " + errorString);
                    }
                    throw new Exception(result);
                }
            }
        }
Exemple #2
0
 private static void Persist(List <PsaDataset> datasets)
 {
     foreach (PsaDataset psaDataset in datasets)
     {
         PsaDatasetDto dataset = PsaDatasetAssembler.FromDomainObjectToDto(psaDataset);
         if (IsKnownDataset(dataset))
         {
             foreach (PsaTraceDto traceDto in dataset.Traces)
             {
                 // skip known traces
                 if (!IsKnownTrace(traceDto))
                 {
                     // persist trace for existing dataset
                     using (VTSDatabase database = new VTSDatabase())
                     {
                         Guid guid      = dataset.Guid;
                         long datasetId = database.PsaDataset.First(d => d.Guid == guid).Id;
                         traceDto.PsaDatasetId = datasetId;
                         VTSWebService.DataAccess.PsaTrace traceEntity = PsaTraceAssembler.FromDtoToEntity(traceDto);
                         database.PsaTrace.Add(traceEntity);
                         database.SaveChanges();
                     }
                 }
             }
         }
         else
         {
             List <PsaTraceDto> bufferList = new List <PsaTraceDto>(dataset.Traces);
             foreach (PsaTraceDto traceDto in bufferList) // to remove known traces
             {
                 if (IsKnownTrace(traceDto))
                 {
                     DateTime    date        = traceDto.Date;
                     string      vin         = traceDto.Vin;
                     PsaTraceDto dtoToRemove = dataset.Traces.FirstOrDefault(t =>
                                                                             t.Date == date && t.Vin == vin);
                     if (dtoToRemove == null)
                     {
                         throw new Exception("Trace not found here!");
                     }
                     dataset.Traces.Remove(dtoToRemove);
                 }
             }
             if (dataset.Traces.Count > 0) // if there are still some new
             {
                 VTSWebService.DataAccess.PsaDataset datasetEntity =
                     PsaDatasetAssembler.FromDtoToEntity(dataset);
                 using (VTSDatabase database = new VTSDatabase())
                 {
                     string vin = dataset.Traces.First().Vin;
                     datasetEntity.VehicleEntityId =
                         database.Vehicle.First(v => v.Vin == vin).Id;
                     database.PsaDataset.Add(datasetEntity);
                     database.SaveChanges();
                 }
             }
         }
     }
 }
 public void Aggregate()
 {
     using (VTSDatabase database = new VTSDatabase())
     {
         foreach (AnalyticStatisticsItemEntity item in database.AnalyticStatisticsItem)
         {
             AggregateAnalyticStatisticsItemEntity(item, database);
         }
         database.SaveChanges();
     }
 }
        private void PersistNewAnalyticStatisticsItem(AnalyticStatisticsItem item)
        {
            AnalyticStatisticsItemEntity entity =
                AnalyticStatisticsItemAssembler.FromDomainObjectToEntity(item);

            using (VTSDatabase database = new VTSDatabase())
            {
                database.AnalyticStatisticsItem.Add(entity);
                database.SaveChanges();
                RememberUpdatedItem(entity);
            }
        }
        private void AddNewValueToExistingItem(AnalyticStatisticsItem item,
                                               AnalyticStatisticsValue value)
        {
            AnalyticStatisticsValueEntity entityToAdd =
                AnalyticStatisticsValueAssembler.FromDomainObjectToEntity(value);

            using (VTSDatabase database = new VTSDatabase())
            {
                AnalyticStatisticsItemEntity existingItem = database.AnalyticStatisticsItem.First(
                    i => i.Type == (int)item.Type &&
                    i.TargetEngineFamilyType == (int)item.TargetEngineFamilyType &&
                    i.TargetEngineType == (int)item.TargetEngineType);
                existingItem.AnalyticStatisticsValue.Add(entityToAdd);
                database.SaveChanges();
                RememberUpdatedItem(existingItem);
            }
        }
 private void UpdateValue(AnalyticStatisticsItem item,
                          AnalyticStatisticsValue value)
 {
     using (VTSDatabase database = new VTSDatabase())
     {
         AnalyticStatisticsItemEntity existingItem = database.AnalyticStatisticsItem.First(
             i => i.Type == (int)item.Type &&
             i.TargetEngineFamilyType == (int)item.TargetEngineFamilyType &&
             i.TargetEngineType == (int)item.TargetEngineType);
         AnalyticStatisticsValueEntity entityToUpdate = existingItem.AnalyticStatisticsValue.First(
             v => v.SourcePsaParametersSetId == value.SourcePsaParametersSetId);
         if (!entityToUpdate.Value.Equals(value.Value))
         {
             entityToUpdate.Value = value.Value;
             database.SaveChanges();
             RememberUpdatedItem(existingItem);
         }
     }
 }
 public void Aggregate(List <long> statisticsItemIds)
 {
     using (VTSDatabase database = new VTSDatabase())
     {
         List <AnalyticStatisticsItemEntity> items =
             new List <AnalyticStatisticsItemEntity>();
         foreach (long id in statisticsItemIds)
         {
             AnalyticStatisticsItemEntity entity = database.
                                                   AnalyticStatisticsItem.First(item => item.Id == id);
             items.Add(entity);
         }
         foreach (AnalyticStatisticsItem item in items)
         {
             AggregateAnalyticStatisticsItemEntity(item, database);
         }
         database.SaveChanges();
     }
 }
Exemple #8
0
 private void LogUnrecognizedVin(string vin)
 {
     if (vin.Length != 17)
     {
         return;
     }
     using (VTSDatabase database = new VTSDatabase())
     {
         if (!database.UnrecognizedVin.Any(
                 uv =>
                 uv.Vin.Equals(vin, StringComparison.OrdinalIgnoreCase)))
         {
             database.UnrecognizedVin.Add(
                 new UnrecognizedVin
             {
                 Date = DateTime.Now,
                 Vin  = vin
             });
             database.SaveChanges();
         }
     }
 }
Exemple #9
0
        /// <summary>
        /// Persists dataset with all duplicate protections.
        /// </summary>
        /// <returns>Persisted dataset guid.</returns>
        public Guid Persist(PsaDatasetDto dataset)
        {
            Guid updatedDatasetGuid = Guid.Empty;

            if (IsKnownDataset(dataset))
            {
                foreach (PsaTraceDto traceDto in dataset.Traces)
                {
                    // skip known traces, skip empty traces
                    if (!IsKnownTrace(traceDto) && traceDto.ParametersSets.Count > 0)
                    {
                        // persist trace for existing dataset
                        using (VTSDatabase database = new VTSDatabase())
                        {
                            Guid             guid   = dataset.Guid;
                            PsaDatasetEntity entity = database.PsaDataset.FirstOrDefault(d => d.Guid == guid);
                            if (entity != null)
                            {
                                traceDto.PsaDatasetId = entity.Id;
                            }
                            PsaTrace traceEntity = PsaTraceAssembler.FromDtoToEntity(traceDto);
                            database.PsaTrace.Add(traceEntity);
                            database.SaveChanges();
                            updatedDatasetGuid = dataset.Guid; // to update analytic data based on updated dataset
                        }
                    }
                }
            }
            else
            {
                List <PsaTraceDto> bufferList = new List <PsaTraceDto>(dataset.Traces);
                foreach (PsaTraceDto traceDto in bufferList) // to remove known traces
                {
                    if (IsKnownTrace(traceDto) && traceDto.ParametersSets.Count > 0)
                    {
                        DateTime    date        = traceDto.Date;
                        string      vin         = traceDto.Vin;
                        PsaTraceDto dtoToRemove = dataset.Traces.FirstOrDefault(t =>
                                                                                t.Date == date && t.Vin == vin);
                        if (dtoToRemove == null)
                        {
                            throw new Exception("Trace not found here!");
                        }
                        dataset.Traces.Remove(dtoToRemove);
                    }
                }
                if (dataset.Traces.Count > 0) // if there are still some new
                {
                    string vin = dataset.Traces.First().Vin;
                    if (!VinChecker.IsValid(vin))
                    {
                        throw new FaultException <VtsWebServiceException>(
                                  new VtsWebServiceException(String.Format("Cannot save data for invalid vin {0}", vin)));
                    }
                    PsaDatasetEntity datasetEntity = PsaDatasetAssembler.FromDtoToEntity(dataset);
                    using (VTSDatabase database = new VTSDatabase())
                    {
                        datasetEntity.VehicleEntityId =
                            database.Vehicle.First(v => v.Vin == vin).Id;
                        database.PsaDataset.Add(datasetEntity);
                        database.SaveChanges();
                        updatedDatasetGuid = dataset.Guid; // to update analytic data based on updated dataset
                    }
                }
            }
            return(updatedDatasetGuid);
        }