/// <summary> /// Delete Third Party address location to database. /// </summary> /// <param name="addressLocationDTO">AddressLocationDTO object</param> /// <returns>Task<int></returns> public async Task <int> DeleteAddressLocation(AddressLocationDataDTO addressLocationDTO) { try { using (loggingHelper.RMTraceManager.StartTrace("DataService.DeleteAddressLocation")) { string methodName = typeof(AddressLocationDataService) + "." + nameof(DeleteAddressLocation); loggingHelper.LogMethodEntry(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodEntryEventId); var addressLocationEntity = await DataContext.AddressLocations.Where(n => n.UDPRN == addressLocationDTO.UDPRN).SingleOrDefaultAsync(); DataContext.AddressLocations.Remove(addressLocationEntity); var deleteAddressLocation = await DataContext.SaveChangesAsync(); loggingHelper.LogMethodExit(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodExitEventId); return(deleteAddressLocation); } } catch (DbUpdateException dbUpdateException) { throw new DataAccessException(dbUpdateException, string.Format(ErrorConstants.Err_SqlAddException, string.Concat("Address Location for UDPRN:", addressLocationDTO.UDPRN))); } catch (NotSupportedException notSupportedException) { notSupportedException.Data.Add(ErrorConstants.UserFriendlyErrorMessage, ErrorConstants.Err_Default); throw new InfrastructureException(notSupportedException, ErrorConstants.Err_NotSupportedException); } catch (ObjectDisposedException disposedException) { disposedException.Data.Add(ErrorConstants.UserFriendlyErrorMessage, ErrorConstants.Err_Default); throw new ServiceException(disposedException, ErrorConstants.Err_ObjectDisposedException); } }
/// <summary> /// Setup for Nunit Tests. /// </summary> protected override void OnSetup() { string sbLocationXY = string.Format("POINT({0} {1})", "1234", "4567"); addressLocationDataDTO = new AddressLocationDataDTO { ID = new Guid(), UDPRN = 158642, LocationXY = DbGeometry.FromText(sbLocationXY.ToString(), 27700), Lattitude = 51.64m, Longitude = -0.71m }; var addressLocation = new List <AddressLocation> { new AddressLocation() { ID = new Guid(), UDPRN = 158642, LocationXY = DbGeometry.FromText(sbLocationXY.ToString(), 27700), Lattitude = 51.64m, Longitude = -0.71m } }; var mockAsynEnumerable = new DbAsyncEnumerable <AddressLocation>(addressLocation); var mockAddressLocationDataService = MockDbSet(addressLocation); mockAddressLocationDataService.As <IQueryable>().Setup(mock => mock.Provider).Returns(mockAsynEnumerable.AsQueryable().Provider); mockAddressLocationDataService.As <IQueryable>().Setup(mock => mock.Expression).Returns(mockAsynEnumerable.AsQueryable().Expression); mockAddressLocationDataService.As <IQueryable>().Setup(mock => mock.ElementType).Returns(mockAsynEnumerable.AsQueryable().ElementType); mockAddressLocationDataService.As <IDbAsyncEnumerable>().Setup(mock => mock.GetAsyncEnumerator()).Returns(((IDbAsyncEnumerable <AddressLocation>)mockAsynEnumerable).GetAsyncEnumerator()); mockAddressLocationDataService.Setup(x => x.Include(It.IsAny <string>())).Returns(mockAddressLocationDataService.Object); mockAddressLocationDataService.Setup(x => x.AsNoTracking()).Returns(mockAddressLocationDataService.Object); mockAddressLocationDBContext = CreateMock <AddressLocationDBContext>(); mockAddressLocationDBContext.Setup(x => x.Set <AddressLocation>()).Returns(mockAddressLocationDataService.Object); mockAddressLocationDBContext.Setup(x => x.AddressLocations).Returns(mockAddressLocationDataService.Object); mockLoggingHelper = CreateMock <ILoggingHelper>(); var rmTraceManagerMock = new Mock <IRMTraceManager>(); rmTraceManagerMock.Setup(x => x.StartTrace(It.IsAny <string>(), It.IsAny <Guid>())); mockLoggingHelper.Setup(x => x.RMTraceManager).Returns(rmTraceManagerMock.Object); mockAddressLocationDBFactory = CreateMock <IDatabaseFactory <AddressLocationDBContext> >(); mockAddressLocationDBFactory.Setup(x => x.Get()).Returns(mockAddressLocationDBContext.Object); mockAddressLocationDBContext.Setup(n => n.SaveChangesAsync()).ReturnsAsync(1); testCandidate = new AddressLocationDataService(mockAddressLocationDBFactory.Object, mockLoggingHelper.Object); }
/// <summary> /// Method to update the Address location based on UDPRN. /// </summary> /// <param name="addressLocationUSRPOSTDTO"></param> private async Task UpdateAddressLocationByUDPRN(AddressLocationUSRPOSTDTO addressLocationUSRPOSTDTO) { DbGeometry spatialLocationXY = GetSpatialLocation(addressLocationUSRPOSTDTO); AddressLocationDataDTO updateAddressLocationDTO = new AddressLocationDataDTO() { UDPRN = (int)addressLocationUSRPOSTDTO.UDPRN, LocationXY = spatialLocationXY, Lattitude = (decimal)addressLocationUSRPOSTDTO.Latitude, Longitude = (decimal)addressLocationUSRPOSTDTO.Longitude }; // Update the address location data record to the database. await addressLocationDataService.UpdateExistingAddressLocationByUDPRN(updateAddressLocationDTO); }
/// <summary> /// This method is used to fetch data for Access Links. /// </summary> /// <param name="uDPRN">UDPRN</param> /// <returns>Address Location DTO</returns> public async Task <object> GetAddressLocationByUDPRNJson(int uDPRN) { using (loggingHelper.RMTraceManager.StartTrace("Business.GetAddressLocationByUDPRNJson")) { string methodName = typeof(ThirdPartyAddressLocationBusinessService) + "." + nameof(GetAddressLocationByUDPRNJson); loggingHelper.LogMethodEntry(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodEntryEventId); AddressLocationDataDTO addressLocationDataDto = await this.addressLocationDataService.GetAddressLocationByUDPRN(uDPRN); Mapper.Initialize(cfg => cfg.CreateMap <AddressLocationDataDTO, AddressLocationDTO>()); AddressLocationDTO addressLocationDto = Mapper.Map <AddressLocationDataDTO, AddressLocationDTO>(addressLocationDataDto); var getAddressLocationJsonData = GetAddressLocationJsonData(addressLocationDto); loggingHelper.LogMethodExit(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodExitEventId); return(getAddressLocationJsonData); } }
/// <summary> /// Add new address location to database. /// </summary> /// <param name="addressLocationDTO">AddressLocationDTO object</param> /// <returns>Task<int></returns> public async Task <int> SaveNewAddressLocation(AddressLocationDataDTO addressLocationDTO) { try { using (loggingHelper.RMTraceManager.StartTrace("DataService.SaveNewAddressLocation")) { string methodName = typeof(AddressLocationDataService) + "." + nameof(SaveNewAddressLocation); loggingHelper.LogMethodEntry(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodEntryEventId); var addressLocationEntity = new AddressLocation(); Mapper.Initialize(cfg => cfg.CreateMap <AddressLocationDataDTO, AddressLocation>()); addressLocationEntity = Mapper.Map <AddressLocationDataDTO, AddressLocation>(addressLocationDTO); GenericMapper.Map(addressLocationDTO, addressLocationEntity); DataContext.AddressLocations.Add(addressLocationEntity); var saveNewAddressLocation = await DataContext.SaveChangesAsync(); loggingHelper.LogMethodExit(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodExitEventId); return(saveNewAddressLocation); } } catch (DbUpdateException dbUpdateException) { throw new DataAccessException(dbUpdateException, string.Format(ErrorConstants.Err_SqlAddException, string.Concat("Address Location for UDPRN:", addressLocationDTO.UDPRN))); } catch (NotSupportedException notSupportedException) { notSupportedException.Data.Add(ErrorConstants.UserFriendlyErrorMessage, ErrorConstants.Err_Default); throw new InfrastructureException(notSupportedException, ErrorConstants.Err_NotSupportedException); } catch (ObjectDisposedException disposedException) { disposedException.Data.Add(ErrorConstants.UserFriendlyErrorMessage, ErrorConstants.Err_Default); throw new ServiceException(disposedException, ErrorConstants.Err_ObjectDisposedException); } }
// TODO : Add method when ready // To be implemented in parallel /// <summary> /// Method to save the list of USR data into the database. /// </summary> /// <param name="addressLocationUsrpostdtos">List of Address Locations</param> /// <returns>Task</returns> public async Task SaveUSRDetails(List <AddressLocationUSRPOSTDTO> addressLocationUsrpostdtos) { int fileUdprn; string addressLocationChangeType = string.Empty; if (addressLocationUsrpostdtos == null) { throw new ArgumentNullException(nameof(addressLocationUsrpostdtos)); } using (loggingHelper.RMTraceManager.StartTrace("Business.SaveUSRDetails")) { string methodName = typeof(ThirdPartyAddressLocationBusinessService) + "." + nameof(SaveUSRDetails); loggingHelper.LogMethodEntry(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodEntryEventId); List <string> categoryNamesSimpleLists = new List <string> { ThirdPartyAddressLocationConstants.TASKNOTIFICATION, ThirdPartyAddressLocationConstants.NETWORKLINKDATAPROVIDER, ThirdPartyAddressLocationConstants.DeliveryPointUseIndicator, ThirdPartyAddressLocationConstants.APPROXLOCATION, ReferenceDataCategoryNames.DeliveryPointOperationalStatus, ReferenceDataCategoryNames.NetworkNodeType }; // Get all the reference data Guids required for the below Guid tasktypeId = GetReferenceData(categoryNamesSimpleLists, ThirdPartyAddressLocationConstants.TASKNOTIFICATION, ThirdPartyAddressLocationConstants.TASKACTION); Guid locationProviderId = GetReferenceData(categoryNamesSimpleLists, ThirdPartyAddressLocationConstants.NETWORKLINKDATAPROVIDER, ThirdPartyAddressLocationConstants.EXTERNAL); Guid operationalStatusGUIDLive = GetReferenceData(categoryNamesSimpleLists, ReferenceDataCategoryNames.DeliveryPointOperationalStatus, ThirdPartyAddressLocationConstants.OperationalStatusGUIDLive, true); Guid networkNodeTypeRMGServiceNode = GetReferenceData(categoryNamesSimpleLists, ReferenceDataCategoryNames.NetworkNodeType, ThirdPartyAddressLocationConstants.NetworkNodeTypeRMGServiceNode, true); Guid approxLocation = GetReferenceData(categoryNamesSimpleLists, ReferenceDataCategoryNames.DeliveryPointOperationalStatus, ThirdPartyAddressLocationConstants.APPROXLOCATION, true); Guid notificationTypeId_GUID = await thirdPartyAddressLocationIntegrationService.GetReferenceDataId(ThirdPartyAddressLocationConstants.USRCATEGORY, ThirdPartyAddressLocationConstants.USRREFERENCEDATANAME); foreach (AddressLocationUSRPOSTDTO addressLocationUSRPOSTDTO in addressLocationUsrpostdtos) { // Get the udprn id for each USR record. fileUdprn = (int)addressLocationUSRPOSTDTO.UDPRN; addressLocationChangeType = addressLocationUSRPOSTDTO.ChangeType; // To save new location if (addressLocationChangeType.Equals(ThirdPartyAddressLocationConstants.INSERT)) { if (!await addressLocationDataService.AddressLocationExists(fileUdprn)) { DbGeometry spatialLocationXY = GetSpatialLocation(addressLocationUSRPOSTDTO); AddressLocationDataDTO newAddressLocationDTO = new AddressLocationDataDTO() { ID = Guid.NewGuid(), UDPRN = (int)addressLocationUSRPOSTDTO.UDPRN, LocationXY = spatialLocationXY, Lattitude = (decimal)addressLocationUSRPOSTDTO.Latitude, Longitude = (decimal)addressLocationUSRPOSTDTO.Longitude }; // Save the address location data record to the database. await addressLocationDataService.SaveNewAddressLocation(newAddressLocationDTO); PostalAddressDataDTO postalAddressDataDTO = await addressLocationDataService.GetPostalAddressData((int)fileUdprn); // Check if the delivery point exists if (postalAddressDataDTO.DeliveryPoints != null && postalAddressDataDTO.DeliveryPoints.Count > 0) { DeliveryPointDTO deliveryPointDTO = await thirdPartyAddressLocationIntegrationService.GetDeliveryPointByPostalAddress(postalAddressDataDTO.ID); // Check if the existing delivery point has an approx location. if (deliveryPointDTO.OperationalStatus_GUID == approxLocation) { deliveryPointDTO.LocationXY = spatialLocationXY; deliveryPointDTO.LocationProvider_GUID = locationProviderId; deliveryPointDTO.UDPRN = fileUdprn; deliveryPointDTO.OperationalStatus_GUID = operationalStatusGUIDLive; deliveryPointDTO.NetworkNodeType_GUID = networkNodeTypeRMGServiceNode; // Update the location details for the delivery point await thirdPartyAddressLocationIntegrationService.UpdateDeliveryPointById(deliveryPointDTO); // Check if a notification exists for the UDPRN. if (await addressLocationDataService.CheckIfNotificationExists(fileUdprn, ThirdPartyAddressLocationConstants.TASKPAFACTION)) { // update the notification if it exists. await thirdPartyAddressLocationIntegrationService.UpdateNotificationByUDPRN(fileUdprn, ThirdPartyAddressLocationConstants.TASKPAFACTION, ThirdPartyAddressLocationConstants.NOTIFICATIONCLOSED); } } else { // Calculates the straight line distance between the existing delivery // point and the new delivery point. var straightLineDistance = GetDeliveryPointDistance(deliveryPointDTO, spatialLocationXY); // Check if the new point is within the tolerance limit if (straightLineDistance <= ThirdPartyAddressLocationConstants.TOLERANCEDISTANCEINMETERS) { deliveryPointDTO.LocationXY = spatialLocationXY; deliveryPointDTO.LocationProvider_GUID = locationProviderId; deliveryPointDTO.UDPRN = fileUdprn; deliveryPointDTO.OperationalStatus_GUID = operationalStatusGUIDLive; deliveryPointDTO.NetworkNodeType_GUID = networkNodeTypeRMGServiceNode; // Update the delivery point location directly in case it is within // the tolerance limits. await thirdPartyAddressLocationIntegrationService.UpdateDeliveryPointById(deliveryPointDTO); // Check if the notification exists for the given UDPRN. if (await addressLocationDataService.CheckIfNotificationExists(fileUdprn, ThirdPartyAddressLocationConstants.TASKPAFACTION)) { // update the notification if it exists. await thirdPartyAddressLocationIntegrationService.UpdateNotificationByUDPRN(fileUdprn, ThirdPartyAddressLocationConstants.TASKPAFACTION, ThirdPartyAddressLocationConstants.NOTIFICATIONCLOSED); } } else { // Get the Postcode Sector by UDPRN PostCodeSectorDTO postCodeSectorDTO = await thirdPartyAddressLocationIntegrationService.GetPostCodeSectorByUDPRN(fileUdprn); PostalAddressDataDTO postalAddressDTO = await addressLocationDataService.GetPostalAddressData(fileUdprn); NotificationDTO notificationDO = new NotificationDTO { ID = Guid.NewGuid(), NotificationType_GUID = notificationTypeId_GUID, NotificationDueDate = DateTime.UtcNow.AddHours(ThirdPartyAddressLocationConstants.NOTIFICATIONDUE), NotificationSource = ThirdPartyAddressLocationConstants.USRNOTIFICATIONSOURCE, Notification_Heading = ThirdPartyAddressLocationConstants.USRACTION, Notification_Message = AddressFields(postalAddressDTO), PostcodeDistrict = (postCodeSectorDTO == null || postCodeSectorDTO.District == null) ? string.Empty : postCodeSectorDTO.District, PostcodeSector = (postCodeSectorDTO == null || postCodeSectorDTO.Sector == null) ? string.Empty : postCodeSectorDTO.Sector, NotificationActionLink = string.Format(ThirdPartyAddressLocationConstants.USRNOTIFICATIONLINK, fileUdprn) }; // Insert the new notification. await thirdPartyAddressLocationIntegrationService.AddNewNotification(notificationDO); } } } } } // To update existing location else if (addressLocationChangeType.Equals(ThirdPartyAddressLocationConstants.UPDATE)) { // Match to Location on UDPRN (update Location) if (await addressLocationDataService.AddressLocationExists(fileUdprn)) { // Update the Address location. await UpdateAddressLocationByUDPRN(addressLocationUSRPOSTDTO); // Update the DP location. Story: RFMO-276 await UpdateDPLocation(addressLocationUSRPOSTDTO, notificationTypeId_GUID); } // No Match to Location on UDPRN - Log Error else { loggingHelper.Log(string.Format(ThirdPartyAddressLocationConstants.NoMatchToAddressOnUDPRN, fileUdprn), TraceEventType.Error); } } // To delete existing location else if (addressLocationChangeType.Equals(ThirdPartyAddressLocationConstants.DELETE)) { await DeleteUSRDetails(addressLocationUSRPOSTDTO); } } loggingHelper.LogMethodExit(methodName, LoggerTraceConstants.ThirdPartyAddressLocationAPIPriority, LoggerTraceConstants.ThirdPartyAddressLocationDataServiceMethodExitEventId); } }