Skip to content

Commit

Permalink
Merge pull request #207 from AAFC-BICoE/29608_Add_storage_unit_to_Seq…
Browse files Browse the repository at this point in the history
…Batch

29608 add storage unit to seq batch
  • Loading branch information
brandonandre authored Jan 4, 2023
2 parents 9342a30 + bcba8c3 commit b4b7e07
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 75 deletions.
11 changes: 11 additions & 0 deletions src/main/java/ca/gc/aafc/seqdb/api/dto/SeqBatchDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.UUID;

import ca.gc.aafc.seqdb.api.entities.StorageRestriction;
import org.javers.core.metamodel.annotation.Id;
import org.javers.core.metamodel.annotation.PropertyName;
import org.javers.core.metamodel.annotation.TypeName;
Expand Down Expand Up @@ -53,4 +54,14 @@ public class SeqBatchDto {
@JsonApiRelation
private ExternalRelationDto protocol;

@JsonApiExternalRelation(type = "storage-unit")
@JsonApiRelation
private ExternalRelationDto storageUnit;

@JsonApiExternalRelation(type = "storage-unit-type")
@JsonApiRelation
private ExternalRelationDto storageUnitType;

private StorageRestriction storageRestriction;

}
15 changes: 15 additions & 0 deletions src/main/java/ca/gc/aafc/seqdb/api/entities/SeqBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
Expand Down Expand Up @@ -83,4 +84,18 @@ public class SeqBatch implements DinaEntity {
private Region region;

private UUID protocol;

@Column(name = "storage_unit")
private UUID storageUnit;

/**
* storage-unit-type should only be used when no specific storageUnit is used.
*/
@Column(name = "storage_unit_type")
private UUID storageUnitType;

@Type(type = "jsonb")
@Column(name = "storage_restriction", columnDefinition = "jsonb")
@Valid
private StorageRestriction storageRestriction;
}
14 changes: 12 additions & 2 deletions src/main/java/ca/gc/aafc/seqdb/api/service/SeqBatchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.UUID;

import ca.gc.aafc.seqdb.api.validation.SeqBatchValidator;
import org.springframework.stereotype.Service;
import org.springframework.validation.SmartValidator;

Expand All @@ -12,16 +13,25 @@

