Skip to content

Commit

Permalink
Allow aliquot receipt (GLT-1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
apmasell committed Oct 13, 2016
1 parent e165541 commit 5ea6a96
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package uk.ac.bbsrc.tgac.miso.core.data.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

Expand Down Expand Up @@ -43,12 +45,15 @@
import uk.ac.bbsrc.tgac.miso.core.exception.ReportingException;
import uk.ac.bbsrc.tgac.miso.core.security.SecurableByProfile;

public class DetailedSampleBuilder implements DetailedSample, SampleAliquot, SampleStock, SampleTissue, SampleTissueProcessing,
SampleCVSlide, SampleLCMTube, Identity {
public class DetailedSampleBuilder
implements DetailedSample, SampleAliquot, SampleStock, SampleTissue, SampleTissueProcessing, SampleCVSlide, SampleLCMTube, Identity {

@SuppressWarnings("unused")
private static final long serialVersionUID = 1L;

private static final List<String> CATEGORY_ORDER = Arrays.asList(Identity.CATEGORY_NAME, SampleTissue.CATEGORY_NAME,
SampleStock.CATEGORY_NAME, SampleAliquot.CATEGORY_NAME);

// Sample attributes
private long sampleId = AbstractSample.UNSAVED_ID;
private Project project;
Expand Down Expand Up @@ -109,6 +114,7 @@ public class DetailedSampleBuilder implements DetailedSample, SampleAliquot, Sam
private SamplePurpose samplePurpose;

// SampleStock attributes
private SampleClass stockClass;
private StrStatus strStatus = StrStatus.NOT_SUBMITTED;
private Double concentration;
private Boolean dnaseTreated;
Expand Down Expand Up @@ -156,7 +162,7 @@ public void setProject(Project project) {
public void addQc(SampleQC sampleQc) throws MalformedSampleQcException {
this.sampleQCs.add(sampleQc);
try {
sampleQc.setSample(this);
sampleQc.setSample(this);
} catch (MalformedSampleException e) {
// This is never actually thrown
throw new RuntimeException(e);
Expand Down Expand Up @@ -629,6 +635,14 @@ public void setTissueMaterial(TissueMaterial tissueMaterial) {
this.tissueMaterial = tissueMaterial;
}

public SampleClass getStockClass() {
return stockClass;
}

public void setStockClass(SampleClass stockClass) {
this.stockClass = stockClass;
}

@Override
public StrStatus getStrStatus() {
return strStatus;
Expand Down Expand Up @@ -853,10 +867,7 @@ public DetailedSample build() {
DetailedSample sample = null;
switch (sampleClass.getSampleCategory()) {
case Identity.CATEGORY_NAME:
Identity identity = new IdentityImpl();
identity.setInternalName(internalName);
identity.setExternalName(externalName);
identity.setDonorSex(donorSex);
Identity identity = buildIdentity();
sample = identity;
break;
case SampleTissue.CATEGORY_NAME:
Expand All @@ -879,11 +890,7 @@ public DetailedSample build() {
}
break;
case SampleStock.CATEGORY_NAME:
SampleStock stock = new SampleStockImpl();
stock.setStrStatus(strStatus);
stock.setConcentration(concentration);
stock.setDNAseTreated(dnaseTreated);
stock.setQCs(sampleQCs);
SampleStock stock = buildStock();
sample = stock;
break;
case SampleAliquot.CATEGORY_NAME:
Expand All @@ -897,23 +904,28 @@ public DetailedSample build() {

if (parent != null) {
sample.setParent(parent);
} else if (!Identity.CATEGORY_NAME.equals(sampleClass.getSampleCategory())) {
Identity identity = new IdentityImpl();
if (externalName == null) {
throw new NullPointerException("Missing externalName");
} else {
DetailedSample parent = null;
int categoryIndex = CATEGORY_ORDER.indexOf(sampleClass.getSampleCategory());
if (categoryIndex < 0) {
throw new IllegalArgumentException("Sample has no parent and cannot infer order from sample category.");
}
identity.setExternalName(externalName);
identity.setInternalName(internalName);
identity.setDonorSex(donorSex);
if (SampleTissue.CATEGORY_NAME.equals(sampleClass.getSampleCategory())) {
sample.setParent(identity);
} else {
if (tissueClass == null) throw new NullPointerException("Missing tissue class");
if (categoryIndex > 0 && externalName != null) {
parent = buildIdentity();
}
if (categoryIndex > 1 && tissueClass != null) {
SampleTissue tissue = buildTissue();
tissue.setParent(parent);
tissue.setSampleClass(tissueClass);
tissue.setParent(identity);
sample.setParent(tissue);
parent = tissue;
}
if (categoryIndex > 2 && stockClass != null) {
SampleStock stock = buildStock();
stock.setParent(parent);
stock.setSampleClass(stockClass);
parent = stock;
}
sample.setParent(parent);
}

sample.setId(sampleId);
Expand Down Expand Up @@ -953,6 +965,26 @@ public DetailedSample build() {
return sample;
}

private Identity buildIdentity() {
if (externalName == null) {
throw new NullPointerException("Missing externalName");
}
Identity identity = new IdentityImpl();
identity.setInternalName(internalName);
identity.setExternalName(externalName);
identity.setDonorSex(donorSex);
return identity;
}

private SampleStock buildStock() {
SampleStock stock = new SampleStockImpl();
stock.setStrStatus(strStatus);
stock.setConcentration(concentration);
stock.setDNAseTreated(dnaseTreated);
stock.setQCs(sampleQCs);
return stock;
}

private SampleTissue buildTissue() {
SampleTissue tissue = new SampleTissueImpl();
tissue.setTimesReceived(timesReceived);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,4 +890,13 @@ public static boolean isAliquotSample(Sample sample) {
return sample instanceof SampleAliquot;
}

public static boolean hasStockParent(Long id, Iterable<SampleValidRelationship> relationships) throws IOException {
for (SampleValidRelationship relationship : relationships) {
if (!relationship.getArchived() && relationship.getChild().getId() == id
&& relationship.getParent().getSampleCategory().equals(SampleStock.CATEGORY_NAME)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DetailedSampleDto extends SampleDto {
private Long parentId;
private String parentUrl;
private String parentAlias;
private Long parentSampleClassId;
private Long parentTissueSampleClassId;
private Long sampleClassId;
private String sampleClassUrl;
private Long detailedQcStatusId;
Expand Down Expand Up @@ -50,12 +50,12 @@ public void setParentAlias(String parentAlias) {
this.parentAlias = parentAlias;
}

public Long getParentSampleClassId() {
return parentSampleClassId;
public Long getParentTissueSampleClassId() {
return parentTissueSampleClassId;
}

public void setParentSampleClassId(Long parentSampleClassId) {
this.parentSampleClassId = parentSampleClassId;
public void setParentTissueSampleClassId(Long parentSampleClassId) {
this.parentTissueSampleClassId = parentSampleClassId;
}

public Long getDetailedQcStatusId() {
Expand Down
56 changes: 28 additions & 28 deletions miso-dto/src/main/java/uk/ac/bbsrc/tgac/miso/dto/Dtos.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package uk.ac.bbsrc.tgac.miso.dto;

import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.getDateAsString;
import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isAliquotSample;
import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isDetailedSample;
import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isIdentitySample;
import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isStockSample;
import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isStringEmptyOrNull;
import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isTissueProcessingSample;
import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isTissueSample;
import static uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.*;

import java.text.DateFormat;
import java.text.ParseException;
Expand Down Expand Up @@ -276,7 +269,7 @@ private static DetailedSampleDto asDetailedSampleDto(DetailedSample from) {
if (from.getParent() != null) {
dto.setParentId(from.getParent().getId());
dto.setParentAlias(from.getParent().getAlias());
dto.setParentSampleClassId(from.getParent().getSampleClass().getId());
dto.setParentTissueSampleClassId(from.getParent().getSampleClass().getId());
}
if (from.getGroupId() != null) {
dto.setGroupId(from.getGroupId());
Expand Down Expand Up @@ -348,14 +341,13 @@ private static DetailedSample toDetailedSample(DetailedSampleDto from) {
}

/**
* Extracts parent details from the DTO, according to three possible cases:
* Extracts parent details from the DTO, according to these possible cases:
*
* <ol>
* <li>parent ID is provided. This implies that the parent exists, so no other parent information will be required</li>
* <li>identity information and parentSampleClassId are provided. This implies that a tissue parent should be created, and that the
* identity may or may not yet exist</li>
* <li>identity information is provided, but no parentSampleClassId. This implies that the direct parent of this sample will be an
* identity, which may or may not yet exist</li>
* <li>identity information and parentTissueSampleClassId are provided. This implies that a tissue parent should be created, and that the
* identity may or may not yet exist. If the sampleClassId is an aliquot, a stockClassId must be provided.</li>
* <li>identity information is provided, but no parentTissueSampleClassId. You must be creating a tissue in this case.</li>
* </ol>
*
* @param childDto
Expand All @@ -365,21 +357,29 @@ private static DetailedSample toDetailedSample(DetailedSampleDto from) {
private static DetailedSample getParent(DetailedSampleDto childDto) {
DetailedSample parent = null;
if (childDto.getParentId() != null) {
// Case 1
parent = new DetailedSampleImpl();
parent.setId(childDto.getParentId());
} else if (childDto instanceof SampleIdentityDto && childDto.getClass() != SampleIdentityDto.class) {
// Case 2 or 3 require Identity details
Identity identity = toIdentitySample((SampleIdentityDto) childDto);
if (childDto instanceof SampleTissueDto && childDto.getParentSampleClassId() != null) {
// Case 2
parent = toTissueSample((SampleTissueDto) childDto);
parent.setSampleClass(new SampleClassImpl());
parent.getSampleClass().setId(childDto.getParentSampleClassId());
parent.setParent(identity);
} else {
// Case 3
parent = identity;
} else {
if (childDto instanceof SampleIdentityDto && childDto.getClass() != SampleIdentityDto.class) {
parent = toIdentitySample((SampleIdentityDto) childDto);
}

if (childDto instanceof SampleTissueDto && childDto.getClass() != SampleTissueDto.class) {
if (childDto.getParentTissueSampleClassId() == null) {
throw new IllegalArgumentException("No tissue class specified.");
}
DetailedSample tissue = toTissueSample((SampleTissueDto) childDto);
tissue.setSampleClass(new SampleClassImpl());
tissue.getSampleClass().setId(childDto.getParentTissueSampleClassId());
tissue.setParent(parent);
parent = tissue;
}
if (childDto instanceof SampleStockDto && childDto.getClass() != SampleStockDto.class) {
DetailedSample stock = toStockSample((SampleStockDto) childDto);
stock.setSampleClass(new SampleClassImpl());
stock.getSampleClass().setId(((SampleAliquotDto) childDto).getStockClassId());
stock.setParent(parent);
parent = stock;
}
}
return parent;
Expand Down Expand Up @@ -1157,7 +1157,7 @@ public static PoolDto asDto(Pool<? extends Poolable<?, ?>> from) {
if (from.getLastModified() != null) {
dto.setLastModified(getDateAsString(from.getLastModified()));
}
Set<DilutionDto> pooledElements = new HashSet<DilutionDto>();
Set<DilutionDto> pooledElements = new HashSet<>();
for (Dilution ld : from.getDilutions()) {
if (ld != null) {
pooledElements.add(asDto(ld));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import java.net.URI;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.web.util.UriComponentsBuilder;

public class SampleAliquotDto extends SampleStockDto {

private Long samplePurposeId;
private String samplePurposeUrl;
@JsonIgnore
private Long stockClassId;

public Long getSamplePurposeId() {
return samplePurposeId;
Expand All @@ -25,6 +28,14 @@ public void setSamplePurposeUrl(String samplePurposeUrl) {
this.samplePurposeUrl = samplePurposeUrl;
}

public Long getStockClassId() {
return stockClassId;
}

public void setStockClassId(Long stockClassId) {
this.stockClassId = stockClassId;
}

@Override
public void writeUrls(URI baseUri) {
super.writeUrls(baseUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SampleClassDto {
private String updatedByUrl;
private String lastUpdated;
private Boolean dnaseTreatable;
private boolean canCreateNew;

public Long getId() {
return id;
Expand Down Expand Up @@ -114,11 +115,18 @@ public void setDNAseTreatable(Boolean dnaseTreatable) {
this.dnaseTreatable = dnaseTreatable;
}

public boolean getCanCreateNew() {
return canCreateNew;
}

public void setCanCreateNew(boolean canCreateNew) {
this.canCreateNew = canCreateNew;
}

@Override
public String toString() {
return "SampleClassDto [id=" + id + ", url=" + url + ", alias=" + alias + ", sampleCategory=" + sampleCategory + ", suffix=" + suffix
+ ", createdById=" + createdById + ", createdByUrl=" + createdByUrl + ", creationDate=" + creationDate + ", updatedById="
+ updatedById + ", updatedByUrl=" + updatedByUrl + ", lastUpdated=" + lastUpdated + ", dnaseTreatable=" + dnaseTreatable + "]";
}

}
Loading

0 comments on commit 5ea6a96

Please sign in to comment.