Commit 54644468 authored by RANNOU's avatar RANNOU 👽
Browse files

[enh] Service: filter observations by observers

parent 8a78e85b
......@@ -65,7 +65,8 @@ public class ObservedLocationRepositoryImpl
return super.toSpecification(filter, fetchOptions)
.and(hasLocationId(filter.getLocationId()))
.and(withStartDate(filter.getStartDate()))
.and(withEndDate(filter.getEndDate()));
.and(withEndDate(filter.getEndDate()))
.and(hasObserverPersonIds(filter.getObserverPersonIds()));
}
@Override
......
......@@ -26,9 +26,13 @@ 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.ObservedLocation;
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.Date;
/**
......@@ -39,6 +43,7 @@ public interface ObservedLocationSpecifications extends RootDataSpecifications<O
String LOCATION_ID_PARAM = "locationId";
String START_DATE_PARAM = "startDate";
String END_DATE_PARAM = "endDate";
String OBSERVER_PERSON_IDS_PARAM = "observerPersonIds";
default Specification<ObservedLocation> hasLocationId(Integer locationId) {
BindableSpecification<ObservedLocation> specification = BindableSpecification.where((root, query, criteriaBuilder) -> {
......@@ -76,5 +81,22 @@ public interface ObservedLocationSpecifications extends RootDataSpecifications<O
return specification;
}
default Specification<ObservedLocation> hasObserverPersonIds(Integer... observerPersonIds) {
if (ArrayUtils.isEmpty(observerPersonIds)) return null;
BindableSpecification<ObservedLocation> specification = BindableSpecification.where((root, query, criteriaBuilder) -> {
// Avoid duplicated entries (because of inner join)
query.distinct(true);
ParameterExpression<Collection> parameter = criteriaBuilder.parameter(Collection.class, OBSERVER_PERSON_IDS_PARAM);
return criteriaBuilder.in(
root.join(ObservedLocation.Fields.OBSERVERS, JoinType.INNER)
.get(IEntity.Fields.ID))
.value(parameter);
});
specification.addBind(OBSERVER_PERSON_IDS_PARAM, Arrays.asList(observerPersonIds));
return specification;
}
}
......@@ -48,5 +48,6 @@ public class ObservedLocationFilterVO implements IRootDataFilter {
private Integer[] locationIds;
private Integer recorderDepartmentId;
private Integer recorderPersonId;
private Integer[] observerPersonIds;
}
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