Skip to content

Commit

Permalink
fix: improve seedlot saving and audit information (#1405)
Browse files Browse the repository at this point in the history
Co-authored-by: Derek Roberts <[email protected]>
  • Loading branch information
Ricardo Campos and DerekRoberts authored Jul 19, 2024
1 parent 666655c commit 8d409a2
Show file tree
Hide file tree
Showing 35 changed files with 264 additions and 107 deletions.
1 change: 1 addition & 0 deletions .github/workflows/.deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ jobs:
-p ORACLE_USER='${{ vars.ORACLE_USER }}'
-p ORACLE_SYNC_USER='${{ vars.ORACLE_SYNC_USER }}'
-p ORACLE_SYNC_PASSWORD='${{ secrets.ORACLE_SYNC_PASSWORD }}'
-p ORACLE_CERT_SECRET='${{ secrets.ORACLE_CERT_SECRET }}'

database:
name: Deploy (database)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
packages: write
strategy:
matrix:
package: [database, backend, frontend, oracle-api, sync]
package: [database, common, backend, frontend, oracle-api, sync]
steps:
- uses: bcgov-nr/[email protected]
id: build
Expand Down
4 changes: 2 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ COPY src ./src
COPY .mvn/ ./.mvn

# Build
RUN ./mvnw -B package -Pnative -DskipTests -Dspring-boot.run.profiles=prod
RUN ./mvnw -B package -Pnative -DskipTests


### Deployer
Expand All @@ -25,4 +25,4 @@ EXPOSE ${PORT}
HEALTHCHECK CMD timeout 10s bash -c 'true > /dev/tcp/127.0.0.1/8090'

# Startup
ENTRYPOINT ["/app/nr-spar-backend"]
ENTRYPOINT ["/app/nr-spar-backend", "-Dspring.profiles.active=prod"]
4 changes: 2 additions & 2 deletions backend/openshift.deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ parameters:
- name: FORESTCLIENTAPI_ADDRESS
value: "https://nr-forest-client-api-prod.api.gov.bc.ca/api"
- name: CPU_REQUEST
value: 50m
value: 100m
- name: CPU_LIMIT
value: 75m
value: 300m
- name: MEMORY_REQUEST
value: 100Mi
- name: MEMORY_LIMIT
Expand Down
21 changes: 20 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ca.bc.gov</groupId>
Expand Down Expand Up @@ -39,6 +39,7 @@
<sonar.organization>bcgov-sonarcloud</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<oci.revision>${project.version}</oci.revision>
<hibernate.version>6.5.2.Final</hibernate.version>
</properties>

<profiles>
Expand Down Expand Up @@ -215,6 +216,24 @@

<build>
<plugins>
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
<execution>
<id>enhance</id>
<goals>
<goal>enhance</goal>
</goals>
<configuration>
<enableLazyInitialization>true</enableLazyInitialization>
<enableDirtyTracking>true</enableDirtyTracking>
<enableAssociationManagement>true</enableAssociationManagement>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ca.bc.gov.backendstartapi.config;

import ca.bc.gov.backendstartapi.dto.CaculatedParentTreeValsDto;
import ca.bc.gov.backendstartapi.dto.CalculatedParentTreeValsDto;
import ca.bc.gov.backendstartapi.dto.CodeDescriptionDto;
import ca.bc.gov.backendstartapi.dto.DescribedEnumDto;
import ca.bc.gov.backendstartapi.dto.FavouriteActivityCreateDto;
Expand Down Expand Up @@ -41,9 +41,9 @@
import ca.bc.gov.backendstartapi.dto.SeedlotReviewElevationLatLongDto;
import ca.bc.gov.backendstartapi.dto.SeedlotReviewGeoInformationDto;
import ca.bc.gov.backendstartapi.dto.SeedlotReviewSeedPlanZoneDto;
import ca.bc.gov.backendstartapi.dto.SeedlotSaveInMemoryDto;
import ca.bc.gov.backendstartapi.dto.SeedlotSourceDto;
import ca.bc.gov.backendstartapi.dto.SeedlotStatusResponseDto;
import ca.bc.gov.backendstartapi.dto.UserDto;
import ca.bc.gov.backendstartapi.dto.oracle.AreaOfUseDto;
import ca.bc.gov.backendstartapi.dto.oracle.AreaOfUseSpuGeoDto;
import ca.bc.gov.backendstartapi.dto.oracle.SpuDto;
Expand Down Expand Up @@ -79,7 +79,7 @@
AreaOfUseDto.class,
AreaOfUseSpuGeoDto.class,
SpzDto.class,
CaculatedParentTreeValsDto.class,
CalculatedParentTreeValsDto.class,
CodeDescriptionDto.class,
DescribedEnumDto.class,
FavouriteActivityCreateDto.class,
Expand Down Expand Up @@ -122,7 +122,6 @@
SeedlotReviewSeedPlanZoneDto.class,
SeedlotSourceDto.class,
SeedlotStatusResponseDto.class,
UserDto.class,
AreaOfUseDto.class,
AreaOfUseSpuGeoDto.class,
SpuDto.class,
Expand All @@ -135,7 +134,8 @@
LongitudeCodeEnum.class,
ConeAndPollenCountHeader.class,
CsvParsingHeader.class,
SmpMixHeader.class
SmpMixHeader.class,
SeedlotSaveInMemoryDto.class,
})
@ImportRuntimeHints(value = {HttpServletRequestRuntimeHint.class})
public class NativeImageConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
""")
@Getter
@Setter
public class CaculatedParentTreeValsDto {
public class CalculatedParentTreeValsDto {
private BigDecimal neValue;
private GeospatialRespondDto geospatialData;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public record PtCalculationResDto(
@Schema(description = "Contains a list of calculated traits.")
List<GeneticWorthTraitsDto> geneticTraits,
@Schema(description = "Various calculated value for the orchard parent trees.")
CaculatedParentTreeValsDto calculatedPtVals,
CalculatedParentTreeValsDto calculatedPtVals,
@Schema(description = "The calculated mean geospatial values for SMP mix.")
GeospatialRespondDto smpMixMeanGeoData) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ca.bc.gov.backendstartapi.dto;

import lombok.Getter;
import lombok.Setter;

/** This class contains values that are used in more than one place. */
@Getter
@Setter
public class SeedlotSaveInMemoryDto {

private Integer primarySpuId;
}
11 changes: 0 additions & 11 deletions backend/src/main/java/ca/bc/gov/backendstartapi/dto/UserDto.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ private boolean matchUserRoleWithResourceRoles(
List<String> rolesRequired, List<String> userRoles) {
for (String requiredRole : rolesRequired) {
if (userRoles.contains(requiredRole)) {
SparLog.info("Request allowed by user role: {}", requiredRole);
SparLog.debug("Request allowed by user role: {}", requiredRole);
return true;
}
}
SparLog.info("Request denied. No enough access levels!");
SparLog.debug("Request denied. No enough access levels!");
return false;
}

Expand All @@ -81,11 +81,11 @@ private boolean matchUserRoleWithResourceRoles(
private List<String> getUserRoles(HttpServletRequest request) {
if (request.getUserPrincipal() instanceof JwtAuthenticationToken jwtToken) {
Set<String> roles = JwtSecurityUtil.getUserRolesFromJwt(jwtToken.getToken());
SparLog.info("User roles: {}", roles);
SparLog.debug("User roles: {}", roles);
return new ArrayList<>(roles);
}

// Test fix!
// Fix for unit testing
SecurityContext context = SecurityContextHolder.getContext();
if (context.getAuthentication().getName().equals("SPARTest")) {
List<String> grantedList = new ArrayList<>();
Expand All @@ -96,7 +96,7 @@ private List<String> getUserRoles(HttpServletRequest request) {
grantedList.add(grant.substring(5));
}
}
SparLog.info("SPAR Test User roles: {}", grantedList);
SparLog.debug("SPAR Test User roles: {}", grantedList);
return grantedList;
}

Expand Down Expand Up @@ -142,7 +142,7 @@ private List<String> getResourceRolesRequired(String[] classNameWithMethod, Stri
}

List<String> roles = Arrays.asList(annotation.value());
SparLog.info("Access level required for {}: {}", uri, roles);
SparLog.debug("Access level required for {}: {}", uri, roles);
return roles;
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ca.bc.gov.backendstartapi.service;

import ca.bc.gov.backendstartapi.config.SparLog;
import ca.bc.gov.backendstartapi.dto.CaculatedParentTreeValsDto;
import ca.bc.gov.backendstartapi.dto.CalculatedParentTreeValsDto;
import ca.bc.gov.backendstartapi.dto.GeneticWorthTraitsDto;
import ca.bc.gov.backendstartapi.dto.GeospatialOracleResDto;
import ca.bc.gov.backendstartapi.dto.GeospatialRequestDto;
Expand Down Expand Up @@ -57,8 +57,8 @@ public PtCalculationResDto calculatePtVals(PtValsCalReqDto ptVals) {

BigDecimal neValue = geneticWorthService.calculateNe(ptVals.orchardPtVals());

CaculatedParentTreeValsDto calculatedVals = new CaculatedParentTreeValsDto();
calculatedVals.setNeValue(neValue); // <- review ne value being overwritten
CalculatedParentTreeValsDto calculatedVals = new CalculatedParentTreeValsDto();
calculatedVals.setNeValue(neValue);

GeospatialRespondDto smpMixGeoData = calcMeanGeospatial(ptVals.smpMixIdAndProps());
SparLog.info("SMP mix mean geospatial calculation complete.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ca.bc.gov.backendstartapi.exception.SeedlotConflictDataException;
import ca.bc.gov.backendstartapi.repository.SeedlotOrchardRepository;
import ca.bc.gov.backendstartapi.security.LoggedUserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -71,23 +72,27 @@ public void saveSeedlotFormStep4(
private void saveSeedlotOrchards(
Seedlot seedlot, String primaryOrchardId, String secondaryOrchardId) {

List<SeedlotOrchard> seedlotsToSaveList = new ArrayList<>();

SeedlotOrchard primary = new SeedlotOrchard(seedlot, true, primaryOrchardId);
primary.setAuditInformation(loggedUserService.createAuditCurrentUser());
seedlotOrchardRepository.save(primary);
seedlotsToSaveList.add(primary);
SparLog.info(
"Primary orchard id {} inserted on Seedlot_Orchard table for seedlot number {}",
"Primary orchard id {} to be saved for Seedlot number {}",
primaryOrchardId,
seedlot.getId());

if (secondaryOrchardId != null) {
SeedlotOrchard secondary = new SeedlotOrchard(seedlot, false, secondaryOrchardId);
secondary.setAuditInformation(loggedUserService.createAuditCurrentUser());
seedlotOrchardRepository.save(secondary);
seedlotsToSaveList.add(secondary);
SparLog.info(
"Secondary orchard id {} inserted on Seedlot_Orchard table for seedlot number {}",
"Secondary orchard id {} to be saved for Seedlot number {}",
secondaryOrchardId,
seedlot.getId());
}

seedlotOrchardRepository.saveAll(seedlotsToSaveList);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import ca.bc.gov.backendstartapi.dto.SeedlotFormOwnershipDto;
import ca.bc.gov.backendstartapi.dto.SeedlotFormParentTreeSmpDto;
import ca.bc.gov.backendstartapi.dto.SeedlotFormSubmissionDto;
import ca.bc.gov.backendstartapi.dto.SeedlotSaveInMemoryDto;
import ca.bc.gov.backendstartapi.dto.SeedlotStatusResponseDto;
import ca.bc.gov.backendstartapi.dto.oracle.AreaOfUseDto;
import ca.bc.gov.backendstartapi.dto.oracle.SpuDto;
Expand Down Expand Up @@ -736,6 +737,9 @@ public SeedlotStatusResponseDto updateSeedlotWithForm(
* 4. Add new ones
*/

// Object to hold data in memory, avoid querying same data
final SeedlotSaveInMemoryDto inMemoryDto = new SeedlotSaveInMemoryDto();

// Step 1 (Collection methods)
// Update the Seedlot instance and tables [seedlot_collection_method]
seedlotCollectionMethodService.saveSeedlotFormStep1(
Expand Down Expand Up @@ -775,7 +779,7 @@ public SeedlotStatusResponseDto updateSeedlotWithForm(

// Update the Seedlot instance only
// Fetch data from Oracle to get the primary Seed Plan Unit id
setBecValues(seedlot, form.seedlotFormOrchardDto().primaryOrchardId());
setBecValues(seedlot, form.seedlotFormOrchardDto().primaryOrchardId(), inMemoryDto);

if (isFromRegularForm) {
// Update the Seedlot instance only
Expand All @@ -794,7 +798,7 @@ public SeedlotStatusResponseDto updateSeedlotWithForm(
// Fetch data from Oracle to get the active Seed Plan Unit id
if (!hasAreaOfUseData(seedlot)) {
SparLog.info("Area of Use data has NOT been set previously, setting area of use data");
setAreaOfUse(seedlot, form.seedlotFormOrchardDto().primaryOrchardId());
setAreaOfUse(seedlot, form.seedlotFormOrchardDto().primaryOrchardId(), inMemoryDto);
}
} else {
updateApplicantAndSeedlot(seedlot, form.applicantAndSeedlotInfo());
Expand Down Expand Up @@ -830,7 +834,8 @@ public SeedlotStatusResponseDto updateSeedlotWithForm(
seedlotNumber, seedlot.getSeedlotStatus().getSeedlotStatusCode());
}

private void setBecValues(Seedlot seedlot, String primaryOrchardId) {
private void setBecValues(
Seedlot seedlot, String primaryOrchardId, SeedlotSaveInMemoryDto inMemoryDto) {
SparLog.info("Begin to set BEC values");

OrchardDto orchardDto =
Expand All @@ -844,6 +849,7 @@ private void setBecValues(Seedlot seedlot, String primaryOrchardId) {
.orElseThrow(NoSpuForOrchardException::new);

Integer primarySpuId = primarySeedPlanUnit.getSeedPlanningUnitId();
inMemoryDto.setPrimarySpuId(primarySpuId);

// Not sure why it's called Bgc in seedlot instead of Bec in orchard
seedlot.setBgcZoneCode(orchardDto.becZoneCode());
Expand Down Expand Up @@ -970,13 +976,19 @@ private boolean hasAreaOfUseData(Seedlot seedlot) {
* @param seedlot the seedlot object to set data to
* @param primaryOrchardId the primary orchard Id to find the spu for
*/
private void setAreaOfUse(Seedlot seedlot, String primaryOrchardId) {
private void setAreaOfUse(
Seedlot seedlot, String primaryOrchardId, SeedlotSaveInMemoryDto inMemoryDto) {
SparLog.info("Begin to set Area of Use values");
ActiveOrchardSpuEntity activeSpuEntity =
orchardService
.findSpuIdByOrchardWithActive(primaryOrchardId, true)
.orElseThrow(NoSpuForOrchardException::new);
Integer activeSpuId = activeSpuEntity.getSeedPlanningUnitId();

Integer activeSpuId = inMemoryDto.getPrimarySpuId();
if (Objects.isNull(activeSpuId)) {
ActiveOrchardSpuEntity activeSpuEntity =
orchardService
.findSpuIdByOrchardWithActive(primaryOrchardId, true)
.orElseThrow(NoSpuForOrchardException::new);
activeSpuId = activeSpuEntity.getSeedPlanningUnitId();
}

AreaOfUseDto areaOfUseDto =
oracleApiProvider
.getAreaOfUseData(activeSpuId)
Expand Down Expand Up @@ -1038,6 +1050,7 @@ private void setAreaOfUse(Seedlot seedlot, String primaryOrchardId) {
List<SeedlotSeedPlanZoneEntity> spzSaveList = new ArrayList<>();
GeneticClassEntity genAclass =
geneticClassRepository.findById("A").orElseThrow(GeneticClassNotFoundException::new);
AuditInformation currentUser = new AuditInformation(loggedUserService.getLoggedUserId());
areaOfUseDto.getSpzList().stream()
.forEach(
spzDto -> {
Expand All @@ -1048,8 +1061,7 @@ private void setAreaOfUse(Seedlot seedlot, String primaryOrchardId) {
genAclass,
spzDto.getIsPrimary(),
spzDto.getDescription());
sspzToSave.setAuditInformation(
new AuditInformation(loggedUserService.getLoggedUserId()));
sspzToSave.setAuditInformation(currentUser);
spzSaveList.add(sspzToSave);
});
seedlotSeedPlanZoneRepository.saveAll(spzSaveList);
Expand Down
Loading

0 comments on commit 8d409a2

Please sign in to comment.