Commit 9bfd1b85 authored by RANNOU's avatar RANNOU 👽
Browse files

[enh] Service/Dao: check if sample TAG_ID is unique on save (#385)

parent 77631480
......@@ -32,11 +32,9 @@ import net.sumaris.core.dao.referential.taxon.TaxonNameRepository;
import net.sumaris.core.dao.technical.Daos;
import net.sumaris.core.event.config.ConfigurationReadyEvent;
import net.sumaris.core.event.config.ConfigurationUpdatedEvent;
import net.sumaris.core.exception.NotUniqueException;
import net.sumaris.core.exception.SumarisTechnicalException;
import net.sumaris.core.model.data.*;
import net.sumaris.core.model.referential.pmfm.Matrix;
import net.sumaris.core.model.referential.pmfm.PmfmEnum;
import net.sumaris.core.model.referential.pmfm.Unit;
import net.sumaris.core.model.referential.pmfm.UnitEnum;
import net.sumaris.core.model.referential.taxon.ReferenceTaxon;
......@@ -79,7 +77,6 @@ public class SampleRepositoryImpl
private TaxonNameRepository taxonNameRepository;
private boolean enableSaveUsingHash;
private boolean enableCheckUniqueTag;
@Autowired
public SampleRepositoryImpl(EntityManager entityManager) {
......@@ -92,7 +89,6 @@ public class SampleRepositoryImpl
@EventListener({ConfigurationReadyEvent.class, ConfigurationUpdatedEvent.class})
public void onConfigurationReady() {
this.enableSaveUsingHash = getConfig().enableSampleHashOptimization();
this.enableCheckUniqueTag = getConfig().enableSampleUniqueTag();
}
@Override
......@@ -309,18 +305,6 @@ public class SampleRepositoryImpl
log.warn(String.format("Updating a sample {id: %s, label: '%s'} without creation_date!", entity.getId(), entity.getLabel()));
}
super.onBeforeSaveEntity(vo, entity, isNew);
// Check if tag_id is unique by program
if (enableCheckUniqueTag) {
long count = this.findAll(SampleFilterVO.builder()
.programLabel(vo.getProgram().getLabel()).tagId(vo.getMeasurementValues().get(PmfmEnum.TAG_ID)).build())
.stream()
.filter(s -> isNew || !Objects.equals(s.getId(), vo.getId()))
.count();
if (count > 0) {
throw new NotUniqueException(String.format("Sample tag '%s' already exists", vo.getLabel()));
}
}
}
@Override
......
......@@ -25,11 +25,14 @@ package net.sumaris.core.service.data;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.config.SumarisConfiguration;
import net.sumaris.core.dao.data.MeasurementDao;
import net.sumaris.core.dao.data.sample.SampleRepository;
import net.sumaris.core.exception.NotUniqueException;
import net.sumaris.core.model.data.IMeasurementEntity;
import net.sumaris.core.model.data.SampleMeasurement;
import net.sumaris.core.model.referential.pmfm.MatrixEnum;
import net.sumaris.core.model.referential.pmfm.PmfmEnum;
import net.sumaris.core.util.Beans;
import net.sumaris.core.vo.data.MeasurementVO;
import net.sumaris.core.vo.data.sample.SampleFetchOptions;
......@@ -48,6 +51,9 @@ import java.util.stream.Collectors;
@Slf4j
public class SampleServiceImpl implements SampleService {
@Autowired
private SumarisConfiguration configuration;
@Autowired
protected SampleRepository sampleRepository;
......@@ -168,6 +174,7 @@ public class SampleServiceImpl implements SampleService {
protected void saveMeasurements(List<SampleVO> result) {
result.forEach(savedSample -> {
checkUniqueTag(savedSample);
if (savedSample.getMeasurementValues() != null) {
measurementDao.saveSampleMeasurementsMap(savedSample.getId(), savedSample.getMeasurementValues());
}
......@@ -180,6 +187,33 @@ public class SampleServiceImpl implements SampleService {
});
}
private void checkUniqueTag(SampleVO savedSample) {
if (!configuration.enableSampleUniqueTag()) return;
String savedSampleTagId = null;
// Get tag_id measurement
if (savedSample.getMeasurementValues() != null) {
savedSampleTagId = savedSample.getMeasurementValues().get(PmfmEnum.TAG_ID.getId());
} else if (savedSample.getMeasurements() != null) {
savedSampleTagId = savedSample.getMeasurements().stream()
.filter(m -> m.getId() == PmfmEnum.TAG_ID.getId())
.map(m -> m.getAlphanumericalValue())
.findFirst().orElse(null);
}
// Check if tag_id is unique by program
if (savedSampleTagId != null) {
long count = sampleRepository.findAll(SampleFilterVO.builder()
.programLabel(savedSample.getProgram().getLabel()).tagId(savedSampleTagId).build())
.stream()
.filter(s -> savedSample.getId() == null || !Objects.equals(s.getId(), savedSample.getId()))
.count();
if (count > 0) {
throw new NotUniqueException(String.format("Sample tag measurement '%s' already exists", savedSampleTagId));
}
}
}
protected void fillDefaultProperties(SampleVO parent, MeasurementVO measurement, Class<? extends IMeasurementEntity> entityClass) {
if (measurement == null) return;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment