DefaultSampleService implements SampleService, PaginatedDataSource<Sample> { @Override public long create(Sample sample) throws IOException { loadChildEntities(sample); boxService.throwIfBoxPositionIsFilled(sample); User changeUser = authorizationManager.getCurrentUser(); sample.setChangeDetails(changeUser); if (isDetailedSample(sample)) { DetailedSample detailed = (DetailedSample) sample; if (!isIdentitySample(sample)) { SampleIdentity identityCopy = getIdentity(detailed); if (detailed.getParent() == null) { throw new IllegalArgumentException(ERR_MISSING_PARENT_ID); } if (detailed.getParent().isSaved()) { detailed.setParent((DetailedSample) get(detailed.getParent().getId())); } else { try { setIdentity(detailed); if (!isIdentitySample(detailed.getParent())) { detailed.setParent(findOrCreateParent(detailed)); } } catch (MisoNamingException e) { throw new ValidationException(e.getMessage()); } } addExternalNames(detailed, identityCopy); validateHierarchy(detailed); } else { if (isUniqueExternalNameWithinProjectRequired() && isExternalNameDuplicatedInProject(sample)) { throw makeDuplicateExternalNameError(((SampleIdentity) sample).getExternalName()); } } } sample.setName(generateTemporaryName()); if (isStringEmptyOrNull(sample.getAlias()) && getNamingScheme(sample).hasSampleAliasGenerator()) { sample.setAlias(generateTemporaryName()); } if (sample.getConcentration() == null) { sample.setConcentrationUnits(null); } if (sample.getVolume() == null) { sample.setVolumeUnits(null); } else { sample.setInitialVolume(sample.getVolume()); } if (isSampleSlide(sample)) { ((SampleSlide) sample).setInitialSlides(((SampleSlide) sample).getSlides()); } LimsUtils.updateParentVolume(sample, null, changeUser); updateParentSlides(sample, null, changeUser); validateChange(sample, null); long savedId = save(sample, true).getId(); if (sample.getParent() != null) { sampleStore.update(sample.getParent()); } boxService.updateBoxableLocation(sample); if (sample.getCreationReceiptInfo() != null) { TransferSample transferSample = sample.getCreationReceiptInfo(); Transfer transfer = transferSample.getTransfer(); Transfer existingTransfer = transferService.listByProperties(transfer.getSenderLab(), transfer.getRecipientGroup(), sample.getProject(), transfer.getTransferTime()).stream() .max(Comparator.comparing(Transfer::getCreationTime)).orElse(null); if (existingTransfer != null) { existingTransfer.getSampleTransfers().add(transferSample); transferSample.setTransfer(existingTransfer); transferService.addTransferSample(transferSample); } else { transferService.create(transfer); } } return savedId; }  void setSampleStore(SampleStore sampleStore); void setAuthorizationManager(AuthorizationManager authorizationManager); void setSampleClassService(SampleClassService sampleClassService); void setSampleValidRelationshipService(SampleValidRelationshipService sampleValidRelationshipService); void setProjectStore(ProjectStore projectStore); void setTissueOriginDao(TissueOriginDao tissueOriginDao); void setTissueTypeDao(TissueTypeDao tissueTypeDao); void setDetailedQcStatusService(DetailedQcStatusService detailedQcStatusService); void setSubprojectService(SubprojectService subprojectService); void setSamplePurposeDao(SamplePurposeDao samplePurposeDao); void setTissueMaterialDao(TissueMaterialDao tissueMaterialDao); void setLabService(LabService labService); void setBoxService(BoxService boxService); void setWorksetService(WorksetService worksetService); void setFileAttachmentService(FileAttachmentService fileAttachmentService); void setNamingSchemeHolder(NamingSchemeHolder namingSchemeHolder); @CoverageIgnore Boolean getAutoGenerateIdBarcodes(); @CoverageIgnore void setAutoGenerateIdBarcodes(Boolean autoGenerateIdBarcodes); @CoverageIgnore Boolean isUniqueExternalNameWithinProjectRequired(); @CoverageIgnore void setUniqueExternalNameWithinProjectRequired(Boolean uniqueExternalNameWithinProjectRequired); @Override Sample get(long sampleId); @Override long create(Sample sample); @Override void confirmExternalNameUniqueForProjectIfRequired(String newExternalName, Sample sample); boolean isValidRelationship(Sample parent, Sample child); @Override long update(Sample sample); @Override List<Sample> list(); @Override Long countAll(); @Override Collection<Sample> listByProjectId(long projectId); @Override List<Sample> listByIdList(List<Long> idList); @Override @Transactional(propagation = Propagation.REQUIRED) Collection<SampleIdentity> getIdentitiesByExternalNameOrAliasAndProject(String externalName, Long projectId, boolean exactMatch); @Override List<Sample> getByAlias(String alias); @Override Sample getByBarcode(String barcode); @Override void addNote(Sample sample, Note note); @Override void deleteNote(Sample sample, Long noteId); @Override AuthorizationManager getAuthorizationManager(); @Override DeletionStore getDeletionStore(); void setDeletionStore(DeletionStore deletionStore); @Override void authorizeDeletion(Sample object); @Override ValidationResult validateDeletion(Sample object); @Override void beforeDelete(Sample object); @Override void afterDelete(Sample object); @Override Sample save(Sample sample); @Override long count(Consumer<String> errorHandler, PaginationFilter... filter); @Override List<Sample> list(Consumer<String> errorHandler, int offset, int limit, boolean sortDir, String sortCol,
      PaginationFilter... filter); @Override EntityReference getNextInProject(Sample sample); @Override EntityReference getPreviousInProject(Sample sample); @Override TransactionTemplate getTransactionTemplate();  }
@Test public void testCreateDetailedSampleMissingIdentity() throws Exception { SampleTissue sample = new SampleTissueImpl(); sample.setSampleClass(new SampleClassImpl()); sample.getSampleClass().setSampleCategory(SampleTissue.CATEGORY_NAME); Mockito.when(sampleClassService.listByCategory(Mockito.eq(SampleIdentity.CATEGORY_NAME))) .thenReturn(Lists.newArrayList(sample.getSampleClass())); exception.expect(IllegalArgumentException.class); sut.create(sample); }
@Test public void testCreateDetailedSampleExistingParentById() throws Exception { SampleIdentity parent = makeParentIdentityWithLookup(); SampleTissue child = makeUnsavedChildTissue(); child.setParent(new SampleIdentityImpl()); child.getParent().setId(parent.getId()); Long newId = 89L; DetailedSample postSave = makeUnsavedChildTissue(); postSave.setId(newId); postSave.setParent(parent); Mockito.when(sampleStore.addSample(Mockito.any(Sample.class))).thenReturn(newId); Mockito.when(sampleStore.getSample(newId)).thenReturn(postSave); mockValidRelationship(parent.getSampleClass(), child.getSampleClass()); sut.create(child); ArgumentCaptor<Sample> createdCapture = ArgumentCaptor.forClass(Sample.class); Mockito.verify(sampleStore).addSample(createdCapture.capture()); Sample created = createdCapture.getValue(); assertTrue("Expected a TissueSample", LimsUtils.isTissueSample(created)); assertNotNull("Child sample should have parent", ((SampleTissue) created).getParent()); assertEquals("Unexpected parent ID", parent.getId(), ((SampleTissue) created).getParent().getId()); }
@Test public void testCreateDetailedSampleExistingParentByExternalName() throws Exception { SampleIdentity parent = makeParentIdentityWithLookup(); SampleTissue child = makeUnsavedChildTissue(); SampleIdentity shellParent = new SampleIdentityImpl(); shellParent.setExternalName(parent.getExternalName()); Long shellParentId = 88L; shellParent.setId(shellParentId); child.setParent(shellParent); Long newId = 89L; SampleTissue postSave = makeUnsavedChildTissue(); postSave.setId(newId); postSave.setParent(parent); Mockito.when(sampleStore.addSample(Mockito.any(Sample.class))).thenReturn(newId); Mockito.when(sampleStore.getSample(newId)).thenReturn(postSave); Mockito.when(sampleStore.getSample(shellParentId)).thenReturn(parent); mockValidRelationship(parent.getSampleClass(), child.getSampleClass()); sut.create(child); ArgumentCaptor<Sample> createdCapture = ArgumentCaptor.forClass(Sample.class); Mockito.verify(sampleStore).addSample(createdCapture.capture()); Sample created = createdCapture.getValue(); assertTrue("Expected a TissueSample", LimsUtils.isTissueSample(created)); assertNotNull("Child sample should have parent", ((SampleTissue) created).getParent()); assertEquals("Unexpected parent ID", parent.getId(), ((SampleTissue) created).getParent().getId()); }