-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #469 from sanger/biorisk
x1210 Support bio risk (block registration)
- Loading branch information
Showing
52 changed files
with
826 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package uk.ac.sanger.sccp.stan.model; | ||
|
||
import javax.persistence.*; | ||
import java.util.Objects; | ||
|
||
import static uk.ac.sanger.sccp.utils.BasicUtils.describe; | ||
|
||
/** | ||
* Biological risk assessment number. | ||
* @author dr6 | ||
*/ | ||
@Entity | ||
public class BioRisk implements HasIntId, HasEnabled { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Integer id; | ||
private String code; | ||
private boolean enabled = true; | ||
|
||
public BioRisk() {} // required no-arg constructor | ||
|
||
public BioRisk(Integer id, String code, boolean enabled) { | ||
this.id = id; | ||
this.code = code; | ||
this.enabled = enabled; | ||
} | ||
|
||
public BioRisk(Integer id, String code) { | ||
this(id, code, true); | ||
} | ||
|
||
public BioRisk(String code) { | ||
this(null, code, true); | ||
} | ||
|
||
/** Primary key */ | ||
@Override | ||
public Integer getId() { | ||
return this.id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
/** The alphanumeric code representing this risk assessment. */ | ||
public String getCode() { | ||
return this.code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return this.enabled; | ||
} | ||
|
||
@Override | ||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return describe(this) | ||
.add("id", id) | ||
.add("code", code) | ||
.add("enabled", enabled) | ||
.reprStringValues() | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || o.getClass() != this.getClass()) return false; | ||
BioRisk that = (BioRisk) o; | ||
return (this.enabled == that.enabled | ||
&& Objects.equals(this.id, that.id) | ||
&& Objects.equals(this.code, that.code) | ||
); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return (id != null ? id.hashCode() : code !=null ? code.hashCode() : 0); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/uk/ac/sanger/sccp/stan/repo/BioRiskRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package uk.ac.sanger.sccp.stan.repo; | ||
|
||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
import uk.ac.sanger.sccp.stan.model.BioRisk; | ||
import uk.ac.sanger.sccp.stan.model.Sample; | ||
|
||
import javax.persistence.EntityNotFoundException; | ||
import java.util.*; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
import static java.util.stream.Collectors.toSet; | ||
import static uk.ac.sanger.sccp.utils.BasicUtils.*; | ||
|
||
/** Repo for {@link BioRisk} */ | ||
public interface BioRiskRepo extends CrudRepository<BioRisk, Integer> { | ||
/** Finds the bio risk with the given code, if it exists. */ | ||
Optional<BioRisk> findByCode(String code); | ||
|
||
/** | ||
* Gets the bio risk with the given code. | ||
* @param code the code of the bio risk to get | ||
* @return the bio risk with the given code | ||
* @exception EntityNotFoundException if no such bio risk exists | ||
*/ | ||
default BioRisk getByCode(String code) throws EntityNotFoundException { | ||
return findByCode(code).orElseThrow(() -> new EntityNotFoundException("Unknown bio risk code: "+repr(code))); | ||
} | ||
|
||
List<BioRisk> findAllByCodeIn(Collection<String> codes); | ||
|
||
/** Finds all bio risks matching the given value for enabled. */ | ||
List<BioRisk> findAllByEnabled(boolean enabled); | ||
|
||
@Query(value = "select bio_risk_id from sample_bio_risk where sample_id=?", nativeQuery = true) | ||
Integer loadBioRiskIdForSampleId(int sampleId); | ||
|
||
@Query(value = "select sample_id, bio_risk_id from sample_bio_risk where sample_id in (?1)", nativeQuery = true) | ||
int[][] _loadBioRiskIdsForSampleIds(Collection<Integer> sampleIds); | ||
|
||
/** Loads the bio risk (if any) linked to the specified sample */ | ||
default Optional<BioRisk> loadBioRiskForSampleId(int sampleId) { | ||
Integer bioRiskId = loadBioRiskIdForSampleId(sampleId); | ||
return bioRiskId == null ? Optional.empty() : findById(bioRiskId); | ||
} | ||
|
||
/** | ||
* Loads the bio risk ids linked to the given sample ids | ||
* @param sampleIds sample ids | ||
* @return a map of sample id to bio risk id, omitting missing values | ||
*/ | ||
default Map<Integer, Integer> loadBioRiskIdsForSampleIds(Collection<Integer> sampleIds) { | ||
int[][] sambr = _loadBioRiskIdsForSampleIds(sampleIds); | ||
if (sambr == null || sambr.length==0) { | ||
return Map.of(); | ||
} | ||
return Arrays.stream(sambr) | ||
.collect(toMap(arr -> arr[0], arr -> arr[1])); | ||
} | ||
|
||
/** | ||
* Loads the bio risks linked to the given sample ids | ||
* @param sampleIds sample ids | ||
* @return a map of sample id to bio risk, omitting missing values | ||
*/ | ||
default Map<Integer, BioRisk> loadBioRisksForSampleIds(Collection<Integer> sampleIds) { | ||
int[][] sambr = _loadBioRiskIdsForSampleIds(sampleIds); | ||
if (sambr==null || sambr.length==0) { | ||
return Map.of(); | ||
} | ||
Set<Integer> bioRiskIds = Arrays.stream(sambr) | ||
.map(arr -> arr[1]) | ||
.collect(toSet()); | ||
Map<Integer, BioRisk> idBioRisks = stream(findAllById(bioRiskIds)) | ||
.collect(inMap(BioRisk::getId)); | ||
return Arrays.stream(sambr) | ||
.collect(toMap(arr -> arr[0], arr -> idBioRisks.get(arr[1]))); | ||
} | ||
|
||
/** | ||
* Records the given bio risk id against the given sample id and operation id | ||
* @param sampleId sample id | ||
* @param bioRiskId bio risk id | ||
* @param opId operation id | ||
*/ | ||
@Modifying | ||
@Query(value = "insert INTO sample_bio_risk (sample_id, bio_risk_id, operation_id) " + | ||
"values (?, ?, ?)", nativeQuery = true) | ||
void recordBioRisk(int sampleId, int bioRiskId, Integer opId); | ||
|
||
/** Links the given bio risk to the given sample and operation */ | ||
default void recordBioRisk(Sample sample, BioRisk bioRisk, int opId) { | ||
recordBioRisk(sample.getId(), bioRisk.getId(), opId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.