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.Aircraft> > SaveAircrafts(AircraftsResponse aircraftsResponse, long?messageId, PropertySearchRequestModel searchModel, bool useSearchIdentifier) { List <Data.Aircraft> newAircrafts = new List <Data.Aircraft>(); List <Data.Aircraft> existingAircrafts = new List <Data.Aircraft>(); for (int i = 0; i < aircraftsResponse.Aircraft.Length; i++) { string msnToUse = aircraftsResponse.Aircraft[i].MSNSerialNumber; if (useSearchIdentifier) { msnToUse = searchModel.IdentifierTypeCode.ToUpper() == "MSN" ? searchModel.Identifier : searchModel.Identifier + "_" + i.ToString(); } var existing = await _context.Aircraft .Include(x => x.AircraftExtension) .Include(x => x.AircraftRegistration) .Include(x => x.AircraftRegistration).ThenInclude(a => a.AircraftRegistrationOperatorEntity) .Include(x => x.AircraftRegistration).ThenInclude(a => a.AircraftRegistrationOperatorPerson) .Include(x => x.AircraftRegistration).ThenInclude(a => a.AircraftRegistrationOwnerEntity) .Include(x => x.AircraftRegistration).ThenInclude(a => a.AircraftRegistrationOwnerPerson) .Include(x => x.AircraftDebt) .Where(x => x.MsnserialNumber == msnToUse && x.AircraftExtension != null && x.AircraftExtension.Deactivated == false) .ToListAsync(); if (!existing.Any()) { Data.Aircraft entity = aircraftsResponse.Aircraft[i].ToEntity(); entity.MsnserialNumber = msnToUse; entity.AircraftExtension = new AircraftExtension { AircraftId = entity.Id, UpdatedAt = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), RequestId = messageId, Deactivated = false }; if (entity != null) { newAircrafts.Add(entity); } } else { foreach (Data.Aircraft item in existing) { item.AircraftExtension.Deactivated = true; foreach (Data.AircraftRegistration reg in item.AircraftRegistration) { _context.AircraftRegistrationOperatorEntity.RemoveRange(reg.AircraftRegistrationOperatorEntity); _context.AircraftRegistrationOperatorPerson.RemoveRange(reg.AircraftRegistrationOperatorPerson); _context.AircraftRegistrationOwnerPerson.RemoveRange(reg.AircraftRegistrationOwnerPerson); _context.AircraftRegistrationOwnerEntity.RemoveRange(reg.AircraftRegistrationOwnerEntity); } _context.AircraftRegistration.RemoveRange(item.AircraftRegistration); _context.AircraftDebt.RemoveRange(item.AircraftDebt); } existing[0].UpdateEntity(aircraftsResponse.Aircraft[i]); existing[0].MsnserialNumber = msnToUse; existing[0].AircraftExtension.Deactivated = false; existing[0].AircraftExtension.UpdatedAt = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); existing[0].AircraftExtension.RequestId = messageId; existingAircrafts.AddRange(existing); } } if (newAircrafts.Count > 0) { _context.Aircraft.AddRange(newAircrafts); } if (existingAircrafts.Count > 0) { _context.Aircraft.UpdateRange(existingAircrafts); } await _context.SaveChangesAsync(); return(newAircrafts.Concat(existingAircrafts).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); } }