private async Task <List <BaseProperty> > SaveResponseEntities(Shared.Enums.PropertyType propertyType, PropertySearchResultModel result, PropertySearchRequestModel searchModel, bool useSearchIdentifier) { if (result == null || result.ResponseObject == null) { return(null); } List <BaseProperty> entities = null; switch (propertyType) { case Shared.Enums.PropertyType.AIRCRAFT: AircraftsResponse aircraftsResponse = result.ResponseObject as AircraftsResponse; if (aircraftsResponse == null) { throw new Exception("Could not convert response to AircraftsResponse"); } if (aircraftsResponse.Aircraft != null && aircraftsResponse.Aircraft.Length > 0) { List <Data.Aircraft> aircrafts = await SaveAircrafts(aircraftsResponse, result.RequestId, searchModel, useSearchIdentifier); entities = (aircrafts).Cast <BaseProperty>().ToList(); } break; case Shared.Enums.PropertyType.VEHICLE: MotorVehicleRegistrationResponse vehiclesResponse = result.ResponseObject as MotorVehicleRegistrationResponse; if (vehiclesResponse == null) { throw new Exception("Could not convert response to MotorVehicleRegistrationResponse"); } if (vehiclesResponse.Vehicles != null && vehiclesResponse.Vehicles.Length > 0) { List <Data.Vehicle> vehicles = await SaveVehicles(vehiclesResponse, result.RequestId, searchModel, useSearchIdentifier); entities = (vehicles).Cast <BaseProperty>().ToList(); } //GetMotorVehicleRegistrationInfoV3ResponseTypeResponse vehiclesResponse = result.ResponseObject as GetMotorVehicleRegistrationInfoV3ResponseTypeResponse; //if (vehiclesResponse == null) // throw new Exception("Could not convert response to GetMotorVehicleRegistrationInfoV3ResponseTypeResponse"); //if (vehiclesResponse.Results != null && vehiclesResponse.Results.Length > 0) //{ // List<Data.Vehicle> vehicles = await SaveVehiclesV3(vehiclesResponse, result.RequestId, searchModel, useSearchIdentifier); // entities = (vehicles).Cast<BaseProperty>().ToList(); //} break; default: break; } return(entities); }
private async Task <List <Data.Vehicle> > SaveVehicles(MotorVehicleRegistrationResponse vehiclesResponse, long?messageId, PropertySearchRequestModel searchModel, bool useSearchIdentifier) { List <Data.Vehicle> newVehicles = new List <Data.Vehicle>(); List <Data.Vehicle> existingVehicles = new List <Data.Vehicle>(); foreach (RegiX.Client.ResponseModels.Vehicle vehicle in vehiclesResponse.Vehicles) { string regNumberToUse = vehicle.VehicleRegistrationNumber; if (useSearchIdentifier) { regNumberToUse = searchModel.Identifier; } var existing = await _context.Vehicle .Include(x => x.VehicleExtension) .Include(x => x.VehicleOwner) .Where(x => x.RegistrationNumber == regNumberToUse && x.VehicleExtension != null && x.VehicleExtension.Deactivated == false) .ToListAsync(); if (!existing.Any()) { Data.Vehicle entity = vehicle.ToEntity(); entity.RegistrationNumber = regNumberToUse; entity.VehicleExtension = new VehicleExtension { VehicleId = entity.Id, UpdatedAt = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), RequestId = messageId, Deactivated = false }; if (entity != null) { newVehicles.Add(entity); } } else { foreach (Data.Vehicle item in existing) { item.VehicleExtension.Deactivated = true; _context.VehicleOwner.RemoveRange(item.VehicleOwner); } existing[0].UpdateEntity(vehicle); existing[0].RegistrationNumber = regNumberToUse; existing[0].VehicleExtension.Deactivated = false; existing[0].VehicleExtension.UpdatedAt = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); existing[0].VehicleExtension.RequestId = messageId; existingVehicles.AddRange(existing); } } if (newVehicles.Count > 0) { _context.Vehicle.AddRange(newVehicles); } if (existingVehicles.Count > 0) { _context.Vehicle.UpdateRange(existingVehicles); } await _context.SaveChangesAsync(); return(newVehicles.Concat(existingVehicles).ToList()); }
private List <object> GetPropertyViewModelsFromResponse(Shared.Enums.PropertyType propertyType, PropertySearchResultModel result, PropertySearchRequestModel searchModel) { if (result == null || result.ResponseObject == null) { return(null); } List <object> entities = null; switch (propertyType) { case Shared.Enums.PropertyType.AIRCRAFT: AircraftsResponse aircraftsResponse = result.ResponseObject as AircraftsResponse; if (aircraftsResponse == null) { throw new Exception("Could not convert response to AircraftsResponse"); } if (aircraftsResponse.Aircraft != null && aircraftsResponse.Aircraft.Length > 0) { //List<AircraftViewModel> aircrafts = SaveAircrafts(aircraftsResponse, result.RequestId, searchModel); List <AircraftViewModel> aircrafts = new List <AircraftViewModel>(); for (int i = 0; i < aircraftsResponse.Aircraft.Length; i++) { AircraftViewModel airViewModel = aircraftsResponse.Aircraft[i].ToViewModel(); airViewModel.ExtensionRequestId = result.RequestId; if (regixCertificateSettings.SaveEntityWithSearchedIdentifier && searchModel.IdentifierTypeCode == "MSN") { airViewModel.MsnserialNumber = searchModel.Identifier; } aircrafts.Add(airViewModel); } entities = (aircrafts).Cast <object>().ToList(); } break; case Shared.Enums.PropertyType.VEHICLE: if (regixCertificateSettings.UseVehicleV3) { GetMotorVehicleRegistrationInfoV3Response vehiclesResponse = result.ResponseObject as GetMotorVehicleRegistrationInfoV3Response; if (vehiclesResponse == null) { throw new Exception("Could not convert response to GetMotorVehicleRegistrationInfoV3Response"); } if (vehiclesResponse.Response != null && vehiclesResponse.Response.Results.Length > 0 && vehiclesResponse.Response.Results[0].VehicleData != null) { List <VehicleViewModel> vehicles = new List <VehicleViewModel>(); VehicleViewModel vehicleViewModel = vehiclesResponse.Response.Results[0].ToViewModel(); vehicleViewModel.ExtensionRequestId = result.RequestId; if (regixCertificateSettings.SaveEntityWithSearchedIdentifier) { vehicleViewModel.RegistrationNumber = searchModel.Identifier; } vehicles.Add(vehicleViewModel); entities = (vehicles).Cast <object>().ToList(); } } else { MotorVehicleRegistrationResponse vehiclesResponse = result.ResponseObject as MotorVehicleRegistrationResponse; if (vehiclesResponse == null) { throw new Exception("Could not convert response to MotorVehicleRegistrationResponse"); } if (vehiclesResponse.Vehicles != null && vehiclesResponse.Vehicles.Length > 0) { //List<Data.Vehicle> vehicles = await SaveVehicles(vehiclesResponse, result.RequestId, searchModel, useSearchIdentifier); List <VehicleViewModel> vehicles = new List <VehicleViewModel>(); foreach (RegiX.Client.ResponseModels.Vehicle vehicle in vehiclesResponse.Vehicles) { VehicleViewModel vehicleViewModel = vehicle.ToViewModel(); vehicleViewModel.ExtensionRequestId = result.RequestId; if (regixCertificateSettings.SaveEntityWithSearchedIdentifier) { vehicleViewModel.RegistrationNumber = searchModel.Identifier; } vehicles.Add(vehicleViewModel); } entities = (vehicles).Cast <object>().ToList(); } } break; case Shared.Enums.PropertyType.VESSEL: RegistrationInfoByOwnerResponse vesselResponse = result.ResponseObject as RegistrationInfoByOwnerResponse; if (vesselResponse == null) { throw new Exception("Could not convert response to RegistrationInfoByOwnerResponse"); } if (vesselResponse.VesselInfo != null) { VesselViewModel vesselViewModel = vesselResponse.VesselInfo.ToViewModel(); vesselViewModel.ExtensionRequestId = result.RequestId; vesselViewModel = SetVesselStatusInModel(vesselViewModel); entities = new List <object>(); entities.Add(vesselViewModel); } break; case Shared.Enums.PropertyType.AGRIFORMACHINERY: AgriculturalMachineryCollectionModel machines = result.ResponseObject as AgriculturalMachineryCollectionModel; entities = (machines.Machines).Cast <object>().ToList(); break; default: break; } return(entities); }
private static BaseResponse ParseResponseToObject <T>(XElement node) { try { if (node == null) { throw new Exception("Node element is null"); } BaseResponse parsedObject = null; if (typeof(T) == typeof(AircraftsResponse)) { parsedObject = new AircraftsResponse(); using (StringReader reader = new StringReader(node.ToString())) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(AircraftsResponse)); parsedObject = (AircraftsResponse)xmlSerializer.Deserialize(reader); return(parsedObject); } } else if (typeof(T) == typeof(MotorVehicleRegistrationResponse)) { parsedObject = new MotorVehicleRegistrationResponse(); using (StringReader reader = new StringReader(node.ToString())) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(MotorVehicleRegistrationResponse)); parsedObject = (MotorVehicleRegistrationResponse)xmlSerializer.Deserialize(reader); return(parsedObject); } } else if (typeof(T) == typeof(GetMotorVehicleRegistrationInfoV3Response)) { parsedObject = new GetMotorVehicleRegistrationInfoV3Response(); using (StringReader reader = new StringReader(node.ToString())) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(GetMotorVehicleRegistrationInfoV3Response)); parsedObject = (GetMotorVehicleRegistrationInfoV3Response)xmlSerializer.Deserialize(reader); return(parsedObject); } } else if (typeof(T) == typeof(ValidPersonResponse)) { parsedObject = new ValidPersonResponse(); using (StringReader reader = new StringReader(node.ToString())) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ValidPersonResponse)); parsedObject = (ValidPersonResponse)xmlSerializer.Deserialize(reader); return(parsedObject); } } else if (typeof(T) == typeof(ValidUICResponse)) { parsedObject = new ValidUICResponse(); using (StringReader reader = new StringReader(node.ToString())) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ValidUICResponse)); parsedObject = (ValidUICResponse)xmlSerializer.Deserialize(reader); return(parsedObject); } } else if (typeof(T) == typeof(RegistrationInfoByOwnerResponse)) { parsedObject = new RegistrationInfoByOwnerResponse(); using (StringReader reader = new StringReader(node.ToString())) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(RegistrationInfoByOwnerResponse)); parsedObject = (RegistrationInfoByOwnerResponse)xmlSerializer.Deserialize(reader); return(parsedObject); } } return(null); } catch (Exception ex) { throw new Exception("Error parsing response to object: " + ex.Message); } }