public void UpdateByRouteAndApproachID(int routeID, string signalId, int newOrderNumber)
        {
            var RouteDetail = (from r in db.RouteSignals
                               where r.RouteId == routeID &&
                               r.SignalId == signalId
                               select r).FirstOrDefault();

            if (RouteDetail != null)
            {
                var newRouteDetail = new RouteSignal();
                newRouteDetail.Order = newOrderNumber;

                try
                {
                    db.Entry(RouteDetail).CurrentValues.SetValues(newRouteDetail);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    //var repository =
                    //    ApplicationEventRepositoryFactory.Create();
                    //var error = new ApplicationEvent();
                    //error.ApplicationName = "MOE.Common";
                    //error.Class = "Models.Repository.ApproachRouteDetailsRepository";
                    //error.Function = "UpdateByRouteAndApproachID";
                    //error.Description = ex.Message;
                    //error.SeverityLevel = ApplicationEvent.SeverityLevels.High;
                    //error.Timestamp = DateTime.Now;
                    //repository.Add(error);
                    //throw;
                }
            }
        }
Beispiel #2
0
        public void Update(DetectionHardware DetectionHardware)
        {
            var g = (from r in db.DetectionHardwares
                     where r.ID == DetectionHardware.ID
                     select r).FirstOrDefault();

            if (g != null)
            {
                db.Entry(g).CurrentValues.SetValues(DetectionHardware);
                db.SaveChanges();
            }
            else
            {
                db.DetectionHardwares.Add(DetectionHardware);
                db.SaveChanges();
            }
        }
Beispiel #3
0
        public void Update(LaneType laneType)
        {
            var g = (from r in db.LaneTypes
                     where r.LaneTypeID == laneType.LaneTypeID
                     select r).FirstOrDefault();

            if (g != null)
            {
                db.Entry(g).CurrentValues.SetValues(laneType);
                db.SaveChanges();
            }
            else
            {
                db.LaneTypes.Add(laneType);
                db.SaveChanges();
            }
        }
        public void Update(MovementType movementType)
        {
            var g = (from r in db.MovementTypes
                     where r.MovementTypeID == movementType.MovementTypeID
                     select r).FirstOrDefault();

            if (g != null)
            {
                db.Entry(g).CurrentValues.SetValues(movementType);
                db.SaveChanges();
            }
            else
            {
                db.MovementTypes.Add(movementType);
                db.SaveChanges();
            }
        }
Beispiel #5
0
        public void Update(Detector detector)
        {
            var g = (from r in _db.Detectors
                     where r.ID == detector.ID
                     select r).FirstOrDefault();

            if (g != null)
            {
                foreach (var i in detector.DetectionTypeIDs)
                {
                    var t = (from r in _db.DetectionTypes
                             where r.DetectionTypeID == i
                             select r).FirstOrDefault();

                    detector.DetectionTypes.Add(t);
                }
                try
                {
                    _db.Entry(g).CurrentValues.SetValues(detector);
                    _db.SaveChanges();
                }
                catch (Exception ex)
                {
                    //var repository =
                    //    ApplicationEventRepositoryFactory.Create();
                    //var error = new ApplicationEvent();
                    //error.ApplicationName = "MOE.Common";
                    //error.Class = "Models.Repository.DetectorRepository";
                    //error.Function = "Update";
                    //error.Description = ex.Message;
                    //error.SeverityLevel = ApplicationEvent.SeverityLevels.High;
                    //error.Timestamp = DateTime.Now;
                    //repository.Add(error);
                    //throw;
                }
            }
        }
