public void UpdateVehicle(DB_Vehicle vehicle)
        {
            if (vehicle == null)
            {
                throw new ArgumentNullException(nameof(vehicle));
            }

            if (vehicle.ID < 1)
            {
                _db.DB_Vehicles.InsertOnSubmit(vehicle);
            }

            _db.SubmitChanges();
        }
示例#2
0
        public void UpdateVehicle(Vehicle vehicle)
        {
            if (vehicle == null)
            {
                throw new ArgumentNullException(nameof(vehicle));
            }

            if (!vehicle.IsValid())
            {
                throw new ArgumentException("vehicle is invalid!");
            }

            lock (vehicle)
            {
                var        isNew = false;
                DB_Vehicle dbVehicle;
                if (vehicle.IsPersisted)
                {
                    dbVehicle = Controller.Instance.Repository.GetVehicle(vehicle.Id);
                }
                else
                {
                    dbVehicle = new DB_Vehicle();
                    isNew     = true;
                }

                #region basic properties
                dbVehicle.DateCreated      = vehicle.DateCreated;
                dbVehicle.DateModified     = DateTime.Now;
                dbVehicle.Description      = (!string.IsNullOrEmpty(vehicle.Description)) ? vehicle.Description : null;
                dbVehicle.MemberUID        = vehicle.MemberUid;
                dbVehicle.ModelID          = vehicle.Model.Id;
                dbVehicle.ManufacturerID   = vehicle.Model.ManufacturerId;
                dbVehicle.VehicleTypeId    = vehicle.Model.Type.Id;
                dbVehicle.PoliceForce      = (!string.IsNullOrEmpty(vehicle.PoliceForce)) ? vehicle.PoliceForce : null;
                dbVehicle.PolicePhoneNo    = (!string.IsNullOrEmpty(vehicle.PolicePhoneNumber)) ? vehicle.PolicePhoneNumber : null;
                dbVehicle.PoliceReference  = (!string.IsNullOrEmpty(vehicle.PoliceReference)) ? vehicle.PoliceReference : null;
                dbVehicle.Registration     = (!string.IsNullOrEmpty(vehicle.Registration)) ? vehicle.Registration : null;
                dbVehicle.TheftDescription = (!string.IsNullOrEmpty(vehicle.TheftDescription)) ? vehicle.TheftDescription : null;
                dbVehicle.VIN          = (!string.IsNullOrEmpty(vehicle.Vin)) ? vehicle.Vin : null;
                dbVehicle.EngineNumber = (!string.IsNullOrEmpty(vehicle.EngineNumber)) ? vehicle.EngineNumber : null;
                dbVehicle.Status       = (byte)vehicle.Status;

                if (vehicle.Colour != null)
                {
                    dbVehicle.ColourID = vehicle.Colour.Id;
                }
                else
                {
                    dbVehicle.ColourID = null;
                }

                if (vehicle.TheftDate.HasValue)
                {
                    dbVehicle.TheftDate = vehicle.TheftDate.Value;
                }
                else
                {
                    dbVehicle.TheftDate = null;
                }

                if (vehicle.TheftLatitude.HasValue)
                {
                    dbVehicle.TheftLatitude = vehicle.TheftLatitude.Value;
                }
                else
                {
                    dbVehicle.TheftLatitude = null;
                }

                if (vehicle.TheftLongitude.HasValue)
                {
                    dbVehicle.TheftLongitude = vehicle.TheftLongitude.Value;
                }
                else
                {
                    dbVehicle.TheftLongitude = null;
                }

                if (vehicle.TheftLatitude.HasValue && vehicle.TheftLongitude.HasValue)
                {
                    dbVehicle.IsLocationApprox = vehicle.IsLocationApproximate;
                }
                else
                {
                    dbVehicle.IsLocationApprox = null;
                }

                if (vehicle.TheftMethod != null)
                {
                    dbVehicle.TheftMethodID = vehicle.TheftMethod.Id;
                }
                else
                {
                    dbVehicle.TheftMethodID = null;
                }

                if (vehicle.Year.HasValue)
                {
                    dbVehicle.Year = vehicle.Year.Value;
                }
                else
                {
                    dbVehicle.Year = null;
                }
                #endregion

                Controller.Instance.Repository.UpdateVehicle(dbVehicle);
                if (isNew)
                {
                    vehicle.Id          = dbVehicle.ID;
                    vehicle.IsPersisted = true;
                    Controller.Instance.CacheManager.Add(vehicle.CacheKey, vehicle);
                }

                #region locations
                for (var i = 0; i < vehicle.TheftLocationPlaces.Count; i++)
                {
                    vehicle.TheftLocationPlaces[i] = Controller.Instance.PlacesController.UpdatePlace(vehicle.TheftLocationPlaces[i]);
                }

                var dbPlaces = Controller.Instance.Repository.GetVehicleTheftLocationPlaces(vehicle.Id);

                // look for deletions by comparing against the current db state.
                var dbVehicleTheftLocations = dbPlaces as DB_VehicleTheftLocation[] ?? dbPlaces.ToArray();
                foreach (var originalPlace in dbVehicleTheftLocations.Where(originalPlace => !vehicle.TheftLocationPlaces.Exists(q => q.Id == originalPlace.LocationID)))
                {
                    Controller.Instance.Repository.DeleteVehicleTheftLocationPlace(originalPlace.VehicleID, originalPlace.LocationID);
                }

                // persist new relationships.
                foreach (var place in vehicle.TheftLocationPlaces.Where(place => dbVehicleTheftLocations.Count(q => q.LocationID == place.Id) <= 0))
                {
                    Controller.Instance.Repository.CreateVehicleLocationPlace(vehicle.Id, place.Id);
                }
                #endregion

                #region photos
                lock (vehicle.Photos)
                {
                    // look for deletions by comparing against the current db state.
                    foreach (var originalDbPhoto in Controller.Instance.Repository.GetVehiclePhotos(vehicle.Id).Where(originalDbPhoto => !vehicle.Photos.Exists(q => q.Id == originalDbPhoto.ID)))
                    {
                        Controller.Instance.Repository.DeleteVehiclePhoto(originalDbPhoto.ID);
                    }

                    foreach (var p in vehicle.Photos)
                    {
                        UpdateVehiclePhoto(p);
                    }
                }
                #endregion

                #region videos
                // look for deletions by comparing against the current db state.
                foreach (var originalDbVideo in Controller.Instance.Repository.GetVehicleVideos(vehicle.Id).Where(originalDbVideo => !vehicle.Videos.Exists(q => q.Id == originalDbVideo.ID)))
                {
                    Controller.Instance.Repository.DeleteVehicleVideo(originalDbVideo.ID);
                }

                foreach (var v in vehicle.Videos)
                {
                    UpdateVehicleVideo(v);
                }
                #endregion

                #region security
                // look for deletions by comparing against the current db state.
                foreach (var originalDbVehicleSecurity in Controller.Instance.Repository.GetVehicleSecurities(vehicle.Id).Where(
                             originalDbVehicleSecurity => !vehicle.SecurityTypes.Exists(q => q.Id == originalDbVehicleSecurity.DB_VehicleSecurityType.ID)))
                {
                    Controller.Instance.Repository.DeleteVehicleSecurity(originalDbVehicleSecurity.ID);
                }

                foreach (var vst in vehicle.SecurityTypes)
                {
                    UpdateVehicleSecurity(vehicle, vst);
                }
                #endregion
            }
        }