Commit 8ea11778 authored by RANNOU's avatar RANNOU 👽
Browse files

[enh] Service/Dao: filter samples by observed locations (#377)

parent 6c58fe73
......@@ -96,6 +96,8 @@ public class SampleRepositoryImpl
return super.toSpecification(filter, fetchOptions)
.and(hasOperationId(filter.getOperationId()))
.and(hasLandingId(filter.getLandingId()))
.and(hasObservedLocationId(filter.getObservedLocationId()))
.and(inObservedLocationIds(filter.getObservedLocationIds()))
.and(addJoinFetch(fetchOptions, true));
}
......
......@@ -25,15 +25,18 @@ package net.sumaris.core.dao.data.sample;
import net.sumaris.core.dao.data.RootDataSpecifications;
import net.sumaris.core.dao.technical.jpa.BindableSpecification;
import net.sumaris.core.dao.technical.model.IEntity;
import net.sumaris.core.model.data.Batch;
import net.sumaris.core.model.data.Landing;
import net.sumaris.core.model.data.Sample;
import net.sumaris.core.vo.data.batch.BatchFetchOptions;
import net.sumaris.core.vo.data.LandingVO;
import net.sumaris.core.vo.data.sample.SampleFetchOptions;
import net.sumaris.core.vo.data.sample.SampleVO;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ParameterExpression;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
......@@ -41,6 +44,8 @@ import java.util.List;
*/
public interface SampleSpecifications extends RootDataSpecifications<Sample> {
String OBSERVED_LOCATION_IDS = "observedLocationIds";
default Specification<Sample> hasOperationId(Integer operationId) {
if (operationId == null) return null;
BindableSpecification<Sample> specification = BindableSpecification.where((root, query, criteriaBuilder) -> {
......@@ -66,6 +71,28 @@ public interface SampleSpecifications extends RootDataSpecifications<Sample> {
return specification;
}
default Specification<Sample> hasObservedLocationId(Integer observedLocationId) {
if (observedLocationId == null) return null;
BindableSpecification<Sample> specification = BindableSpecification.where((root, query, criteriaBuilder) -> {
query.orderBy(criteriaBuilder.asc(root.get(Sample.Fields.RANK_ORDER)));
ParameterExpression<Integer> param = criteriaBuilder.parameter(Integer.class, LandingVO.Fields.OBSERVED_LOCATION_ID);
return criteriaBuilder.equal(root.get(Sample.Fields.LANDING).get(Landing.Fields.OBSERVED_LOCATION).get(IEntity.Fields.ID), param);
});
specification.addBind(LandingVO.Fields.OBSERVED_LOCATION_ID, observedLocationId);
return specification;
}
default Specification<Sample> inObservedLocationIds(Integer... observedLocationIds) {
if (ArrayUtils.isEmpty(observedLocationIds)) return null;
BindableSpecification<Sample> specification = BindableSpecification.where((root, query, criteriaBuilder) -> {
query.orderBy(criteriaBuilder.asc(root.get(Sample.Fields.RANK_ORDER)));
ParameterExpression<Collection> param = criteriaBuilder.parameter(Collection.class, OBSERVED_LOCATION_IDS);
return criteriaBuilder.in(root.get(Sample.Fields.LANDING).get(Landing.Fields.OBSERVED_LOCATION).get(IEntity.Fields.ID)).value(param);
});
specification.addBind(OBSERVED_LOCATION_IDS, Arrays.asList(observedLocationIds));
return specification;
}
default Specification<Sample> addJoinFetch(SampleFetchOptions fetchOptions, boolean addQueryDistinct) {
if (fetchOptions == null || !fetchOptions.isWithMeasurementValues()) return null;
......
......@@ -56,6 +56,10 @@ public class SampleFilterVO implements IRootDataFilter {
private Integer landingId;
private Integer observedLocationId;
private Integer[] observedLocationIds;
private Integer parentId;
}
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