Beispiel #6
0
        public void AddOrUpdate(Approach approach)
        {
            var g = (from r in _db.Approaches
                     where r.ApproachID == approach.ApproachID
                     select r).FirstOrDefault();

            if (approach.Detectors != null)
            {
                foreach (var det in approach.Detectors)
                {
                    AddDetectiontypestoDetector(det);
                }
            }
            if (g != null)
            {
                try
                {
                    _db.Entry(g).CurrentValues.SetValues(approach);
                    _db.SaveChanges();
                }
                catch (Exception ex)
                {
                    //var repository =
                    //    ApplicationEventRepositoryFactory.Create();
                    //var error = new ApplicationEvent
                    //{
                    //    ApplicationName = "MOE.Common",
                    //    Class = "Models.Repository.ApproachRepository",
                    //    Function = "Update",
                    //    Description = ex.Message,
                    //    SeverityLevel = ApplicationEvent.SeverityLevels.High,
                    //    Timestamp = DateTime.Now
                    //};
                    //repository.Add(error);
                    //throw;
                }
            }
            else
            {
                try
                {
                    foreach (var d in approach.Detectors)
                    {
                        if (d.DetectionTypes == null && d.DetectionTypeIDs != null)
                        {
                            d.DetectionTypes = _db.DetectionTypes
                                               .Where(dt => d.DetectionTypeIDs.Contains(dt.DetectionTypeID)).ToList();
                        }
                    }
                    _db.Approaches.Add(approach);
                    _db.SaveChanges();
                }

                catch (Exception ex)
                {
                    //var repository =
                    //    ApplicationEventRepositoryFactory.Create();
                    //var error = new ApplicationEvent
                    //{
                    //    ApplicationName = "MOE.Common",
                    //    Class = "Models.Repository.ApproachRepository",
                    //    Function = "Add",
                    //    Description = ex.Message,
                    //    SeverityLevel = ApplicationEvent.SeverityLevels.High,
                    //    Timestamp = DateTime.Now
                    //};
                    //repository.Add(error);
                    //throw;
                }
            }
        }
Beispiel #7
0
        //private void CopyApproaches(Signal signalFromDb, Signal newSignal)
        //{
        //    var approaches = (from r in _db.Approaches
        //        where r.VersionID == signalFromDb.VersionID
        //        select r).ToList();

        //    foreach (var apprFromDb in approaches)
        //    {
        //        var newApp = new Approach();

        //        newApp.SignalID = newSignal.SignalID;
        //        newApp.Description = apprFromDb.Description;
        //        newApp.DirectionTypeID = apprFromDb.DirectionTypeID;
        //        newApp.ProtectedPhaseNumber = apprFromDb.ProtectedPhaseNumber;
        //        newApp.DirectionTypeID = apprFromDb.DirectionTypeID;
        //        newApp.IsProtectedPhaseOverlap = apprFromDb.IsProtectedPhaseOverlap;
        //        newApp.IsPermissivePhaseOverlap = apprFromDb.IsPermissivePhaseOverlap;
        //        newApp.MPH = apprFromDb.MPH;
        //        newApp.PermissivePhaseNumber = apprFromDb.PermissivePhaseNumber;
        //        newApp.VersionID = newSignal.VersionID;

        //        _db.Approaches.Add(newApp);
        //        _db.SaveChanges();

        //        CopyDetectors(apprFromDb, newApp);
        //    }
        //}

        //private void CopyDetectors(Approach apprFromDb, Approach newApp)
        //{
        //    var detectorsFromDb = (from r in _db.Detectors
        //        where r.ApproachID == apprFromDb.ApproachID
        //        select r).ToList();

        //    foreach (var detFromDb in detectorsFromDb)
        //    {
        //        var newDetector = new Detector();

        //        newDetector.DecisionPoint = detFromDb.DecisionPoint;
        //        newDetector.LatencyCorrection = detFromDb.LatencyCorrection;
        //        newDetector.ApproachID = newApp.ApproachID;
        //        newDetector.DateAdded = DateTime.Today;
        //        newDetector.DetChannel = detFromDb.DetChannel;
        //        newDetector.DetectionHardwareID = detFromDb.DetectionHardwareID;
        //        newDetector.DetectorID = detFromDb.DetectorID;
        //        newDetector.LaneNumber = detFromDb.LaneNumber;
        //        if(detFromDb.DetectorCommentIDs != null)
        //            newDetector.DetectorCommentIDs.AddRange(detFromDb.DetectorCommentIDs);
        //        newDetector.MovementTypeID = detFromDb.MovementTypeID;
        //        newDetector.MinSpeedFilter = detFromDb.MinSpeedFilter;
        //        newDetector.DistanceFromStopBar = detFromDb.DistanceFromStopBar;
        //        if(newDetector.DetectionTypes == null)
        //            newDetector.DetectionTypes = new List<DetectionType>();
        //        newDetector.DetectionTypes.AddRange(detFromDb.DetectionTypes);
        //        _db.Detectors.Add(newDetector);
        //        _db.SaveChanges();
        //    }
        //}


        //public bool CheckReportAvialabilityForSignal(string signalId, int metricTypeId)
        //{
        //    var signal = GetLatestVersionOfAllSignals().Find(s => s.SignalID == signalId);
        //    return signal.CheckReportAvailabilityForSignal(metricTypeId);
        //}


        public void Update(Signal incomingSignal)
        {
            var signalFromDatabase = (from r in _db.Signals
                                      where r.VersionID == incomingSignal.VersionID
                                      select r).FirstOrDefault();

            if (signalFromDatabase != null)
            {
                if (incomingSignal.VersionActionId == 0)
                {
                    incomingSignal.VersionActionId = signalFromDatabase.VersionActionId;
                }
                _db.Entry(signalFromDatabase).CurrentValues.SetValues(incomingSignal);
                if (incomingSignal.Approaches != null)
                {
                    foreach (var a in incomingSignal.Approaches)
                    {
                        var approach =
                            signalFromDatabase.Approaches.FirstOrDefault(app => app.ApproachID == a.ApproachID);
                        if (approach != null)
                        {
                            if (!a.Equals(approach))
                            {
                                _db.Entry(approach).CurrentValues.SetValues(a);
                            }
                        }
                        else
                        {
                            signalFromDatabase.Approaches.Add(a);
                        }
                        if (a.Detectors != null)
                        {
                            foreach (var newDetector in a.Detectors)
                            {
                                var detectorFromDatabase = signalFromDatabase.GetDetectorsForSignal()
                                                           .FirstOrDefault(d => d.ID == newDetector.ID);
                                if (newDetector.DetectionTypes == null)
                                {
                                    newDetector.DetectionTypes = _db.DetectionTypes.Where(x =>
                                                                                          newDetector.DetectionTypeIDs.Contains(x.DetectionTypeID)).ToList();
                                }
                                if (detectorFromDatabase != null)
                                {
                                    if (!newDetector.Equals(detectorFromDatabase))
                                    {
                                        if (detectorFromDatabase.DetectionTypes == null)
                                        {
                                            detectorFromDatabase.DetectionTypes = new List <DetectionType>();
                                        }
                                        var deletedDetectionTypes = detectorFromDatabase.DetectionTypes
                                                                    .Except(newDetector.DetectionTypes).ToList();
                                        var addedDetectionTypes = newDetector.DetectionTypes
                                                                  .Except(detectorFromDatabase.DetectionTypes).ToList();

                                        deletedDetectionTypes.ForEach(delDet =>
                                                                      detectorFromDatabase.DetectionTypes.Remove(delDet));
                                        foreach (var n in addedDetectionTypes)
                                        {
                                            if (_db.Entry(n).State == EntityState.Detached)
                                            {
                                                _db.DetectionTypes.Attach(n);
                                            }
                                            detectorFromDatabase.DetectionTypes.Add(n);
                                        }

                                        //var detectionTypes = _db.DetectionTypes.Where(x => gd.DetectionTypeIDs.Contains(x.DetectionTypeID)).ToList();
                                        //graphDetector.DetectionTypes = detectionTypes;
                                        //graphDetector.DetectionTypeIDs = gd.DetectionTypeIDs;

                                        _db.Entry(detectorFromDatabase).CurrentValues.SetValues(newDetector);
                                    }
                                }
                                else
                                {
                                    if (newDetector.DetectionTypes == null)
                                    {
                                        newDetector.DetectionTypes = _db.DetectionTypes.Where(x =>
                                                                                              newDetector.DetectionTypeIDs.Contains(x.DetectionTypeID)).ToList();
                                    }
                                    approach.Detectors.Add(newDetector);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (var a in incomingSignal.Approaches)
                {
                    foreach (var gd in a.Detectors)
                    {
                        gd.DetectionTypes = _db.DetectionTypes
                                            .Where(x => gd.DetectionTypeIDs.Contains(x.DetectionTypeID)).ToList();
                    }
                }
                _db.Signals.Add(incomingSignal);
            }
            try
            {
                _db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }