Skip to content

Commit

Permalink
PSP-8732 Add ability to control map marker within file to additional …
Browse files Browse the repository at this point in the history
…file types (#4191)

* Update backend models and mapping of new Location property

* Rename common interface

* Remove casting protection on PropertyService methods

* Update backend services

* Test updates

* Update frontend models

* Lint fixes

* Fix faulty logic when loading research file properties
  • Loading branch information
asanchezr authored Jul 15, 2024
1 parent 8ba7dae commit c2a988a
Show file tree
Hide file tree
Showing 29 changed files with 302 additions and 154 deletions.
4 changes: 2 additions & 2 deletions source/backend/api/Services/AcquisitionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public PimsAcquisitionFile Add(PimsAcquisitionFile acquisitionFile, IEnumerable<

PopulateAcquisitionChecklist(acquisitionFile);

// Update file specific marker locations
// Update marker locations in the context of this file
foreach (var incomingAcquisitionProperty in acquisitionFile.PimsPropertyAcquisitionFiles)
{
_propertyService.PopulateNewFileProperty(incomingAcquisitionProperty);
Expand Down Expand Up @@ -304,7 +304,7 @@ public PimsAcquisitionFile UpdateProperties(PimsAcquisitionFile acquisitionFile,
throw new BusinessRuleViolationException("The file you are editing is not active or hold, so you cannot save changes. Refresh your browser to see file state.");
}

// Get the current properties in the research file
// Get the current properties in the acquisition file
var currentFileProperties = _acquisitionFilePropertyRepository.GetPropertiesByAcquisitionFileId(acquisitionFile.Internal_Id);

// Check if the property is new or if it is being updated
Expand Down
33 changes: 27 additions & 6 deletions source/backend/api/Services/DispositionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public PimsDispositionFile Add(PimsDispositionFile dispositionFile, IEnumerable<
MatchProperties(dispositionFile, userOverrides);
ValidatePropertyRegions(dispositionFile);

// Update marker locations in the context of this file
foreach (var incomingDispositionProperty in dispositionFile.PimsDispositionFileProperties)
{
_propertyService.PopulateNewFileProperty(incomingDispositionProperty);
}

var newDispositionFile = _dispositionFileRepository.Add(dispositionFile);
_dispositionFileRepository.CommitTransaction();

Expand Down Expand Up @@ -479,7 +485,7 @@ public List<DispositionFileExportModel> GetDispositionFileExport(DispositionFilt

public PimsDispositionFile UpdateProperties(PimsDispositionFile dispositionFile, IEnumerable<UserOverrideCode> userOverrides)
{
_logger.LogInformation("Updating disposition file properties...");
_logger.LogInformation("Updating disposition file properties with DispositionFile id: {id}", dispositionFile.Internal_Id);
_user.ThrowIfNotAllAuthorized(Permissions.DispositionEdit, Permissions.PropertyView, Permissions.PropertyAdd);
_user.ThrowInvalidAccessToDispositionFile(_userRepository, _dispositionFileRepository, dispositionFile.Internal_Id);

Expand All @@ -489,31 +495,46 @@ public PimsDispositionFile UpdateProperties(PimsDispositionFile dispositionFile,

ValidatePropertyRegions(dispositionFile);

// Get the current properties in the research file
var currentProperties = _dispositionFilePropertyRepository.GetPropertiesByDispositionFileId(dispositionFile.Internal_Id);
// Get the current properties in the disposition file
var currentFileProperties = _dispositionFilePropertyRepository.GetPropertiesByDispositionFileId(dispositionFile.Internal_Id);

// Check if the property is new or if it is being updated
foreach (var incomingDispositionProperty in dispositionFile.PimsDispositionFileProperties)
{
// If the property is not new, check if the name has been updated.
if (incomingDispositionProperty.Internal_Id != 0)
{
PimsDispositionFileProperty existingProperty = currentProperties.FirstOrDefault(x => x.Internal_Id == incomingDispositionProperty.Internal_Id);
var needsUpdate = false;
PimsDispositionFileProperty existingProperty = currentFileProperties.FirstOrDefault(x => x.Internal_Id == incomingDispositionProperty.Internal_Id);
if (existingProperty.PropertyName != incomingDispositionProperty.PropertyName)
{
existingProperty.PropertyName = incomingDispositionProperty.PropertyName;
needsUpdate = true;
}

var incomingGeom = incomingDispositionProperty.Location;
var existingGeom = existingProperty.Location;
if (existingGeom is null || (incomingGeom is not null && !existingGeom.EqualsExact(incomingGeom)))
{
_propertyService.UpdateFilePropertyLocation(incomingDispositionProperty, existingProperty);
needsUpdate = true;
}

if (needsUpdate)
{
_dispositionFilePropertyRepository.Update(existingProperty);
}
}
else
{
// New property needs to be added
_dispositionFilePropertyRepository.Add(incomingDispositionProperty);
var newFileProperty = _propertyService.PopulateNewFileProperty(incomingDispositionProperty);
_dispositionFilePropertyRepository.Add(newFileProperty);
}
}

// The ones not on the new set should be deleted
List<PimsDispositionFileProperty> differenceSet = currentProperties.Where(x => !dispositionFile.PimsDispositionFileProperties.Any(y => y.Internal_Id == x.Internal_Id)).ToList();
List<PimsDispositionFileProperty> differenceSet = currentFileProperties.Where(x => !dispositionFile.PimsDispositionFileProperties.Any(y => y.Internal_Id == x.Internal_Id)).ToList();
foreach (var deletedProperty in differenceSet)
{
_dispositionFilePropertyRepository.Delete(deletedProperty);
Expand Down
7 changes: 4 additions & 3 deletions source/backend/api/Services/IPropertyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public interface IPropertyService

void UpdateLocation(PimsProperty incomingProperty, ref PimsProperty propertyToUpdate, IEnumerable<UserOverrideCode> overrideCodes);

T PopulateNewFileProperty<T>(T fileProperty);
T PopulateNewFileProperty<T>(T fileProperty)
where T : IFilePropertyEntity;

void UpdateFilePropertyLocation<T>(T incomingFileProperty, T filePropertyToUpdate)
where T : IWithPropertyEntity;
where T : IFilePropertyEntity;

IList<PimsHistoricalFileNumber> GetHistoricalNumbersForPropertyId(long propertyId);

Expand All @@ -71,6 +72,6 @@ void UpdateFilePropertyLocation<T>(T incomingFileProperty, T filePropertyToUpdat
/// <param name="fileProperties">The file properties to re-project.</param>
/// <returns>The file properties with transformed spatial locations.</returns>
List<T> TransformAllPropertiesToLatLong<T>(List<T> fileProperties)
where T : IWithPropertyEntity;
where T : IFilePropertyEntity;
}
}
40 changes: 35 additions & 5 deletions source/backend/api/Services/LeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,17 @@ public PimsLease Add(PimsLease lease, IEnumerable<UserOverrideCode> userOverride
var pimsUser = _userRepository.GetByKeycloakUserId(_user.GetUserKey());
pimsUser.ThrowInvalidAccessToLeaseFile(lease.RegionCode);

var leasesWithProperties = AssociatePropertyLeases(lease, userOverrides);
var leaseWithProperties = AssociatePropertyLeases(lease, userOverrides);

lease.PimsLeaseChecklistItems = GetActiveChecklistItemsForLease();

return _leaseRepository.Add(leasesWithProperties);
// Update marker locations in the context of this file
foreach (var incomingLeaseProperty in leaseWithProperties.PimsPropertyLeases)
{
_propertyService.PopulateNewFileProperty(incomingLeaseProperty);
}

return _leaseRepository.Add(leaseWithProperties);
}

public IEnumerable<PimsPropertyLease> GetPropertiesByLeaseId(long leaseId)
Expand All @@ -209,8 +215,8 @@ public PimsLease Update(PimsLease lease, IEnumerable<UserOverrideCode> userOverr
pimsUser.ThrowInvalidAccessToLeaseFile(currentLease.RegionCode); // need to check that the user is able to access the current lease as well as has the region for the updated lease.
pimsUser.ThrowInvalidAccessToLeaseFile(lease.RegionCode);

var currentProperties = _propertyLeaseRepository.GetAllByLeaseId(lease.LeaseId);
var newPropertiesAdded = lease.PimsPropertyLeases.Where(x => !currentProperties.Any(y => y.Internal_Id == x.Internal_Id)).ToList();
var currentFileProperties = _propertyLeaseRepository.GetAllByLeaseId(lease.LeaseId);
var newPropertiesAdded = lease.PimsPropertyLeases.Where(x => !currentFileProperties.Any(y => y.Internal_Id == x.Internal_Id)).ToList();

if (newPropertiesAdded.Any(x => x.Property.IsRetired.HasValue && x.Property.IsRetired.Value))
{
Expand All @@ -228,13 +234,37 @@ public PimsLease Update(PimsLease lease, IEnumerable<UserOverrideCode> userOverr

_leaseRepository.Update(lease, false);
var leaseWithProperties = AssociatePropertyLeases(lease, userOverrides);

// Update marker locations in the context of this file
foreach (var incomingLeaseProperty in leaseWithProperties.PimsPropertyLeases)
{
// If the property is not new, check if the marker location has been updated.
if (incomingLeaseProperty.Internal_Id != 0)
{
var existingFileProperty = currentFileProperties.FirstOrDefault(x => x.Internal_Id == incomingLeaseProperty.Internal_Id);

var incomingGeom = incomingLeaseProperty?.Location;
var existingGeom = existingFileProperty?.Location;
if (existingGeom is null || (incomingGeom is not null && !existingGeom.EqualsExact(incomingGeom)))
{
_propertyService.UpdateFilePropertyLocation(incomingLeaseProperty, existingFileProperty);
incomingLeaseProperty.Location = existingFileProperty?.Location;
}
}
else
{
// New property needs to be added
_propertyService.PopulateNewFileProperty(incomingLeaseProperty);
}
}

_propertyLeaseRepository.UpdatePropertyLeases(lease.Internal_Id, leaseWithProperties.PimsPropertyLeases);

_leaseRepository.UpdateLeaseConsultations(lease.Internal_Id, lease.ConcurrencyControlNumber, lease.PimsLeaseConsultations);

_leaseRepository.UpdateLeaseRenewals(lease.Internal_Id, lease.ConcurrencyControlNumber, lease.PimsLeaseRenewals);

List<PimsPropertyLease> differenceSet = currentProperties.Where(x => !lease.PimsPropertyLeases.Any(y => y.Internal_Id == x.Internal_Id)).ToList();
List<PimsPropertyLease> differenceSet = currentFileProperties.Where(x => !lease.PimsPropertyLeases.Any(y => y.Internal_Id == x.Internal_Id)).ToList();
foreach (var deletedProperty in differenceSet)
{
var totalAssociationCount = _propertyRepository.GetAllAssociationsCountById(deletedProperty.PropertyId);
Expand Down
41 changes: 17 additions & 24 deletions source/backend/api/Services/PropertyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,37 +426,31 @@ public void UpdateLocation(PimsProperty incomingProperty, ref PimsProperty prope
}
}

/// <inheritdoc />
public T PopulateNewFileProperty<T>(T fileProperty)
where T : IFilePropertyEntity
{
// TODO: Remove this casting when LOCATION gets added to all remaining file-property types (research, disposition, lease)
if (fileProperty is PimsPropertyAcquisitionFile acquisitionFileProperty)
// convert spatial location from lat/long (4326) to BC Albers (3005) for database storage
var geom = fileProperty.Location;
if (geom is not null && geom.SRID != SpatialReference.BCALBERS)
{
// convert spatial location from lat/long (4326) to BC Albers (3005) for database storage
var geom = acquisitionFileProperty.Location;
if (geom is not null && geom.SRID != SpatialReference.BCALBERS)
{
var newCoords = _coordinateService.TransformCoordinates(geom.SRID, SpatialReference.BCALBERS, geom.Coordinate);
acquisitionFileProperty.Location = GeometryHelper.CreatePoint(newCoords, SpatialReference.BCALBERS);
}
var newCoords = _coordinateService.TransformCoordinates(geom.SRID, SpatialReference.BCALBERS, geom.Coordinate);
fileProperty.Location = GeometryHelper.CreatePoint(newCoords, SpatialReference.BCALBERS);
}

return fileProperty;
}

/// <inheritdoc />
public void UpdateFilePropertyLocation<T>(T incomingFileProperty, T filePropertyToUpdate)
where T : IWithPropertyEntity
where T : IFilePropertyEntity
{
// TODO: Remove this casting when LOCATION gets added to all remaining file-property types (research, disposition, lease)
if (incomingFileProperty is PimsPropertyAcquisitionFile incomingAcquisitionProperty
&& filePropertyToUpdate is PimsPropertyAcquisitionFile acquisitionPropertyToUpdate)
// convert spatial location from lat/long (4326) to BC Albers (3005) for database storage
var geom = incomingFileProperty.Location;
if (geom is not null && geom.SRID != SpatialReference.BCALBERS)
{
// convert spatial location from lat/long (4326) to BC Albers (3005) for database storage
var geom = incomingAcquisitionProperty.Location;
if (geom is not null && geom.SRID != SpatialReference.BCALBERS)
{
var newCoords = _coordinateService.TransformCoordinates(geom.SRID, SpatialReference.BCALBERS, geom.Coordinate);
acquisitionPropertyToUpdate.Location = GeometryHelper.CreatePoint(newCoords, SpatialReference.BCALBERS);
}
var newCoords = _coordinateService.TransformCoordinates(geom.SRID, SpatialReference.BCALBERS, geom.Coordinate);
filePropertyToUpdate.Location = GeometryHelper.CreatePoint(newCoords, SpatialReference.BCALBERS);
}
}

Expand Down Expand Up @@ -494,14 +488,13 @@ public IList<PimsHistoricalFileNumber> UpdateHistoricalFileNumbers(long property

/// <inheritdoc />
public List<T> TransformAllPropertiesToLatLong<T>(List<T> fileProperties)
where T : IWithPropertyEntity
where T : IFilePropertyEntity
{
foreach (var fileProperty in fileProperties)
{
// TODO: Remove this casting when LOCATION gets added to all remaining file-property types (research, disposition, lease)
if (fileProperty is PimsPropertyAcquisitionFile acquisitionFileProperty && acquisitionFileProperty.Location is not null)
if (fileProperty.Location is not null)
{
acquisitionFileProperty.Location = TransformCoordinates(acquisitionFileProperty.Location);
fileProperty.Location = TransformCoordinates(fileProperty.Location);
}

TransformPropertyToLatLong(fileProperty.Property);
Expand Down
34 changes: 28 additions & 6 deletions source/backend/api/Services/ResearchFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public PimsResearchFile Add(PimsResearchFile researchFile, IEnumerable<UserOverr

MatchProperties(researchFile, userOverrideCodes);

// Update marker locations in the context of this file
foreach (var incomingResearchProperty in researchFile.PimsPropertyResearchFiles)
{
_propertyService.PopulateNewFileProperty(incomingResearchProperty);
}

var newResearchFile = _researchFileRepository.Add(researchFile);
_researchFileRepository.CommitTransaction();
return newResearchFile;
Expand All @@ -96,37 +102,53 @@ public PimsResearchFile Update(PimsResearchFile researchFile)

public PimsResearchFile UpdateProperties(PimsResearchFile researchFile, IEnumerable<UserOverrideCode> userOverrideCodes)
{
_logger.LogInformation("Updating research file properties...");
_user.ThrowIfNotAuthorized(Permissions.ResearchFileEdit);
_logger.LogInformation("Updating research file properties with ResearchFile id: {id}", researchFile.Internal_Id);
_user.ThrowIfNotAllAuthorized(Permissions.ResearchFileEdit, Permissions.PropertyView, Permissions.PropertyAdd);

ValidateVersion(researchFile.Internal_Id, researchFile.ConcurrencyControlNumber);

MatchProperties(researchFile, userOverrideCodes);

// Get the current properties in the research file
var currentProperties = _researchFilePropertyRepository.GetAllByResearchFileId(researchFile.Internal_Id);
var currentFileProperties = _researchFilePropertyRepository.GetAllByResearchFileId(researchFile.Internal_Id);

// Check if the property is new or if it is being updated
foreach (var incomingResearchProperty in researchFile.PimsPropertyResearchFiles)
{
// If the property is not new, check if the name has been updated.
if (incomingResearchProperty.Internal_Id != 0)
{
PimsPropertyResearchFile existingProperty = currentProperties.FirstOrDefault(x => x.Internal_Id == incomingResearchProperty.Internal_Id);
var needsUpdate = false;
PimsPropertyResearchFile existingProperty = currentFileProperties.FirstOrDefault(x => x.Internal_Id == incomingResearchProperty.Internal_Id);
if (existingProperty.PropertyName != incomingResearchProperty.PropertyName)
{
existingProperty.PropertyName = incomingResearchProperty.PropertyName;
needsUpdate = true;
}

var incomingGeom = incomingResearchProperty.Location;
var existingGeom = existingProperty.Location;
if (existingGeom is null || (incomingGeom is not null && !existingGeom.EqualsExact(incomingGeom)))
{
_propertyService.UpdateFilePropertyLocation(incomingResearchProperty, existingProperty);
needsUpdate = true;
}

if (needsUpdate)
{
_researchFilePropertyRepository.Update(existingProperty);
}
}
else
{
// New property needs to be added
_researchFilePropertyRepository.Add(incomingResearchProperty);
var newFileProperty = _propertyService.PopulateNewFileProperty(incomingResearchProperty);
_researchFilePropertyRepository.Add(newFileProperty);
}
}

// The ones not on the new set should be deleted
List<PimsPropertyResearchFile> differenceSet = currentProperties.Where(x => !researchFile.PimsPropertyResearchFiles.Any(y => y.Internal_Id == x.Internal_Id)).ToList();
List<PimsPropertyResearchFile> differenceSet = currentFileProperties.Where(x => !researchFile.PimsPropertyResearchFiles.Any(y => y.Internal_Id == x.Internal_Id)).ToList();
foreach (var deletedProperty in differenceSet)
{
_researchFilePropertyRepository.Delete(deletedProperty);
Expand Down
Loading

0 comments on commit c2a988a

Please sign in to comment.