Search in sources :

Example 1 with BulkQcSaveOperation

use of uk.ac.bbsrc.tgac.miso.core.service.BulkQcSaveOperation in project miso-lims by miso-lims.

the class DefaultQualityControlService method startBulkOperation.

private BulkQcSaveOperation startBulkOperation(List<QC> items, ThrowingFunction<QC, QC, IOException> action) throws IOException {
    QcTarget qcTarget = qcTypeService.get(items.get(0).getType().getId()).getQcTarget();
    BulkQcSaveOperation operation = new BulkQcSaveOperation(qcTarget, items, authorizationManager.getCurrentUser());
    // Authentication is tied to the thread, so use this same auth in the new thread
    Authentication auth = SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication();
    Thread thread = new Thread(() -> {
        SecurityContextHolder.getContextHolderStrategy().getContext().setAuthentication(auth);
        try {
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    while (operation.hasMore()) {
                        try {
                            QC item = operation.getNextItem();
                            QC saved = action.apply(item);
                            operation.addSuccess(saved.getId());
                        } catch (ValidationException e) {
                            operation.addFailure(e);
                            status.setRollbackOnly();
                        } catch (Exception e) {
                            operation.setFailed(e);
                            status.setRollbackOnly();
                        }
                    }
                }
            });
        } catch (Exception e) {
            // Exception during transaction commit
            operation.setFailed(e);
        }
        if (operation.isFailed()) {
            Exception exception = operation.getException();
            if (!(exception instanceof BulkValidationException)) {
                LoggerFactory.getLogger(DefaultQualityControlService.class).error("Bulk save failed", exception);
            }
        }
        operation.setComplete();
    });
    thread.start();
    return operation;
}
Also used : ContainerQC(uk.ac.bbsrc.tgac.miso.core.data.qc.ContainerQC) LibraryQC(uk.ac.bbsrc.tgac.miso.core.data.qc.LibraryQC) QC(uk.ac.bbsrc.tgac.miso.core.data.qc.QC) SampleQC(uk.ac.bbsrc.tgac.miso.core.data.qc.SampleQC) PoolQC(uk.ac.bbsrc.tgac.miso.core.data.qc.PoolQC) ValidationException(uk.ac.bbsrc.tgac.miso.core.service.exception.ValidationException) BulkValidationException(uk.ac.bbsrc.tgac.miso.core.service.exception.BulkValidationException) Authentication(org.springframework.security.core.Authentication) QcTarget(uk.ac.bbsrc.tgac.miso.core.data.qc.QcTarget) TransactionStatus(org.springframework.transaction.TransactionStatus) BulkValidationException(uk.ac.bbsrc.tgac.miso.core.service.exception.BulkValidationException) BulkQcSaveOperation(uk.ac.bbsrc.tgac.miso.core.service.BulkQcSaveOperation) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) ValidationException(uk.ac.bbsrc.tgac.miso.core.service.exception.ValidationException) BulkValidationException(uk.ac.bbsrc.tgac.miso.core.service.exception.BulkValidationException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 Authentication (org.springframework.security.core.Authentication)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1 ContainerQC (uk.ac.bbsrc.tgac.miso.core.data.qc.ContainerQC)1 LibraryQC (uk.ac.bbsrc.tgac.miso.core.data.qc.LibraryQC)1 PoolQC (uk.ac.bbsrc.tgac.miso.core.data.qc.PoolQC)1 QC (uk.ac.bbsrc.tgac.miso.core.data.qc.QC)1 QcTarget (uk.ac.bbsrc.tgac.miso.core.data.qc.QcTarget)1 SampleQC (uk.ac.bbsrc.tgac.miso.core.data.qc.SampleQC)1 BulkQcSaveOperation (uk.ac.bbsrc.tgac.miso.core.service.BulkQcSaveOperation)1 BulkValidationException (uk.ac.bbsrc.tgac.miso.core.service.exception.BulkValidationException)1 ValidationException (uk.ac.bbsrc.tgac.miso.core.service.exception.ValidationException)1