|
1 | 1 | package com.researchspace.service.inventory.impl;
|
2 | 2 |
|
3 | 3 | import com.axiope.search.InventorySearchConfig.InventorySearchDeletedOption;
|
4 |
| -import com.researchspace.api.v1.auth.ApiRuntimeException; |
5 | 4 | import com.researchspace.api.v1.model.ApiFieldToModelFieldFactory;
|
6 | 5 | import com.researchspace.api.v1.model.ApiInventoryRecordInfo;
|
7 | 6 | import com.researchspace.api.v1.model.ApiInventorySearchResult;
|
|
34 | 33 | import com.researchspace.model.inventory.Container;
|
35 | 34 | import com.researchspace.model.inventory.Container.ContainerType;
|
36 | 35 | import com.researchspace.model.inventory.InventoryRecord;
|
| 36 | +import com.researchspace.model.inventory.MovableInventoryRecord; |
37 | 37 | import com.researchspace.model.inventory.Sample;
|
38 | 38 | import com.researchspace.model.inventory.SampleSeriesHelper2;
|
39 | 39 | import com.researchspace.model.inventory.SubSample;
|
@@ -594,43 +594,31 @@ public ApiSample markSampleAsDeleted(Long sampleId, boolean forceDelete, User us
|
594 | 594 | try {
|
595 | 595 | dbSample = getIfExists(dbSample.getId());
|
596 | 596 | if (!dbSample.isDeleted()) {
|
597 |
| - /* first try deleting undeleted subsamples */ |
598 |
| - if (!forceDelete) { |
599 |
| - long ssInContainersCount = |
600 |
| - dbSample.getActiveSubSamples().stream() |
601 |
| - .filter( |
602 |
| - ss -> |
603 |
| - ss.getParentContainer() != null |
604 |
| - && !ContainerType.WORKBENCH.equals( |
605 |
| - ss.getParentContainer().getContainerType())) |
606 |
| - .count(); |
607 |
| - if (ssInContainersCount > 0) { |
608 |
| - throw new ApiRuntimeException( |
609 |
| - "sample.deletion.failure.subsamples.in.containers", |
610 |
| - dbSample.getGlobalIdentifier(), |
611 |
| - ssInContainersCount); |
612 |
| - } |
| 597 | + long ssInContainersCount = |
| 598 | + dbSample.getActiveSubSamples().stream() |
| 599 | + .filter(MovableInventoryRecord::isStoredInContainer) |
| 600 | + .count(); |
| 601 | + if (forceDelete || ssInContainersCount == 0) { |
| 602 | + dbSample |
| 603 | + .getActiveSubSamples() |
| 604 | + .forEach(ss -> subSampleMgr.markSubSampleAsDeleted(ss.getId(), user, true)); |
| 605 | + dbSample.refreshActiveSubSamples(); |
| 606 | + dbSample.recalculateTotalQuantity(); |
| 607 | + |
| 608 | + /* then delete the sample */ |
| 609 | + dbSample.setRecordDeleted(true); |
| 610 | + dbSample = sampleDao.save(dbSample); |
| 611 | + publisher.publishEvent(new InventoryDeleteEvent(dbSample, user)); |
613 | 612 | }
|
614 |
| - dbSample |
615 |
| - .getActiveSubSamples() |
616 |
| - .forEach(ss -> subSampleMgr.markSubSampleAsDeleted(ss.getId(), user, true)); |
617 |
| - dbSample.refreshActiveSubSamples(); |
618 |
| - dbSample.recalculateTotalQuantity(); |
619 |
| - |
620 |
| - /* then delete the sample */ |
621 |
| - dbSample.setRecordDeleted(true); |
622 |
| - dbSample = sampleDao.save(dbSample); |
623 |
| - publisher.publishEvent(new InventoryDeleteEvent(dbSample, user)); |
624 | 613 | }
|
625 | 614 | } finally {
|
626 | 615 | if (temporaryLock) {
|
627 | 616 | unlockItemAfterEdit(dbSample, user);
|
628 | 617 | }
|
629 | 618 | }
|
630 |
| - |
631 |
| - ApiSample deleted = getOutgoingApiSample(dbSample, user); |
632 |
| - updateOntologyOnRecordChanges(deleted, user); |
633 |
| - return deleted; |
| 619 | + ApiSample apiSampleResult = getOutgoingApiSample(dbSample, user); |
| 620 | + updateOntologyOnRecordChanges(apiSampleResult, user); |
| 621 | + return apiSampleResult; |
634 | 622 | }
|
635 | 623 |
|
636 | 624 | @Override
|
|
0 commit comments