@Service
public class SeqBatchService extends DefaultDinaService<SeqBatch> {


private final SeqBatchValidator validator;

public SeqBatchService(
@NonNull BaseDAO baseDAO,
@NonNull SmartValidator sv) {
@NonNull SmartValidator sv,
SeqBatchValidator validator) {
super(baseDAO, sv);
this.validator = validator;
}

@Override
protected void preCreate(SeqBatch entity) {
entity.setUuid(UUID.randomUUID());
}

@Override
public void validateBusinessRules(SeqBatch entity) {
applyBusinessRule(entity, validator);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ca.gc.aafc.seqdb.api.validation;

import ca.gc.aafc.seqdb.api.entities.SeqBatch;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class SeqBatchValidator implements Validator {

private static final String ERROR_MESSAGE_KEY = "validation.constraint.violation.storageUnitOrType";

private final MessageSource messageSource;

public SeqBatchValidator(MessageSource messageSource) {
this.messageSource = messageSource;
}

@Override
public boolean supports(Class<?> clazz) {
return SeqBatch.class.isAssignableFrom(clazz);
}

@Override
public void validate(Object target, Errors errors) {
if (!supports(target.getClass())) {
throw new IllegalArgumentException("SeqBatchValidator not supported for class " + target.getClass());
}
checkStorageOrTypeNotBoth(errors, (SeqBatch) target);
}

private void checkStorageOrTypeNotBoth(Errors errors, SeqBatch seqBatch) {
if (seqBatch.getStorageUnit() != null && seqBatch.getStorageUnitType() != null) {
String errorMessage = getMessage(ERROR_MESSAGE_KEY);
errors.rejectValue("storageUnit", ERROR_MESSAGE_KEY, errorMessage);
}
}

private String getMessage(String key) {
return messageSource.getMessage(key, null, LocaleContextHolder.getLocale());
}
}
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
<include file="db/changelog/migrations/31-Add_reaction_date_to_seqbatch.xml"/>
<include file="db/changelog/migrations/32-Add_sequencing_facility.xml"/>
<include file="db/changelog/migrations/33-Add_well_coordinates_SeqReaction.xml"/>
<include file="db/changelog/migrations/34-Add_storage_SeqBatch.xml"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://local.xsd/dbchangelog-4.4.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">

<changeSet id="34-Add_storage_SeqBatch" context="schema-change" author="cgendreau">
<addColumn tableName="seq_batch">
<column name="storage_unit" type="uuid"/>
<column name="storage_unit_type" type="uuid"/>
<column name="storage_restriction" type="jsonb"/>
</addColumn>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ void seqBatch_SpecValid() {
SeqBatchDto seqBatch = SeqBatchTestFixture.newSeqBatch();
// clear external relationships
seqBatch.setExperimenters(null);
seqBatch.setStorageUnit(null);
seqBatch.setStorageUnitType(null);

OpenAPI3Assertions.assertRemoteSchema(OpenAPIConstants.SEQDB_API_SPECS_URL, "SeqBatch",
sendPost(SeqBatchDto.TYPENAME, JsonAPITestHelper.toJsonAPIMap(SeqBatchDto.TYPENAME, JsonAPITestHelper.toAttributeMap(seqBatch),
JsonAPITestHelper.toRelationshipMap(
List.of(JsonAPIRelationship.of("region", RegionDto.TYPENAME, regionUuid),
JsonAPIRelationship.of("thermocyclerProfile", ThermocyclerProfileDto.TYPENAME, thermocyclerProfileUuid),
JsonAPIRelationship.of("protocol", "protocol", UUID.randomUUID().toString()))),
JsonAPIRelationship.of("protocol", "protocol", UUID.randomUUID().toString()),
JsonAPIRelationship.of("storageUnit", "storage-unit", UUID.randomUUID().toString()))),
null)
).extract().asString(),
ValidationRestrictionOptions.builder().allowableMissingFields(Set.of("experimenters")).build());
ValidationRestrictionOptions.builder().allowableMissingFields(Set.of("experimenters", "storageUnitType")).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.UUID;

import javax.inject.Inject;
import javax.validation.ValidationException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -85,6 +86,16 @@ public void createSeqBatch_onSuccess_SeqBatchCreated() {
assertEquals(TEST_PROTOCOL_UUID.toString(), created.getProtocol().getId());
}

@Test
public void createSeqBatch_proveBothStorage_Exception() {
SeqBatchDto newDto = SeqBatchTestFixture.newSeqBatch();
newDto.setRegion(regionTest);
newDto.setThermocyclerProfile(thermocyclerProfileTest);
newDto.setStorageUnit(ExternalRelationDto.builder().id(UUID.randomUUID().toString()).type("storage-unit").build());
newDto.setStorageUnitType(ExternalRelationDto.builder().id(UUID.randomUUID().toString()).type("storage-unit-type").build());
assertThrows(ValidationException.class, () -> seqBatchRepository.create(newDto));
}

@Test
public void updateSeqBatch_onSuccess_PcrBatchUpdated() {
SeqBatchDto newDto = SeqBatchTestFixture.newSeqBatch();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.gc.aafc.seqdb.api.testsupport.fixtures;

import ca.gc.aafc.seqdb.api.dto.SeqBatchDto;
import ca.gc.aafc.seqdb.api.testsupport.factories.StorageRestrictionFactory;
import ca.gc.aafc.seqdb.api.testsupport.factories.TestableEntityFactory;

import java.time.LocalDate;
Expand All @@ -16,6 +17,8 @@ public static SeqBatchDto newSeqBatch() {
seqBatchDto.setCreatedBy(CREATED_BY);
seqBatchDto.setReactionDate(LocalDate.of(2020,3,2));
seqBatchDto.setName(TestableEntityFactory.generateRandomName(10));

seqBatchDto.setStorageRestriction(StorageRestrictionFactory.newStorageRestriction().build());
return seqBatchDto;
}

Expand Down

0 comments on commit b4b7e07

Please sign in to comment.